Max Flows 2: The Excess Scaling Algorithm and Variants. James Orlin Algorithmic ideas from the previous lecture • Augmenting path algorithm • an improvement algorithm that is guaranteed to be optimal • Max-Flow Min-Cut: • using a variation of LP duality to establish optimality and distance from optimality. • Scaling • Solving a sequence of problems that converges to the original problem. • Efficient when reoptimizing can be done quickly. • Simple data structures • keeping track of distance labels and current arcs sped up the algorithm 2 Algorithmic Ideas in this lecture • Preflows: • A relaxation of flows that leads to improved algorithms. • A new scaling algorithm • Potential function analysis • a method of establishing a faster running time by using “global” information about the algorithm. 3 Running times for max flow algorithms O(nm2) O(n2m) O(n3) O(n2 m½ ) O(nm log2 n) O(nm log n) O(nm log n2/m) Edmonds and Karp (1972) Dinic (1970) Karzanov (1974) Malhotra, Kumar & Maheshwari (1977) Cherkasky (1977) Galil and Naaman (1980) Sleator and Tarjan (1983) Goldberg and Tarjan (1988) 4 More Improvements in running time O(nm+ n2+ε ) O(nm logα n ) King Rao and Tarjan (1992) King Rao and Tarjan (1994) α = m/(n log n) O(n3 / log n ) O(nm) O(nm / log n) Cheriyan, Hagerup, and Mehlhorn (1996) O. (2011) O. (2011) if m = O(n) and under some other conditions. Remark 1: The max flow problem is solvable in O(nm / log n) steps when m = Ω(n2) and when m = O(n). Remark 2: The improvements by O. (2011) are very involved. 5 Weakly polynomial running times O(nm log U) O(nm + n2 log U) O(nm + n2 log½ U) O(m3/2 log n log U) O(n2/3 m log(n2/m) log U) O(n2/3 m log U) Gabow (1985) Ahuja and Orlin (1989) Ahuja, Orlin, and Tarjan (1989) Goldberg and Rao (1998) Goldberg and Rao (1998) O. (2011) Further improvements if log Umax/Umin = O(n1/3) Remark: The improvement by O. (2011) is very simple 6 Preflows At each intermediate stages we permit more flow arriving at nodes than leaving (except for s) A preflow is a function x: A R s.t. 0 x u and such that e(i) = ∑j∈N xji - ∑j∈N xij ≥ 0, for all i N – {s, t}. i.e., e(i) = excess at i = net excess flow into node i. The excess is required to be nonnegative. 7 A Feasible Preflow 2 2 3 0 2 5 2 1 3 s 2 4 0 3 2 3 1 The excess e(j) at each node j ≠ s, t is the flow in minus the flow out. t Note: total excess = flow out of s minus flow into t. In preflow push algorithms, we typically send flow in only one arc at a time. 8 Converting a preflow into a flow 1 21 2 23 0 2 5 2 1 23 s 32 Any preflow can be converted into a flow that preserves the flow into t. 2 4 0 2 3 t Run flow decomposition and then keep only the paths from s to t. 1 Path Flow Path Flow s-3-t 2 s-2 1 s-3 1 s-2-5-t 2 s-4-t 2 s-4-2 1 9 Distances We will maintain distance labels. Next: how to maintain distance labels efficiently. 10 Exact Distance Labels Distance labels d( ) are exact for G(x) if for all j with a path to t, d(j) is the length of the shortest path from j to t. d( ) 4 3 2 1 i 0 t P = the shortest path from i to t in G(x) An arc is (i, j) is admissible if d(i) = d(j) + 1. Lemma. Suppose all distance labels are exact. Then, an arc (i, j) is admissible if and only if it is on a shortest path from i to t. 11 Maintaining Exact Distance Labels At each iteration, CurrentArc(i) is the first admissible arc in A(i). The current arcs form a tree (or forest), whose arcs are directed into node t. 7 6 5 6 4 4 3 5 7 2 2 3 1 8 0 t 1 We will update CurrentArc(i) if it is inadmissible. We will update d(i) if there is no admissible arc out of i. Suppose CurrentArc(i) = (i, j). Arc (i, j) may become inadmissible if 1. (i, j) is saturated or 2. the distance label of j is increased. 12 An update of CurrentArc Example 1. Suppose that flow is sent in (7, 2) and the arc is saturated. 7 Then (7, 3) becomes admissible. 6 5 4 6 1 4 6 Tail Head 7 7 7 7 7 2 3 4 6 8 4 3 5 7 2 2 3 1 8 0 t 7 2 3 Res. Cap 0 1 4 2 0 8 13 Example 2: there is a cascading effect Example 2. Suppose now that (3, 8) is saturated. Node 3 increases its distance label to 4. (3, 5) is admissible. (7, 3) is inadmissible and node 7 is updated next. 7 6 5 6 1 Tail Head Res. Cap 4 4 3 5 7 3 5 6 2 2 3 3 7 2 1 8 3 8 0 0 t 14 Example 3: a gap is created Example 3. Suppose now that (5, 2) is saturated. Node 5 increases its distance label to 5. But now there is no node with distance label 3. 7 6 5 6 4 4 3 5 5 2 0 2 2 5 3 3 1 8 5 4 6 0 t 1 7 3 Tail Head Res. Cap 15 Example 3: continued ∞7 1 3 4 5 6 7 6 5 When a gap is created, all nodes with distance level above the gap have their distance level set to ∞. 4 3 2 2 1 8 0 t 16 Time for updating distance labels Lemma. 1. The arc list of each node i is scanned at most once between updates of d(i). 2. The arc list is scanned once if d(i) needs to be updated. 3. d(i) is updated at most n-1 times for each i. Conclusion. Each arc is scanned O(n) times. The total running time for updating CurrentArc and d( ) is O(nm). 17 On the order in which things are done Typically, the generic preflow push algorithm is presented before the excess scaling algorithm. But not here. 18 Scaling Algorithms 1. Define a concept called Δoptimal, where Δ is some positive integer, and where a 1-optimal solution is optimal for the original problem. 2. Develop a subroutine that efficiently determines Δ0optimum solution where Δ0 is some (possibly large) power of 2 3. Develop a subroutine Improve-Approx that transforms a Δ-optimal solution into a Δ/2-optimal solution. Generic Scaling Algorithm Δ := 2K for some selected value K determine a Δ-optimal solution x while Δ > 1 do y := ImproveApprox(x, Δ) x := y Δ := Δ/2 19 Initialize 1. Determine exact distance labels for each node. 2. For each arc (s, j), send usj units of flow from s to j. This results in an excess e(j) = usj. 3. d(s) := ∞; 4. Choose Δ0 to be the minimum power of 2 that is greater than max (usj : j ∈ N}. 5 e(5) = us5 7 e(7) = us7 s d(s) := ∞ 20 Excess- Scaling Preliminaries A preflow x is called Δ-optimal if 1. 0 ≤ e(i) < Δ for all i with 1 ≤ d(i) < ∞. 2. the distance labels are exact 3. d(s) = ∞; that is, there is no path from s to t in G(x). Subroutine ImproveApprox(x,Δ): takes a flow that is -maximum and outputs a flow that is /2-maximum. ImproveApprox terminates when e(i) < Δ/2 for all i with 1 ≤ d(i) < ∞. After initialization, the flow is Δ0-optimal. 21 Push-Relabel(i) INPUT: a node i with e(i) ≥ Δ/2. Push-Relabel(i) begin while e(i) > 0 do if there is an admissible arc (i, j) then send min {e(i), Δ - e(j) – 1, rij) units of flow in (i, j) else replace d(i) by 1 + min{d(j) : rij > 0} update r, e, CurrentArc, and d( ); end 22 Examples of Push-Relabel Δ = 64 level k+2 6 k+1 4 k Case 1. e(7) = 62 r73 = 40 e(3) = 10 Case 2. e(7) = 62 r73 = 40 e(3) = 30 7 62 40 k-1 3 Case 3. e(7) = 62 r73 = 0 10 22 29 62 0 40 7 0 52 63 30 23 Excess Scaling Algorithm Procedure Improve-Approx(r, Δ) begin while there is a Δ/2-active node do begin let i = argmin (d(i) : i is Δ/2-active} Push-Relabel(i) end end; 24 Pushing in the 64-scaling phase s 9 j 32 ≤ e(j) < 64 “large excess” 8 i 7 e(i) < 32 6 62 7 5 4 6 3 9 3 2 2 s 7 1 4 55 0 t 25 2 8 4 40 5 0 Choose a large excess node i at minimum distance level. 25 For each admissible arc (i,j), j is not large excess. Pushing in the 64-scaling phase. 62 7 2 j 5 0 7 e(i) < 32 28 Rule: push as before, except that no node excess can exceed Δ-1. 25 2 4 “large excess” 40 i 4 32 ≤ e(j) < 64 5 35 2 5 63 Push 2 units in (7,4). Then push 35 units in (7,5) Send min{e(i), rij, Δ-e(j)-1} units of flow in (i,j) 26Any non-saturating push has at least Δ/2 units of flow Bounding the number of pushes Each arc (i, j) has at most one saturating push in between relabels of i. It may have several nonsaturating pushes. We will show that the number of non-saturating pushes per scaling phase is O(n2). We will use a potential function argument. We will define a potential function Φ. • Each non-saturating push will decrease Φ by .5. We will bound this number by bounding the total increase in Φ over all iterations. 27 Elevators movements 10 9 8 7 6 5 4 3 2 1 0 An elevator is in a building that is n stories high. It can go up multiple floors at a time, but it can only go down one floor at a time. In m movements of the elevator, how many floors can it travel upwards? Note: it can travel n floors in a single move. 28 UP ≤ DOWN + n. 10 9 It can go up at most n floors more than it goes down. 8 7 6 5 4 3 2 It can go down at most m floors in m moves. In m moves, it can travel at most 2m + n floors. If m > n, it travels at most 3m floors, or fewer than 3 floors per move on average. 1 0 29 Finding a min-distance Δ/2-active node. 10 9 8 7 Δ/2-active nodes at level k 6 {j, j’} 5 4 ∅ ∅ 3 ∅ 2 {i} 1 ∅ 0 ∅ For each distance level store a doubly linked list of Δ/2-active nodes. After pushing flow from a node i, the next push is from a node with d(j) ≥ d(i) – 1. Start with distance level d(i) – 1 and seek the first distance level with a Δ/2-active node. Number of steps in m pushes is at most 2m + n. 30 Next: Bounding the number of pushes We will create a potential function Φ. We will use the up and down argument to bound the amount of decrease in Φ over all iterations. This will bound the number of pushes. 31 A potential function for excess-scaling s 9 Φ = jN e(j) d(j) / Δ 8 7 6 5 4 6 3 9 3 2 2 s 7 1 4 55 0 t 32 node 2 3 4 5 6 7 8 9 e(j) 4 0 1 0 5 35 55 40 e(j)d(j) 8 0 1 0 20 70 165 120 8 Φ = 384 / 64 = 6 The potential is the “gravitational potential” measured in units of Δ Increases in the potential function s 9 Φ = jN e(j) d(j) / Δ 8 Relabels: 7 Φ increases by at most k if d(j) increases by k. 6 Φ increases by O(n2) over all relabels. 5 4 6 3 9 3 2 2 s 7 1 4 55 0 t 33 8 Start of scaling phase. Φ doubles when Δ is replaced by Δ/2. Φ increases by O(n2 log U) over all scaling phases. Decreases in the potential function s 9 Φ = jN e(j) d(j) / Δ 8 Push: 7 Φ decreases by δ/Δ if δ units of flow are sent in (i, j) 6 Φ decreases by at least ½ in any large push 5 4 6 3 9 3 2 2 s 7 1 4 55 0 t 34 8 Conclusion: number of large pushes is at most 2n2 plus the total increase in Φ. This is O(n2 log U) large pushes. Summary of Excess Scaling Algorithm • Converts Δ-optimal flows into Δ/2-optimal flows • O(nm) running time (excluding time for large pushes) • O(n2) time for large pushes per scaling phase. • O(nm + n2 log U) time overall. 35 On the analysis of the potential function Note: the potential function Φ increased O(n2) because of relabels, and increased O(n2 log U) when Δ was replaced by Δ/2. Speeding up the algorithm: • If we can reduce the total flow at the end of the algorithm to O(nΔ/K) for some K, then the potential increase would be O( (n2 log U)/K). • We will show how to do this for K = log½ U. • Another bottleneck appears; so one cannot make K any larger. 36 General Algorithmic Rule Single bottleneck rule If the bottleneck in an algorithm is caused by one “thing”, then try to speed up the “thing.” 37 Excess Scaling with Waves Carry out a sequence of “waves” during a scaling phase. Rules for the wave. 1. Select the lowest level Δ/2-active node, if one exists. Else, select the highest level node i with 1 < e(i) < Δ/2. Push flow from the selected node. 2. If a node has no admissible arcs, then delete it from G(x) for the rest of the wave (and add it back after the wave). 3. The wave ends when there are no nodes with positive excess remaining. 4. After the wave, update the distance labels and the CurrentArc( ). 38 Excess Scaling with Waves Procedure Improve-Approx(r, Δ) begin while the total excess is greater than nΔ / log½ U do perform a wave update distance labels and current arcs end; 39 Examples of Pushes During a Wave There are no Δ/2-active nodes. Node 7 is the highest level node with excess. Arcs (7, 3) and (7, 4) are admissible. Δ = 64 level k 30 15 20 k-1 20 2 7 30Example 7 3 4 5 Example 1 Case 1. Send 20 10 7 units in arc (7, 3). Node 3 becomes 15 0 Δ/2-active. 40 3 4 15 20 5 8 3 4 3 7 30 Example 6 5 Case 2. Send 20 0 7 units in arc (7, 3). Send 10 units 5 in 0 arc (7, 4). Delete node 7 from LIST. 28 3 4 15 10 3 4 4 5 Case 3. Send 6 20 7 units in arc (7, 3). Send 4 units 0 in 0 arc (7, 4). Delete node 7 from LIST. 16 3 4 9 40 Waves during the Δ-scaling Phase 1. No node is ever Δ-active during the Δ-scaling phase. 2. At the end of the wave, each active node j (that is, e(j) > 0) will increase its distance label. (There is no path from j to t of admissible arcs). 4 6 3 7 3 2 2 1 An arc may become inadmissible when it is saturated. This can cause a “cascading effect of deletions. 1 4 55 e.g., suppose (1, 5) is saturated. 0 t suppose (2, 4) is saturated. 41 Waves during the Δ-scaling Phase 3. There is at most one non-saturating push from a node with e(i) < Δ/2. 4 6 3 7 3 2 2 1 1 4 55 0 t 4. If no nodes are Δ/2 active, then the highest level active node will never receive any more flow during the wave. If e(6) := 0, then e(6) = 0 for the rest of the wave. If the Δ-scaling phase does not end after a wave, there were at least n/log½ U distance increases. At the end of the wave, each node with excess will be relabeled. If the Δ-scaling phase didn’t end, then the total excess is at least nΔ/log½ U. 42 Putting it all together 1. 2. There are O(log U + n log½ U ) waves. Each wave leads to O(n) small non-saturating pushes. 3. Each non-saturating push from a Δ/2-active node decreases Φ by at least .5 4. Φ increases by O(n2 / log½ U ) following a scaling phase. It increases by O(n2 log½ U ) in total. 5. Conclusion: Number of non-saturating pushes is O(n2 log½ U ). Total run time because of the waves is O(n2 log½ U ), if log U < n2 43 summary of techniques • Preflows: A relaxation of flows that leads to improved algorithms. It is quite common to solve problems by solving relaxations. • Potential function analysis • a method of establishing a faster running time by using “global” information about the algorithm. • surprisingly useful in analyzing data structures and algorithms • Excess scaling • Excess scaling with waves. (If there is a single bottleneck operation or a single cause of the bottleneck, sometimes the running time can be improved by combining two approaches.) 44 Research notes on preflow push Pushing from the active node with the largest distance label leads to O(n2 m.5) nonsaturating pushes. A very efficient data structure called dynamic trees reduces the running time to O(nm log n2/m). GoldbergTarjan (1986) The “excess scaling technique” of Ahuja and Orlin (1989) reduced the running time to O(nm + n2 log U). Ahuja, Orlin, and Tarjan (1989): further very small improvements. Goldberg and Rao (1998). An even more efficient algorithm for max flows. Orlin [2011] max flows in O(nm) time and sometimes less. 45