Maximum Flow Some of these slides are adapted from Introduction and Algorithms by Kleinberg and Tardos. Princeton University • COS 423 • Theory of Algorithms • Spring 2001 • Kevin Wayne Contents Contents. Maximum flow problem. Minimum cut problem. Max-flow min-cut theorem. Augmenting path algorithm. Capacity-scaling. Shortest augmenting path. 2 Maximum Flow and Minimum Cut Max flow and min cut. Two very rich algorithmic problems. Cornerstone problem in combinatorial optimization. Beautiful mathematical duality. Nontrivial applications / reductions. Network connectivity. Bipartite matching. Data mining. Open-pit mining. Airline scheduling. Image processing. Project selection. Baseball elimination. Network reliability. Security of statistical data. Distributed computing. Egalitarian stable matching. Distributed computing. Many many more . . . 3 Max Flow Network Max flow network: G = (V, E, s, t, u) . (V, E) = directed graph, no parallel arcs. Two distinguished nodes: s = source, t = sink. u(e) = capacity of arc e. 10 s Capacity 5 15 2 9 5 4 15 15 10 3 8 6 10 4 6 15 10 4 30 7 t 4 Flows An s-t flow is a function f: E that satisfies: For each e E: 0 f(e) u(e) (capacity) f (e ) For each v V – {s, t}: e in to v f (e ) : e in to v 4 10 s Capacity Flow 0 5 15 0 (conservation) e out of v f (e ) : f (w, v ) w : ( w ,v ) E e out of v 2 0 9 4 4 0 15 3 4 8 4 0 0 6 4 f (e ) 30 0 f (v , w ) w : ( v ,w ) E 5 15 0 0 10 6 4 10 15 0 0 10 t 7 5 Flows An s-t flow is a function f: E that satisfies: For each e E: 0 f(e) u(e) (capacity) For each v V – {s, t}: f (e ) e in to v f (e ) (conservation) e out of v MAX FLOW: find s-t flow that maximizes net flow out of the source. f 4 10 s Capacity Flow 0 5 15 0 2 0 9 4 4 0 15 3 4 8 4 0 0 6 f (e ) e out of s 5 15 0 0 10 6 4 10 15 0 0 10 t Value = 4 4 30 0 7 6 Flows An s-t flow is a function f: E that satisfies: For each e E: 0 f(e) u(e) (capacity) For each v V – {s, t}: f (e ) e in to v f (e ) (conservation) e out of v MAX FLOW: find s-t flow that maximizes net flow out of the source. 10 10 s Capacity Flow 3 5 15 11 2 6 9 4 4 0 15 3 8 8 4 0 1 6 5 15 0 6 10 6 8 10 15 0 10 10 t Value = 24 4 30 11 7 7 Flows An s-t flow is a function f: E that satisfies: For each e E: 0 f(e) u(e) (capacity) For each v V – {s, t}: f (e ) e in to v f (e ) (conservation) e out of v MAX FLOW: find s-t flow that maximizes net flow out of the source. 10 10 s Capacity Flow 4 5 15 14 2 9 9 4 0 1 15 3 8 8 4 0 4 6 5 15 0 9 10 6 9 10 15 0 10 10 t Value = 28 4 30 14 7 8 Networks Network Nodes Arcs Flow telephone exchanges, cables, fiber optics, computers, satellites microwave relays voice, video, packets gates, registers, processors wires current joints rods, beams, springs heat, energy hydraulic reservoirs, pumping stations, lakes pipelines fluid, oil financial stocks, currency transactions money airports, rail yards, street intersections highways, railbeds, airway routes freight, vehicles, passengers sites bonds energy communication circuits mechanical transportation chemical 9 Cuts An s-t cut is a node partition (S, T) such that s S, t T. The capacity of an s-t cut (S, T) is: u(e ) : u(v , w ). e out of S (v ,w ) E v S, w T Min s-t cut: find an s-t cut of minimum capacity. 10 s 5 15 2 9 5 4 15 15 10 3 8 6 10 4 6 15 10 t Capacity = 30 4 30 7 10 Cuts An s-t cut is a node partition (S, T) such that s S, t T. The capacity of an s-t cut (S, T) is: u(e ) : u(v , w ). e out of S (v ,w ) E v S, w T Min s-t cut: find an s-t cut of minimum capacity. 10 s 5 15 2 9 5 4 15 15 10 3 8 6 10 4 6 15 10 t Capacity = 62 4 30 7 11 Cuts An s-t cut is a node partition (S, T) such that s S, t T. The capacity of an s-t cut (S, T) is: u(e ) : u(v , w ). e out of S (v ,w ) E v S, w T Min s-t cut: find an s-t cut of minimum capacity. 10 s 5 15 2 9 5 4 15 15 10 3 8 6 10 4 6 15 10 t Capacity = 28 4 30 7 12 Flows and Cuts L1. Let f be a flow, and let (S, T) be a cut. Then, the net flow sent across the cut is equal to the amount reaching t. f (e ) f (e ) e out of S 10 10 s 4 5 15 10 Value = 24 e in to S f e out of s 2 6 9 4 4 0 15 3 8 8 4 0 0 6 4 f (e ) 30 10 5 15 0 6 10 6 8 10 15 0 10 10 t 7 13 Flows and Cuts L1. Let f be a flow, and let (S, T) be a cut. Then, the net flow sent across the cut is equal to the amount reaching t. f (e ) f (e ) e out of S 10 10 s 4 5 15 10 Value = 24 e in to S f e out of s 2 6 9 4 4 0 15 3 8 8 4 0 0 6 4 f (e ) 30 10 5 15 0 6 10 6 8 10 15 0 10 10 t 7 14 Flows and Cuts L1. Let f be a flow, and let (S, T) be a cut. Then, the net flow sent across the cut is equal to the amount reaching t. f (e ) f (e ) e out of S 10 10 s 4 5 15 10 Value = 24 e in to S f e out of s 2 6 9 4 4 0 15 3 8 8 4 0 0 6 4 f (e ) 30 10 5 15 0 6 10 6 8 10 15 0 10 10 t 7 15 Flows and Cuts f (e ) f (e ) f . Let f be a flow, and let (S, T) be a cut. Then, e out of S e in to S Proof by induction on |S|. Base case: S = { s }. Inductive hypothesis: assume true for |S| < k. – consider cut (S, T) with |S| = k – S = S' { v } for some v s, t, |S' | = k-1 – adding v to S' increase cut capacity by cap(S', T') = | f |. f (e ) f (e ) 0 e out of v e in to v t t v v s s S' Before S After 16 Flows and Cuts L2. Let f be a flow, and let (S, T) be a cut. Then, | f | cap(S, T). Proof. f f (e ) f (e ) e out of S e in to S f (e ) e out of S S u (e ) e out of S 4 8 T cap(S, T) s t 7 6 Corollary. Let f be a flow, and let (S, T) be a cut. If |f| = cap(S, T), then f is a max flow and (S, T) is a min cut. 17 Max Flow and Min Cut Corollary. Let f be a flow, and let (S, T) be a cut. If |f| = cap(S, T), then f is a max flow and (S, T) is a min cut. 10 10 s 4 5 15 15 2 9 9 4 0 1 15 3 8 8 4 0 4 6 4 Cut capacity = 28 30 15 5 15 0 9 10 6 9 10 15 0 10 10 t 7 Flow value = 28 18 Max-Flow Min-Cut Theorem MAX-FLOW MIN-CUT THEOREM (Ford-Fulkerson, 1956): In any network, the value of the max flow is equal to the value of the min cut. "Good characterization." Proof IOU. 10 10 s 4 5 15 15 2 9 9 4 0 1 15 3 8 8 4 0 4 6 4 Cut capacity = 28 30 15 5 15 0 9 10 6 9 10 15 0 10 10 t 7 Flow value = 28 19 Towards an Algorithm Find an s-t path where each arc has u(e) > f(e) and "augment" flow along the path. 4 0 4 s 0 10 0 4 2 5 0 4 0 4 0 13 3 0 10 t Flow value = 0 20 Towards an Algorithm Find an s-t path where each arc has u(e) > f(e) and "augment" flow along the path. Repeat until you get stuck. 4 0 4 s X 0 10 10 0 4 2 5 0 4 0 4 X 0 10 13 3 X 0 10 10 t Flow value = 10 21 Towards an Algorithm Find an s-t path where each arc has u(e) > f(e) and "augment" flow along the path. Repeat until you get stuck. Greedy algorithm fails. Flow value = 10 4 0 4 s 10 10 2 4 5 s 10 10 0 4 0 4 4 0 4 10 13 3 5 4 5 4 5 2 5 10 10 t Flow value = 14 4 5 6 13 3 10 10 t 22 Residual Arcs Original graph G = (V, E). Flow f(e). Arc e = (v, w) E. Capacity v 17 w 6 Flow Residual graph: Gf = (V, Ef ). Residual arcs e = (v, w) and eR = (w, v). "Undo" flow sent. Residual capacity v 11 w 6 Residual capacity 23 Residual Graph and Augmenting Paths Residual graph: Gf = (V, Ef ). Ef = {e : f(e) < u(e)} {eR : f(e) > 0}. 4 0 4 G s 10 10 5 0 4 0 4 2 u (e ) f (e ) if e E uf (e ) if e R E f(e) 0 4 10 13 4 3 10 10 t 5 4 4 Gf 4 4 s 10 2 10 3 3 10 t 24 Augmenting Path Augmenting path = path in residual graph. 4 4 0 X 4 G s 10 10 5 4 X 0 4 4 X 0 4 2 0 4 X 4 10 X 6 13 4 3 10 10 t 5 4 4 Gf 4 4 s 10 2 10 3 3 10 t 25 Augmenting Path Augmenting path = path in residual graph. Max flow no augmenting paths ??? 4 4 4 G s 10 10 5 4 4 4 4 2 4 4 6 13 4 3 5 10 10 t Flow value = 14 4 4 Gf 4 4 s 10 2 6 7 3 10 t 26 Max-Flow Min-Cut Theorem Augmenting path theorem (Ford-Fulkerson, 1956): A flow f is a max flow if and only if there are no augmenting paths. MAX-FLOW MIN-CUT THEOREM (Ford-Fulkerson, 1956): the value of the max flow is equal to the value of the min cut. We prove both simultaneously by showing the TFAE: (i) f is a max flow. (ii) There is no augmenting path relative to f. (iii) There exists a cut (S, T) such that |f| = cap(S, T). 27 Proof of Max-Flow Min-Cut Theorem We prove both simultaneously by showing the TFAE: (i) f is a max flow. (ii) There is no augmenting path relative to f. (iii) There exists a cut (S, T) such that |f| = cap(S, T). (i) (ii) We show contrapositive. Let f be a flow. If there exists an augmenting path, then we can improve f by sending flow along path. (ii) (iii) Next slide. (iii) (i) This was the Corollary to Lemma 2. 28 Proof of Max-Flow Min-Cut Theorem We prove both simultaneously by showing the TFAE: (i) f is a max flow. (ii) There is no augmenting path relative to f. (iii) There exists a cut (S, T) such that |f| = cap(S, T). (ii) (iii) Let f be a flow with no augmenting paths. Let S be set of vertices reachable from s in residual graph. – clearly s S, and t S by definition of f f f (e ) e out of S S f (e ) T e in to S t u (e ) e out of S cap (S, T) s Original Network 29 Augmenting Path Algorithm Augment (f, P) b bottleneck(P) FOREACH e P IF (e E) // forward arc f(e) f(e) + b ELSE // backwards arc f(eR) f(e) - b RETURN f FordFulkerson (V, E, s, t) FOREACH e E f(e) 0 Gf residual graph WHILE (there exists augmenting path P) f augment(f, P) update Gf RETURN f 30 Running Time Assumption: all capacities are integers between 0 and U. Invariant: every flow value f(e) and every residual capacities uf (e) remains an integer throughout the algorithm. Theorem: the algorithm terminates in at most | f * | nU iterations. Corollary: if U = 1, then algorithm runs in O(mn) time. Integrality theorem: if all arc capacities are integers, then there exists a max flow f for which every flow value f(e) is an integer. Note: algorithm may not terminate on some pathological instances (with irrational capacities). Moreover, flow value may not even converge to correct answer. 31 Choosing Good Augmenting Paths Use care when selecting augmenting paths. 4 0 100 0 100 1 0 s t 100 100 0 0 2 32 Choosing Good Augmenting Paths Use care when selecting augmenting paths. 1 0 X 100 4 0 100 1 1 X 0 s 100 t 100 0 X 0 1 2 33 Choosing Good Augmenting Paths Use care when selecting augmenting paths. 4 1 100 0 100 1 1 s t 100 100 0 1 2 34 Choosing Good Augmenting Paths Use care when selecting augmenting paths. 4 1 100 0 1 X 100 0 1 X 1 s 100 t 100 1 X 0 1 2 35 Choosing Good Augmenting Paths Use care when selecting augmenting paths. 4 1 100 1 100 1 0 s t 100 100 1 1 2 200 iterations possible. 36 Choosing Good Augmenting Paths Use care when selecting augmenting paths. Some choices lead to exponential algorithms. Clever choices lead to polynomial algorithms. Goal: choose augmenting paths so that: Can find augmenting paths efficiently. Few iterations. Edmonds-Karp (1972): choose augmenting path with Max bottleneck capacity. (fat path) Sufficiently large capacity. (capacity-scaling) Fewest number of arcs. (shortest path) 37 Capacity Scaling Intuition: choosing path with highest bottleneck capacity increases flow by max possible amount. Don't worry about finding exact highest bottleneck path. Maintain scaling parameter . Let Gf () be the subgraph of the residual graph consisting of only arcs with capacity at least . 4 110 4 102 1 s 122 t 170 2 Gf 110 102 s t 122 170 2 Gf (100) 38 Capacity Scaling Intuition: choosing path with highest bottleneck capacity increases flow by max possible amount. Don't worry about finding exact highest bottleneck path. Maintain scaling parameter . Let Gf () be the subgraph of the residual graph consisting of only arcs with capacity at least . ScalingMaxFlow(V, E, s, t) FOREACH e E, f(e) 0 smallest power of 2 greater than or equal to U WHILE ( 1) Gf() -residual graph WHILE (there exists augmenting path P in Gf()) f augment(f, P) update Gf() / 2 RETURN f 39 Capacity Scaling: Analysis L1. If all arc capacities are integers, then throughout the algorithm, all flow and residual capacity values remain integers. Thus, = 1 Gf() = Gf , so upon termination f is a max flow. L2. The outer while loop repeats 1 + log2 U times. Initially U < 2U, and decreases by a factor of 2 each iteration. L3. Let f be the flow at the end of a -scaling phase. Then value of the maximum flow is at most | f | + m . L4. There are at most 2m augmentations per scaling phase. Let f be the flow at the end of the previous scaling phase. L3 |f*| | f | + m (2). Each augmentation in a -phase increases | f | by at least . Theorem. The algorithm runs in O(m2 log (2U) ) time. 40 Capacity Scaling: Analysis L3. Let f be the flow at the end of a -scaling phase. Then value of the maximum flow is at most |f| + m . We show that at the end of a -phase, there exists a cut (S, T) such that cap(S, T) | f | + m . Choose S to be the set of nodes reachable from s in Gf(). – f clearly s S, and t S by definition of S f (e ) f (e ) e out of S (u (e ) ) t e in to S u (e ) e out of S T e in to S e out of S S e out of S s e in to S cap(S, T) - mΔ Original Network 41 Choosing Good Augmenting Paths Use care when selecting augmenting paths. Some choices lead to exponential algorithms. Clever choices lead to polynomial algorithms. Goal: choose augmenting paths so that: Can find augmenting paths efficiently. Few iterations. Edmonds-Karp (1972): choose augmenting path with Max bottleneck capacity. (fat path) Sufficiently large capacity. (capacity-scaling) Fewest number of arcs. (shortest path) 42 Shortest Augmenting Path Intuition: choosing path via breadth first search. Easy to implement. – may implement by coincidence! Finds augmenting path with fewest number of arcs. ShortestAugmentingPath(V, E, s, t) FOREACH e E f(e) 0 Gf residual graph WHILE (there exists augmenting path) find such a path P by BFS f augment(f, P) update Gf RETURN f 43 Shortest Augmenting Path: Overview of Analysis L1. Throughout the algorithm, the length of the shortest path never decreases. Proof ahead. L2. After at most m shortest path augmentations, the length of the shortest augmenting path strictly increases. Proof ahead. Theorem. The shortest augmenting path algorithm runs in O(m2n) time. O(m+n) time to find shortest augmenting path via BFS. O(m) augmentations for paths of exactly k arcs. If there is an augmenting path, there is a simple one. 1k<n O(mn) augmentations. 44 Shortest Augmenting Path: Analysis Level graph of (V, E, s). For each vertex v, define (v) to be the length (number of arcs) of shortest path from s to v. LG = (V, EG) is subgraph of G that contains only those arcs (v,w) E with (w) = (v) + 1. 2 5 3 6 2 5 s 3 6 t =0 =1 =2 =3 G: s t L G: 45 Shortest Augmenting Path: Analysis Level graph of (V, E, s). For each vertex v, define (v) to be the length (number of arcs) of shortest path from s to v. L = (V, F) is subgraph of G that contains only those arcs (v,w) E with (w) = (v) + 1. Compute in O(m+n) time using BFS, deleting back and side arcs. P is a shortest s-v path in G if and only if it is an s-v path L. 2 5 3 6 L: s =0 =1 =2 t =3 46 Shortest Augmenting Path: Analysis L1. Throughout the algorithm, the length of the shortest path never decreases. Let f and f' be flow before and after a shortest path augmentation. Let L and L' be level graphs of Gf and Gf ' Only back arcs added to Gf ' – path with back arc has length greater than previous length 2 5 3 6 L s =0 =1 =2 2 5 3 6 t =3 L' s t 47 Shortest Augmenting Path: Analysis L2. After at most m shortest path augmentations, the length of the shortest augmenting path strictly increases. At least one arc (the bottleneck arc) is deleted from L after each augmentation. No new arcs added to L until length of shortest path strictly increases. 2 5 3 6 L s =0 =1 =2 2 5 3 6 t =3 L' s t 48 Shortest Augmenting Path: Review of Analysis L1. Throughout the algorithm, the length of the shortest path never decreases. L2. After at most m shortest path augmentations, the length of the shortest augmenting path strictly increases. Theorem. The shortest augmenting path algorithm runs in O(m2n) time. O(m+n) time to find shortest augmenting path via BFS. O(m) augmentations for paths of exactly k arcs. O(mn) augmentations. Note: (mn) augmentations necessary on some networks. Try to decrease time per augmentation instead. Dynamic trees O(mn log n) Simple idea Sleator-Tarjan, 1983 O(mn2) 49 Shortest Augmenting Path: Improved Version Two types of augmentations. Normal augmentation: length of shortest path doesn't change. Special augmentation: length of shortest path strictly increases. L3. Group of normal augmentations takes O(mn) time. Explicitly maintain level graph - it changes by at most 2n arcs after each normal augmentation. Start at s, advance along an arc in LG until reach t or get stuck. – if reach t, augment and delete at least one arc – if get stuck, delete node 2 5 3 6 LG s =0 =1 =2 t =3 50 Shortest Augmenting Path: Improved Version 2 5 LG augment s 3 6 2 5 t LG delete 3 s 3 6 2 5 t augment LG s 6 t 51 Shortest Augmenting Path: Improved Version 2 5 LG augment s 6 2 t 5 LG s 6 t STOP: length of shortest path must have strictly increased 52 Shortest Augmenting Path: Improved Version AdvanceRetreat(V, E, f, s, t) ARRAY pred[v V] LG level graph of Gf v s, pred[v] nil REPEAT WHILE (there exists (v,w) LG) pred[w] v, v w IF (v = t) P path defined by pred[] f augment(f, P) update LG v s, pred[v] nil delete v from LG UNTIL (v = s) advance augment retreat RETURN f 53 Shortest Augmenting Path: Improved Version Two types of augmentations. Normal augmentation: length of shortest path doesn't change. Special augmentation: length of shortest path strictly increases. L3. Group of normal augmentations takes O(mn) time. Explicitly maintain level graph - it changes by at most 2n arcs after each normal augmentation. Start at s, advance along an arc in LG until reach t or get stuck. – if reach t, augment and delete at least one arc – if get stuck, delete node – at most n advance steps before one of above events Theorem. Algorithm runs in O(mn2) time. O(mn) time between special augmentations. At most n special augmentations. 54 Choosing Good Augmenting Paths: Summary Method Augmenting path Augmentations nU Running time mnU Max capacity m log U m log U (m + n log n) Capacity scaling m log U m2 log U Improved capacity scaling m log U mn log U Shortest path mn m2n Improved shortest path mn mn2 First 4 rules assume arc capacities are between 0 and U. 55 History Year Discoverer Method Big-Oh 1951 Dantzig Simplex mn2U 1955 Ford, Fulkerson Augmenting path mnU 1970 Edmonds-Karp Shortest path m2n 1970 Dinitz Shortest path mn2 1972 Edmonds-Karp, Dinitz Capacity scaling m2 log U 1973 Dinitz-Gabow Capacity scaling mn log U 1974 Karzanov Preflow-push n3 1983 Sleator-Tarjan Dynamic trees mn log n 1986 Goldberg-Tarjan FIFO preflow-push mn log (n2 / m) ... ... ... ... Length function m3/2 log (n2 / m) log U mn2/3 log (n2 / m) log U 1997 Goldberg-Rao 56