Professional Documents
Culture Documents
UNIT 5 Dijkstra's Algorithm
UNIT 5 Dijkstra's Algorithm
INSTITUTE OF
M SCIENCE AND
TECHNOLOGY,
CHENNAI.
18CSC201J
DATA STRUCTURES AND ALGORITHMS
Unit- V
SR
INSTITUTE OF SCIENCE
M AND
TECHNOLOGY,
CHENNAI.
Dijkstra's algorithm
Shortest Path Problem
Single-Source Shortest Path Problem - The problem of finding
shortest paths from a source vertex v to all other vertices in the
graph.
Applications
- Maps (Map Quest, Google Maps)
- Routing Systems
Dijkstra's algorithm
0 ∞
2
A B
4 1 3 10
2 2 ∞
∞ C D E
5 8 ∞ 4 6
1
F G
∞ ∞
0 2
2
A B
1
4 3 10
2 2 ∞
∞ C D E
5 8 1 4 6
Distance(B) = 2 1
F G
Distance(D) = 1
∞ ∞
Now, select the node with minimum distance. Weights are 2 and 1 hence 1 is
selected i.e., D is chosen
0 2
2
A B
4 1 3 10
2 2 ∞
∞ C D E
5 8 1 4 6
1
F G
∞ ∞
Neighbors of D are selected and their distances are updated.
Distance of C = Distance of D + weight of D to C = 1+ 2=3
Distance of E = Distance of D + weight of D to E = 1+ 2=3
Distance of F = Distance of D + weight of D to F = 1+ 8=9
Distance of G = Distance of D + weight of D to G = 1+ 4=5
0 2
2
A B
4 1 3 10
2
2 3
3 C D E
5 8 1 4 6
1
F G
9 5
Pick vertex in List with minimum distance and update neighbors.
B has the minimum distance. Neighbours of B are D and E.
Distance of D is not updated since D is already visited.
Previous distance of E = 3
New distance of E = 2+10 = 12
Distance of E not updated since it is larger than previously computed.
0 2
2
A B
4 1 3 10
2 2 3
3 C D E
5 8 1 4 6
1
F G
9 5
Pick vertex List with minimum distance and update neighbors. E is selected.
Distance of G not updated since it is larger than previously computed.
0 2
2
A B
4 1 3 10
2 2 3
3 C D E
5 8 1 4 6
1
F G
9 5
Pick vertex List with minimum distance and update neighbors.
C is selected. Neighbour is F.
Distance of F = Distance of C + weight of C to F = 3 + 5 = 8
0 2
2
A B
4 1 3 10
2 2 3
3 C D E
5 8 1 4 6
1
F G
8 5
Pick vertex List with minimum distance and update neighbors
G is selected. F is the neighbour.
Previous distance of F is 8.
Distance of F = Distance of G+weight from G to F = 5+1
Update the distance of F as 6.
0 2
2
A B
4 1 3 10
2 2 3
3 C D E
5 8 1 4 6
1
F G
6 5
F is selected and there are no neighbors to F. This is resultant the shortest path graph
0 2
2
A B
4 1 3 10
2 2 3
3 C D E
5 8 1 4 6
1
F G
6 5
References:
1. A.V.Aho, J.E Hopcroft , J.D.Ullman, Data structures and Algorithms, Pearson Education, 2003
2. Reema Thareja, Data Structures Using C, 1st ed., Oxford Higher Education, 2011
Thank
You