OR 215 Network Flows Spring 1999 M. Hartmann THE LABEL CORRECTING ALGORITHM Optimality Conditions Label Correcting Algorithm Shortest Paths in Acyclic Networks (again) All-Pairs Shortest Paths Problem Floyd-Warshall Algorithm Successive Shortest Paths Algorithm OPTIMALITY CONDITIONS Setting: Directed graph G = (N, A) cij = length of arc (i,j) node 1 is the source d(j) = length of a shortest path from 1 to j Optimality Conditions: P1. d(j) = min { d(i) + cij : i N } for j 1. P2. d(1) = 0. Why are these conditions necessary? Are these conditions sufficient? (What does it mean to be sufficient?) 0 1 1 3 2 0 0 4 Lemma. Let d(j) be the minimum distances from node 1 to each node j. Then d(j) satisfies P1 and P2. Theorem. Let f(1),…,f(n) satisfy the following properties for a directed graph G = (N, A): 1. f(1) = 0 2. f(i) is the length of some path from node 1 to node i 3. f(j) f(i) + cij for all (i,j) A Then f(j) is the minimum length of a path from node 1 to node j. Proof: By condition 2, f(j) is an upper bound on the length of a shortest path from 1 to j. We will show that it is also a lower bound. Consider any directed path P from node 1 to node j. Let P consist of nodes 1 = i1,i2,i3,...,ik = j. Condition 3 implies that f(i2) f(i1) + ci1i2 = ci1i2 f(i3) f(i2) + ci2i3 ... f(ik) f(ik-1) + cik-1ik Adding these inequalities yields f(j) = f(ik) (i,j)P cij . Therefore f(j) is a lower bound on the length of any directed path from the source to node j, including a shortest path from s to j. A GENERIC SHORTEST PATH ALGORITHM Notation: d(j) = length of a path from 1 to j (at the end of the algorithm d(j) is the shortest path length) Pred(j) = Predecessor of j in the path of length d(j) from node 1 to node j cij = length of arc (i,j) Major Assumption: There is no negative cycle. (We will relax this assumption later.) algorithm LABEL CORRECTING; begin d(1) := 0 and Pred(1) := ; d(j) := for each j N \ {1}; while some arc (i,j) satisfies d(j) > d(i) + cij do begin d(j) := d(i) + cij; Pred(j) := i ; end; end 3 1 2 1 1 1 2 3 1 3 4 4 d( ) Pred Example: Scan arcs (1,2), (1,3), (3,4), (2,3) in order. Proof of Finiteness: At each iteration d(j) decreases by an integral amount for some j; moreover, each d(j) is bounded from below by -nC, where C = max { |cij| : (i,j) A } Proof of Correctness: Suppose that the algorithm terminates. Look at the ending conditions. What if we were allowed to have a negative cycle? 4 2 2 4 3 1 3 2 3 1 5 1 2 3 4 5 d( ) 0 Pred 2 3 4 5 2 3 4 5 1 d( ) Pred 1 d( ) Pred SHORTEST PATHS IN AN ACYCLIC NETWORK (or longest paths) Preprocessing Step: Label the nodes 1,…,n so that i < j for all (i,j) A (topological sort). The Algorithm: Let L = {1,2,...,n}. Carry out the label correcting algorithm, by scanning the adjacency lists A(1),..., A(n) in that order. (The algorithm terminates after scanning each arc just once.) 3 1 2 3 2 5 1 1 6 1 2 4 2 2 7 1 3 7 8 4 6 5 Theorem: If the label correcting algorithm is applied to an acyclic network that is topologically sorted, then the minimum distances are determined in O(m) steps. IMPROVEMENTS Type 1: Choose a rule for selecting arcs in Step 2 so as to improve the computational complexity. Type 2: Choose rules for selecting arcs in Step 2 so as to improve the running time in practice. A. Maintain a node list L during the algorithm. If we select i L, then we will examine all of A(i), rather than just examining a single arc (i,j). B. Avoid looking at arcs when possible. Modified Label Correcting: After examining the arcs in A(i), do not consider A(i) until d(i) is decreased; add i to L after d(i) is decreased. C. Consider different rules for scanning the nodes from L; e.g., scan L in a FIFO ordering. Pape: The first time time a d(j) changes, add j to the end of L; on all subsequent changes add j to the beginning of L. Empirical Study: Cherkassy, Goldberg and Radzik in Math. Programming 73 (1996) 129-174. THE MODIFIED LABEL CORRECTING ALGORITHM Suppose d(j) d(i) + cij at some iteration. The d( )’s are monotonically decreasing, so if at some later iteration d(j) > d(i) + cij , then d(i) must have decreased. algorithm MODIFIED LABEL CORRECTING; begin d(1) := 0 and Pred(1) := ; d(j) := for each j N \ {1}; LIST := {1}; while LIST do begin take out an element i from LIST; for each (i,j) A(i) if d(j) > d(i) + cij then begin d(j) := d(i) + cij ; pred(j) := i; if j LIST then add j to LIST; end; end; end Theorem. Suppose that G = (N,A) has no negative length cycles. The modified label correcting algorithm using a queue (FIFO) implementation of LIST finds a shortest path from 1 to j for all j in N in O(nm) steps. Proof: Let d(j) denote the length of the shortest path from node 1 to node j. A pass of the algorithm consists of scanning those nodes j whose distance labels d(j) changed (decreased) in the previous pass. 3 1|32|43|4 1 1 2 1 3 1 4 pass 3 A(1) = {(1,3), (1,2)} We will show that the label correcting algorithm finds the shortest paths after at most n-1 passes. First we can show by induction that d(j) is always the length of some walk from 1 to j; hence d(j) is an upper bound on the length of the shortest path from 1 to j (because there are no negative cycles). Claim: After k passes, the algorithm correctly determines d(j) for all nodes j for which there is a shortest path from node 1 to node j consisting of at most k arcs. (Note that this implies that the algorithm finds all of the shortest paths after at most n -1 passes.) Proof by induction: It is easily seen to be true for k = 1. After one pass, the algorithm finds all shortest paths consisting of a single arc. Assume inductively that the claim is true for value k-1. 1 i j shortest path from 1 to j consisting of k arcs After k -1 passes, the label d(i) is the length of the shortest path from node 1 to node i. If the label d(i) is set on pass l < k, the algorithm scans arc (i,j) and compares the label d(j) to d(i) + cij on pass l or pass l +1. ALL-PAIRS SHORTEST PATHS PROBLEM Problem: Find the shortest paths from node i to node j, for each pair [i,j] of nodes in the network G=(V,A). Reallocation of Housing: A housing authority has many houses at its disposal, grouped according to number of bedrooms, rent, etc. As tenants requirements change, the housing authority would like to relocate them to the group of their choice. Simple exchanges are not always possible; a cyclic change may involve multiple moves. How can we identify a cyclic change? If many tenant reassigments are possible, how can the authority’s management identify a cyclic change with the fewest number of moves? FLOYD-WARSHALL ALGORITHM Let dk[i,j] be the shortest length of a path from i to j , all of whose internal nodes are among {1,2,…,k -1}. Property. dk+1[i,j] = min { dk[i,j] , dk[i,k] + dk[k,j] }. algorithm FLOYD -WARSHALL; begin d[i,j] := and Pred[i,j] := for all i j ; d[i,i] := 0 for each i N ; d[i,j] := cij and Pred[i,j] := i for each (i,j) A ; for each k = 1 to n do for each [i,j] if d[i,j] > d[i,k] + d[k,j] then begin d[i,j] := d[i,k] + d[k,j] ; all-pairs label correcting step Pred[i,j] := Pred[k,j] ; end; end This O(n3) algorithm is well suited for dense graphs. What about sparse graphs? TRANSFORMING NEGATIVE ARC LENGTHS TO NON-NEGATIVE ARC LENGTHS For any path P, let c(P) denote the length of P. Let be any real valued n-vector. Let c ij = cij + (i) - (j) . Let c(P) denote the length of P with respect to c. Claim: If P is a path from i to j , c(P) = c(P) + (i) - (j) . Proof by induction: It is true for paths consisting of a single arc. Assume that it is true for paths with k -1 arcs. l i Let P' be the path from i to l. Then c(P) = c(P') + c lj = [ c(P') + (i) - (l) ] + [ clj + (l) - (j) ] = c(P) + (i) - (j) . j Corollary. A shortest path from i to j with respect to c must also be a shortest path from i to j with respect to c. SUCCESSIVE SHORTEST PATHS ALGORITHM Idea: Choose so that c ij = cij + (i) - (j) 0 for all (i,j). Let (j) = d(j), the length of a shortest path from 1 to j. By the optimality conditions, d(i) + cij d(j) for all (i,j). An all-pairs shortest path algorithm: (Negative arc lengths allowed, but no negative length cycles.) Step 1. Find the shortest path from node 1 to all other nodes. Step 2. Replace cij by cij + d(i) - d(j) for all arcs (i,j) A. Step 3. Run Dijkstra's algorithm starting from node j , for each j = 2 to n. Running time: O(nm + n(m + n log C)) = O(nm + n2 log C).