Algorithm Design and Analysis LECTURE 21 Network Flow • Finish bipartite matching • Capacity-scaling algorithm Adam Smith 10/11/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Marriage Theorem Marriage Theorem. [Frobenius 1917, Hall 1935] Let G = (L R, E) be a bipartite graph with |L| = |R|. Then, G has a perfect matching iff |N(S)| |S| for all subsets S L. Pf. This was the previous observation. 1 1' 2 2' No perfect matching: S = { 2, 4, 5 } L 3 3' 4 4' 5 5' N(S) = { 2', 5' }. R Proof of Marriage Theorem Pf. Suppose G does not have a perfect matching. Formulate as a max flow problem with 1 constraints on edges from L to R and let (A, B) be min cut in G'. Key property #1 of this graph: min-cut cannot use edges. So cap(A, B) = | L B | + | R A | Key property #2: integral flow is still a matching – By max-flow min-cut, cap(A, B) < | L |. Choose S = L A. – Since min cut can't use edges: N(S) R A. |N(S )| | R A | = cap(A, B) - | L B | < | L | - | L B | = | S|. ▪ 1 G' A s S = {2, 4, 5} 1 1 L B = {1, 3} R A = {2', 5'} 3' 3 4' 5' 1' 2' 4 5 1 2 1 t N(S) = {2', 5'} Bipartite Matching: Running Time Which max flow algorithm to use for bipartite matching? Generic augmenting path: O(m val(f*) ) = O(mn). Capacity scaling: O(m2 log C ) = O(m2). Shortest augmenting path (not covered in class): O(m n1/2). Non-bipartite matching. Structure of non-bipartite graphs is more complicated, but well-understood. [Tutte-Berge, Edmonds-Galai] Blossom algorithm: O(n4). [Edmonds 1965] Best known: O(m n1/2). [Micali-Vazirani 1980] Recently: better algorithms for dense graphs, e.g. O(n2.38) [Harvey, 2006] Exercise A bipartite graph is k-regular if |L|=|R| and every vertex (regardless of which side it is on) has exactly k neighbors Prove or disprove: every k-regular bipartite graph has a perfect matching Faster algorithms when capacities are large Ford-Fulkerson: Exponential Number of Augmentations Q. Is generic Ford-Fulkerson algorithm polynomial in input size? m, n, and log C A. No. If max capacity is C, then algorithm can take C iterations. 1 1 1 0 X 0 C C 1 X 0 1 s 1 t C C 0 0 1 X X 0 0 1 X C C 1 0 1 X 0 X s 2 Intuition: we’re choosing the wrong paths! t C C X 0 1 0 1 X 2 Choosing Good Augmenting Paths Use care when selecting augmenting paths. Some choices lead to exponential algorithms. Clever choices lead to polynomial algorithms. If capacities are irrational, algorithm not guaranteed to terminate! Goal: choose augmenting paths so that: Can find augmenting paths efficiently. Few iterations. Choose augmenting paths with: [Edmonds-Karp 1972, Dinitz 1970] Max bottleneck capacity. Sufficiently large bottleneck capacity. Fewest number of edges. 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 . Gf () = subgraph of the residual graph with only arcs ofcapacity at least . 4 110 4 102 1 s 122 t 170 2 Gf 110 102 s t 122 170 2 Gf (100) Capacity Scaling Scaling-Max-Flow(G, s, t, c) { foreach e E f(e) 0 smallest power of 2 greater than or equal to C Gf residual graph while ( 1) { Gf() -residual graph while (there exists augmenting path P in Gf()) { f augment(f, c, P) // augment flow by update Gf() } / 2 } return f } Capacity Scaling: Correctness Assumption. All edge capacities are integers between 1 and C. Integrality invariant. All flow and residual capacity values are integral. Correctness. If the algorithm terminates, then f is a max flow. Pf. By integrality invariant, when = 1 Gf() = Gf. Upon termination of = 1 phase, there are no augmenting paths. ▪ Capacity Scaling: Running Time Lemma 1. The outer while loop repeats 1 + log2 C times. Pf. Initially C < 2C. decreases by a factor of 2 each iteration. ▪ Lemma 2. Let f be the flow at the end of a -scaling phase. Then the value of the maximum flow f* is at most v(f) + m . proof on next slide (Sanity check: |v(f*) – v(f)| m, and shrinks, so v(f) converges towards v(f*) ) Lemma 3. There are at most 2m augmentations per scaling phase. Let f be the flow at the end of the previous scaling phase. Lemma 2 v(f*) v(f) + m (2). Each augmentation in a -phase increases v(f) by at least . ▪ Theorem. The scaling max-flow algorithm finds a max flow in O(m log C) augmentations. It can be implemented to run in O(m2 log C) time. ▪ (Why?) Capacity Scaling: Running Time Lemma 2. Let f be the flow at the end of a -scaling phase. Then value of the maximum flow is at most v(f) + m . Pf. (almost identical to proof of max-flow min-cut theorem) We show that at the end of a -phase, there exists a cut (A, B) such that cap(A, B) v(f) + m . Choose A to be the set of nodes reachable from s in Gf(). By definition of A, s A. By definition of f, t A. v( f ) f (e) f (e) e out of A (c(e) ) c(e) e out of A B t e in to A e out of A A e in to A e out of A e in to A s cap(A, B) - m So v(f*)-v(f) ≤ cap(A,B) – v(f) ≤ m original network Best Known Algorithms For Max Flow Reminder: The scaling max-flow algorithm runs in O(m2 log C) time. Currently there are algorithms that run in time • O(mn log n) • O(n3) • O(min(n2/3, m1/2) m log n log C) Active topic of research: • Flow algorithms for specific types of graphs • Special cases (bipartite matching, etc) • Multi-commodity flow •… What you should know about max flow How Ford-Fulkerson works How to use it to find min cuts How to use it to solve other problems (more examples coming) • max matching • project selection • edge-disjoint paths • vertex-disjoint paths • … To study: • Review lecture notes/ textbook • Solved exercises in book • Homework problems • Homework exercises • Solve lots of problems!