Algorithms LECTURE 14 Shortest Paths II BellmanFord algorithm
Algorithms LECTURE 14 Shortest Paths II • Bellman-Ford algorithm • Floyd-Warshal algorithm 4 November 16, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 1
Negative-weight cycles Recall: If a graph G = (V, E) contains a negativeweight cycle, then some shortest paths may not exist. … Example: <0 uu November 16, 2005 vv Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 2
Negative-weight cycles Recall: If a graph G = (V, E) contains a negativeweight cycle, then some shortest paths may not exist. … Example: <0 uu vv Bellman-Ford algorithm: Finds all shortest-path lengths from a source s ∈ V to all v ∈ V or determines that a negative-weight cycle exists. November 16, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 3
Bellman-Ford algorithm d[s] ← 0 for each v ∈ V – {s} do d[v] ← ∞ initialization for i ← 1 to | V | – 1 do for each edge (u, v) ∈ E do if d[v] > d[u] + w(u, v) relaxation then d[v] ← d[u] + w(u, v) step for each edge (u, v) ∈ E do if d[v] > d[u] + w(u, v) then report that a negative-weight cycle exists At the end, d[v] = δ(s, v), if no negative-weight cycles. Time = O(V E). November 16, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 4
Example of Bellman-Ford BB – 1 3 AA 4 November 16, 2005 CC 1 5 2 2 D D EE – 3 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 5
Example of Bellman-Ford ∞ BB – 1 0 3 AA 4 CC ∞ 1 5 2 ∞ 2 D D ∞ EE – 3 Initialization. November 16, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 6
Example of Bellman-Ford ∞ BB – 1 0 AA 4 5 4 7 3 CC ∞ 1 3 1 ∞ 2 8 2 6 5 2 D D ∞ EE – 3 Order of edge relaxation. November 16, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 7
Example of Bellman-Ford – 1 0 AA 4 5 4 November 16, 2005 ∞ BB 7 3 CC ∞ 1 3 1 ∞ 2 8 2 6 5 2 D D ∞ EE – 3 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 8
Example of Bellman-Ford – 1 0 AA 4 5 4 November 16, 2005 ∞ BB 7 3 CC ∞ 1 3 1 ∞ 2 8 2 6 5 2 D D ∞ EE – 3 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 9
Example of Bellman-Ford – 1 0 AA 4 5 4 November 16, 2005 ∞ BB 7 3 CC ∞ 1 3 1 ∞ 2 8 2 6 5 2 D D ∞ EE – 3 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 10
Example of Bellman-Ford – 1 0 AA 4 5 4 November 16, 2005 − 1 ∞ BB 7 3 CC ∞ 1 3 1 ∞ 2 8 2 6 5 2 D D ∞ EE – 3 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 11
Example of Bellman-Ford – 1 0 AA 4 5 4 November 16, 2005 − 1 BB 7 3 CC 4 ∞ 1 3 1 ∞ 2 8 2 6 5 2 D D ∞ EE – 3 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 12
Example of Bellman-Ford – 1 0 AA 4 5 4 November 16, 2005 − 1 BB 7 3 CC 4 1 3 1 ∞ 2 8 2 6 5 2 D D ∞ EE – 3 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 13
Example of Bellman-Ford – 1 0 AA 4 5 4 November 16, 2005 − 1 BB 7 3 CC 2 4 1 3 1 ∞ 2 8 2 6 5 2 D D ∞ EE – 3 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 14
Example of Bellman-Ford – 1 0 AA 4 5 4 November 16, 2005 − 1 BB 7 3 CC 2 1 3 1 ∞ 2 8 2 6 5 2 D D ∞ EE – 3 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 15
Example of Bellman-Ford − 1 BB – 1 0 AA 4 5 4 7 3 CC 2 1 3 1 ∞ 2 8 2 6 5 2 D D ∞ EE – 3 End of pass 1. November 16, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 16
Example of Bellman-Ford – 1 0 AA 4 5 4 November 16, 2005 − 1 BB 7 3 CC 2 1 3 1 1 ∞ 2 8 2 6 5 2 D D ∞ EE – 3 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 17
Example of Bellman-Ford – 1 0 AA 4 5 4 November 16, 2005 − 1 BB 7 3 CC 2 1 3 1 1 2 8 2 6 5 2 D D ∞ EE – 3 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 18
Example of Bellman-Ford – 1 0 AA 4 5 4 November 16, 2005 − 1 BB 7 3 CC 2 1 3 1 1 2 8 2 6 5 2 D D 1 ∞ EE – 3 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 19
Example of Bellman-Ford – 1 0 AA 4 5 4 November 16, 2005 − 1 BB 7 3 CC 2 1 3 1 5 1 2 8 2 6 2 D D 1 EE – 3 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 20
Example of Bellman-Ford – 1 0 AA 4 5 4 November 16, 2005 − 1 BB 7 3 CC 2 1 3 1 5 1 2 8 2 6 2 D D 1 EE – 3 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 21
Example of Bellman-Ford – 1 0 AA 4 5 4 November 16, 2005 − 1 BB 7 3 CC 2 1 3 1 5 1 2 8 2 6 2 D D 1 EE – 3 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 22
Example of Bellman-Ford – 1 0 AA 4 5 4 November 16, 2005 − 1 BB 7 3 CC 2 1 3 1 5 1 2 8 2 6 2 D D 1 EE – 3 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 23
Example of Bellman-Ford – 1 0 AA 4 5 4 November 16, 2005 − 1 BB 7 3 CC 2 1 3 1 1 2 8 2 6 5 2 D D − 2 1 EE – 3 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 24
Example of Bellman-Ford − 1 BB – 1 0 AA 4 5 4 7 3 CC 2 1 3 1 1 2 8 2 6 5 2 D D − 2 EE – 3 End of pass 2 (and 3 and 4). November 16, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 25
Correctness Theorem. If G = (V, E) contains no negativeweight cycles, then after the Bellman-Ford algorithm executes, d[v] = δ(s, v) for all v ∈ V. November 16, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 18. 26
Shortest paths Single-source shortest paths • Nonnegative edge weights ◆Dijkstra’s algorithm: O(E + V lg V) • General ◆Bellman-Ford algorithm: O(VE) • DAG ◆One pass of Bellman-Ford: O(V + E) November 21, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 19. 27
Shortest paths Single-source shortest paths • Nonnegative edge weights ◆Dijkstra’s algorithm: O(E + V lg V) • General ◆Bellman-Ford: O(VE) • DAG ◆One pass of Bellman-Ford: O(V + E) All-pairs shortest paths • Nonnegative edge weights ◆Dijkstra’s algorithm |V| times: O(VE + V 2 lg V) • General ◆Three algorithms today. November 21, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 19. 28
All-pairs shortest paths Input: Digraph G = (V, E), where V = {1, 2, …, n}, with edge-weight function w : E → R. Output: n × n matrix of shortest-path lengths δ(i, j) for all i, j ∈ V. November 21, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 19. 29
All-pairs shortest paths Input: Digraph G = (V, E), where V = {1, 2, …, n}, with edge-weight function w : E → R. Output: n × n matrix of shortest-path lengths δ(i, j) for all i, j ∈ V. IDEA: • Run Bellman-Ford once from each vertex. • Time = O(V 2 E). • Dense graph (n 2 edges) ⇒ Θ(n 4) time in the worst case. Good first try! November 21, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 19. 30
Pseudocode for Floyd- Warshall for k ← 1 to n do for i ← 1 to n do for j ← 1 to n do if cij > cik + ckj then cij ← cik + ckj relaxation Notes: • Okay to omit superscripts, since extra relaxations can’t hurt. • Runs in Θ(n 3) time. • Simple to code. • Efficient in practice. November 21, 2005 Copyright © 2001 -5 by Erik D. Demaine and Charles E. Leiserson L 19. 31
- Slides: 31