floyd-warshall algorithm - sjtubasics.sjtu.edu.cn/~xiaojuan/algo16/slides/7demofloydwarshall.pdf ·...

36
Xiaojuan Cai, School of Software, SJTU. Floyd-Warshall algorithm 0 1 2 10 5 (a) 6 4 3 2 9 s t x y z 2 6 6 6 6 4 0 10 1 5 1 1 0 1 2 1 1 1 0 1 4 1 3 9 0 2 2 1 6 1 0 3 7 7 7 7 5 s t x y z s t x y z 2

Upload: others

Post on 21-Feb-2020

18 views

Category:

Documents


0 download

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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