Chapter 7 • Ford-Fulkerson algorithm • Step-by-step walk through of an example • Worst-case number of augmentations • Edmunds-Karp modification • Time complexity bounds May 3 Soviet Rail Network, 1955 Reference: On the history of the transportation and maximum flow problems. Alexander Schrijver in Math Programming, 91: 3, 2002. Maximum Flow and Minimum Cut Max flow and min cut. Two very rich algorithmic problems. Cornerstone problems in combinatorial optimization. Beautiful mathematical duality. Nontrivial applications / reductions. Data mining. Open-pit mining. Project selection. Airline scheduling. Bipartite matching. Baseball elimination. Image segmentation. Network connectivity. Network reliability. Distributed computing. Egalitarian stable matching. Security of statistical data. Network intrusion detection. Multi-camera scene reconstruction. Many many more . . . Minimum Cut Problem Flow network. Abstraction for material flowing through the edges. G = (V, E) = directed graph, no parallel edges. Two distinguished nodes: s = source, t = sink. c(e) = capacity of edge e. 10 source s capacity 5 15 2 9 5 4 15 15 10 3 8 6 10 4 6 15 4 30 7 10 t sink Cuts Def. An s-t cut is a partition (A, B) of V with s A and t B. cap( A, B) Def. The capacity of a cut (A, B) is: c(e) e out ofA Capacity = 10 + 5 + 15 = 30 10 s 5 2 9 5 4 15 15 10 3 8 6 10 4 6 15 4 30 7 A 15 10 t Cuts Def. An s-t cut is a partition (A, B) of V with s A and t B. Def. The capacity of a cut (A, B) is: cap( A, B) 5 s A 15 e out ofA Capacity = 9 + 15 + 8 + 30 = 62 10 c(e) 2 9 5 4 15 15 10 3 8 6 10 4 6 15 4 30 7 10 t Minimum Cut Problem Min s-t cut problem. Find an s-t cut of minimum capacity. Capacity = 10 + 8 + 10 = 28 10 5 s A 15 2 9 5 4 15 15 10 3 8 6 10 4 6 15 4 30 7 10 t Minimum Cut Problem Min s-t cut problem. Find an s-t cut of minimum capacity. Suppose the edge capacity represents the cost of removing the edge. What is the least expensive way to disrupt connection from s to t? This is the min-cut problem. Apparently, this is the version that motivated researchers in Rand corporation with a view of breaking down the Russian railway system. 10 5 s A 15 Capacity = 10 + 8 + 10 = 28 2 9 5 4 15 15 10 3 8 6 10 4 6 15 4 30 7 10 t Flows Def. An s-t flow is a function that satisfies: For each e E: 0 f (e) c(e) For each v V – {s, t}: f (e) f (e) (capacity) (conservation) e in tov Def. The value of a flow f is: e out ofv v( f ) f (e) . e out of s 0 4 10 2 9 4 4 0 15 0 5 s 5 0 15 0 4 4 3 8 10 6 0 capacity 15 flow 0 4 0 6 4 t 0 15 0 30 10 0 10 Value = 4 7 Flows Def. An s-t flow is a function that satisfies: For each e E: 0 f (e) c(e) For each v V – {s, t}: f (e) f (e) (capacity) (conservation) e in tov Def. The value of a flow f is: 10 10 5 v( f ) f (e) . e out of s 6 2 9 4 4 0 15 3 s e out ofv 5 6 15 0 8 8 3 8 10 6 1 capacity 15 flow 11 4 0 6 4 t 10 15 11 30 10 0 10 Value = 24 7 Maximum Flow Problem Max flow problem. Find s-t flow of maximum value. 10 5 s 10 2 9 4 15 3 8 5 9 1 15 10 6 10 9 10 9 0 0 15 4 flow 4 6 4 30 8 4 14 15 7 10 0 0 capacity t 14 Value = 28 Java applet implementation: http://www-b2.is.tokushima-u.ac.jp/~ikeda/suuri/maxflow/MaxflowApp.shtml?demo5 What is the capacity of the cut < {s}, V \ {s} >? 12 Java applet implementation: http://www-b2.is.tokushima-u.ac.jp/~ikeda/suuri/maxflow/MaxflowApp.shtml?demo5 What is the capacity of the cut < {s}, V \ {s} >? 10 What is the capacity of the cut < V\ {v9, t}, {v9, t}>? 13 Java applet implementation: http://www-b2.is.tokushima-u.ac.jp/~ikeda/suuri/maxflow/MaxflowApp.shtml?demo5 What is the capacity of the cut < {s}, V \ {s} >? 10 What is the capacity of the cut < V\ {v9, t}, {v9, t}>? 8 14 Java applet implementation: http://www-b2.is.tokushima-u.ac.jp/~ikeda/suuri/maxflow/MaxflowApp.shtml?demo5 What is the capacity of the min-cut? 15 Java applet implementation: http://www-b2.is.tokushima-u.ac.jp/~ikeda/suuri/maxflow/MaxflowApp.shtml?demo5 What is the capacity of the min-cut? It is not obvious! Can you find a cut of capacity 6? 16 Java applet implementation: http://www-b2.is.tokushima-u.ac.jp/~ikeda/suuri/maxflow/MaxflowApp.shtml?demo5 What is the capacity of the min-cut? It is not obvious! Can you find a cut of capacity 6? Answer: < {s, v1}, V \ {s, v1}> Max-flow algorithm will find a cut of minimum capacity. 17 Flows and Cuts Flow value lemma. Let f be any flow, and let (A, B) be any s-t cut. Then, the net flow sent across the cut is equal to the amount leaving s. f (e) f (e) v( f ) e out ofA 6 2 9 4 4 0 15 10 10 3 s e in to A 5 5 6 15 0 8 8 3 A 8 10 6 1 15 4 0 11 6 4 t 10 15 11 30 10 0 10 Value = 24 7 Flows and Cuts Flow value lemma. Let f be any flow, and let (A, B) be any s-t cut. Then, the net flow sent across the cut is equal to the amount leaving s. f (e) f (e) v( f ) e out ofA 6 2 9 4 4 0 15 10 10 3 s e in to A 5 5 6 15 0 8 8 3 A 8 10 6 1 15 4 0 11 6 30 t 10 15 11 4 10 7 0 10 Value = 6 + 0 + 8 - 1 + 11 = 24 Flows and Cuts Flow value lemma. Let f be any flow, and let (A, B) be any s-t cut. Then, the net flow sent across the cut is equal to the amount leaving s. f (e) f (e) v( f ) e out ofA 6 2 9 4 4 0 15 10 10 3 s e in to A 5 5 6 15 0 8 8 3 A 8 10 6 1 15 4 0 11 6 30 t 10 15 11 4 10 7 0 10 Value = 10 - 4 + 8 - 0 + 10 = 24 Flows and Cuts Flow value lemma. Let f be any flow, and let (A, B) be any s-t cut. Then f (e) f (e) v( f ) . e out ofA Pf. e in toA v( f ) f (e) e out ofs f (e) f (e) v A e out ofv e in to v f (e) f (e). e out ofA e in to A by flow conservation, all terms except v = s are 0 Flows and Cuts Weak duality. Let f be any flow, and let (A, B) be any s-t cut. Then the value of the flow f is at most the capacity of the cut (A, B). Cut capacity = 30 10 s 5 Flow value 30 2 9 5 4 15 15 10 3 8 6 10 4 6 15 4 30 t A 15 10 Capacity = 30 7 Flows and Cuts Weak duality. Let f be any flow. Then, for any s-t cut (A, B) we have v(f) cap(A, B). Pf. A v( f ) f (e) f (e) e out of A 4 f (e) 8 e in to A e out of A c (e) s e out of A cap(A,B ) ▪ 7 6 B t Certificate of Optimality Corollary. Let f be any flow, and let (A, B) be any cut. If v(f) = cap(A, B), then f is a max flow and (A, B) is a min cut. Value of flow = 28 Cut capacity = 28 Flow value 28 9 2 9 4 1 15 10 10 0 4 5 s 5 9 15 0 9 8 3 8 10 6 4 A 15 4 0 14 6 10 15 14 4 30 10 7 0 10 t Towards a Max Flow Algorithm Greedy algorithm. Start with f(e) = 0 for all edge e E. Find an s-t path P where each edge has f(e) < c(e). Augment flow along path P. Repeat until you get stuck. 1 0 0 20 10 t 30 0 s 10 20 0 0 2 Flow value = 0 Towards a Max Flow Algorithm Greedy algorithm. Start with f(e) = 0 for all edge e E. Find an s-t path P where each edge has f(e) < c(e). Augment flow along path P. Repeat until you get stuck. 1 20 X 0 0 20 10 30 X 0 20 s t 10 20 0 X 0 20 2 Flow value = 20 Towards a Max Flow Algorithm Greedy algorithm. Start with f(e) = 0 for all edge e E. Find an s-t path P where each edge has f(e) < c(e). Augment flow along path P. Repeat until you get stuck. locally optimality global optimality 1 20 20 s 0 10 20 20 t 30 20 10 0 greedy = 20 1 s 20 20 2 10 10 10 10 opt = 30 t 30 10 20 20 2 Residual Graph Original edge: e = (u, v) E. Flow f(e), capacity c(e). capacity u v 17 6 flow Residual edge. "Undo" flow sent. e = (u, v) and eR = (v, u). Residual capacity: residual capacity u c(e) f (e) if e E c f (e) if e R E f (e) Residual graph: Gf = (V, Ef ). Residual edges with positive residual capacity. Ef = {e : f(e) < c(e)} {eR : c(e) > 0}. 11 v 6 residual capacity Ford-Fulkerson Algorithm 2 4 4 capacity G: s 10 2 8 6 10 10 3 9 5 10 t Augmenting Path Algorithm Augment(f, c, P) { b bottleneck(P) foreach e P { if (e E) f(e) f(e) + b else f(eR) f(e) - b } return f } forward edge reverse edge Ford-Fulkerson(G, s, t, c) { foreach e E f(e) 0 Gf residual graph while (there exists augmenting path P) { f Augment(f, c, P) update Gf } return f } Max-Flow Min-Cut Theorem Augmenting path theorem. Flow f is a max flow iff 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. Proof strategy. We prove both simultaneously by showing the TFAE: (i) There exists a cut (A, B) such that v(f) = cap(A, B). (ii) Flow f is a max flow. (iii) There is no augmenting path relative to f. (i) (ii) This was the corollary to weak duality lemma. (ii) (iii) We show contrapositive. Let f be a flow. If there exists an augmenting path, then we can improve f by sending flow along path. Proof of Max-Flow Min-Cut Theorem (iii) (i) Let f be a flow with no augmenting paths. Let A be set of vertices reachable from s in residual graph. By definition of A, s A. By definition of f, t A. v( f ) f (e) f (e) e out ofA e in to A A c(e) B e out ofA t cap(A, B) s original network The basic Ford Fulkerson algorithm 1 for each edge (u, v) E [G] 2 do f [u, v] = 0 3 f [v, u] = 0 4 while there exists a path p from s to t in the residual network Gf 5 do cf (p) = min {cf (u, v) | (u, v) is in p} 6 for each edge (u, v) in p 7 do f [u, v] = f [u, v] + cf (p) 8 f [v, u] = - f [u, v] The basic Ford Fulkerson algorithm example of an execution 1 2 3 4 (residual) network Gf v1 12 v3 S t v2 14 v4 5 6 7 8 for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v] The basic Ford Fulkerson algorithm example of an execution 1 2 3 4 (residual) network Gf v1 0/12 v3 S t v2 0/14 v4 5 6 7 8 for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v] The basic Ford Fulkerson algorithm example of an execution 1 2 3 4 (residual) network Gf v1 12 v3 S t v2 14 v4 5 6 7 8 for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v] The basic Ford Fulkerson algorithm example of an execution 1 2 3 4 (residual) network Gf v1 12 v3 S t v2 14 v4 5 6 7 8 for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v] temporary variable: cf (p) = 12 The basic Ford Fulkerson algorithm example of an execution 1 2 3 4 (residual) network Gf v1 12 v3 S t v2 14 new flow network 12/12 G v1 5 6 7 8 v4 v3 S t v2 14 for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v] v4 temporary variable: cf (p) = 12 The basic Ford Fulkerson algorithm example of an execution 1 2 3 4 (residual) network Gf v1 12 v3 S t v2 14 5 6 7 8 v4 new flow network G v1 12/12 v3 S t v2 14 v4 for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v] The basic Ford Fulkerson algorithm example of an execution 1 2 3 4 (residual) network Gf v1 12 v3 S t v2 14 new flow network 12/12 G v1 5 6 7 8 v4 v3 S t v2 14 v4 for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v] The basic Ford Fulkerson algorithm example of an execution 1 2 3 4 (residual) network Gf v1 12 v3 S t v2 14 new flow network 12/12 G v1 5 6 7 8 v4 v3 S t v2 14 v4 for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v] The basic Ford Fulkerson algorithm example of an execution 1 2 3 4 (residual) network Gf v1 12 v3 S t v2 14 new flow network 12/12 G v1 5 6 7 8 v4 v3 S t v2 14 for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v] v4 temporary variable: cf (p) = 4 The basic Ford Fulkerson algorithm example of an execution 1 2 3 4 (residual) network Gf v1 12 v3 S t v2 14 new flow network 12/12 G v1 5 6 7 8 v4 v3 S t v2 4/14 for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v] v4 temporary variable: cf (p) = 4 The basic Ford Fulkerson algorithm example of an execution 1 2 3 4 (residual) network Gf v1 12 v3 S t 5 6 7 8 4 v2 10 new flow network 12/12 G v1 v4 v3 S t v2 4/14 v4 for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v] The basic Ford Fulkerson algorithm example of an execution 1 2 3 4 (residual) network Gf v1 12 v3 S t 5 6 7 8 4 v2 10 new flow network 12/12 G v1 v4 v3 S t v2 4/14 v4 for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v] The basic Ford Fulkerson algorithm example of an execution 1 2 3 4 (residual) network Gf v1 12 v3 S t 5 6 7 8 4 v2 10 new flow network 12/12 G v1 v4 v3 S t v2 4/14 v4 for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v] The basic Ford Fulkerson algorithm example of an execution (residual) network Gf v1 12 1 2 3 4 v3 S t 5 6 7 8 4 v2 10 new flow network 12/12 G v1 v4 v3 S t v2 4/14 for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v] v4 temporary variable: cf (p) = 7 The basic Ford Fulkerson algorithm example of an execution 1 2 3 4 (residual) network Gf v1 12 v3 S t 5 6 7 8 4 v2 10 new flow network 12/12 G v1 v4 v3 S t v2 11/14 for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v] v4 temporary variable: cf (p) = 7 The basic Ford Fulkerson algorithm example of an execution 1 2 3 4 (residual) network Gf v1 12 v3 S t 5 6 7 8 11 v2 3 new flow network 12/12 G v1 v4 v3 S t v2 11/14 v4 for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v] The basic Ford Fulkerson algorithm example of an execution 1 2 3 4 (residual) network Gf v1 12 v3 S t 5 6 7 8 11 v2 3 new flow network 12/12 G v1 v4 v3 S t v2 11/14 for each edge (u, v) E [G] do f [u, v] = 0 f [v, u] = 0 while there exists a path p from s to t in the residual network Gf do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p do f [u, v] = f [u, v] + cf(p) f [v, u] = - f [u, v] v4 Finally we have: | f | = f (s, V) = 23 Running Time Assumption. All capacities are integers between 1 and C. Invariant. Every flow value f(e) and every residual capacities cf (e) remains an integer throughout the algorithm. Theorem. The algorithm terminates in at most v(f*) n C iterations. Pf. Each augmentation increase value by at least 1. ▪ Corollary. If C = 1, Ford-Fulkerson algorithm runs in O(mn) time. Integrality theorem. If all capacities are integers, then there exists a max flow f for which every flow value f(e) is an integer. Pf. Since algorithm terminates, theorem follows from invariant. ▪ 7.3 Choosing Good Augmenting Paths Analysis of the Ford Fulkerson algorithm Running time (arbitrary choice of p) 1 2 3 4 5 6 7 8 for each edge (u, v) E [G] do f [u, v] = 0 O(|E|) f [v, u] = 0 while there exists a path p from s to t O(|E| in the residual network Gf ) do cf(p) = min{cf(u, v) | (u, v) p} for each edge (u, v) in p O(|E| do f [u, v] = f [u, v] + cf(p) ) f [v, u] = - f [u, v] O(|E| |fmax|) running time: O ( |E| |fmax| ) with fmax as maximum flow (1) The augmenting path is chosen arbitrarily and all capacities are integers Analysis of the Ford Fulkerson algorithm Running time (arbitrary choice of p) Consequencies of an arbitrarily choice: Example if |f*| is large: s 1 v1 t v2 running time: O ( |E| |fmax| ) with fmax as maximum flow (1) The augmenting path is chosen arbitrarily and all capacities are integers Analysis of the Ford Fulkerson algorithm Running time (arbitrary choice of p) Consequencies of an arbitrarily choice: Example if |f*| is large: residual network Gf t s v2 1 s v1 1/1 v1 t v2 running time: O ( |E| |fmax| ) with fmax as maximum flow (1) The augmenting path is chosen arbitrarily and all capacities are integers Analysis of the Ford Fulkerson algorithm Running time (arbitrary choice of p) Consequencies of an arbitrarily choice: Example if |f*| is large: residual network Gf v1 s t s v2 1 1 v1 t v2 running time: O ( |E| |fmax| ) with fmax as maximum flow (1) The augmenting path is chosen arbitrarily and all capacities are integers Analysis of the Ford Fulkerson algorithm Running time with Edmonds-Karp algorithm Informal idea of the proof: (1) for all vertices v V\{s,t}: the shortest path distance f(s,v) in Gf increases monotonically with each flow augmentation f (s,v) < f´ (s,v) u1 4 v3 S t u2 4 v4 running time: O ( |V| |E|² ) (2) Edmonds-Karp algorithm augmenting path is found by breath-first search and has to be a shortest path from p to t