Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 18 Prof. Erik Demaine.

Similar presentations


Presentation on theme: "1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 18 Prof. Erik Demaine."— Presentation transcript:

1 1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 18 Prof. Erik Demaine

2 2 Negative-weight cycles Recall: If a graph G = (V, E) contains a negative- weight cycle, then some shortest paths may not exist. 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. © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18.

3 3 Bellman-Ford algorithm d[s] 0 for each v V – {s} do d[v] for i 1 to |V| – 1 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 do if d[v] > d[u] + w(u, v) then report that a negative-weight cycle exists At the end, d[v] = (s, v). Time = O(VE) initialization relaxation step © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18.

4 4 Example of Bellman-Ford © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18.

5 5 Example of Bellman-Ford © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18.

6 6 Example of Bellman-Ford © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18.

7 7 Example of Bellman-Ford © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18.

8 8 Example of Bellman-Ford © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18.

9 9 Example of Bellman-Ford © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18.

10 10 Example of Bellman-Ford © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18.

11 11 Example of Bellman-Ford © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18.

12 12 Example of Bellman-Ford Note: Values decrease monotonically. © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18.

13 13 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. Proof. Let v V be any vertex, and consider a shortest path p from s to v with the minimum number of edges. Since p is a shortest path, we have (s, v i ) = (s, v i-1 ) + w(v i-1, v i ). © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18.

14 14 Correctness (continued) Initially, d[v 0 ] = 0 = (s, v 0 ), and d[s] is unchanged by subsequent relaxations (because of the lemma from Lecture 17 that d[v] (s, v)). After 1 pass through E, we have d[v 1 ] = (s, v 1 ). After 2 passes through E, we have d[v 2 ] = (s, v 2 ). After k passes through E, we have d[v k ] = (s, v k ). Since G contains no negative-weight cycles, p is simple. Longest simple path has |V| – 1 edges. © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18.

15 15 Detection of negative- weight cycles Corollary. If a value d[v] fails to converge after |V| – 1 passes, there exists a negative-weight cycle in G reachable from s. © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18.

16 16 DAG shortest paths If the graph is a directed acyclic graph (DAG), we first topologically sort the vertices. Determine f : V {1, 2, …, |V|} such that (u, v) E f (u) < f (v). O(V + E) time using depth-first search. Walk through the vertices u V in this order, relaxing the edges in Adj[u], thereby obtaining the shortest paths from s in a total of O(V + E) time. © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18.

17 17 Linear programming Let A be an m× n matrix, b be an m-vector, and c be an n-vector. Find an n-vector x that maximizes c T x subject to Ax b, or determine that no such solution exists. © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18.

18 18 Linear-programming algorithms Algorithms for the general problem Simplex methods practical, but worst-case exponential time. Ellipsoid algorithm polynomial time, but slow in practice. Interior-point methods polynomial time and competes with simplex. Feasibility problem: No optimization criterion. Just find x such that Ax b. In general, just as hard as ordinary LP. © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18.

19 19 Solving a system of difference constraints Linear programming where each row of A contains exactly one 1, one –1, and the rest 0s. Example: Solution: Constraint graph: (The A matrix has dimensions |E| × |V|.) © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18. x 1 – x 2 3 x 2 – x 3 –2 x 1 – x 3 2 x j – x i w ij x 1 = 3 x 2 = 0 x 3 = 2 x j – x i w ij

20 20 Unsatisfiable constraints Theorem. If the constraint graph contains a negative-weight cycle, then the system of differences is unsatisfiable. Proof. Suppose that the negative-weight cycle is v 1 v 2 v k v 1. Then, we have Therefore, no values for the x i can satisfy the constraints. © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18. x 2 – x 1 w 12 x 3 – x 2 w 23 x k – x k-1 w k–1, k x 1 – x k w k1 0 weight of cycle < 0

21 21 Satisfying the constraints Theorem. Suppose no negative-weight cycle exists in the constraint graph. Then, the constraints are satisfiable. Proof. Add a new vertex s to V with a 0-weight edge to each vertex v i V. Note: No negative-weight cycles introduced shortest paths exist. © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18.

22 22 Proof (continued) Claim: The assignment x i = (s, v i ) solves the constraints. Consider any constraint x j – x i w ij, and consider the shortest paths from s to v j and v i : The triangle inequality gives us (s,v j ) (s, v i ) + w ij. Since x i = (s, v i ) and x j = (s, v j ), the constraint x j – x i w ij is satisfied. © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18.

23 23 Bellman-Ford and linear programming Corollary. The Bellman-Ford algorithm can solve a system of m difference constraints on n variables in O(mn) time. Single-source shortest paths is a simple LP problem. In fact, Bellman-Ford maximizes x 1 + x 2 + + x n subject to the constraints x j – x i w ij and x i 0 (exercise). Bellman-Ford also minimizes max i {x i } – min i {x i } (exercise). © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18. …

24 24 Application to VLSI layout compaction Integrated -circuit features: minimum separation λ Problem: Compact (in one dimension) the space between the features of a VLSI layout without bringing any features too close together. © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18.

25 25 VLSI layout compaction Constraint: x 2 – x 1 d 1 + λ Bellman-Ford minimizes max i {x i } – min i {x i }, which compacts the layout in the x-dimension. © 2001 by Charles E. Leiserson Introduction to Algorithms Day 31 L18.


Download ppt "1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 18 Prof. Erik Demaine."

Similar presentations


Ads by Google