Maximum Flow Haidong Xue (Haydon) 10/28/2013 What is a flow network? • A no-selfloop, weak connected directed graph (V, E) • Among vertices in V V – There is one and only one source vertex π – There is one and only one sink vertex π‘ – For each vertex v, there is a path π → π£ → π‘ • No antiparallel edges in E E – if π£1 , π£2 ∈ πΈ, π£2 , π£1 ∉ πΈ • A Capacity is defined on each edge: π π’, π£ What is a flow network? • A flow on a flow network – π π’, π£ • ο’π’, π£οπ: 0 ≤ π(π’, π£) ο£ π(π’, π£) – i.e. π does not exceed π (capasity constraint) • ο’π’οπ ο π , π‘ : οπ£ οπ π π’, π£ = οπ£ οπ π π£, π’ – i. e. the net flow is 0 (flow conservation) Note: In some definition of flow networks, negative flows are allowed; in these slides, we keep consistent with the textbook Red: flow Green: capacity What is a flow network? • Are they flow networks with valid flows? 12/12 15/20 11/16 1/4 17/12 20/20 11/16 6/4 4/9 8/13 4/9 7/7 4/4 13/13 11/14 Not connected Violate the capacity constraint 12/12 12/12 15/20 15/20 11/16 11/16 1/4 4/9 1/4 7/7 4/4 8/13 11/14 Multiple Sinks 4/9 7/7 4/4 10/13 11/14 Violate the flow conservation rule Red: flow Green: capacity What is a flow network? • Are they flow networks with valid flows? 0/7 12/12 12/12 15/20 11/16 1/4 4/9 0/10 7/7 4/4 8/13 have a self-loop 15/20 11/16 4/9 t 7/7 4/4 8/13 4/9 7/7 4/4 8/13 have an antiparallel edge 12/12 1/4 1/4 11/14 11/14 s 15/20 11/16 11/14 Page 710 Fig 26.1(b) 1 source; 1 sink; no self-loop; no antiparallel edge; no flow exceed capacity; for all non-source non-sink vertices, total flow is 0. What if the real problem has antiparallel edges or multiple sinks or sources? v1 v1 10 10 4 4 V’ 10 Add an intermediate vertex to eliminate antiparallel edges v2 s1 10 ο₯ ο₯ sο’ ο₯ 3 s2 s3 12 5 s4 s5 t1 15 6 8 t2 20 14 ο₯ ο₯ v2 7 11 13 18 t3 ο₯ ο₯ tο’ ο₯ 2 Add a super-source and a super sink Why do we disallow antiparallel edges? What is the max-flow problem? 12/12 15/20 11/16 s 1/4 4/9 t 7/7 4/4 8/13 11/14 The flow of a flow network, |π|: the net output of the source i.e., |π | = π£ οπ π(π , π£)-οπ£ οπ π(π£, π ) In the above example, |π | = 11+8=19 What is the max-flow problem? Given a flow network, there could be more than one valid flows: 10/12 15/20 9/16 s 1/4 2/9 |π | = 17 t 7/7 2/4 8/13 9/14 12/12 15/20 11/16 s 1/4 4/9 |π | = 19 t 7/7 The maximum-flow problem: Given a flow network, find a valid flow that has the maximum |π | 4/4 8/13 11/14 12/12 19/20 12/16 s 4 9 t 7/7 4/4 11/13 11/14 |π | = 23 (it is a max-flow) Find a maximum flow • Input: a flow network 12 20 16 s 4 t 7 9 4 13 14 • Output: a flow net work with max-flow 12/12 19/20 12/16 s 4 9 t 7/7 4/4 11/13 11/14 Find a maximum flow- residual networks • What are the possible modifications can be made on an edge that does not break the capacity constraint? 8/12 4/20 4/16 s 4/4 4/9 t 7 4/4 4/13 4/14 Increase it at most by 10 = c-f=14-4; Decrease it at most by 4=f. To represent a possible modifications, residual networks are introduced Find a maximum flow - residual networks 12/12 π£1 11/16 s 1/4 8/13 11 3 8 5 π£2 t π£3 12 15 5 5 4 11 3 t 7 π£4 A flow network πΊ = (π, πΈ) with a valid flow 4/4 π£4 11/14 1 15/20 7/7 4/9 π£1 5 s π£2 π£3 4 A residual network of πΊ, πΊπ = π, πΈπ where πΈπ = π’, π£ ∈ π × π: ππ π’, π£ > 0 ππ π’, π£ π π’, π£ − π π’, π£ ππ(π’, π£) ∈ πΈ = π π£, π’ ππ (π£, π’) ∈ πΈ 0 Find a maximum flow – augmenting paths 11/16 12/12 π£1 s 1/4 8/13 π£2 π£3 t 7/7 4/9 11/14 11 s 4/4 π£4 π£1 5 15/20 3 8 5 π£2 π£3 12 1 5 5 4 11 3 πΊπ ππ πΊ πΊ An augmenting path is a simple path from s to t in the residual network πΊπ π£1 5 11 s 3 8 5 π£2 π£3 12 1 15 5 5 4 11 3 t 7 π£4 4 15 t 7 π£4 4 Find a maximum flow – augmenting paths An augmenting path is a simple path from s to t in the residual network πΊπ π£1 5 11 s 3 8 4/5 π£3 12 1 π£2 15 4/5 5 11 t 7 4/4 π£4 4 3 The residual capacity of an augmenting path π: ππ(π) = min{ππ(π’, π£): (π’, π£) ππ ππ π} The flow defined by π, ππ : ππ π’, π£ = ππ π , 0, ππ π = 4 ππ (π’, π£) ππ ππ π ππ‘βπππ€ππ π Guaranteed by Corollary 26.3 , modifying πΊ according to this flow on πΊπ does not break the flow conservation rule Find a maximum flow – augmenting paths 11/16 12/12 π£1 s 1/4 8/13 π£2 π£3 4/9 11/14 t 7/7 11 s 4/4 π£4 π£1 5 15/20 3 1 8 4/5 π£2 π£3 12 15 4/5 5 11 π£4 3 Modify πΊ according to ππ 12/12 π£1 s π£3 1/4 19/20 t 7/7 0/9 12/13 4 πΊπ ππ πΊ π€ππ‘β πππ‘β π πππ ππππ€ ππ πΊ π€ππ‘β π 11/16 t 7 4/4 π£2 11/14 πΊ π€ππ‘β "π ↑ ππ " π£4 4/4 Find a maximum flow • Corollary 26.3 guarantees that: – π ↑ ππ is a valid flow in πΊ – π ↑ ππ > |π| • Theorem 26.6 (Max-flow min-cut theorem) further tells that: – If there is no augmenting path in πΊπ , π in πΊ is a maximum flow Do you have any idea to develop an algorithm to find a max-flow? Find a maximum flow – the basic FordFulkerson algorithm FORD-FULKERSON(πΊ, π , π‘) Initialize |π| to 0 for each π’, π£ ο πΊ. πΈ (π’, π£). π = 0; while there exist path π from π to π‘ in πΊπ ππ(π) βΆ= min{ππ(π’, π£): (π’, π£) ππ ππ π}; for each (π’, π£) in p if π’, π£ ∈ πΊ. πΈ, π’, π£ . π = (π’, π£). π+ ππ π ; else π’, π£ . π = π’, π£ . π − ππ π ; Is it correct? What is the time complexity? Repeatedly find a ππ , and apply the modification until there is no augmenting path Find a maximum flow – Analysis of F-F • Is it correct in general? – No! – A counter example: http://en.wikipedia.org/wiki/Ford%E2%80%93Fulkerson_algorithm#Nonterminating_example • Is it correct with integer capacities? – – – – Yes. To find a π, if using DFS or BFS that is O(|V|+|E|)=O(|E|) At most, need to it |π ∗ | times Time complexity: π(|πΈ||π ∗ |) Find a maximum flow – Edmonds-Karp algorithm EDMONDS-KARP(πΊ, π , π‘) for each π’, π£ ο πΊ. πΈ (π’, π£). π = 0; while there exist a BFS path π from π to π‘ in πΊπ ππ(π) βΆ= min{ππ(π’, π£): (π’, π£) ππ ππ π}; for each (π’, π£) in p if π’, π£ ∈ πΊ. πΈ, π’, π£ . π = (π’, π£). π+ ππ π ; else π’, π£ . π = π’, π£ . π − ππ π ; Find a maximum flow – Analysis of E-K • Theorem 26.8 – The total number of flow augmentations performed by the algorithm is O(|V||E|) • Time complexity – π(|πΈ|) ∗ π(|π||πΈ|) = π(|π||πΈ|2 ) Find a maximum flow – Example πΊ πΊπ 12 12 20 16 s 4 9 t 7 4 13 20 16 s 4 4 13 14 14 12/12 12 12/20 12/16 s 4 9 8 4 t 7 12 s 12 4 4 13 4 13 14 12 12/12 12/20 12/16 4 9 4/14 8 4 t 7 4/4 4/13 t 7 9 14 s t 7 9 12 s 12 4 9 4 9 4 10 t 7 4 Find a maximum flow – Example πΊ πΊπ 12 12/12 12/20 12/16 s 4 9 12 s t 7 12 4 4 9 12/12 12 19/20 12/16 4 4 10 4/14 s t 7 9 4 4/4 4/13 8 4 9 t 7/7 11/14 A max-flow 12 s 19 4 9 11 4/4 11/13 1 4 11 2 t 7 4 3 π‘ cannot be reached by π Maximum bipartite matching • A problem can be converted to the max-flow problem • Bipartite Graph – A undirected graph (V, E) – π = πΏ ∪ π , πΏ ∩ π = ∅ – All edges in E go between πΏ and π – Every vertex in V has at least one incident edge Maximum bipartite matching L R L R L R Maximum bipartite matching A matching π: π ⊆ πΈ, L R π =1 L R π =2 L R π =3 The maximum bipartite matching problem: find a M with the maximum π Maximum bipartite matching Convert it to a max-flow problem L R L R s t Set each edge with a capacity of 1 By Lemma 26.9, π = |π| Summary • Max-flow problem – – – – – – Flow network πΊ The flow of πΊ The residual network πΊπ ππ πΊ A augmenting path π of πΊπ ππ , ππ Ford-Fulkerson, Edmonds-Karps • Maximum bipartite Matching – Bipartite graph – Matching – Convert a bipartite graph to a flow net work FYI • Your next assignment has already been posted • Chaoyang’s will be holding extended office hours on Wednesdays from 12:00pm-4:00pm • ACM has an event tomorrow Room 460, University Center Distributed Content Delivery Systems with Real Time Route Optimization Free knowledge, pizza, soda, donut, and a door price Pop Quiz Write down your official name clearly on the right-top corner in the form: Last Name, First Name. (e.g. Xue, Haidong)