dijkstra's algorithm demo
TRANSCRIPT
Lecture slides by Kevin WayneCopyright © 2005 Pearson-Addison Wesley
http://www.cs.princeton.edu/~wayne/kleinberg-tardos
Last updated on 11/15/17 10:10 AM
4. GREEDY ALGORITHMS II
‣ Dijkstra’s algorithm demo
‣ Dijkstra’s algorithm demo(efficient implementation)
4. GREEDY ALGORITHMS II
‣ Dijkstra’s algorithm demo
‣ Dijkstra’s algorithm demo(efficient implementation)
・Initialize S ← { s } and d[s] ← 0.
・Repeatedly choose unexplored node v ∉ S which minimizes add v to S; set d[v] ← π(v) and pred[v] ← argmin.
Dijkstra’s algorithm demo
3
4 8
3
16 2
67 5
1
d[s] 0
S s
the length of a shortest path from s to some node u in explored part S, followed by a single edge e = (u, v)
�(v) = mine = (u,v) : u�S
d[u] + �e
Dijkstra’s algorithm demo
・Initialize S ← { s } and d[s] ← 0.
・Repeatedly choose unexplored node v ∉ S which minimizes add v to S; set d[v] ← π(v) and pred[v] ← argmin.
4
4 8
16
0
0 + 4 = 4 0 + 8 = 8
0 + 16 = 16
sS
d[s]
the length of a shortest path from s to some node u in explored part S, followed by a single edge e = (u, v)
�(v) = mine = (u,v) : u�S
d[u] + �e
Dijkstra’s algorithm demo
・Initialize S ← { s } and d[s] ← 0.
・Repeatedly choose unexplored node v ∉ S which minimizes add v to S; set d[v] ← π(v) and pred[v] ← argmin.
5
v
0
4
8
3
16
4 + 3 = 7 0 + 8 = 8
0 + 16 = 16
s
S
the length of a shortest path from s to some node u in explored part S, followed by a single edge e = (u, v)
�(v) = mine = (u,v) : u�S
d[u] + �e
pred[v]
Dijkstra’s algorithm demo
・Initialize S ← { s } and d[s] ← 0.
・Repeatedly choose unexplored node v ∉ S which minimizes add v to S; set d[v] ← π(v) and pred[v] ← argmin.
6
0
4 7
16
7
1
7 + 1 = 8
0 + 16 = 16 7 + 7 = 14
s
S
the length of a shortest path from s to some node u in explored part S, followed by a single edge e = (u, v)
�(v) = mine = (u,v) : u�S
d[u] + �e
Dijkstra’s algorithm demo
・Initialize S ← { s } and d[s] ← 0.
・Repeatedly choose unexplored node v ∉ S which minimizes add v to S; set d[v] ← π(v) and pred[v] ← argmin.
7
0
4 7 8
16
7 5 6
0 + 16 = 16 7 + 7 = 14 8 + 5 = 13 8 + 6 = 14
s
S
the length of a shortest path from s to some node u in explored part S, followed by a single edge e = (u, v)
�(v) = mine = (u,v) : u�S
d[u] + �e
Dijkstra’s algorithm demo
・Initialize S ← { s } and d[s] ← 0.
・Repeatedly choose unexplored node v ∉ S which minimizes add v to S; set d[v] ← π(v) and pred[v] ← argmin.
8
0
4 7 8
13 + 2 = 15 8 + 6 = 14
6
2
13
s
S
the length of a shortest path from s to some node u in explored part S, followed by a single edge e = (u, v)
�(v) = mine = (u,v) : u�S
d[u] + �e
Dijkstra’s algorithm demo
・Initialize S ← { s } and d[s] ← 0.
・Repeatedly choose unexplored node v ∉ S which minimizes add v to S; set d[v] ← π(v) and pred[v] ← argmin.
9
v
0
4 7 8
1413
s
S
�(v) = mine = (u,v) : u�S
d[u] + �e
Dijkstra’s algorithm demo
・Initialize S ← { s } and d[s] ← 0.
・Repeatedly choose unexplored node v ∉ S which minimizes add v to S; set d[v] ← π(v) and pred[v] ← argmin.
10
v
0
4 7 8
1413
�(v) = mine = (u,v) : u�S
d[u] + �e
s
4π[v]
pred[v]
4. GREEDY ALGORITHMS II
‣ Dijkstra’s algorithm demo
‣ Dijkstra’s algorithm demo(efficient implementation)
Initialization.
・For all v ≠ s : π [v] ← ∞.
・For all v ≠ s : pred [v] ← null.
・S ← ∅ and π [s] ← 0.
s
Dijkstra’s algorithm demo (efficient implementation)
12
4 8
3
16 2
67 5
1
0π[s]
Basic step. Choose unexplored node u ∉ S with minimum π [u].
・Add u to S.
・For each edge e = (u, v) leaving u, if π [v] > π [u] + ℓe then:
- π [v] ← π [u] + ℓe
- pred[v] ← e
s
Dijkstra’s algorithm demo (efficient implementation)
13
4 8
16
0
0 + 4 = 4 0 + 8 = 8
0 + 16 = 16
Basic step. Choose unexplored node u ∉ S with minimum π [u].
・Add u to S.
・For each edge e = (u, v) leaving u, if π [v] > π [u] + ℓe then:
- π [v] ← π [u] + ℓe
- pred[v] ← e
v
s
Dijkstra’s algorithm demo (efficient implementation)
14
4 8
16
0
0 + 4 = 4 0 + 8 = 8
0 + 16 = 16
pred[v]
Basic step. Choose unexplored node u ∉ S with minimum π [u].
・Add u to S.
・For each edge e = (u, v) leaving u, if π [v] > π [u] + ℓe then:
- π [v] ← π [u] + ℓe
- pred[v] ← e
Dijkstra’s algorithm demo (efficient implementation)
15
0
4 8
16
3
4 + 3 = 7
s
Basic step. Choose unexplored node u ∉ S with minimum π [u].
・Add u to S.
・For each edge e = (u, v) leaving u, if π [v] > π [u] + ℓe then:
- π [v] ← π [u] + ℓe
- pred[v] ← e
Dijkstra’s algorithm demo (efficient implementation)
16
0
4 8
16
3
4 + 3 = 7
s
Basic step. Choose unexplored node u ∉ S with minimum π [u].
・Add u to S.
・For each edge e = (u, v) leaving u, if π [v] > π [u] + ℓe then:
- π [v] ← π [u] + ℓe
- pred[v] ← e
Dijkstra’s algorithm demo (efficient implementation)
17
0
4 7
16
7
1
7 + 1 = 8
7 + 7 = 14
s
Basic step. Choose unexplored node u ∉ S with minimum π [u].
・Add u to S.
・For each edge e = (u, v) leaving u, if π [v] > π [u] + ℓe then:
- π [v] ← π [u] + ℓe
- pred[v] ← e
Dijkstra’s algorithm demo (efficient implementation)
18
0
4 7
16
7
1
7 + 1 = 8
7 + 7 = 14
s
Basic step. Choose unexplored node u ∉ S with minimum π [u].
・Add u to S.
・For each edge e = (u, v) leaving u, if π [v] > π [u] + ℓe then:
- π [v] ← π [u] + ℓe
- pred[v] ← e
Dijkstra’s algorithm demo (efficient implementation)
19
0
4 7
14
8
5 6
8 + 5 = 138 + 6 = 14
s
Basic step. Choose unexplored node u ∉ S with minimum π [u].
・Add u to S.
・For each edge e = (u, v) leaving u, if π [v] > π [u] + ℓe then:
- π [v] ← π [u] + ℓe
- pred[v] ← e
Dijkstra’s algorithm demo (efficient implementation)
20
0
4 7
14
8
5 6
8 + 5 = 138 + 6 = 14
s
Basic step. Choose unexplored node u ∉ S with minimum π [u].
・Add u to S.
・For each edge e = (u, v) leaving u, if π [v] > π [u] + ℓe then:
- π [v] ← π [u] + ℓe
- pred[v] ← e
Dijkstra’s algorithm demo (efficient implementation)
21
0
4 7
13
8
1413 + 2 = 15
2s
Basic step. Choose unexplored node u ∉ S with minimum π [u].
・Add u to S.
・For each edge e = (u, v) leaving u, if π [v] > π [u] + ℓe then:
- π [v] ← π [u] + ℓe
- pred[v] ← e
Dijkstra’s algorithm demo (efficient implementation)
22
0
4 7
13
8
14
s
Basic step. Choose unexplored node u ∉ S with minimum π [u].
・Add u to S.
・For each edge e = (u, v) leaving u, if π [v] > π [u] + ℓe then:
- π [v] ← π [u] + ℓe
- pred[v] ← e
Dijkstra’s algorithm demo (efficient implementation)
23
0
4 7
13
8
14
s
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.
24
v
s
0
4 7
13
8
14
π[v]
pred[v]