Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms LECTURE 14 Shortest Paths II Bellman-Ford algorithm

Similar presentations


Presentation on theme: "Algorithms LECTURE 14 Shortest Paths II Bellman-Ford algorithm"— Presentation transcript:

1 Algorithms LECTURE 14 Shortest Paths II Bellman-Ford algorithm
Floyd-Warshal algorithm 4 November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

2 Negative-weight cycles
Recall: If a graph G = (V, E) contains a negative- weight cycle, then some shortest paths may not exist. Example: < 0 uu vv November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

3 Negative-weight cycles
Recall: If a graph G = (V, E) contains a negative- weight 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 © by Erik D. Demaine and Charles E. Leiserson

4 Bellman-Ford algorithm
d[s] ← 0 for each v ∈ V – {s} do d[v] ← ∞ for i ← 1 to |V| – 1 initialization do for each edge (u, v) ∈ E do if d[v] > d[u] + w(u, v) then d[v] ← d[u] + w(u, v) for each edge (u, v) ∈ E relaxation step 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 © by Erik D. Demaine and Charles E. Leiserson

5 Example of Bellman-Ford
BB 2 –1 2 AA 3 EE 1 –3 4 CC DD 5 November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

6 Example of Bellman-Ford
BB 2 –1 EE 2 AA 3 1 –3 4 CC DD 5 Initialization. November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

7 Example of Bellman-Ford
BB 1 2 –1 7 3 4 2 AA 3 EE 1 5 4 8 –3 2 6 CC DD 5 Order of edge relaxation. November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

8 Example of Bellman-Ford
BB 1 2 –1 7 3 4 2 AA 3 EE 1 5 4 8 –3 2 6 CC DD 5 November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

9 Example of Bellman-Ford
BB 1 2 –1 7 3 4 2 AA 3 EE 1 5 4 8 –3 2 6 CC DD 5 November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

10 Example of Bellman-Ford
BB 1 2 –1 7 3 4 2 AA 3 EE 1 5 4 8 –3 2 6 CC DD 5 November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

11 Example of Bellman-Ford
−1 BB 1 2 –1 7 3 4 2 AA 3 EE 1 5 4 8 –3 2 6 CC DD 5 November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

12 Example of Bellman-Ford
−1 BB 1 2 –1 7 3 4 2 AA 3 EE 1 5 4 8 –3 2 6 CC 4 DD 5 November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

13 Example of Bellman-Ford
−1 BB 1 2 –1 7 3 4 2 AA 3 EE 1 5 4 8 –3 2 6 CC 4 DD 5 November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

14 Example of Bellman-Ford
−1 BB 1 2 –1 7 3 4 2 AA 3 EE 1 5 4 8 –3 2 6 CC 2 DD 5 4 November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

15 Example of Bellman-Ford
−1 BB 1 2 –1 7 3 4 2 AA 3 EE 1 5 4 8 –3 2 6 CC 2 DD 5 November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

16 Example of Bellman-Ford
−1 BB 1 2 –1 7 3 4 2 AA 3 EE 1 5 4 8 –3 2 6 CC 2 DD 5 End of pass 1. November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

17 Example of Bellman-Ford
−1 BB 1 2 –1 1 7 3 4 2 AA 3 EE 1 5 4 8 –3 2 6 CC 2 DD 5 November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

18 Example of Bellman-Ford
−1 BB 1 2 –1 1 7 3 4 2 AA 3 EE 1 5 4 8 –3 2 6 CC 2 DD 5 November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

19 Example of Bellman-Ford
−1 BB 1 2 –1 1 7 3 4 2 AA 3 EE 1 5 4 8 –3 2 6 CC 2 DD 1 5 November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

20 Example of Bellman-Ford
−1 BB 1 2 –1 1 7 3 4 2 AA 3 EE 1 5 4 8 –3 2 6 CC 2 DD 1 5 November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

21 Example of Bellman-Ford
−1 BB 1 2 –1 1 7 3 4 2 AA 3 EE 1 5 4 8 –3 2 6 CC 2 DD 1 5 November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

22 Example of Bellman-Ford
−1 BB 1 2 –1 1 7 3 4 2 AA 3 EE 1 5 4 8 –3 2 6 CC 2 DD 1 5 November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

23 Example of Bellman-Ford
−1 BB 1 2 –1 1 7 3 4 2 AA 3 EE 1 5 4 8 –3 2 6 CC 2 DD 1 5 November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

24 Example of Bellman-Ford
−1 BB 1 2 –1 1 7 3 4 2 AA 3 EE 1 5 4 8 –3 2 6 CC 2 DD −2 5 1 November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

25 Example of Bellman-Ford
−1 BB 1 2 –1 1 7 3 4 2 AA 3 EE 1 5 4 8 –3 2 6 CC 2 DD −2 5 End of pass 2 (and 3 and 4). November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

26 Correctness Theorem. If G = (V, E) contains no negative- weight cycles, then after the Bellman-Ford algorithm executes, d[v] = δ(s, v) for all v ∈ V. November 16, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

27 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 © by Erik D. Demaine and Charles E. Leiserson

28 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 Dijkstra’s algorithm |V| times: O(VE + V 2 lg V) Three algorithms today. November 21, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

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. November 21, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

30 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 2E). Dense graph (n2 edges) ⇒ Θ(n 4) time in the worst case. Good first try! November 21, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson

31 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 Θ(n3) time. Simple to code. Efficient in practice. November 21, 2005 Copyright © by Erik D. Demaine and Charles E. Leiserson


Download ppt "Algorithms LECTURE 14 Shortest Paths II Bellman-Ford algorithm"

Similar presentations


Ads by Google