An overview of the most simple algorithms used in data structures for path finding. Dijkstra, Breadth First Search, Depth First Search, Best First Search and A-star
Naturally feel free to copy for assignments and all
5. Overview
Dijkstra's algorithm - Works on both directed and undirected graphs. However, all edges
must have nonnegative weights.
Input:
Weighted graph G={E,V} and source vertex v∈V, such that all edge weights are
nonnegative
Output:
Lengths of shortest paths (or the shortest paths themselves) from a given source
vertex v∈V to all other vertices
7. Algorithm
1. Let distance of start vertex from start vertex = 0
2. Let distance of all other vertices from start = infinity
3. Repeat
a. Visit the unvisited vertex with the smallest known distance from the start
vertex
b. For the current vertex, examine its unvisited neighbours
c. For the current vertex, calculate distance of each neighbour from start
vertex
d. If the calculated distance of a vertex is less than the known distance,
update the shortest distance
e. Update the previous vertex for each of the updated distances
f. Add the current vertex to the list of visited vertices
4. Until all vertices visited
8. Time Complexity (List)
The simplest implementation of the Dijkstra's algorithm stores vertices in an ordinary linked list or
array
V vertices and E edges
Initialization O(V)
While loop O(V)
Find and remove min distance vertices O(V)
Potentially E updates
Update costs O(1)
Total time O(V2
+ E) = O(V2
)
9. Time Complexity (Priority Queue)
For sparse graphs, (i.e. graphs with much less than |V2
| edges) Dijkstra's implemented more
efficiently by priority queue
Initialization O(V) using O(V) buildHeap
While loop O(V)
Find and remove min distance vertices O(log V) using O(log V) deleteMin
Potentially E updates
Update costs O(log V) using decreaseKey
Total time O(VlogV + ElogV) = O(ElogV)
11. Overview
Breadth First Search Algorithm (BFS) is a traversing algorithm where you
should start traversing from a selected node (source or starting node) and
traverse the graph layerwise thus exploring the neighbour nodes (nodes which
are directly connected to source node). You must then move towards the
next-level neighbour nodes.
12. Approach
Idea: Traverse nodes in layers.
Problem: There are cycles in graphs, so each node will be visited infinite times.(does not
occur in trees.)
Approach:
1) Add root node to the queue, and mark it as visited(already explored).
2) Loop on the queue as long as it's not empty.
a) Get and remove the node at the top of the queue(current).
b) For every non-visited child of the current node, do the following:
i) Mark it as visited.
ii) Check if it's the goal node, If so, then return it.
iii) Otherwise, push it to the queue.
3) If queue is empty, then goal node was not found!
14. Pseudocode
BFS (G, s) //Where G is the graph and s is the source node
let Q be queue.
Q.enqueue( s ) //Inserting s in queue until all its neighbour vertices are marked.
mark s as visited.
while ( Q is not empty) It will run ‘V’ times.
//Removing that vertex from queue,whose neighbour will be visited now
v = Q.dequeue( )
//processing all the neighbours of v
for all neighbours w of v in Graph G It will run ‘E’ times.
if w is not visited
Q.enqueue( w ) //Stores w in Q to further visit its neighbour
mark w as visited.
17. Overview
Depth-first search(DFS) is an algorithm for traversing or searching tree
or graph data structures. The algorithm starts at the root node
(selecting some arbitrary node as the root node in the case of a graph)
and explores as far as possible along each branch before backtracking.
18. Algorithm
1) Start by putting any one of the graphs vertices on the top of the stack.
2) Take the top item of the stack and add it to the visited list.
3) Create a list of that vertex adjacent nodes. Add the ones which aren’t in the
visited list to the top of the stack.
4) Keep repeating steps 2 and 3 until the stack is empty.
21. Time Complexity
The time complexity of DFS is the entire tree is traversed is
O(V) where V is the number of nodes. In the case of the
graph, the time complexity is O(V+E) where V is the number
of vertices and E is the number of edges
23. Overview
Best first search is a traversal technique that decides which node is to be
visited next by checking which node is the most promising one and then
check it.
24. How it works
● Best First Search falls under the
category of Heuristic Search or
Informed Search.
● It uses an evaluation function to
decide which adjacent is most
promising and then explore.
● Performance of the algorithm
depends on how well the cost
function is designed.
26. Pseudocode
Best-First-Search(Grah g, Node start)
1) Create an empty PriorityQueue
PriorityQueue pq;
2) Insert "start" in pq.
pq.insert(start)
3) Until PriorityQueue is empty
u = PriorityQueue.DeleteMin
If u is the goal
Exit
Else
Foreach neighbor v of u
If v "Unvisited"
Mark v "Visited"
pq.insert(v)
Mark u "Examined"
End procedure
27. Time Complexity
The worst case time complexity of the algorithm is given by O(n*logn)
Performance of the algorithm depends on the cost or evaluation function
29. Overview
A* is an informed search algorithm, or a best-first search, meaning that it is formulated in
terms of weighted graphs: starting from a specific starting node of a graph, it aims to find
a path to the given goal node having the smallest cost (least distance travelled, shortest
time, etc.). What A* search algorithm does it that at each step it picks the node according
to a value of F which is the sum of ‘g’ and ‘h’.at each step it picks the node having the
lowest ‘f’ and then process it.
f(n) = g(n) + h(n)
30. ALGORITHM
Step 1: Place the starting node into OPEN and find its f(n) value.
Step 2: Remove the node from OPEN, having the smallest f(n) value.
If it is a goal node then stop and return success.
Step 3: Else remove the node from OPEN, find all its successors.
Step 4: Find the f(n) value of all successors; place them into OPEN
and place the removed node into CLOSE.
Step 5: Go to Step-2.
Step 6: Exit.
32. TIME COMPLEXITY
When using the optimal heuristic, A* will be O(n) in both space and time complexity if
we disregard the complexity of the heuristic calculation itself. Again n is the length of
the solution path.
33. ADVANTAGES AND DISADVANTAGES
● It is the best one from other techniques to solve
very complex problems.
● It is optimally efficient, i.e. there is no other
optimal algorithm guaranteed to expand fewer
nodes than A*.
● This algorithm is complete if the branching factor
is finite and every action has fixed cost.
● The speed execution of A* search is highly
dependant on the accuracy of the heuristic
algorithm that is used to compute h (n).