Lecture 7: Shortest Paths in Graphs with Negative Arc Lengths Reading: AM&O Chapter
Label Correcting Methods Assume the network G is allowed to have negative arc lengths but no directed negativelyweighted cycles. Assume that there exist a directed (s, j)-path for every node j, and that all arc lengths are integer. Let C be the largest absolute value of an arc length. Recall Bellman s conditions for a set of node labels d j to comprise correct shortest (s, j)-path distances: (a) for each node j, there exists at least one (s, j)-path of length d j (b) Bellman s Equations are satisfied: d s = 0 d j = min{d i + c ij : i B(j)} j s. Idea of label correcting: Look for arcs (i, j) for which d j > d i + c ij and correct d j to satisfy d j = d i + c ij
Generic Label Correcting Procedure set labels d j = { 0 j = s otherwise while d j > d i + c ij for some arc (i, j) { set d j = d i + c ij, Pred(j)=(i, j) } Correctness and Complexity: 1. The Pred() graph will eventually include every node, and can never produce a cycle (Why?). Thus every node i will eventually have a path from s of length exactly d i. Labels are nonincreasing, and each of the n labels can become no smaller than nc. Thus the algorithm must terminate in at most n C iterations, with labels satistying Bellman s conditions. Problem: Not polynomial
Modified Label Correcting Algorithm Again we can modify FINDPATH to improve the complexity of the generic procedure. { 0 v = s set labels d v = otherwise set LIST = {s}, while LIST begin select k LIST for i A(k) do if d i > d k + c ki then begin set d i = d k + c ki set Pred(i)=(k, i) if i LIST than add i to LIST end remove k from LIST end
Example Problem 0 s 1 node 1 LIST - 0 1 Example Problem
0 s 1 node 1 LIST 0 1 1 0,
Example Problem 0 s 1 10 node 1 LIST 0 1 1 0, 0 10,
Example Problem s 10 0 1 10 node 1 LIST 0 1 1 0, 0 10, 0 10 10,
Example Problem 0 s 1 10 1 10 node 1 LIST 0 1 1 0, 0 10, 0 10 10, 0 10 10 1,
Example Problem 0 s 1 10 1 10 node 1 LIST 0 1 1 0, 0 10, 0 10 10, 0 10 10 1, 0 10 10 1,
Example Problem 9 0 1 s 1 10 node 1 LIST 0 1 1 0, 0 10, 0 10 10, 0 10 10 1, 0 10 10 1, 0 10 10 1 0 9 10 1
Example Problem 9 0 1 s 1 10 node 1 LIST 0 1 1 0, 0 10, 0 10 10, 0 10 10 1, 0 10 10 1, 0 10 10 1 0 9 10 1 0 9 10 1 0 9 10 1
Theorem: If elements from LIST are chosen in FIFO order, then the modified label correcting algorithm finds all minimum length (s, i)-paths in O(nm) steps. Proof: Because of the FIFO search order, a node i is reexamined only after every other node whose label was changed before i has been examined. Since a node that has not had its label changed will not affect any other labels, we can assume WLOG that in a particular pass of the algorithm all nodes are examined.
Claim: After the k th stage, the algorithm has determined the correct shortest path distance for all nodes i for which the shortest (s, i)-paths has at most k arcs. Proof of claim: Clearly true for k = 0. Now assume that the claim is true after k 0 passes. Since WLOG the algorithm examines all nodes in the (k + 1) st pass, then any node for whom the shortest path is of length k + 1 must be given the correct shortest path distance in the (k+1) st pass. Therefore the algorithm will have found the correct shortest distance labels after at most n 1 passes, each pass of which requires looking at possibly all of the edges. The total complexity is thus O(nm).
Converting Negative Arc Weights to Nonnegative Arc Weights Properties of shortest path distances: It follows directly from Label-Correcting Algorithm that the final set of distances d i satisfy (i) d i d j + c ij for every arc (i, j), and (ii) d i = d j + c ij for every arc in the shortest path tree. Useful Fact: Let π i, i N be any set of numbers assigned to the nodes, and set c π ij = c ij π i + π j. Then every path P between two nodes s and t will have c π -length c π (P ) = c(p ) π s + π t.
Valuable Application Suppose you solved the Shortest s-to-all Path Problem for a graph G with general arc weights, and obtained the correct shortest (s, i)-path distances d i, i N. If we set π = d above, then it follows directly from the the above two observations that (i) c π ij = c ij + d i d j 0 for all arcs (i, j), (ii) c π ij = 0 for all arcs in the shortest path tree, (iii) for any pair of nodes s and t, the shortest c π -length (s, t)-paths and the shortest c-length (s, t)-paths will be identical. In particular, it follows from (i) and (iii) that once you find all shortest (s, i)-paths for one s using, say label correcting methods, than any other shortest (i, j)-paths can be found using Dijkstra s Algorithm (which is much faster).
Example 1 9 10 1 0 9 10 Final Label Correcting Distances Adjusted Arc Weights 0 1 0 0 1 0 0 0 1 s 1 s
Example: Finding shortest pairs between all pairs of nodes involves n applications of a shortest (s, j)-path algorithm (one for each s). Using the Modified Label Correcting Algorithm will take O(n m) time, but with the above procedure the time is now O(nS(m, n)) Where S(m, n) is the fastest implementation of Dijkstra s Algorithm. This could be as small as O(nm + n log n) (Fibonacci heaps) or O(nm + n log C) (radix+fibonacci heaps).
Matrix Methods for Finding All-Pairs Shortest Paths Given: n n matrix C of arc costs for n-node graph G where c ij = 0 if i = j and c ij = if arc (i, j) is not present for which there are no negative weight directed cycles. Find: n n matrix D all-pair shortest path lengths (that is, d ij = shortest path from i to j, and if no (i, j)-path exists). General Procedure: Produce the sequence D 0, D 1,..., D r of n n matrices, with D 0 = C and D r = D.
Matrix Multiplication Method D k has entries [ d k shortest length of an (i, j)-path [i, j] = containing at most k + 1 arcs The update from D k 1 to D k is the matrix multiplication D k = D k 1 C, with the arithmetic operations redefined as follows: where numbers were multiplied, they are now added; where numbers were added, the minimum is now found. and D n now represents the all-pairs shortest path lengths. ] Complexity: O(n log n) (by successive squaring )
Example 1 1-1 D 0 = D 1 = D = 0 1 0 1 0 0 0 1 1 0 1 0 0 1 0 0 1 0 1 0 1 0 0 1 0
Floyd-Warshall Method D k has entries d k [i, j] = shortest length of an (i, j)-path all of whose interior nodes are in the set {1,,..., k} The update from D k 1 to D k is produced by the pivot operation d k [i, j] = min{d k 1 [i, j], d k 1 [i, k] + d k 1 [k, j]} and D n now represents the all-pairs shortest path lengths.
Floyd-Warshall Procedure Pred[i, j] = node preceding j on the shortest (i, j)- path Initialize: set d[i, j] = Pred[i, j] = i all i, j 0 i = j c ij (i, j) A i j, (i, j) A for k = 1,..., n do for i = 1,..., n do for j = 1,..., n do if d[i, j] > d[i, k] + d[k, j] then begin set d[i, j] = d[i, k] + d[k, j] set Pred[i, j]=pred[k, j] end Complexity: O(n )
Example 1 1-1 D k ( 0 1 0 1 0 0 ( 0 1 0 1 0 0 ( 0 1 0 1 0 0 1 0 ( 0 1 0 1 0 0 1 0 ( 0 1 0 1 0 1 0 0 1 0 ) ) ) ) ) k = 0 k = 1 k = k = (no change) k = Pred k ( 1 1 1 1 ( 1 1 1 1 1 ( 1 1 1 1 1 ( 1 1 1 1 1 ( 1 1 1 ) ) ) ) ) Shortest (1,)-path: 1=Pred(1,) =Pred(1,) =Pred(1,)
Finding Negative Cycles The Floyd-Warshall Algorithm will find negative cycles naturally. The first time a negative cycle is encountered, it will do so by creating a negative number in one of the diagonal elements of the array. The corresponding change in the Pred array will allow you to reconstruct the cycle. 1 1 1 D k ( 0 1 0 1 0 0 ( 0 1 0 1 0 0 ) ) 0 1 0 1 0 1 k = 0 k = 1 k = Pred k ( 1 1 1 1 ( 1 1 1 1 1 1 1 1 1 1 1 ) ) Cycle: =Pred(,) =Pred(,1) 1=Pred(,)
Application: The Tramp Steamer Problem A tramp steamer travels from port to port, doing business at each of the ports it visits. The net profit counting travel costs of making the trip from Port i to Port j is p ij, and the time (in days) to get from Port i to Port j is τ ij (all data are positive integers). The steamer captain wants to choose a circuit W : i 1, i,..., i k = i 1 which has maximum mean daily profit µ(w ) = k 1 j=1 p i j i j+1 k 1 j=1 τ. i j i j+1
Choose a target value µ and attempt to determine if there is a route W which exceeds the daily profit target, µ(w ) > µ, or equivalently, (u,v) W (µτ uv p uv ) < 0 Letting c uv = µτ uv p uv, we are interested in finding a negative length cycle in G. Cases: G has a negative cycle: Then µ underestimates the value of the maximum mean daily profit. Increase µ and try again. G has only positive length cycles: Then µ overestimates the value of the maximum mean daily profit. Decrease µ and try again. G has a minimum cycle W of zero length: Then µ = µ is the correct value for the cycle with maximum mean daily profit, and W is the optimal cycle.
Binary Search to Find the Correct µ Facts: Let P = max{p uv : (u, v) A} and τ 0 = max{τ uv : (u, v) A} 1. The minimum value for µ is P, and the maximum value for µ is P.. If W 1 and W are two cycles in G with different mean daily profits µ(w 1 ) and µ(w ), then µ(w 1 ) µ(w ) 1/(nτ 0 ) The binary search procedure searches for µ within an interval [µ, µ] = {µ : µ µ µ} known to contain µ, progressively halving the interval based on the outcome of the negative cycle procedure.
The Procedure 1. Begin with the interval [ P, P ]. Now for interval [µ, µ] obtained in a general step of the procedure:. Test for a negative cycle, using the value µ 0 = (µ + µ)/. If a negative cycle is found in G, then reset the interval to [µ, µ] = [µ 0, µ]. If only positive cycles are found, then reset the interval to [µ, µ] = [µ, µ 0 ]. If the minimum length cycle has zero length, then halt the algorithm, returning the value µ = µ and the corresponding cycle of maximum mean daily profit.
Termination of the algorithm: If the algorithm fails to find a zero length cycle by the time the interval [µ, µ] is small enough so that µ µ < 1/(nτ 0 ), then there must be exactly one number µ [µ, µ] having denominator 1/(nτ 0 ). By Fact this must be the maximum mean daily profit value, and so we run Step one time to find the optimal cycle W. Complexity of procedure: The number of steps in the binary search procedure is O(log((nτ 0 ) P )) = O(log((nτ 0 ) P )), each of which requires one search for negative cycles. Using Floyd-Warshall, the complexity becomes O(n log((nτ 0 ) P ))).
Example Consider the following tramp steamer problem, where the arcs are marked p ij, τ ij : 1,,,,1,1, Note that 1 1 is the maximum length cycle using either p ij or p ij τ for ij arc weights. Using binary search with P = and τ 0 =, we have the following sequence of stages:
1 [µ, µ] = [, ], µ 0 = 0 1 0 0 0 1 [µ, µ] = [0, ], µ 0 =
1 1 0 [µ, µ] = [, ], µ 0 = 1 1 1 1/ 1/ [µ, µ] = [, ], µ 0 =
1 1/ / 1/ 1/ / / [µ, µ] = [, /], µ 0 = 9/ At this point we obtain zero-length cycle 1, which is the minimum-mean-length cycle, of mean length + + 1 + 1 + = 9. Note that if no zero-length cycle was found, we would have continued until the interval size reached µ µ < 1/(nτ 0 ) =.01 which would have taken another stages.