Dijkstra's Algorithm Demo
-
Upload
nguyendiep -
Category
Documents
-
view
240 -
download
2
Transcript of Dijkstra's Algorithm Demo
![Page 1: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/1.jpg)
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)
![Page 2: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/2.jpg)
4. GREEDY ALGORITHMS II
‣ Dijkstra’s algorithm demo
‣ Dijkstra’s algorithm demo(efficient implementation)
![Page 3: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/3.jpg)
・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
![Page 4: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/4.jpg)
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
![Page 5: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/5.jpg)
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]
![Page 6: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/6.jpg)
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
![Page 7: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/7.jpg)
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
![Page 8: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/8.jpg)
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
![Page 9: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/9.jpg)
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
![Page 10: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/10.jpg)
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]
![Page 11: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/11.jpg)
4. GREEDY ALGORITHMS II
‣ Dijkstra’s algorithm demo
‣ Dijkstra’s algorithm demo(efficient implementation)
![Page 12: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/12.jpg)
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]
![Page 13: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/13.jpg)
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
![Page 14: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/14.jpg)
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]
![Page 15: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/15.jpg)
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
![Page 16: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/16.jpg)
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
![Page 17: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/17.jpg)
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
![Page 18: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/18.jpg)
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
![Page 19: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/19.jpg)
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
![Page 20: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/20.jpg)
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
![Page 21: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/21.jpg)
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
![Page 22: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/22.jpg)
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
![Page 23: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/23.jpg)
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
![Page 24: Dijkstra's Algorithm Demo](https://reader034.fdocuments.in/reader034/viewer/2022052318/58a2d86f1a28aba46e8b743d/html5/thumbnails/24.jpg)
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]