DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of...
Transcript of DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of...
![Page 1: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/1.jpg)
ROBERT SEDGEWICK | KEVIN WAYNE
F O U R T H E D I T I O N
Algorithms
http://algs4.cs.princeton.edu
Algorithms ROBERT SEDGEWICK | KEVIN WAYNE
DIJKSTRA'S ALGORITHM DEMO
![Page 2: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/2.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
2
0
4
7
1 3
5
2
6
s
69
8
4
5
7
1
54
15
312
20
13
11
9
an edge-weighted digraph
0→1 5.0
0→4 9.0
0→7 8.0
1→2 12.0
1→3 15.0
1→7 4.0
2→3 3.0
2→6 11.0
3→6 9.0
4→5 4.0
4→6 20.0
4→7 5.0
5→2 1.0
5→6 13.0
7→5 6.0
7→2 7.0
![Page 3: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/3.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
3
0
4
7
1 3
5
2
6
choose source vertex 0
v distTo[] edgeTo[]
0 0.0 -
1
2
3
4
5
6
7
![Page 4: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/4.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
4
4
7
1 3
5
2
6
relax all edges pointing from 0
9
8
5
0
0
∞
∞
∞
v distTo[] edgeTo[]
0 0.0 -
1
2
3
4
5
6
7
![Page 5: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/5.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
∞
Dijkstra's algorithm demo
5
4
7
1 3
5
2
6
relax all edges pointing from 0
9
8
5
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2
3
4 9.0 0→4
5
6
7 8.0 0→7
0
∞
5
0
∞
8
9
![Page 6: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/6.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
6
0
4
7
3
5
2
6
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2
3
4 9.0 0→4
5
6
7 8.0 0→7
1
![Page 7: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/7.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
7
0
4
7
1 3
5
2
6
choose vertex 1
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2
3
4 9.0 0→4
5
6
7 8.0 0→7
![Page 8: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/8.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
8
0
4
7
1 3
5
2
6
relax all edges pointing from 1
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2
3
4 9.0 0→4
5
6
7 8.0 0→7
4
15
12
5
∞
∞
8
![Page 9: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/9.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
9
0
4
7
1 3
5
2
6
relax all edges pointing from 1
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 17.0 1→2
3 20.0 1→3
4 9.0 0→4
5
6
7 8.0 0→7
4
15
12
✔
∞
∞5
17
20
8
![Page 10: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/10.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
10
0
4
1 3
5
2
6
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 17.0 1→2
3 20.0 1→3
4 9.0 0→4
5
6
7 8.0 0→7
7
![Page 11: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/11.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
11
0
4
7
1 3
5
2
6
choose vertex 7
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 17.0 1→2
3 20.0 1→3
4 9.0 0→4
5
6
7 8.0 0→7
![Page 12: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/12.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
∞
Dijkstra's algorithm demo
12
0
4
7
1 3
5
2
6
relax all edges pointing from 7
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 17.0 1→2
3 20.0 1→3
4 9.0 0→4
5
6
7 8.0 0→7
6
78
17
![Page 13: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/13.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
13
0
4
7
1 3
5
2
6
relax all edges pointing from 7
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 15.0 7→2
3 20.0 1→3
4 9.0 0→4
5 14.0 7→5
6
7 8.0 0→7
6
78
17
∞ 14
15
![Page 14: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/14.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
14
0
4
7
1 3
5
2
6
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 15.0 7→2
3 20.0 1→3
4 9.0 0→4
5 14.0 7→5
6
7 8.0 0→7
![Page 15: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/15.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
15
0
4
7
1 3
5
2
6
select vertex 4
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 15.0 7→2
3 20.0 1→3
4 9.0 0→4
5 14.0 7→5
6
7 8.0 0→7
![Page 16: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/16.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
16
0
4
7
1 3
5
2
6
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 15.0 7→2
3 20.0 1→3
4 9.0 0→4
5 14.0 7→5
6
7 8.0 0→7
relax all edges pointing from 4
4
5
20
8
14
9 ∞
![Page 17: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/17.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
17
0
4
7
1 3
5
2
6
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 15.0 7→2
3 20.0 1→3
4 9.0 0→4
5 13.0 4→5
6 29.0 4→6
7 8.0 0→7
relax all edges pointing from 4
4
5
20
✔
∞ 29
8
14
9
13
![Page 18: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/18.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
18
0
4
7
1 3
5
2
6
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 15.0 7→2
3 20.0 1→3
4 9.0 0→4
5 13.0 4→5
6 29.0 4→6
7 8.0 0→7
![Page 19: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/19.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
19
0
4
7
1 3
5
2
6
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 15.0 7→2
3 20.0 1→3
4 9.0 0→4
5 13.0 4→5
6 29.0 4→6
7 8.0 0→7
select vertex 5
![Page 20: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/20.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
20
0
4
7
1 3
5
2
6
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 15.0 7→2
3 20.0 1→3
4 9.0 0→4
5 13.0 4→5
6 29.0 4→6
7 8.0 0→7
relax all edges pointing from 5
1
13
29
13
15
![Page 21: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/21.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
21
0
4
7
1 3
5
2
6
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 14.0 5→2
3 20.0 1→3
4 9.0 0→4
5 13.0 4→5
6 26.0 5→6
7 8.0 0→7
relax all edges pointing from 5
1
13
29
13
15 14
26
![Page 22: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/22.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
22
0
4
7
1 3
5
2
6
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 14.0 5→2
3 20.0 1→3
4 9.0 0→4
5 13.0 4→5
6 26.0 5→6
7 8.0 0→7
![Page 23: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/23.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
23
0
4
7
1 3
5
2
6
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 14.0 5→2
3 20.0 1→3
4 9.0 0→4
5 13.0 4→5
6 26.0 5→6
7 8.0 0→7
select vertex 2
![Page 24: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/24.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
24
0
4
7
1 3
5
2
6
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 14.0 5→2
3 20.0 1→3
4 9.0 0→4
5 13.0 4→5
6 26.0 5→6
7 8.0 0→7
relax all edges pointing from 2
3
11
26
14
20
![Page 25: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/25.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
25
0
4
7
1 3
5
2
6
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 14.0 5→2
3 17.0 2→3
4 9.0 0→4
5 13.0 4→5
6 25.0 2→6
7 8.0 0→7
relax all edges pointing from 2
3
11
26
14
20 17
25
![Page 26: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/26.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
26
0
4
7
1 3
5
2
6
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 14.0 5→2
3 17.0 2→3
4 9.0 0→4
5 13.0 4→5
6 25.0 2→6
7 8.0 0→7
![Page 27: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/27.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
27
0
4
7
1 3
5
2
6
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 14.0 5→2
3 17.0 2→3
4 9.0 0→4
5 13.0 4→5
6 25.0 2→6
7 8.0 0→7
select vertex 3
![Page 28: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/28.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
28
0
4
7
1
5
2
6
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 14.0 5→2
3 17.0 2→3
4 9.0 0→4
5 13.0 4→5
6 25.0 2→6
7 8.0 0→7
relax all edges pointing from 3
9
3
25
20
![Page 29: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/29.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
29
0
4
7
1
5
2
6
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 14.0 5→2
3 17.0 2→3
4 9.0 0→4
5 13.0 4→5
6 25.0 2→6
7 8.0 0→7
relax all edges pointing from 3
9
✔
3
25
20
![Page 30: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/30.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
30
0
4
7
1
5
2
6
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 14.0 5→2
3 17.0 2→3
4 9.0 0→4
5 13.0 4→5
6 25.0 2→6
7 8.0 0→7
3
![Page 31: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/31.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
31
0
4
7
1
5
2
6
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 14.0 5→2
3 17.0 2→3
4 9.0 0→4
5 13.0 4→5
6 25.0 2→6
7 8.0 0→7
3
select vertex 6
![Page 32: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/32.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
32
0
4
7
1
5
2
6
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 14.0 5→2
3 17.0 2→3
4 9.0 0→4
5 13.0 4→5
6 25.0 2→6
7 8.0 0→7
3
relax all edges pointing from 6
![Page 33: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/33.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
33
0
4
7
1
5
2
6
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 14.0 5→2
3 17.0 2→3
4 9.0 0→4
5 13.0 4→5
6 25.0 2→6
7 8.0 0→7
3
![Page 34: DIJKSTRA S ALGORITHM DEMO - Princeton University · ・Consider vertices in increasing order of distance from s (non-tree vertex with the lowest distTo[] value). ・Add vertex to](https://reader034.fdocuments.in/reader034/viewer/2022050418/5f8e3114c16fe357626ec69d/html5/thumbnails/34.jpg)
・Consider vertices in increasing order of distance from s
(non-tree vertex with the lowest distTo[] value).
・Add vertex to tree and relax all edges pointing from that vertex.
Dijkstra's algorithm demo
34
0
4
7
1
5
2
6
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 14.0 5→2
3 17.0 2→3
4 9.0 0→4
5 13.0 4→5
6 25.0 2→6
7 8.0 0→7
3
shortest-paths tree from vertex s
s