Download presentation
Presentation is loading. Please wait.
1
Instructor: Shengyu Zhang
CSC3160: Design and Analysis of Algorithms Week 8: Maximum Network Flow Instructor: Shengyu Zhang
2
Transportation Total Flow: 16 flow a 4 c capacity 8 8 6 10 8 6 6 2 10 8 8 10 s 10 b 9 d 10 t Suppose we want to transport commodity from π to π‘ in a directed graph πΊ=(π,πΈ). Each directed edge π’,π£ βπΈ has a capacity Max amount of commodity allowed Question: How much can we transport?
3
Technically We have a capacity function π:πΈβ β + .
Total Flow: 16 flow a 4 c capacity 8 8 6 10 8 6 6 2 10 8 8 10 s 10 b 9 d 10 t We have a capacity function π:πΈβ β + . We want a flow function π:πΈβ β + , s.t. Capacity constraint: β π’,π£ βπΈ, π(π’,π£)β€π(π’,π£). Flow conservation: βπ’β{π ,π‘}, π£,π’ βπΈ π(π£,π’) = π’,π£ βπΈ π(π’,π£) Incoming flow = outgoing flow Goal: max π π ,π£ βπΈ π π ,π£ β π’,π βπΈ π π’,π Net flow = flow going out of source β flow coming back into source
4
Improve it ο Can we improve this? Can we further improve this?
Total Flow: 16 19 3 flow a 4 c capacity 10 8 8 7 6 9 10 8 6 6 2 10 8 9 8 10 9 s 10 b 9 d 10 t Can we improve this? For some edges: we gave too much. For some other edges: we didnβt give enough. Can we further improve this?
5
Network Flow Can you give a good algorithm?
Methodology 5: Approach to the optimum by a sequence of improvements.
6
Improve it little by little
Total Flow: 16 18 +2=2 flow a 4 c capacity +2=10 8 8 6 +2=8 10 8 6 6 2 10 8 8 10 +1 +1 s 10 b 9 d 10 t We can find a path π βπβπβπ‘ on which we can add 2 (on every edge) Total flow becomes 18. We also like to add 1 via π βπβπβπ‘, β¦ but the edge πβπ‘ is a bottleneck. Actually the edge πβπ is as well.
7
Improve it little by little
Total Flow: 16 18 19 +2=2 flow +1=3 a 4 c capacity +2=10 8 8 -1=7 6 +2=8 10 8 6 6 +1=9 2 10 8 +1 8 10 +1 s 10 b 9 d 10 t We can still squeeze some juice along the path πβπβπ‘. But vertex π already allocates all its incoming 10 flow. Letβs withdraw 1 unit on the edge πβπ and assign it along πβπβπ‘ ! Total flow becomes 19. In some sense, it looks like we injected a unit of flow along π βπβπβπβπβπ‘.
8
Improve it little by little
Total Flow: 16 18 19 +2=2 flow +1=3 a 4 c capacity +2=10 8 8 -1=7 6 +2=8 10 8 6 6 +1=9 2 10 8 +1 8 10 +1 s 10 b 9 d 10 t Ford-Fulkerson Algorithm: initialize flow π to 0 while there exists an augmenting path π Inject more flow along π (as much as possible) return π Question 1: What is an augmenting path?
9
Improve it little by little
Total Flow: 16 18 19 +2=2 flow +1=3 a 4 c capacity +2=10 8 8 -1=7 6 +2=8 10 8 6 6 +1=9 2 10 8 +1 8 10 +1 s 10 b 9 d 10 t Case 1: if the capacity hasnβt been used up for each edge on the path, then itβs an augmenting path. Case 2: if some edge π’βπ£ already has a flow, then it amounts to a capacity in direction π£βπ’ By withdrawing the previously assigned flow.
10
Improve it little by little
Total Flow: 16 18 19 +2=2 flow +1=3 a 4 c capacity +2=10 8 8 -1=7 6 +2=8 10 8 6 6 +1=9 2 10 8 +1 8 10 +1 s 10 b 9 d 10 t Question 2: How to find an augmenting path? By residual networks.
11
We can still inject up to π(π’,π£)βπ(π’,π£) flow from π’ to π£
Residual networks For a flow π, the residual capacity ππ is: ππ(π’,π£)=π(π’,π£)βπ(π’,π£) ππ(π£,π’)=π(π£,π’)+π(π’,π£) Residual network: πΊπ=(π, πΈ π ), where πΈ π ={ π’,π£ βπΓπ: π π (π’,π£)>0}. Now an augmenting path is just a path from π to π‘ in the residual network. π(π’,π£) We can still inject up to π(π’,π£)βπ(π’,π£) flow from π’ to π£ π(π’,π£) π’ π£ π(π£,π’) π(π’,π£)βπ(π’,π£) π£ π’ π(π£,π’)+π(π’,π£) We can withdraw π flow from π’ to π£ first, and then inject up to π(π£,π’) flow from π£ to π’
12
Residual networks The residual network gives the info of how we can get more flow from π to π‘ in the graph. And how much. So we define an augmenting path to be a path from π to π‘ in the residual network. Now finding an augmenting path amounts to finding a path from π to π‘ in the residual network, which we know how to do e.g. BFS algorithm.
13
FORD-FULKERSON(πΊ, π , π‘) for each edge π’,π£ βπΈ // Initialization πΊ π =πΊ
π(π’,π£)β0, π(π£,π’)β0 πΊ π =πΊ while there exists a path π from π to π‘ in the residual network πΊ π π π π β min {ππ(π’,π£): (π’,π£) ππ ππ π} // max to inject on π for each edge (π’,π£) ππ π // update flow if π(π£,π’)=0, // no backward flow on this edge π(π’,π£)βπ(π’,π£)+ π π (π) else if π π (π)β€π(π£,π’) // has backward flow, withdraw part π(π£,π’)βπ(π£,π’)β π π (π) else // has backward flow, withdraw all, add forward flow π(π’,π£)β π π (π)βπ(π£,π’) π(π£,π’)β0 Update the residual network πΊ π .
14
FORD-FULKERSON(πΊ, π , π‘) for each edge π’,π£ βπΈ πΊ π =πΊ
π(π’,π£)β0, π(π£,π’)β0 πΊ π =πΊ while there exists path π from π to π‘ in residual network πΊ π π π π β min {ππ(π’,π£): (π’,π£) is in π} for each edge (π’,π£) in π if π(π£,π’)=0, π(π’,π£)βπ(π’,π£)+ π π (π) else if π π (π)β€π(π£,π’) π(π£,π’)βπ(π£,π’)β π π (π) else π(π’,π£)βππ(π)βπ(π£,π’) π(π£,π’)β0 Update the residual network πΊ π . πΊ a 4 c 8 8 10 8 10 2 6 8 s 10 b 9 d 10 t πΊ π a 4 c 10 8 2 6 10 s 10 b 9 d 10 t π π (π)=8, flow=8
15
FORD-FULKERSON(πΊ, π , π‘) for each edge π’,π£ βπΈ πΊ π =πΊ
π(π’,π£)β0, π(π£,π’)β0 πΊ π =πΊ while there exists path π from π to π‘ in residual network πΊ π π π π β min {ππ(π’,π£): (π’,π£) is in π} for each edge (π’,π£) in π if π(π£,π’)=0, π(π’,π£)βπ(π’,π£)+ π π (π) else if π π (π)β€π(π£,π’) π(π£,π’)βπ(π£,π’)β π π (π) else π(π’,π£)βππ(π)βπ(π£,π’) π(π£,π’)β0 Update the residual network πΊ π . πΊ a 4 c 8 8 10 8 10 2 6 8 s 10 b 9 d 10 t πΊ π a 4 c 8 8 2 2 6 10 s 10 b 9 d 2 t 8 π π π =8, flow=8 New π π (π)? New flow? New πΊ π ?
16
Questions left How to find an augmenting path?
What if, at some step, there is no augmenting path in the residual network? Can we conclude that weβve found the maximum flow?
17
Cut Cut: a partition of vertices into two parts π and π.
capacity of cut (π,π): π’βπ,π£βπ π(π’,π£) . Fact. Flow β€ capacity of any cut (π,π). Proof. flow value of π = net flow from π to π = flow π to πβ flow π to π // conservation = π’βπ,π£βπ π(π’,π£) β π’βπ,π£βπ π(π£,π’) β€ π’βπ,π£βπ π(π’,π£) How good is this upper bound of flow?
18
Max-flow min-cut Theorem.
An important fact relating max flow and min cut: the previous upper bound is perfect as long as we find a correct cut. [Theorem] The following are equivalent: π is a maximum flow in πΊ πΊ π contains no augmenting paths. [Proof] 1 β 2: trivial since otherwise π can be further increased. Next: 2 β 1. In the proof youβll see a cut with capacity achieving the max flow.
19
πΊ π contains no augmenting paths β π is a maximum flow in πΊ
Consider all vertices in πΊ π reachable from π . Call the set π. The rest is π. π βπ, π‘βπ. Consider this cut (π,π): Two types of crossing edges in πΊ Type 1: πβπ. π’,π£ :π’βπ,π£βπ. Type 2: πβπ. π£,π’ :π’βπ,π£βπ. For type 1: π(π’,π£)=π(π’,π£) Otherwise π£ is reachable from π in πΊ π ! π π‘ π’ π£ π π
20
πΊ π contains no augmenting paths β π is a maximum flow in πΊ
Consider all vertices in πΊ π reachable from π . Call the set π. The rest is π. π βπ, π‘βπ. Consider this cut (π,π): Two types of crossing edges in πΊ Type 1: πβπ. π’,π£ :π’βπ,π£βπ Type 2: πβπ. π£,π’ :π’βπ,π£βπ For type 1: π(π’,π£)=π(π’,π£) Otherwise π£ is reachable from π in πΊ π ! For type 2: π(π£,π’)=0. Otherwise π£ is also reachable from π in πΊ π ! π£ π π‘ π’ π π Done! Why?
21
Any cut gives an upper bound!
flow value of π = flow βπβπβ β flow βπβπβ = π’,π£ :type 1 π(π’,π£) β π’,π£ :type 2 π(π£,π’) β€ π’,π£ :type 1 π(π’,π£) i.e. the best we can hope for π is to use up full capacity of all type 1 edges not to use any capacity of any type 2 edge. This essentially repeats the proof of flow β€ cut capacity. Last slide: If πΊ π has no augmenting path, then π already satisfies these two properties. Thus π achieves π’,π£ :type 1 π(π’,π£) ---It is maximum. π π‘ π π
22
Next How to find an augmenting path?
It matters. If we donβt pick a good path, it may take forever and may not even converge to the optimum. [Edmonds-Karp] Use a shortest path will do. Unweighted, thus BFS suffices. [Fact] At most π(|π|β|πΈ|) augmenting path findings. Using BFS costs π(|πΈ|) for each path, thus π(|π|β πΈ 2 ) for the total cost.
23
Edmonds-Karp Algorithm
for each edge π’,π£ βπΈ π(π’,π£)β0, π(π£,π’)β0 πΊ π =πΊ while we can use BFS to find a shortest path π from π to π‘ in the residual network πΊ π ππ(π)βminβ‘{ππ(π’,π£): (π’,π£) is in π} Update the flow π Update the residual network πΊ π . Complexity? Depends on how many iterations are executed in the while loop. [Thm] π(|π|β|πΈ|) iterations.
24
Edmonds-Karp Algorithm
for each edge π’,π£ βπΈ π(π’,π£)β0, π(π£,π’)β0 πΊ π =πΊ while we can use BFS to find a shortest path π from π to π‘ in the residual network πΊ π ππ(π)βminβ‘{ππ(π’,π£): (π’,π£) is in π} Update the flow π Update the residual network πΊ π . An edge (π’,π£) is critical on an augmenting path π if the βminβ in ππ(π)βminβ‘{ππ(π’,π£): (π’,π£) is in π} is achieved by (π’,π£)
25
Analysis [Lemma] Any (π’,π£) can be critical at most |π|/2 times.
Once we prove this, we are done proving the theorem of βπ(|π||πΈ|) iterationsβ, because there are |πΈ| edges, so at most |π||πΈ|/2 iterations in total.
26
Proof of the lemma Weβll prove that each time (π’,π£) becomes critical, the distance π(π ,π’) increases by at least 2. π(π ,π’): least number of edges on a path from π to π’ in graph πΊ π Since 0<π(π ,π’)<|π|, (π’,π£) is critical at most |π|/2 times.
27
Proof (continued) Since augmenting paths are shortest paths, when (π’,π£) is critical for the first time, we have π π (π ,π£)= π π (π ,π’)+1. π π : distance on πΊ π . Once the flow is augmented, the edge (π’,π£) disappears from the residual network. Critical: π(π’,π£)=π(π’,π£), So π π (π’,π£)=0, i.e. (π’,π£) disappears from the residual network. t s u v
28
Proof (continued) It cannot reappear later on another augmenting path until after the flow from π’ to π£ is decreased, which occurs only if (π£,π’) appears on an augmenting path. If πβ² is the flow in πΊ when this event occurs, then we have π π β² (π ,π’)= π π β² (π ,π£)+1. t s u v
29
Proof (continued) Weβve shown Now if π π β² (π ,π£)β₯ π π (π ,π£) β¦
π π (π ,π£)= π π (π ,π’)+1 π π β² (π ,π’)= π π β² (π ,π£)+1 Now if π π β² (π ,π£)β₯ π π (π ,π£) β¦ Then π π β² (π ,π’)= π π β² (π ,π£)+1 β₯ ππ(π ,π£) + 1 = ππ(π ,π’) + 2. Mission accomplished! Exercise!
30
Application: Max bipartite matching
Weβve learned maximum flow problem and algorithms. Next we apply it to solve the maximum bipartite matching problem.
31
Maximum bipartite matching
Bipartite graph: πΊ=(π,πΈ) that can be partitioned into two parts with all edges crossing π=πΏβͺπ
with πΏβ©π
=β
, All edges π,π βπΈ have πβπΏ and πβπ
. Matching: a collection of edges π π , π π that are vertex disjoint All π π βs are distinct. So are all π π βs. Question: Find a max matching in a bipartite graph. Max matching: matching with maximum number of edges. πΏ π
32
Then a simple transformation or reduction works.
Methodology 0: See whether the problem can be reduced to another one whose answer is known. Very often, the problem that you are facing appeared to other people before. Solutions are known. Also very often, the problem is probably new, but itβs very similar to, or essentially the same as an old one. Then a simple transformation or reduction works.
33
Orient existing edges from πΏ to π
.
34
Orient existing edges from πΏ to π
. Add one more node π .
Link π to all vertices in πΏ. Add one more node π‘. Link all vertices in π
to π‘. All capacities (on edges) are 1. πΏ π
π π‘
35
Equivalence πΏ π
[Fact] β matching of size π in original graph β β integral flow of value π in the new graph Integral: flow is integer on each edge β: For matching { π π , π π :π=1,β¦,π}, give a unit flow to each edge (π , π π ), π π , π π , and π π ,π‘ . β: Since all capacities are 1 and flow is integral, flow on each edge is either 0 or 1. So there are π βmiddleβ edges π π , π π with flow 1. And these edges are all vertex-disjoint because of the flow conservation. π π‘
36
So itβs sufficient to find a maximum integral flow in the new graph.
πΏ π
[Fact] β matching of size π in original graph β β integral flow of value π in the new graph Integral: flow is integer on each edge [Fact] maximum matching in the original graph β maximum integral flow in the new graph. So itβs sufficient to find a maximum integral flow in the new graph. Weβve learned how to find a maximum flow. But how to handle the integral constraint? Answer: We donβt handle it. π π‘
37
Integral constraint: automatic
[Fact] In a graph with integral capacities, max flow is achieved by integral flows. Why? By our algorithm! Each time we follow an augmenting path to increase the flow β¦ by how much? π π (π)βminβ‘{ π π (π’,π£): (π’,π£) is in π} Itβs an integer! So the total flow is always an integer during the algorithm. In particular, the final answer, i.e. a max flow, is an integral flow.
38
algorithm Overall, the algorithm is as follows. Create the new graph.
πΏ π
Overall, the algorithm is as follows. Create the new graph. Orienting edges, adding π and π‘, giving unit capacity. Find a max flow of the new graph. Output middle edges with flow 1. π π‘
39
Summary Network flow problem. Augmenting path algorithm.
Why correct? Max-flow Min-cut Theorem. How to find? One way: Shortest one by BFS. Complexity? π π πΈ 2 by analysis. One application: max bipartite matching
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.