Algorithims Section 10 Dijkstras Algorithm
Algorithims Section 10 Dijkstras Algorithm
Dijkstra's Algorithm
This algorithm aims to find the shortest-path in a directed or undirected graph
with non-negative edge weights.
Problem Statement
Given a weighted graph G, the objective is to find the shortest path from a given
source vertex to all other vertices of G. The graph has the following
characteristics-
• Set of vertices V
• Set of weighted edges E such that (q,r) denotes an edge between vertices q
and r and cost(q,r) denotes its weight
Dijkstra's Algorithm:
• This is a single-source shortest path algorithm and aims to find solution to
the given problem statement
• This algorithm works for both directed and undirected graphs
• It works only for connected graphs
• The graph should not contain negative edge weights
• The algorithm predominantly follows Greedy approach for finding locally
optimal solution. But, it also uses Dynamic Programming approach for
building globally optimal solution, since the previous solutions are stored
and further added to get final distances from the source vertex
• The main logic of this algorithm is basedon the following formula-
dist[r]=min(dist[r], dist[q]+cost[q][r])
This formula states that distance vertex r, which is adjacent to vertex q, will be
updated if and only if the value of dist[q]+cost[q][r] is less than dist[r].
• dist is a 1-D array which, at every step, keeps track of the shortest distance
from source vertex to all other vertices, and
• cost is a 2-D array, representing the cost adjacency matrix for the graph
• This formula uses both Greedy and Dynamic approaches. The Greedy
approach is used for finding the minimum distance value, whereas the
Dynamic approach is used for combining the previous solutions (dist[q] is
already calculated and is used to calculate dist[r])
Algorithm-
Input Data-
• Cost Adjacency Matrix for Graph G, say cost
• Source vertex, say s
Output Data-
• Spanning tree having shortest path from s to all other vertices in G
Following are the steps used for finding the solution-
Step 1; Set dist[s]=0, S=ϕ // s is the source vertex and S is a 1-D array having all
the visited vertices
Step 2: For all nodes v except s, set dist[v]= ∞
Step 3: find q not in S such that dist[q] is minimum // vertex q should not be
visited
Step 4: add q to S // add vertex q to S since it has now been visited
Step 5: update dist[r] for all r adjacent to q such that r is not in S //vertex r should
not be visited dist[r]=min(dist[r], dist[q]+cost[q][r]) //Greedy and Dynamic
approach
Step 6: Repeat Steps 3 to 5 until all the nodes are in S // repeat till all the vertices
have been visited
Step 7: Print array dist having shortest path from the source vertex u to all other
vertices
Step 8: Exit
example-
Given the above weighted and connected graph and source vertex s, following
steps are used for finding the tree representing shortest path between s and all
other vertices-
Step A- Initialize the distance array (dist) using the following steps of algorithm –
• Step 1- Set dist[s]=0, S=ϕ // u is the source vertex and S is a 1-D array
having all the visited vertices
• Step 2- For all nodes v except s, set dist[v]= ∞
Set of visited vertices (S) S A B C D
0 ∞ ∞ ∞ ∞
Note-
• There can be multiple shortest path spanning trees for the same graph
depending on the source vertex
Djikstra's algorithm pseudocode
1. function dijkstra(G, S)
2. for each vertex V in G
3. distance[V] <- infinite
4. previous[V] <- NULL
5. If V != S, add V to Priority Queue Q
6. distance[S] <- 0
7. while Q IS NOT EMPTY
8. U <- Extract MIN from Q
9. for each unvisited neighbour V of U
a. tempDistance <- distance[U] + edge_weight(U, V)
b. if tempDistance < distance[V]
c. distance[V] <- tempDistance
d. previous[V] <- U
10. return distance[], previous[]