Dijkstra’s Algorithm
•
Used to find the shortest path between vertices
in a directed graph
•
Does not work with negative edges
Algorithm Steps
•
Start with a matrix for the distances between
the source and all other vertices
–
Initialize all distances to infinity
–
Initialize the source vertex to 0
•
Extract the minimum vertex
–
Vertex with the lightest edge
•
Relax the edges from the vertex that was
extracted
Complexity
•
Can be O(V
2) or O (E log(V))
–
E = edges
–
V = vertices
Graph
A B C
D E F
G H I
Table
A B C D E F G H I
x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
Extractable Set
Iteration 1
Extractable Set
•
A = 0
Graph
9
A B C
D E F
G H I
Relaxation
•
The distance from A to B is 7 because
–
The distance from A to A is zero
–
The weight of edge (A, B) is 7
–
0 + 7 = 7
•
The distance from A to D is 20 because
Table
A B C D E F G H I
x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞
Graph
A B C
D E F
G H I
Table
A B C D E F G H I
x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞
Extractable Set
Iteration 2
Extractable Set
•
B = 7
•
D = 20
Graph
17
A B C
D E F
G H I
Relaxation
•
The distance from A to C is 10 because
–
The distance from A to B is 7
–
The weight of edge (B, C) is 3
–
7 + 3 = 10
•
The distance from A to E is 9 because
Table
A B C D E F G H I
x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞
B 0 7 10 20 9 ∞ ∞ ∞ ∞
Graph
A B C
D E F
G H I
Table
A B C D E F G H I
x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞
B 0 7 10 20 9 ∞ ∞ ∞ ∞
Extractable Set
•
C = 10
•
D = 20
Iteration 3
Extractable Set
•
C = 10
•
D = 20
•
E = 9
Graph
25
A B C
D E F
G H I
Relaxation
•
The distance from A to H is 27 because
–
The distance from A to E is 9
–
The weight of edge (E, H) is 18
–
9 + 18 = 27
•
The distance from A to D is 20 because
–
The distance from A to E is 9
–
The weight of edge (E, D) is 14
Table
A B C D E F G H I
x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞
B 0 7 10 20 9 ∞ ∞ ∞ ∞
E 0 7 10 20 9 ∞ ∞ 27 ∞
Graph
A B C
D E F
G H I
Table
A B C D E F G H I
x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞
B 0 7 10 20 9 ∞ ∞ ∞ ∞
E 0 7 10 20 9 ∞ ∞ 27 ∞
Extractable Set
•
C = 10
•
D = 20
Iteration 4
Extractable Set
•
C = 10
•
D = 20
•
H = 27
Graph
33
A B C
D E F
G H I
Extract vertex C and relax all edges outgoing from vertex C
Relaxation
•
The distance from A to F is 13 because
Table
A B C D E F G H I
x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞
B 0 7 10 20 9 ∞ ∞ ∞ ∞
E 0 7 10 20 9 ∞ ∞ 27 ∞
C 0 7 10 20 9 13 ∞ 27 ∞
Graph
A B C
D E F
G H I
Table
A B C D E F G H I
x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞
B 0 7 10 20 9 ∞ ∞ ∞ ∞
E 0 7 10 20 9 ∞ ∞ 27 ∞
C 0 7 10 20 9 13 ∞ 27 ∞
Extractable Set
•
D = 20
•
F = 13
Iteration 5
Extractable Set
•
D = 20
•
F = 13
•
H = 27
Graph
41
A B C
D E F
G H I
Relaxation
•
The distance from A to E does not change
–
Vertex E was already extracted
–
There are no negative edges
•
The distance from A to I is 38 because
–
The distance from A to F is 13
–
The weight of edge (F, I) is 25
Table
A B C D E F G H I
x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞
B 0 7 10 20 9 ∞ ∞ ∞ ∞
E 0 7 10 20 9 ∞ ∞ 27 ∞
C 0 7 10 20 9 13 ∞ 27 ∞
F 0 7 10 20 9 13 ∞ 27 38
Graph
A B C
D E F
G H I
Table
A B C D E F G H I
x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞
B 0 7 10 20 9 ∞ ∞ ∞ ∞
E 0 7 10 20 9 ∞ ∞ 27 ∞
C 0 7 10 20 9 13 ∞ 27 ∞
F 0 7 10 20 9 13 ∞ 27 38
Extractable Set
•
D = 20
•
H = 27
Iteration 6
Extractable Set
•
D = 20
•
H = 27
•
I = 38
Graph
49
A B C
D E F
G H I
Relaxation
•
The distance from A to G is 28 because
Table
A B C D E F G H I
x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞
B 0 7 10 20 9 ∞ ∞ ∞ ∞
E 0 7 10 20 9 ∞ ∞ 27 ∞
C 0 7 10 20 9 13 ∞ 27 ∞
F 0 7 10 20 9 13 ∞ 27 38
D 0 7 10 20 9 13 28 27 38
Graph
A B C
D E F
G H I
Table
A B C D E F G H I
x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞
B 0 7 10 20 9 ∞ ∞ ∞ ∞
E 0 7 10 20 9 ∞ ∞ 27 ∞
C 0 7 10 20 9 13 ∞ 27 ∞
F 0 7 10 20 9 13 ∞ 27 38
D 0 7 10 20 9 13 28 27 38
Extractable Set
Iteration 7
Extractable Set
•
G = 28
•
H = 27
•
I = 38
Graph
57
A B C
D E F
G H I
Relaxation
•
The distance from A to I is still 38 because
–
The distance from A to H is 27
–
The weight of edge (H, I) is 58
–
27 + 58 = 85
Table
A B C D E F G H I
x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞
B 0 7 10 20 9 ∞ ∞ ∞ ∞
E 0 7 10 20 9 ∞ ∞ 27 ∞
C 0 7 10 20 9 13 ∞ 27 ∞
F 0 7 10 20 9 13 ∞ 27 38
D 0 7 10 20 9 13 28 27 38
H 0 7 10 20 9 13 28 27 38
Graph
A B C
D E F
G H I
Table
A B C D E F G H I
x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞
B 0 7 10 20 9 ∞ ∞ ∞ ∞
E 0 7 10 20 9 ∞ ∞ 27 ∞
C 0 7 10 20 9 13 ∞ 27 ∞
F 0 7 10 20 9 13 ∞ 27 38
D 0 7 10 20 9 13 28 27 38
H 0 7 10 20 9 13 28 27 38
Extractable Set
Iteration 8
Extractable Set
•
G = 28
•
I = 38
Graph
65
A B C
D E F
G H I
Relaxation
•
The distance from A to G is still 28 because
Table
A B C D E F G H I
x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞
B 0 7 10 20 9 ∞ ∞ ∞ ∞
E 0 7 10 20 9 ∞ ∞ 27 ∞
C 0 7 10 20 9 13 ∞ 27 ∞
F 0 7 10 20 9 13 ∞ 27 38
D 0 7 10 20 9 13 28 27 38
H 0 7 10 20 9 13 28 27 38
G 0 7 10 20 9 13 28 27 38
Graph
A B C
D E F
G H I
Table
A B C D E F G H I
x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞
B 0 7 10 20 9 ∞ ∞ ∞ ∞
E 0 7 10 20 9 ∞ ∞ 27 ∞
C 0 7 10 20 9 13 ∞ 27 ∞
F 0 7 10 20 9 13 ∞ 27 38
D 0 7 10 20 9 13 28 27 38
H 0 7 10 20 9 13 28 27 38
G 0 7 10 20 9 13 28 27 38
Extractable Set
Iteration 9
Extractable Set
•
I = 38
Graph
73
A B C
D E F
G H I
Relaxation
Table
A B C D E F G H I
x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞
B 0 7 10 20 9 ∞ ∞ ∞ ∞
E 0 7 10 20 9 ∞ ∞ 27 ∞
C 0 7 10 20 9 13 ∞ 27 ∞
F 0 7 10 20 9 13 ∞ 27 38
D 0 7 10 20 9 13 28 27 38
H 0 7 10 20 9 13 28 27 38
G 0 7 10 20 9 13 28 27 38
I 0 7 10 20 9 13 28 27 38
Graph
A B C
D E F
G H I
Table
A B C D E F G H I
x 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
A 0 7 ∞ 20 ∞ ∞ ∞ ∞ ∞
B 0 7 10 20 9 ∞ ∞ ∞ ∞
E 0 7 10 20 9 ∞ ∞ 27 ∞
C 0 7 10 20 9 13 ∞ 27 ∞
F 0 7 10 20 9 13 ∞ 27 38
D 0 7 10 20 9 13 28 27 38
H 0 7 10 20 9 13 28 27 38
G 0 7 10 20 9 13 28 27 38
I 0 7 10 20 9 13 28 27 38
Extractable Set
•
None