Lecture 7: Shortest Paths in Graphs with Negative Arc Lengths Reading: AM&O Chapter 5 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 dj to comprise correct shortest (s, j)-path distances: (a) for each node j, there exists at least one (s, j)-path of length dj (b) Bellman’s Equations are satisfied: ds = 0 dj = min{di + cij : i ∈ B(j)} j ̸= s. Idea of label correcting: Look for arcs (i, j) for which dj > di + cij and correct dj to satisfy dj = di + cij Generic Label Correcting Procedure { set labels dj = 0 j=s ∞ otherwise while dj > di + cij for some arc (i, j) { set dj = di + cij , Pred(j)=(i, j) } Correctness and Complexity: 1. The Pred() graph will eventually include every node, and can never produce a cycle (Why?) 2. Thus every node i will eventually have a path from s of length exactly di 3. Labels are nonincreasing, and each of the n labels can become no smaller than −nC 4. Thus the algorithm must terminate in at most n2C 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. { set labels dv = 0 v=s ∞ otherwise set LIST = {s}, while LIST ̸= ∅ begin select k ∈ LIST for i ∈ A(k) do if di > dk + cki then begin set di = dk + cki set Pred(i)=(k, i) if i ̸∈ LIST than add i to LIST end remove k from LIST end Example Problem 4 2 5 4 6 0 s 1 −5 2 6 5 4 3 node - 1 0 6 3 2 ∞ 4 3 ∞ 4 ∞ 5 ∞ 6 ∞ Example Problem LIST 1 6 2 4 5 4 6 0 s 1 −5 2 6 5 4 3 6 node – 1 1 0 0 6 3 2 ∞ 6 4 3 ∞ 6 4 ∞ ∞ 5 ∞ ∞ 6 ∞ ∞ LIST 1 2,3 Example Problem 6 2 10 5 4 4 6 0 s 1 −5 2 6 5 4 3 6 node – 1 2 1 0 0 0 6 3 2 ∞ 6 6 4 3 ∞ 6 6 4 ∞ ∞ ∞ 5 ∞ ∞ 10 6 ∞ ∞ ∞ LIST 1 2,3 3,5 Example Problem 6 2 10 5 4 4 6 0 s 1 −5 2 6 5 3 6 node – 1 2 3 1 0 0 0 0 6 3 2 ∞ 6 6 6 4 3 ∞ 6 6 6 4 ∞ ∞ ∞ 10 4 10 5 ∞ ∞ 10 10 6 ∞ ∞ ∞ ∞ LIST 1 2,3 3,5 5,4 Example Problem 6 2 10 5 4 4 6 0 s 1 −5 2 3 6 5 3 6 node – 1 2 3 5 1 0 0 0 0 0 14 6 2 ∞ 6 6 6 6 4 3 ∞ 6 6 6 6 4 ∞ ∞ ∞ 10 10 4 10 5 ∞ ∞ 10 10 10 6 ∞ ∞ ∞ ∞ 14 LIST 1 2,3 3,5 5,4 4,6 Example Problem 5 2 10 5 4 4 6 0 s 1 −5 2 3 6 5 3 6 node – 1 2 3 5 4 1 0 0 0 0 0 0 14 6 2 ∞ 6 6 6 6 5 4 3 ∞ 6 6 6 6 6 4 ∞ ∞ ∞ 10 10 10 4 10 5 ∞ ∞ 10 10 10 10 6 ∞ ∞ ∞ ∞ 14 14 LIST 1 2,3 3,5 5,4 4,6 6,2 Example Problem 5 2 9 4 5 4 6 0 s 1 −5 2 3 6 5 3 6 node – 1 2 3 5 4 6 2 1 0 0 0 0 0 0 0 0 14 6 2 ∞ 6 6 6 6 5 5 5 4 3 ∞ 6 6 6 6 6 6 6 4 ∞ ∞ ∞ 10 10 10 10 9 4 10 5 ∞ ∞ 10 10 10 10 10 10 6 ∞ ∞ ∞ ∞ 14 14 14 14 LIST 1 2,3 3,5 5,4 4,6 6,2 2 5 Example Problem 5 2 9 4 5 4 6 0 s 1 −5 2 3 6 5 3 6 node – 1 2 3 5 4 6 2 5 6 1 0 0 0 0 0 0 0 0 0 0 13 6 2 ∞ 6 6 6 6 5 5 5 5 5 4 3 ∞ 6 6 6 6 6 6 6 6 6 4 ∞ ∞ ∞ 10 10 10 10 9 9 9 4 10 5 ∞ ∞ 10 10 10 10 10 10 10 10 6 ∞ ∞ ∞ ∞ 14 14 14 14 13 13 LIST 1 2,3 3,5 5,4 4,6 6,2 2 5 6 – 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 kth 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 + cij for every arc (i, j), and (ii) d∗i = d∗j + cij 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 = cij − π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 ∗ − d∗ ≥ 0 for all arcs (i, j), (i) cπ = c + d ij ij 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 5 2 9 4 5 4 6 0 s 1 −5 2 13 6 3 6 5 3 6 4 10 4 Final Label−Correcting Distances 5 2 9 5 0 0 1 0 s 1 0 1 13 6 2 0 2 3 6 0 4 10 Adjusted Arc Weights 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(n2m) 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 + n2 log n) (Fibonacci heaps) or O(nm + √ 2 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 cij = 0 if i = j and cij = ∞ 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, dij = shortest path from i to j, and ∞ if no (i, j)-path exists). General Procedure: Produce the sequence D0, D 1, . . . , D r of n × n matrices, with D0 = C and Dr = D. Matrix Multiplication Method Dk has entries [ shortest length of an (i, j)-path k d [i, j] = containing at most k + 1 arcs ] The update from Dk−1 to Dk is the “matrix multiplication” Dk = Dk−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 Dn−2 now represents the all-pairs shortest path lengths. Complexity: O(n3 log n) (by “successive squaring”) Example 2 1 2 -2 1 1 4 3 3 D0 = 0 ∞ ∞ 1 0 1 ∞ 2 ∞ ∞ 0 ∞ ∞ −2 3 0 D1 0 −1 4 1 0 1 3 2 = ∞ ∞ 0 ∞ 0 −2 −1 0 D2 0 −1 0 1 0 1 3 2 = ∞ ∞ 0 ∞ 0 −2 −1 0 Floyd-Warshall Method Dk has entries shortest length of an (i, j)-path dk [i, j] = all of whose interior nodes are in the set {1, 2, . . . , k} The update from Dk−1 to Dk is produced by the pivot operation dk [i, j] = min{dk−1[i, j], dk−1[i, k] + dk−1[k, j]} and Dn now represents the all-pairs shortest path lengths. Floyd-Warshall Procedure Pred[i, j] = node preceding j on the shortest (i, j)path 0 Initialize: set d[i, j] = cij ∞ i=j (i, j) ∈ A i ̸= j, (i, j) ̸∈ A Pred[i, j] = i all i, j 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(n3) Example 1 2 2 -2 1 1 4 3 3 Predk Dk ( ( ( ( ( 0 2 ∞ ∞ ∞ 0 ∞ −2 ∞ 1 0 3 1 ∞ ∞ 0 0 2 ∞ ∞ ∞ 0 ∞ −2 ∞ 1 0 3 1 3 ∞ 0 0 2 ∞ 0 ∞ 0 ∞ −2 ∞ 1 0 −1 1 3 ∞ 0 0 2 ∞ 0 ∞ 0 ∞ −2 ∞ 1 0 −1 1 3 ∞ 0 0 2 ∞ 0 −1 0 ∞ −2 0 1 0 −1 1 3 ∞ 0 ) ( k=0 ) ( k=1 ) ( k=2 ( ) k=3 (no change) ) ( k=4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 1 3 4 1 2 3 2 1 2 3 4 1 2 3 2 1 1 3 4 1 2 3 2 1 2 3 4 1 2 3 2 1 1 3 4 1 2 3 2 4 2 3 4 2 2 3 2 1 1 3 4 ) ) ) ) ) Shortest (1,3)-path: 1=Pred(1,4)→4=Pred(1,2)→2=Pred(1,3)→3 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 2 2 −4 1 1 4 3 3 Predk Dk ( ( 0 2 ∞ ∞ ∞ 0 ∞ −4 ∞ 1 0 3 1 ∞ ∞ 0 0 2 ∞ ∞ ∞ 0 ∞ −4 ∞ 1 0 3 1 3 ∞ 0 0 2 ∞ −2 ∞ 0 ∞ −4 ∞ 1 0 −3 ) ( k=0 ) 1 3 ∞ −1 ( k=1 k=2 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 1 3 4 1 2 3 2 Cycle: 4=Pred(4,2)→2=Pred(4,1)→1=Pred(4,4)→4 1 2 3 4 1 2 3 2 1 1 3 1 ) ) 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 pij , 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 : i1, i2, . . . , ik = i1 which has maximum mean daily profit ∑k−1 j=1 pij ij+1 µ(W ) = ∑k−1 . j=1 τij ij+1 Choose a target value µ and attempt to determine if there is a route W which exceeds the daily profit target, µ(W ) > µ, or equivalently, ∑ (µτuv − puv ) < 0 (u,v)∈W Letting cuv = µτuv − puv , 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{puv : (u, v) ∈ A} and τ0 = max{τuv : (u, v) ∈ A} 1. The minimum value for µ∗ is −P , and the maximum value for µ∗ is P . 2. If W1 and W2 are two cycles in G with different mean daily profits µ(W1) and µ(W2), then |µ(W1) − µ(W2)| ≥ 1/(nτ0)2 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: 2. Test for a negative cycle, using the value µ0 = (µ + µ ¯)/2 3. If a negative cycle is found in G, then reset the interval to [µ, µ ¯] = [µ0, µ ¯] 4. If only positive cycles are found, then reset the interval to [µ, µ ¯] = [µ, µ0] 5. 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 ¯] is small enough by the time the interval [µ, µ so that µ ¯ − µ < 1/(nτ0)2, then there must be exactly one number µ∗ ∈ [µ, µ ¯] having denominator 1/(nτ0)2. By Fact 2 this must be the maximum mean daily profit value, and so we run Step 5 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)2P )) = O(log((nτ0)2P )), each of which requires one search for negative cycles. Using Floyd-Warshall, the complexity becomes O(n3 log((nτ0)2P ))). Example Consider the following tramp steamer problem, where the arcs are marked pij , τij : 1 4,2 3 2,2 2,1 4,2 4 2,2 2 3,1 5 Note that 1 − 3 − 4 − 5 − 2 − 1 is the maxp imum length cycle using either pij or τij for ij arc weights. Using binary search with P = 4 and τ0 = 2, we have the following sequence of stages: 1 −4 3 −2 −2 −4 4 −2 2 −3 5 [µ, µ ¯] = [−4, 4], 1 0 µ0 = 0 3 2 0 0 4 2 2 −1 [µ, µ ¯] = [0, 4], 5 µ0 = 2 1 2 3 4 1 2 4 4 2 5 0 [µ, µ ¯] = [2, 4], 1 1 µ0 = 3 3 3 1/2 1 4 3 2 −1/2 [µ, µ ¯] = [2, 3], 5 µ0 = 5 2 1 1/2 3 5/2 1/4 1/2 4 5/2 2 −3/4 [µ, µ ¯] = [2, 5/2], 5 µ0 = 9/4 At this point we obtain zero-length cycle 1 − 5 − 2, which is the minimum-mean-length cycle, of mean length 2+3+4 9 = . 1+1+2 4 Note that if no zero-length cycle was found, we would have continued until the interval size reached µ ¯ − µ < 1/(nτ0)2 = .01 which would have taken another 5 stages.
© Copyright 2024 ExpyDoc