DemoDijkstra algorithm
DemoDijkstra algorithm
G REEDY A LGORITHMS II
d[s] 0
S s 16 2
4 8 7 5 6
3 1
3
Dijkstra’s algorithm demo
d[s] 0 0 + 16 = 16
S s 16
4 8
0+4=4 0+8=8
4
Dijkstra’s algorithm demo
0 0 + 16 = 16
s 16
S 8
pred[v]
v 3
4 4+3=7
0+8=8 5
Dijkstra’s algorithm demo
0 + 16 = 16
0 7 + 7 = 14
s 16
S 7
4 7 7+1=8
6
Dijkstra’s algorithm demo
0 + 16 = 16
7 + 7 = 14
0 8 + 5 = 13 8 + 6 = 14
s 16
S 7 5 6
4 7 8
7
Dijkstra’s algorithm demo
13 + 2 = 15
0 13 8 + 6 = 14
s 2
S 6
4 7 8
8
Dijkstra’s algorithm demo
0 13 14
4 7 8
9
Dijkstra’s algorithm demo
0 13 14
pred[v]
π[v] 4 7 8
10
4. G REEDY A LGORITHMS II
Initialization.
For all v ≠ s : π [v] ← ∞.
For all v ≠ s : pred [v] ← null.
S ← ∅ and π [s] ← 0.
π[s] 0
s 16 2
4 8 7 5 6
3 1
12
Dijkstra’s algorithm demo (efficient implementation)
pred[v] ← e
0 0 + 16 = 16
s 16
4 8
0+4=4 0+8=8
13
Dijkstra’s algorithm demo (efficient implementation)
pred[v] ← e
0 0 + 16 = 16
s 16
4 8
pred[v]
0+4=4 0+8=8
14
Dijkstra’s algorithm demo (efficient implementation)
pred[v] ← e
0 16
4 8
4+3=7 15
Dijkstra’s algorithm demo (efficient implementation)
pred[v] ← e
0 16
4 8
4+3=7 16
Dijkstra’s algorithm demo (efficient implementation)
pred[v] ← e
7 + 7 = 14
0 16
4 7 7+1=8
17
Dijkstra’s algorithm demo (efficient implementation)
pred[v] ← e
7 + 7 = 14
0 16
4 7 7+1=8
18
Dijkstra’s algorithm demo (efficient implementation)
pred[v] ← e
8 + 5 = 13
0 14 8 + 6 = 14
5 6
4 7 8
19
Dijkstra’s algorithm demo (efficient implementation)
pred[v] ← e
8 + 5 = 13
0 14 8 + 6 = 14
5 6
4 7 8
20
Dijkstra’s algorithm demo (efficient implementation)
pred[v] ← e
13 + 2 = 15
0 13 14
s 2
4 7 8
21
Dijkstra’s algorithm demo (efficient implementation)
pred[v] ← e
0 13 14
4 7 8
22
Dijkstra’s algorithm demo (efficient implementation)
pred[v] ← e
0 13 14
4 7 8
23
Dijkstra’s algorithm demo (efficient implementation)
Termination.
π [v] = length of a shortest s↝v path.
pred[v] = last edge on a shortest s↝v path.
0 13 14
pred[v]
π[v] 4 7 8
24