Shortest Path Algorithms
Shortest Path Algorithms
Introduction:
Google Maps
Road Networks
Logistics Research
Dijkstra’s Algorithm and Bellman Ford Algorithm are the famous algorithms used
for solving single-source shortest path problems.
Dijkstra’s Algorithm:
It is used for solving the single source shortest path problem which gives the shortest
paths from one particular source node to all the other nodes of the given graph.
Conditions for Dijkstra’s Algorithm-
1. Dijkstra’s algorithm works only for those graphs that are connected.
2. Dijkstra’s algorithm works only for those graphs that do not contain any edges with
negative weights.
3. Dijkstra’s algorithm works for directed as well as undirected graphs.
Algorithm
1. for each vertex v Є V // vertex set
dis(source) = 0 // distance of source is 0
dis(v) = ∞ // distance of remaining vertexes is ∞
2. Q = |V| // vertex set
3. while Q ≠ Ф
u = Extract-Min (Q) // selecting minimum edge
for each vertex v Є adj[u] //
◦ if dis(u) + weight(u,v) < dis(v) then
▪ dis(v) = dis(u) + weight(u, v)
I) Problem:’ For the given undirected graph, find the shortest path and its cost by
considering A as its single source using Dijkstra’s Algorithm.
Solution:
Step 1: The source is the vertex A. Using a table on vertex and visiting nodes apply the given
cost from the above graph.
Visited
A B C D E F G H
Vertex
0A
A 8A 2A 5A α α α
Step 2: Find the minimum cost among 8,2,5 and select that vertex as visited one. Here it is C
= 2A. Thus the current cost =2.
Apply the cost in the tabular column.
Cost = Minimum( Previous cost , Current cost + length)
(C,B) = Min(8 , α) = 8
(C,D) = Min(5 , 2+2) = 4
(C,E) = Min(α,2+5)= 7
Visited
A B C D E F G H
Vertex
0A
A 8A 2A 5A α α α α
C --- 8A 2A 4C 7C α α α
Step3: Find the minimum cost among 8,4,7 and select that vertex as visited one. Here it is
D= 4C. Thus the current cost =4.
Apply the cost in the tabular column.
Cost = Minimum( Previous cost , Current cost + length)
(D,B) = Min(8 , 4+2) = 6
(D,E) = Min(7, 4+1) = 5 (D,F) = Min(α,4+6)= 10
(D,G) = Min(α,4+3)= 7
Visited
A B C D E F G H
Vertex
0A
A 8A 2A 5A α α α α
C --- 8A 2A 4C 7C α α α
D --- 6D --- 4C 5D 10D 7D α
Step 4: Find the minimum cost among 6,5,10,7 and select that vertex as visited one. Here it
is E= 5D. Thus the current cost =5.
Apply the cost in the tabular column.
Cost = Minimum( Previous cost , Current cost + length)
(E,B) = Min(6 , 5+ α) = 6
(E,F) = Min(10, 5+ α) = 10 (E,H) = Min(α, α)= α
(E,G) = Min(7,5+1)= 6
Visited
A B C D E F G H
Vertex
0A
A 8A 2A 5A α α α α
C --- 8A 2A 4C 7C α α α
D --- 6D --- 4C 5D 10D 7D α
E --- 6D --- --- 5D 10D 6E α
Step 5: Find the minimum cost among 6,10,6 and select one vertex as visited one. Here it is
B= 6D. Thus the current cost =6.
Apply the cost in the tabular column.
Cost = Minimum( Previous cost , Current cost + length)
(B,F) = Min(10 , 6+ 13) = 10
(B,G) = Min(6, 6+ α) = 6
(B,H) = Min(α, α)= α
Visited
A B C D E F G H
Vertex
0A
A 8A 2A 5A α α α α
C --- 8A 2A 4C 7C α α α
D --- 6D --- 4C 5D 10D 7D α
E --- 6D --- --- 5D 10D 6E α
B --- 6D --- --- --- 10D 6E α
Step 5: Find the minimum cost among 10,6 and select one vertex as visited one. Here it is
G= 6E. Thus the current cost =6.
Apply the cost in the tabular column.
Cost = Minimum( Previous cost , Current cost + length)
(G,F) = Min(10 , 6+ 2) = 8
(G,H) = Min(α, 6+6)= 12
Visited
A B C D E F G H
Vertex
0A
A 8A 2A 5A α α α α
C --- 8A 2A 4C 7C α α α
D --- 6D --- 4C 5D 10D 7D α
E --- 6D --- --- 5D 10D 6E α
B --- 6D --- --- --- 10D 6E α
G --- --- --- --- --- 8G 6E 12G
Step 6: Find the minimum cost among 8,12 and select one vertex as visited one. Here it is
F= 8G. Thus the current cost =8.
Apply the cost in the tabular column.
Cost = Minimum( Previous cost , Current cost + length)
(F,H) = Min(12 , 8+ 3) = 11
Visited A B C D E F G H
Vertex
0A
A 8A 2A 5A α α α α
C --- 8A 2A 4C 7C α α α
D --- 6D --- 4C 5D 10D 7D α
E --- 6D --- --- 5D 10D 6E α
B --- 6D --- --- --- 10D 6E α
G --- --- --- --- --- 8G 6E 12G
Step 7: Find the minimum cost among 11 and select one vertex as visited one. Here it is
H=11F. Thus the current cost =8.
Apply the cost in the tabular column.
Cost = Minimum( Previous cost , Current cost + length)
Visited A B C D E F G H
Vertex
0A
A 8A 2A 5A α α α α
C --- 8A 2A 4C 7C α α α
D --- 6D --- 4C 5D 10D 7D α
E --- 6D --- --- 5D 10D 6E α
B --- 6D --- --- --- 10D 6E α
G --- --- --- --- --- 8G 6E 12G
F --- --- --- --- --- 8G --- 11F
Output:
The Shortest path using Dijkstra’s Algorithm is as follows: