![Page 1: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/1.jpg)
Algorithms, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2012 · October 26, 2014 11:53:34 AM
AlgorithmsF O U R T H E D I T I O N
R O B E R T S E D G E W I C K K E V I N W A Y N E
4.4 BELLMAN-FORD DEMO
![Page 2: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/2.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
2
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
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
![Page 3: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/3.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
3
4
7
1 3
5
2
6
initialize
0
v distTo[] edgeTo[]
0 0.0 -
1
2
3
4
5
6
7
![Page 4: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/4.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
4
4
7
1 3
5
2
6
pass 0
0
0 5
∞
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
v distTo[] edgeTo[]
0 0.0 -
1
2
3
4
5
6
7
distTo[1]
distTo[0]
![Page 5: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/5.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
5
4
7
1 3
5
2
6
pass 0
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2
3
4
5
6
7
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
0
0 5
∞ 5
![Page 6: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/6.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
6
4
7
1 3
5
2
6
pass 0
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2
3
4
5
6
7
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
0
0
∞
9
![Page 7: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/7.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
7
4
7
1 3
5
2
6
pass 0
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2
3
4 9.0 0→4
5
6
7
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
0
0
∞ 9
9
![Page 8: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/8.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
8
4
7
1 3
5
2
6
0
pass 0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2
3
4 9.0 0→4
5
6
7
0
∞8
![Page 9: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/9.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
9
4
7
1 3
5
2
6
0
pass 0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
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
∞ 88
![Page 10: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/10.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
10
4
7
1 3
5
2
6
0
pass 0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
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
∞
5
12
![Page 11: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/11.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
11
4
7
1 3
5
2
6
0
pass 0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 17.0 1→2
3
4 9.0 0→4
5
6
7 8.0 0→7
∞
5
12
17
![Page 12: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/12.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
12
4
7
1 3
5
2
6
0
pass 0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
∞v distTo[] edgeTo[]
0 0.0 -
1 5.0 0→1
2 17.0 1→2
3
4 9.0 0→4
5
6
7 8.0 0→7
5
15
![Page 13: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/13.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
13
4
7
1 3
5
2
6
0
pass 0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
∞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
5
15
20
![Page 14: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/14.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
14
4
7
1 3
5
2
6
0
pass 0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
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
5
8
4
![Page 15: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/15.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
15
4
7
1 3
5
2
6
0
pass 0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
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
17
20
3
![Page 16: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/16.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
16
4
7
1 3
5
2
6
0
pass 0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
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
17
∞
11
![Page 17: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/17.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
17
4
7
1 3
5
2
6
0
pass 0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
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 28.0 2→6
7 8.0 0→7
17
∞ 28
11
![Page 18: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/18.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
18
4
7
1 3
5
2
6
0
pass 0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
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 28.0 2→6
7 8.0 0→7
20
28
9
![Page 19: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/19.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
19
4
7
1 3
5
2
6
0
pass 0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
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 28.0 2→6
7 8.0 0→7
9
∞4
![Page 20: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/20.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
20
4
7
1 3
5
2
6
0
pass 0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
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 13.0 4→5
6 28.0 2→6
7 8.0 0→7
9
∞ 134
![Page 21: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/21.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
21
4
7
1 3
5
2
6
0
pass 0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
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 13.0 4→5
6 28.0 2→6
7 8.0 0→7
9 28
20
![Page 22: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/22.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
22
4
7
1 3
5
2
6
0
pass 0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
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 13.0 4→5
6 28.0 2→6
7 8.0 0→7
9
8
5
![Page 23: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/23.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
23
4
7
1 3
5
2
6
0
pass 0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
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 13.0 4→5
6 28.0 2→6
7 8.0 0→7
17
13
1
![Page 24: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/24.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
24
4
7
1 3
5
2
6
0
pass 0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
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 28.0 2→6
7 8.0 0→7
17
13
1
14
![Page 25: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/25.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
25
4
7
1 3
5
2
6
0
pass 0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
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 28.0 2→6
7 8.0 0→7
28
13 13
![Page 26: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/26.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
26
4
7
1 3
5
2
6
0
pass 0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
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
28
13 13
26
![Page 27: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/27.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
27
4
7
1 3
5
2
6
0
pass 0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
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
8
13
6
![Page 28: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/28.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
28
4
7
1 3
5
2
6
0
pass 0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
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
8
147
![Page 29: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/29.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
29
4
7
1 3
5
2
6
pass 1
0
0 5
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
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
5
![Page 30: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/30.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
30
4
7
1 3
5
2
6
pass 1
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
0
0
9
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
9
![Page 31: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/31.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
31
4
7
1 3
5
2
6
0
pass 1
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
0
8
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
8
![Page 32: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/32.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
32
4
7
1 3
5
2
6
0
pass 1
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
5
12
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
14
![Page 33: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/33.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
33
4
7
1 3
5
2
6
0
pass 1
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
5
15 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
20
![Page 34: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/34.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
34
4
7
1 3
5
2
6
0
pass 1
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
5
8
4
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 35: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/35.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
35
4
7
1 3
5
2
6
0
pass 1
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
14
20
3
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 36: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/36.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
36
4
7
1 3
5
2
6
0
pass 1
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
14
20
3
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 26.0 5→6
7 8.0 0→7
17
2-3 successfully relaxedin pass 1, but not pass 0
![Page 37: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/37.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
37
4
7
1 3
5
2
6
0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
14
11
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 26.0 5→6
7 8.0 0→7
26
pass 1
![Page 38: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/38.jpg)
Repeat V times: relax all E edges.
26
Bellman-Ford algorithm demo
38
4
7
1 3
5
2
6
0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
14
25
11
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
2-6 successfully relaxedin pass 0 and pass 1
pass 1
![Page 39: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/39.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
39
4
7
1 3
5
2
6
0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
17
9
25
pass 1
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 40: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/40.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
40
4
7
1 3
5
2
6
0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
9
4
pass 1
13
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 41: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/41.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
41
4
7
1 3
5
2
6
0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
9 25
20
pass 1
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 42: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/42.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
42
4
7
1 3
5
2
6
0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
9
8
5
pass 1
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 43: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/43.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
43
4
7
1 3
5
2
6
0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
14
13
1
pass 1
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 44: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/44.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
44
4
7
1 3
5
2
6
0
pass 1
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
25
13 13
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 45: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/45.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
45
4
7
1 3
5
2
6
0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
8
13
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
pass 1
![Page 46: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/46.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
46
4
7
1 3
5
2
6
0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
8
147
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
pass 1
![Page 47: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/47.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
47
4
7
1 3
5
2
6
pass 2, 3, 4, … (no further changes)
0
0→1 0→4 0→7 1→2 1→3 1→7 2→3 2→6 3→6 4→5 4→6 4→7 5→2 5→6 7→5 7→2
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 48: 4.4 BELLMAN-FORD DEMOxiaojuan/algo16/slides/7DemoBellmanFor… · Repeat V times: relax all E edges. Bellman-Ford algorithm demo 3 4 7 1 3 5 2 6 initialize 0 v distTo[] edgeTo[] 0](https://reader034.vdocuments.us/reader034/viewer/2022051918/600ad23c9473ea2d4c6d5ff8/html5/thumbnails/48.jpg)
Repeat V times: relax all E edges.
Bellman-Ford algorithm demo
48
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