Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter...
Transcript of Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter...
![Page 1: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/1.jpg)
1
Chapter 24: Single-Source Shortest-Path
![Page 2: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/2.jpg)
2
About this lecture
• What is the problem about ?
• Dijkstra’s Algorithm [1959]
• ~ Prim’s Algorithm [1957]
• Folklore Algorithm for DAG [???]
• Bellman-Ford Algorithm
• Discovered by Bellman [1958], Ford [1962]
• Allowing negative edge weights
![Page 3: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/3.jpg)
3
• Let G = (V,E) be a weighted graph
• the edges in G have weights
• can be directed/undirected
• can be connected/disconnected
• Let s be a special vertex, called source
Target: For each vertex v, compute the length of shortest path from s to v
Single-Source Shortest Path
![Page 4: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/4.jpg)
4
• E.g.,
Single-Source Shortest Path
4
8
11
8 7 9
10
14 4
2 1
2
6 7 s
4
8
11
8 7 9
10
14 4
2 1
2
6 7 s
0
4 12 19
21
11 9
14
8
![Page 5: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/5.jpg)
5
Relax • A common operation that is used in the
algorithms is called Relax :
when a vertex v can be reached from the source with a certain distance, we examine an outgoing edge, say (v,w), and check if we can improve w
• E.g., 4
8
11
8
1
2
6 7 s
0
4 ?
?
? ?
v Can we improve this?
Can we improve these?
![Page 6: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/6.jpg)
6
Dijkstra’s Algorithm
Dijkstra(G, s)
For each vertex v,
Mark v as unvisited, and set d(v) = 1 ; Set d(s) = 0 ;
while (there is unvisited vertex) {
v = unvisited vertex with smallest d ;
Visit v, and Relax all its outgoing edges; } return d ;
![Page 7: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/7.jpg)
7
Example
4
8
11
8 7
9
10
14 4
2 1
2
6 7
s 1
1
1 1
1
1 0
1
1
4
8
11
8 7 9
10
14 4
2 1
2
6 7 1
1
1
1
1 0
4
8
Relax
1 s
![Page 8: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/8.jpg)
8
Example
4
8
11
8 7
9
10
14 4
2 1
2
6 7
s 1
1
1 1
1
1 0
4
8
4
8
11
8 7 9
10
14 4
2 1
2
6 7 1
1
1
1
1 0
4
8
Relax
12 s
![Page 9: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/9.jpg)
9
Example
4
8
11
8 7
9
10
14 4
2 1
2
6 7
s 1
1
1
1
1 0
4
8
12
4
8
11
8 7 9
10
14 4
2 1
2
6 7 1
1
1
0
4
8
Relax
12
9
15
s
![Page 10: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/10.jpg)
10
Example
4
8
11
8 7
9
10
14 4
2 1
2
6 7
s 1
1
1
0
4
8
12
9
15
4
8
11
8 7 9
10
14 4
2 1
2
6 7 1
1
0
4
8
Relax
12
9
15
11
s
![Page 11: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/11.jpg)
11
Example
4
8
11
8 7
9
10
14 4
2 1
2
6 7
s 1
1
0
4
8
12
9
15
11
4
8
11
8 7 9
10
14 4
2 1
2
6 7 0
4
8
Relax
12
9
15
11
25
21
s
![Page 12: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/12.jpg)
12
Example
4
8
11
8 7
9
10
14 4
2 1
2
6 7
s 0
4
8
12
9
15
11
25
21
4
8
11
8 7 9
10
14 4
2 1
2
6 7 0
4
8
Relax
12
9
14
11
19
21
s
![Page 13: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/13.jpg)
13
Example
4
8
11
8 7
9
10
14 4
2 1
2
6 7
s 0
4
8
12
9
14
11
19
21
Relax
4
8
11
8 7
9
10
14 4
2 1
2
6 7
s 0
4
8
12
9
14
11
19
21
![Page 14: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/14.jpg)
14
Example
4
8
11
8 7
9
10
14 4
2 1
2
6 7
s 0
4
8
12
9
14
11
19
21
Relax
4
8
11
8 7
9
10
14 4
2 1
2
6 7
s 0
4
8
12
9
14
11
19
21
![Page 15: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/15.jpg)
15
Example
4
8
11
8 7
9
10
14 4
2 1
2
6 7
s 0
4
8
12
9
14
11
19
21
Relax
4
8
11
8 7
9
10
14 4
2 1
2
6 7
s 0
4
8
12
9
14
11
19
21
![Page 16: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/16.jpg)
16
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
![Page 17: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/17.jpg)
17
Correctness
Theorem:
The kth vertex closest to the source s is selected at the kth step inside the while loop of Dijkstra’s algorithm
Also, by the time a vertex v is selected, d(v) will store the length of the shortest path from s to v
How to prove ? (By induction)
![Page 18: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/18.jpg)
18
Proof • Both statements are true for k = 1 ;
• Let vj = jth closest vertex from s
• Now, suppose both statements are true for k = 1, 2, …, r-1
• Consider the rth closest vertex vr
• If there is no path from s to vr
d(vr) = 1 is never changed • Else, there must be a shortest path
from s to vr ; Let vt be the vertex immediately before vr in this path
![Page 19: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/19.jpg)
19
• Then, we have t r-1 (why??)
d(vr) is set correctly once vt is selected, and the edge (vt,vr) is relaxed (why??)
After that, d(vr) is fixed (why??)
d(vr) is correct when vr is selected ; also, vr must be selected at the rth step, because no unvisited nodes can have a smaller d value at that time
Thus, the proof of inductive case completes
Proof (cont)
![Page 20: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/20.jpg)
20
Performance
• Dijkstra’s algorithm is similar to Prim’s
• By using Fibonacci Heap (Chapter 19),
• Relax Decrease-Key
• Pick vertex Extract-Min
• Running Time:
• O(V) Insert/Extract-Min
• At most O(E) Decrease-Key
Total Time: O(E + V log V)
![Page 21: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/21.jpg)
21
Finding Shortest Path in DAG
We have a faster algorithm for DAG :
DAG-Shortest-Path(G, s)
Topological Sort G ;
For each v, set d(v) = 1 ; Set d(s) = 0 ; for (k = 1 to |V|) {
v = kth vertex in topological order ;
Relax all outgoing edges of v ; } return d ;
![Page 22: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/22.jpg)
22
Example
Topological Sort
8
s 4 3 2 6
5
11
4
8
11
3
2
6 5
s
![Page 23: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/23.jpg)
23
Example
8
s 4 3 2 6
5
11
1 0 1 1 1 1
Process this node
8
s 4 3 2 6
5
11
Relax
1 1 1 0 1 1
![Page 24: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/24.jpg)
24
Example
8
s 4 3 2 6
5
11
1 0 1 1 1 1
8
s 4 3 2 6
5
11
Relax
1 1 1 0 3 11
Process this node
![Page 25: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/25.jpg)
25
Example
Process this node
8
s 4 3 2 6
5
11
1 0 1 1 3 11
8
s 4 3 2 6
5
11
Relax
1 1 0 3 11 5
![Page 26: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/26.jpg)
26
Example
Process this node
8
s 4 3 2 6
5
11
1 0 1 3 11 5
8
s 4 3 2 6
5
11
Relax
1 0 3 10 5 11
![Page 27: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/27.jpg)
27
Example
Process this node
8
s 4 3 2 6
5
11
1 0 3 10 5 11
8
s 4 3 2 6
5
11
Relax
1 0 3 10 5 11
![Page 28: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/28.jpg)
28
Example
Process this node
8
s 4 3 2 6
5
11
Relax
1 0 3 10 5 11
8
s 4 3 2 6
5
11
1 0 3 10 5 11
![Page 29: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/29.jpg)
29
Correctness
Theorem:
By the time a vertex v is selected, d(v) will store the length of the shortest path from s to v
How to prove ? (By induction)
![Page 30: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/30.jpg)
30
Proof • Let vj = jth vertex in the topological order
• We will show that d(vk) is set correctly when vk is selected, for k = 1,2, …, |V|
• When k = 1,
vk = v1 = leftmost vertex
If it is the source, d(vk) = 0
If it is not the source, d(vk) = 1
In both cases, d(vk) is correct (why?)
Base case is correct
![Page 31: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/31.jpg)
31
Proof (cont)
• Now, suppose the statement is true for k = 1, 2, …, r-1
• Consider the vertex vr
• If there is no path from s to vr
d(vr) = 1 is never changed
• Else, we shall use similar arguments as proving the correctness of Dijkstra’s algorithm …
![Page 32: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/32.jpg)
32
• First, let vt be the vertex immediately before vr in the shortest path from s to vr
t r-1
d(vr) is set correctly once vt is selected, and the edge (vt,vr) is relaxed
After that, d(vr) is fixed
d(vr) is correct when vr is selected
Thus, the proof of inductive case completes
Proof (cont)
![Page 33: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/33.jpg)
33
Performance • DAG-Shortest-Path selects vertex
sequentially according to topological order
• no need to perform Extract-Min
• We can store the d values of the vertices in a single array Relax takes O(1) time
• Running Time:
• Topological sort : O(V + E) time
• O(V) select, O(E) Relax : O(V + E) time
Total Time: O(V + E)
![Page 34: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/34.jpg)
34
Handling Negative Weight Edges
• When a graph has negative weight edges, shortest path may not be well-defined
v 4
8
11 -7
s
-7
What is the shortest path from s to v?
E.g.,
![Page 35: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/35.jpg)
35
Handling Negative Weight Edges
• The problem is due to the presence of a cycle C, reachable by the source, whose total weight is negative
C is called a negative-weight cycle
• How to handle negative-weight edges ??
if input graph is known to be a DAG, DAG-Shortest-Path is still correct
For the general case, we can use Bellman-Ford algorithm
![Page 36: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/36.jpg)
36
Bellman-Ford Algorithm
Bellman-Ford(G, s) // runs in O(VE) time
For each v, set d(v) = 1 ; Set d(s) = 0 ; for (k = 1 to |V|-1)
Relax all edges in G in any order ; /* check if s reaches a neg-weight cycle */
for each edge (u,v), if (d(v) > d(u) + weight(u,v)) return “something wrong !!” ;
return d ;
![Page 37: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/37.jpg)
37
Example 1
4
8
3 -7 s
8
-2
10
Relax all
0
Relax all
Relax all
10
4
8
3 -7 s
8
-2
10 0
4
8
3 -7 s
8
-2
0
4
8
3 -7 s
8
-2
10 0
4
8
4
7
4
7
0
∞
∞
∞ ∞
∞
∞
∞
∞
∞
![Page 38: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/38.jpg)
38
Example 1
After the 4th Relax all
10
4
8
3 -7 s
8
-2
0
4
7
0
10
After checking, we found that there is nothing wrong distances are correct
![Page 39: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/39.jpg)
39
Example 2
4
8
3 -7 s
8
-2
1
Relax all
0
∞
∞ Relax all
Relax all
1
4
8
3 -7 s
8
-2
1 0
4
8
3 -7 s
8
-2
0
4
8
3 -7 s
8
-2
1 0
4
8
4
7
1 1
0
-7
2
∞
∞ ∞
∞
∞
![Page 40: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/40.jpg)
40
Example 2
After the 4th Relax all
1
4
8
3 -7 s
8
-2
0
-7
-8
-15
-6
After checking, we found that something must be wrong distances are incorrect
This edge shows something must
be wrong …
![Page 41: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/41.jpg)
41
Correctness (Part 1)
Theorem:
If the graph has no negative-weight cycle, then for any vertex v with shortest path from s consists of k edges, Bellman-Ford sets d(v) to the correct value after the kth Relax all (for any ordering of edges in each Relax all )
How to prove ? (By induction)
![Page 42: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/42.jpg)
42
Corollary
Corollary: If there is no negative-weight cycle, then when Bellman-Ford terminates,
d(v) ≤ d(u) + weight(u,v)
for all edge (u,v)
Proof: By previous theorem, d(u) and d(v) are the length of shortest path from s to u and v, respectively. Thus, we must have
d(v) ≤ length of any path from s to v
d(v) ≤ d(u) + weight(u,v)
![Page 43: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/43.jpg)
43
“Something Wrong” Lemma
Lemma: If there is a negative-weight cycle, then when Bellman-Ford terminates,
d(v) > d(u) + weight(u,v)
for some edge (u,v)
How to prove ? (By contradiction)
![Page 44: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/44.jpg)
44
• Firstly, we know that there is a cycle
C = (v1, v2, … , vk, v1)
whose total weight is negative
• That is, Si = 1 to k weight(vi, vi+1) ‹ 0
• Now, suppose on the contrary that
d(v) ≤ d(u) + weight(u,v)
for all edge (u,v) at termination
Proof
![Page 45: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/45.jpg)
45
• Can we obtain another bound for
Si = 1 to k weight(vi, vi+1) ?
• By rearranging, for all edge (u,v)
weight(u,v) ≥ d(v) - d(u)
Si = 1 to k weight(vi, vi+1)
≥ Si = 1 to k (d(vi+1) - d(vi)) = 0 (why?)
Contradiction occurs !!
Proof (cont)
![Page 46: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/46.jpg)
46
Correctness (Part 2)
Theorem:
There is a negative-weight cycle in the input graph if and only if when Bellman-Ford terminates,
d(v) > d(u) + weight(u,v)
for some edge (u,v)
• Combining the previous corollary and lemma, we have:
![Page 47: Chapter 24: Single-Source Shortest-Pathhscc.cs.nthu.edu.tw/~sheujp/lecture_note/10al/Chapter 24...Single-Source Shortest Path 4 • E.g., Single-Source Shortest Path 4 8 11 8 7 9 10](https://reader034.fdocuments.net/reader034/viewer/2022051806/5ffa97c9542b5e71273e9913/html5/thumbnails/47.jpg)
47
Homework
• Exercises: 24.1-1, 24.1-2, 24.-13, 24.3-1, 24.3-3
• Exercises: 24.2-4, 24.3-5 (Due: Dec. 31)