floyd-warshall algorithm - sjtubasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7demofloydwarshall.pdf ·...
TRANSCRIPT
![Page 1: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/1.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz
2
![Page 2: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/2.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz
The shortest path from u to v that passes none vertex
2
![Page 3: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/3.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz
The shortest path from u to v that passes none vertex
2
![Page 4: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/4.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz
The shortest path from u to v that passes none vertex
2
![Page 5: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/5.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz
The shortest path from u to v that passes none vertex
2
![Page 6: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/6.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12
The shortest path from u to v that passes none vertex
2
![Page 7: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/7.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12
The shortest path from u to v that passes none vertex
2
![Page 8: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/8.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that passes none vertex
2
![Page 9: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/9.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that passes none vertex
2
![Page 10: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/10.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s
2
![Page 11: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/11.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s
2
![Page 12: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/12.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s
11
2
![Page 13: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/13.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s
11
4 2
![Page 14: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/14.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s
11
4 2
![Page 15: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/15.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s, t
11
4 2
![Page 16: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/16.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s, t
11
4 2
![Page 17: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/17.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s, t
11 15
4 2
![Page 18: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/18.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s, t
11 15
5
4 2
![Page 19: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/19.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s, t
11 15
5
4 2
![Page 20: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/20.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s, t, x
11 15
5
4 2
![Page 21: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/21.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s, t, x
11 15
5
4 2
![Page 22: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/22.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s, t, x
11 15
5
7
4 2
![Page 23: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/23.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s, t, x
11 15
5
7
4
4 2
![Page 24: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/24.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s, t, x
11 15
5
7
4
10 4 2
![Page 25: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/25.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s, t, x
11 15
5
7
4
10 4
8
2
![Page 26: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/26.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s, t, x
11 15
5
7
4
10 4
8 9
2
![Page 27: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/27.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s, t, x
11 15
5
7
4
10 4
8 9
2
![Page 28: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/28.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s, t, x, y
11 15
5
7
4
10 4
8 9
2
![Page 29: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/29.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s, t, x, y
11 15
5
7
4
10 4
8 9
2
![Page 30: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/30.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s, t, x, y
11 15
5
7
4
10
11
4
8 9
2
![Page 31: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/31.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s, t, x, y
11 15
5
7
4
10
11
4 4
8 9
2
![Page 32: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/32.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s, t, x, y
11 15
5
7
4
10
11
4
14
4
8 9
2
![Page 33: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/33.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s, t, x, y
11 15
5
7
4
10
11
4
14 6
4
8 9
2
![Page 34: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/34.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s, t, x, y
11 15
5
7
4
10
11
4
14 6
6
4
8 9
2
![Page 35: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/35.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s, t, x, y
11 15
5
7
4
10
11
4
14 6
6
4
8 9
2
![Page 36: Floyd-Warshall algorithm - SJTUbasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7DemoFloydWarshall.pdf · Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 24.3 Dijkstra’s](https://reader030.vdocument.in/reader030/viewer/2022040115/5e57f49a98995048867daada/html5/thumbnails/36.jpg)
Xiaojuan Cai, School of Software, SJTU.
Floyd-Warshall algorithm24.3 Dijkstra’s algorithm 659
0
∞ ∞
∞ ∞
0
∞
∞
1
2
10
5
(c)
10
5
0
8
5
14
7
0
8
5
13
7
0
8
5
9
7
0
5
9
7
8
6432 9
7s
t x
y z
1
2
10
5
(f)
6432 9
7s
t x
y z
1
2
10
5
(b)
6432 9
7s
t x
y z
1
2
10
5
(e)
6432 9
7s
t x
y z
1
2
10
5
(a)
6432 9
7s
t x
y z
1
2
10
5
(d)
6432 9
7s
t x
y z
Figure 24.6 The execution of Dijkstra’s algorithm. The source s is the leftmost vertex. Theshortest-path estimates appear within the vertices, and shaded edges indicate predecessor values.Black vertices are in the set S , and white vertices are in the min-priority queue Q D V ! S . (a) Thesituation just before the first iteration of the while loop of lines 4–8. The shaded vertex has the mini-mum d value and is chosen as vertex u in line 5. (b)–(f) The situation after each successive iterationof the while loop. The shaded vertex in each part is chosen as vertex u in line 5 of the next iteration.The d values and predecessors shown in part (f) are the final values.
and added to S exactly once, so that the while loop of lines 4–8 iterates exactly jV jtimes.Because Dijkstra’s algorithm always chooses the “lightest” or “closest” vertex
in V ! S to add to set S , we say that it uses a greedy strategy. Chapter 16 explainsgreedy strategies in detail, but you need not have read that chapter to understandDijkstra’s algorithm. Greedy strategies do not always yield optimal results in gen-eral, but as the following theorem and its corollary show, Dijkstra’s algorithm doesindeed compute shortest paths. The key is to show that each time it adds a vertex uto set S , we have u:d D ı.s; u/.
Theorem 24.6 (Correctness of Dijkstra’s algorithm)Dijkstra’s algorithm, run on a weighted, directed graph G D .V; E/ with non-negative weight function w and source s, terminates with u:d D ı.s; u/ for allvertices u 2 V .
2
66664
0 10 1 5 11 0 1 2 11 1 0 1 41 3 9 0 22 1 6 1 0
3
77775
s t x y z
stxyz 12 7
The shortest path from u to v that may passes s, t, x, y, z
11 15
5
7
4
10
11
4
14 6
6
4
8 9
2