Lecture 10: Polynomial Time Algorithms for Max Flows (Reading: AM&O Ch. 7) Complexity of the Ford-Fulkerson Algorithm A naive application of the Ford-Fulkerson Labeling Algorithm is O(mnU ), where U is the maximum of the arc capacities. This is not a polynomial-time algorithm. Techniques for improving complexity: 1. Augment flow along shortest length augmenting paths 2. Augment flow along maximum capacity augmenting paths 3. Push flow simultaneously along multiple augmenting paths Shortest Length Augmenting Paths Fact: If flow is augmented along shortest augmenting paths, then max flow is reached after at most mn/2 flow augmentations. To prove this, we will give a labeling scheme which successively finds shortest augmenting paths, until no more augmenting paths can be found. At any stage of the F-F Algorithm, let G(x) be the current residual graph. We maintain a set d(i) of labels that will be used to find the length of the shortest (i, t)-path in G(x). The labels d(i) will always satisfy d(t) = 0 d(i) ≤ d(j) + 1, (i, j) ∈ G(x) (∗) We will successively update these labels until shortest (s, t)-path Γ = s = i1, i2, . . . , ik = t is found in G(x), that is, each arc (ij , ij+1) satisfies the Bellman Equations, that is they satisfy (∗) and in addition d(ij ) = d(ij+1) + 1. Call such an arc admissible. The updating procedure tries to find an (s, t) path of admissible arcs. This is done through a depth-first search, except that when a backtrack occurs from a node i the distance label d(i) is corrected using Bellman’s Equations: d(i) = min{d(j) + 1 : (i, j) ∈ G(x)}. Shortest Augmenting Path Algorithm Initialize: Set x = 0, construct G(x), and find the shortest (i, t) path distances d(i), and set i = s. while d(s) < n if i = t then (augment) Augment flow along the path indicated by P red(), and set i = s else if there exists an admissible arc (i, j) in G(x) then (advance) Set P red(j) = i, i = j else (retreat) Set d(i) = min{d(j) + 1 : (i, j) ∈ G(x)}, i = P red(i). end if end while Shortest Augmenting Path Example [3] s [2] 5 4 1 [2] 7 6 9 4 1 [1] 3 7 2 2 10 [3] 4 3 [2] [3] 5 6 [1] [2] 3 [0] [2] 7 4 1 8 t 8 4 5 6 2 4 1 9 6 [1] 3 2 2 10 [3] 4 3 [2] [4] 8 2 [1] [3] 5 6 1 2 6 [1] 3 3 [2] [7] 3 5 8 4 [1] [3] 5 [0] [2] 3 4 1 6 5 4 4 3 1 2 1 [1] 7 1 3 [6] 2 6 3 [5] 3 3 5 [4] 6 7 3 7 2 4 8 2 2 2 4 1 7 2 [3] 5 [2] 4 3 2 3 [0] 4 1 8 2 2 5 6 7 1 7 4 8 [0] 5 8 Lemma 1: At each stage of the Shortest Augmenting Path Algorithm the d(i)’s continue to satisfy (∗). Further, all labels are nondecreasing and for each retreat operation from node i the distance label on i strictly increases. Proof: That labels are nondecreasing follows from the labeling procedure. For the other two properties, consider each possible move. advance: No change occurs either in G(x) nor in d(). retreat: No change occurs in G(x). Then the d(i)’s continue to satisfy (∗) and since there are no admissible arcs out of i then d(i) < d(j) + 1 for all (i, j) ∈ G(x), so that d(i) must strictly increase. Finally, since d(i) increases and no other d value changes, then (∗) holds for any other i′. augment: No change occurs in the d(i)’s, but arcs will be removed, and sometimes added, to G(x). A removed arc can never cause (∗) to be violated, and an arc (i, j) can only be added in the reverse direction along the admissible (s, t)-path, that is, d(i) = d(j) − 1 < d(j) + 1. Thus (∗) is still satisfied. bottleneck arc: arc of G(x) which is saturated in the augmentation operation (that is, the actual arc is a forward arc driven to capacity, or a backward arc driven to 0). Lemma 2: Let arc (i, j) be a bottleneck arc (in the same direction) in two successive flow augmentation operations. Then between the first and second augmentations both d(i) and d(j) must increase by at least 2 units. Proof: Since arc (i, j) is involved in two augmentations for which it is the bottleneck, then it must be involved in an intermediate augmentation for which (j, i) appears on the augmenting path. Let d(·), d′(·), and d′′(·) be the distance labels immediately before each of these three augmentations. Since augmentations are done along shortest augmenting paths and labels are nondecreasing, then d′′(i) = d′′(j)+1 ≥ d′(j)+1 = d′(i)+2 ≥ d(i)+2. Ditto for d(j). Conclusion: The max flow is reached after at most mn/2 shortest path flow augmentations. Proof: 1. Every flow augmentation has at least one bottleneck arc. 2. Every successive time an (i, j) is a bottleneck arc, the label of i increases by at least 2 units. 3. The largest value of any label d(i) is the maximum length of a (i, t)-path, which is n − 1. 4. Therefore each arc can be a bottleneck in at most n/2 flow augmentations. It follows that there can be at most mn/2 flow augmentations until the max flow is found. Theorem 1: The Shortest Augmenting Path Algorithm runs in O(n2m) time. Proof: Initializing distances: O(m) (using breadth-first search) Number of augment steps: mn/2 (Lemma 2) Time per augment step: O(n) Number of retreat steps: n2 (n increases per label) Time for retreat/relabel step: O(|A(i)|) times the number of retreats for node i, or a total of O(mn) for all retreats/relabels Number of advance steps: number of retreat steps + n(number of augmentations) = n2 + n2m/2 Time per advance step: O(1) Total complexity: O(m + n2m + n2 + mn) = O(n2m) Important Property of Residual Networks There is a 1-1 correspondence between flows xr in G(x) of value v r and flows x̃ in G of value v +v r defined as follows: x̃ij = xij + xrij − xrji xrij = (x̃ij − xij )+ (positive part) xrji = (xij − x̃ij )+ By the construction of G(x) it follows that for any valid capacitated flow xr in G(x) of value v r , the corresponding flow x̃ is a valid capacitated flow in G whose value is exactly v + v r . Vice versa, any valid capacitated flow f˜ in G of value ṽ corresponds to valid capacitated flow flow xr in G(x) with value v r = ṽ − v. x ij ,u ij 2 7,8 8,10 1,1 1 5,6 3 8,8 8,10 4 0,1 1 6,6 6,10 3 Current flow r 1 8 4 1 1 1 7 1,1 1,1 4 5 x rij 1,1 2 7 8 6,10 2 1 2 4 New flow 2 ij x~ ij 2 6 3 Residual network 4 4 5 6 3 Flow In particular, if xr = x∗ is the max flow in G(x) of value v ∗, then the corresponding flow x̃ in G must be a max flow as well, with value v ∗ + v. Maximum Capacity Augmenting Paths An alternative choice for augmenting paths is to augment along maximum capacity augmenting paths. Let U be the maximum capacity of an arc in G. Fact(AM&O Sect. 7.3): If flow is always augmented along maximum capacity paths, then the max flow will be reached after at most 2m log nU flow augmentations. Proof: Let v ∗ be the maximum flow in G. At any stage of the Max Flow algorithm, let x be the current flow with value v, and consider flow in the residual network G(x). By the property of flows in residual networks, we know that the max flow in G(x) has value v ∗ − v. Further, by the Flow Decomposition Theorem, this flow can be decomposed into at most m (s, t)-paths with positive flow, and so at one of these must have value at least (v ∗ − v)/m. In particular, the first augmentation in a max capacity augmentation routine will decrease the flow by at least (v ∗ − v)/m units. Now consider a set of 2m consecutive maximum capacity flow augmentations starting at flow x. If each of these augments flow by at least (v ∗ − v)/2m units, then by the end of 2m flow augmentations we have reached max flow. On the other hand, if one of these augmentations is less than (v ∗ − v)/2m units, then at that point the max flow in the residual network must have value less than (v ∗ − v)/2, that is, the flow value of the residual network has decreased by a factor of 2. Since the flow in the original (residual) network is at most nU , then the maximum number of augmentations is at most 2m log nU as required. Conclusion: The implementation of the Max Flow Algorithm using maximum capacity augmenting paths has complexity O(m(m+n log n) log nU ), (using Fibonacci heaps). Bottleneck: Finding max capacity augmenting paths is expensive. Solution: Find “large capacity” augmenting paths instead. More specifically, ith scaling phase: Find all augmenting paths of capacity at least ∆ = U/2i. This is done by considering the ∆-residual network G(x, ∆) of edges of G(x) having capacity at least ∆. G(x, ∆) can be constructed and updated in O(m) time, and the appropriate path is found by applying FINDPATH to G(f, ∆). Capacity Scaling Algorithm Initialize: Set x = 0, ∆ = U/2 while ∆ ≥ 1 while G(x, ∆) contains an (s, t)-path do Find path P in G(x, ∆) Push max flow through P and update G(x, ∆) end do while ∆ = ∆/2 end do while Capacity Scaling Example 2 2 5 27 19 s 16 1 24 3 2 2 4 32 5 27 19 s t 9 17 ∆=16 6 24 16 1 5 27 6 t 24 1 6 17 9 17 3 ∆=8 2 32 2 3 5 10 19 16 1 4 32 4 7 17 2 19 17 6 16 1 6 9 17 3 15 3 9 4 15 4 17 ∆=4 2 2 5 10 10 9 1 7 17 7 9 17 3 6 2 5 12 10 17 6 7 7 1 3 9 6 6 4 4 26 ∆=2 2 2 5 4 4 1 15 1 23 1 15 17 32 4 23 6 9 4 3 2 1 2 5 4 6 Theorem 2: The capacity scaling algorithm solves the maximum flow problem in O(m2 log U ) time. Proof: We show that the algorithm performs at most 2m augmentations during each scaling phase. At the end of the ith scaling phase with value ∆ there are no more (s, t)-paths in G(x, ∆). By the Duality Theorem for (s, t)paths, this means that there must be an (s, t)cut (X, X̄) having no arcs of G(x, ∆), i.e., all arcs of G(x) in (X, X̄) have capacity less than ∆. This means that the max flow in G(x) can be at most the capacity of (X, X̄) in G(x), which is in turn less than m∆. Now consider the (i + 1)st scaling phase. Since the max flow in G(x) is at at most m∆, and the capacity of every augmenting path is at least ∆/2 in this phase, then there can be at most 2m flow augmentations occurring in this phase. Since there are at most log U scaling phases then there are at most 2m log U flow augmentations. Finding the paths in G(x, ∆) requires O(m) time, for a total of O(m2 log U ) for the algorithm. Improving Capacity Scaling to O(mn log U ): Use the Shortest Augmenting Path Algorithm to send flow along shortest augmenting paths in G(x, ∆). Since the number of augmentations in each scaling phase is at most 2m, then the number of augment and advance steps per scaling phase is now 2m and 2mn, respectively, for a total of O(m + mn + n2) = O(mn) per scaling phase, or O(mn log U ) overall. Other Improvements Augmenting along Multiple Paths: Idea: Push as much flow as you can through all augmenting paths simultaneously, by pushing flow along individual arcs subject to capacity and “throughput constraints”. Pre-flow Push: Instead of reconstructing the shortestpath network each time, modify the Shortest Path Algorithm to allow you to augment flow through all shortest paths while you update labels. This involves a “two-wave” process, pushing flow toward t — possibly leaving excess flow at nodes — and then going back toward s correcting any excess flow at each node. Complexity: Depends upon the search order. “Generic” Preflow-Push: O(n2m) Dinic’s Algorithm (FIFO Preflow Push implementation): Construct the subnetwork Gs of G(x) consisting of all shortest length (augmenting) paths. Pre-flow push can be implemented to find the max flow in Gs in O(n2) time.) Repeat this for successively longer paths (using a variant of Lemma 2), until the longest — i.e. (n − 1)-arc — paths have been saturated. Total complexity: O(n3) (See AM&O, Sections 7.5&7.7) √ 2 “Highest Label” Preflow-Push: O(n m)) “Excess Scaling” Preflow-Push: O(nm + n2 log U ) Using Dynamic Trees The time-consuming operations in the Shortest Augmenting Path Algorithm turn out to be (a) performing the flow augmentations and (b) having to advance over parts of shortest augmenting paths that do not change after the augmentation. To alleviate this, we maintain a set of path fragments, that is, pieces of shortest augmenting paths, that we can hook together to form longer and longer path fragments until a shortest augmenting (s, t)-path is found. Then we can use one number to keep track of the max flow/residual capacity in each path fragment. Implementational Issues: 1. How many fragments are we going to need to keep track of at one time? 2. When we hook up fragments (not necessarily at their endpoints) how do we change the residual flow of the resulting fragment(s)? 3. When we find an augmenting path, how do we break up the fragment at the bottleneck arcs and adjust the residual capacities along the resulting fragments? Data structure: dynamic trees: fragments are kept in rooted partial shortest path (in-)trees, keeping track of the capacity of the (unique) path any node of this tree to its root. When a new shortest augmenting path arc is added, it hooks up the root of one tree to some node of another tree, and the nodes of the new tree are updated simultaneously to represent the larger path fragment. When a tree contains an (s, t)-path, then an augmentation occurs, causing the tree fragment to be cut at each of the bottleneck arcs, with the path capacities on all appropriate path fragments updated to reflect the augmentation. Fact: The associated dynamic tree operations involved with the above procedure can be performed in “average” time O(log n) per flow augmentation, and so the dynamic tree implementation of the shortest augmenting path algorithm solves the max flow problem in O(mn log n) time. This is the best known data-independent complexity for sparse graphs. However, the implementation of this algorithm is so complex and the data structures involve so much overhead that the dynamic tree algorithm is of limited (currently no) practical use. Summary of Max Flow Algorithms Naive Augmenting Path Naive Shortest Augmenting Path Naive Maximum Capacity Path Shortest Augmenting Path with Labeling Capacity Scaling Generic Preflow-Push FIFO Preflow-push Highest Label Preflow-Push Excess Scaling Preflow-Push Dynamic Tree Shortest Augmenting Path O(nmU ) O(nm2 ) O(m(m + n log n) log U ) O(n2 m) O(nm log U ) O(n2 m) O(n3 )√ O(n2 m)) O(nm + n2 log U ) O(nm log n) Algorithms for Special Cases Bipartite Networks O(n21 m) (where n1 = the smaller of the two parts of the√bipartition) Planar networks O(n n log n) (s, t)-planar undirected networks O(n log n) √ 2/3 m, m m}) Unit capacitiy networks O(min{n √ Unit capacity “simple” networks O(m n) Apparent lower bound on the max flow problem O(mn), but nobody has managed to give a compelling reason why.