Divide and Conquer - jhc.sjtu.edu.cn
Transcript of Divide and Conquer - jhc.sjtu.edu.cn
Network Flow
Max-Flow: Edmonds-Karp Algorithm, Dinitzโs AlgorithmMax Bipartite Matching: HopcroftโKarpโKarzanov algorithm
Residual Network ๐บ๐
Given ๐บ = (๐, ๐ธ), ๐, and a flow ๐
๐บ๐ = (๐, ๐ธ๐) and the associated capacity ๐๐: ๐ธ๐ โ โ+ are defined as follows:
โช ๐ข, ๐ฃ โ ๐ธ๐ if one of the followings holdsโ ๐ข, ๐ฃ โ ๐ธ and ๐ ๐ข, ๐ฃ < ๐(๐ข, ๐ฃ): in this case, ๐๐ ๐ข, ๐ฃ = ๐ ๐ข, ๐ฃ โ ๐(๐ข, ๐ฃ)
โ ๐ฃ, ๐ข โ ๐ธ and ๐ ๐ฃ, ๐ข > 0: in this case, ๐๐ ๐ข, ๐ฃ = ๐ ๐ฃ, ๐ขu
ts
v
20/20
20/30
10
20/2010
u
ts
v
10
10
๐บ ๐บ๐
20
20
20 10
Last Lecture โ Ford-Fulkerson Method
โช Always terminates for integer/rational capacities
โช Not guaranteed to terminate for irrational capacities
โช Time complexity for integer capacities: ๐ ๐ธ โ ๐ฃ(๐)max
โ not a polynomial time
Edmonds-Karp Algorithm
EdmondsKarp(๐บ = ๐, ๐ธ , ๐ , ๐ก, ๐):
1. initialize ๐ such that โ๐ โ ๐ธ: ๐ ๐ = 0; initialize ๐บ๐ โ ๐บ;
2. while there is an ๐ -๐ก path on ๐บ๐:
3. find such a path ๐ by BFS;
4. find an edge ๐ โ ๐ with minimum capacity ๐;
5. update ๐ that pushes ๐ units of flow along ๐;
6. update ๐บ๐;
7. endwhile
8. return ๐
Edmonds-Karp Algorithm
Why BFS?
โช BFS maintains the distancesโ distance: num of edges, not weighted distance
๐ ๐ก
dist = 1 dist = 2 dist = 3 dist = 4dist = 5
dist = 6dist = 0
A path found by an iteration of Edmonds-Karp Algorithm
Why BFS?
โช In the residual network ๐บ๐, a new appeared edge can only goes from a vertex at distance ๐ก + 1 to a vertex at distance ๐ก.
โช Addition of such edges does not decrease the distance between ๐ and ๐ข for every ๐ข โ ๐.
โช [Key Observation in this lecture!] Thus, dist(๐ข) is non-decreasing throughout the algorithm for every ๐ข โ ๐.
๐ ๐ก
dist โฅ 1 dist โฅ 2 dist โฅ 3 dist โฅ 4dist โฅ 5
dist โฅ 6dist = 0
The updates to the edges in ๐บ๐
Weak Monotonicity to Strong Monotonicity
โช dist(๐ข) can only be one of 0, 1, 2,โฆ , |๐|,โโ It can only be increased for |๐| + 1 times!
โช Itโs great that BFS buys us distance monotonicity!
โช However, weak monotonicity is not enough.
โช To make a progress, we need dist(๐ข) strictly increases for some ๐ข โ ๐, so that we can upper bound the number of iterations.
Counterexample: dist for all vertices remain unchanged after an iteration.
3
3
4
10
2
1
1
5
1
2
5
s
a
d
c
e
b
t
2
1
4
10
2
1
1
5
1
2
5
s
a
d
c
e
b
t3
Towards Strong Monotonicityโฆ
โช Observation: At least one edge (๐ข, ๐ฃ) on ๐ is โsaturatedโ, and this edge will be deleted in the next iteration.
โช Each iteration will remove an edge from a vertex at distance ๐ to a vertex at distance ๐ + 1.
โช Intuitively, we cannot keep removing such edges while keeping the distances of all vertices unchanged.
Towards Strong Monotonicity
โช Suppose we are at the ๐ + 1 -th iteration. ๐๐ is the current flow, and ๐ is the path found in ๐บ๐๐ at the ๐ + 1 -thiteration.
โช We say that an edge (๐ข, ๐ฃ) is critical if the amount of flow pushed along ๐ is ๐๐๐(๐ข, ๐ฃ).
โช A critical edge disappears in ๐บ๐๐+1 , but it may reappear in the futureโฆ
โช We will try to bound the number of times (๐ข, ๐ฃ) becomes critical.
Between two โcriticalโ
๐ ๐ก
๐ข ๐ฃ
๐
A flow along ๐ in ๐บ๐๐ where (๐ข, ๐ฃ) becomes critical
๐ ๐ก
๐ข ๐ฃIn ๐บ๐๐+1, (๐ข, ๐ฃ) disappears, and (๐ฃ, ๐ข) appears.
Before the next time (๐ข, ๐ฃ) becomes critical again, (๐ข, ๐ฃ) must first reappear!
๐ ๐ก
๐ข ๐ฃBefore (๐ข, ๐ฃ) reappears, the algorithm must have found ๐ going through (๐ฃ, ๐ข).
Between two โcriticalโ
๐ ๐ก
๐ข ๐ฃ
๐
๐ ๐ก
๐ข ๐ฃ
dist๐ ๐ฃ = dist๐ ๐ข + 1
dist๐+๐ ๐ข = dist๐+๐ ๐ฃ + 1
โช Distance monotonicity: dist๐+๐ ๐ฃ โฅ dist๐ ๐ฃ .
โช Thus, dist๐+๐ ๐ข = dist๐+๐ ๐ฃ + 1 โฅ dist๐ ๐ฃ + 1 โฅ dist๐ ๐ข + 2.
โช The distance of ๐ข from ๐ increases by 2 between two โcriticalโ.
Putting Together
โช The distance of ๐ข from ๐ increases by 2 between two โcriticalโ.
โช Distance takes value from 0, 1,โฆ , ๐ ,โ , and never decrease.
โช Thus, each edge can only be critical for ๐( ๐ ) times.
โช At least 1 edge become critical in one iteration.
โช Total number of iterations is ๐( ๐ โ ๐ธ ).
โช Each iteration takes ๐ ๐ธ time.
โช Overall time complexity for Edmonds-Karp: ๐ ๐ โ ๐ธ 2 .
โช It can handle the issue with irrational numbers!
Can we improve?
โช Each iteration takes ๐ ๐ธ time to find a shortest ๐ -๐ก path by BFS.
โช However, each shortest ๐ -๐ก path has length at most |๐|.
โช Idea: push flow from multipleshortest ๐ -๐ก paths in one iteration!
Dinicโs Algorithm (Dinitzโs Algorithm)
โช Proposed by Yefim Dinitz
โช Updated by Shimon Even and Alon Itai
โช Time complexity: ๐ ๐ 2 โ ๐ธ
Dinicโs Algorithm โ high-level ideas
โช Build a level graph:โ Vertices at Level ๐ are at distance ๐.
โ Only edges go from a level to the next level are kept.
โ Can be done in ๐ ๐ธ time using a similar idea to BFS.
3
3
4
10
2
1
1
5
1
2
5
s
a
d
c
e
b
t3
3
4
2
1
1
5
2
5
s
a
d
c
e
b
t
Level 1 Level 2 Level 3
Dinicโs Algorithm โ high-level ideas
โช Find a blocking flow on the level graph:โ Push flow on multiple ๐ -๐ก paths.
โ Each ๐ -๐ก path must contain a critical edge!
3
2/3
4/4
2/2
1
1
4/5
2/2
4/5
s
a
d
c
e
b
t
Level 1 Level 2 Level 3
Dinicโs Algorithm โ high-level ideas
โช Find a blocking flow on the level graph:โ Push flow on multiple ๐ -๐ก paths.
โ Each ๐ -๐ก path must contain a critical edge!
1/3
1/3
4/4
1/2
1/1
1
4/5
2/2
4/5
s
a
d
c
e
b
t
a blocking flow
1/3
3
4/4
2
1/1
1
4/5
1/2
4/5
s
a
d
c
e
b
t
not a blocking flow: path s-a-b-t contains no critical edge
Dinicโs Algorithm โ Overview
โช Initialize ๐ to be the empty flow and ๐บ๐ = ๐บ.
โช Iteratively do the followings until dist ๐ก = โ:
โ Construct the level graph ๐บ๐ฟ๐ for ๐บ๐.
โ Find a blocking flow on ๐บ๐ฟ๐.
โ Update ๐ and ๐บ๐.
Questions Remain
1. How many iterations do we need before termination?
2. How do we find a blocking flow?
Questions Remain
1. How many iterations do we need before termination?
2. How do we find a blocking flow?
Simple yet important observations
โช In the level graph ๐บ๐ฟ๐๐ at every iteration ๐, every ๐ -๐ก path has
length dist๐ ๐ก .
โช Every shortest ๐ -๐ก path in ๐บ๐๐ also appears in ๐บ๐ฟ๐๐.
ts
๐บ๐ฟ๐๐
Distance Monotonicity
โช After one iteration, a new edge (๐ข, ๐ฃ) appearing in ๐บ๐๐+1
(but not in ๐บ๐๐) must be โbackwardโ: dist๐ ๐ข = dist๐ ๐ฃ + 1.
ts
Level ๐ Level ๐ + 1
๐บ๐ฟ๐๐
u
vA new edge that will appear in ๐บ๐๐+1
Distance Monotonicity
โช After one iteration, a new edge (๐ข, ๐ฃ) appearing in ๐บ๐๐+1
(but not in ๐บ๐๐) must satisfy dist๐ ๐ข = dist๐ ๐ฃ + 1.
โช Such additions of edges cannot reduce the distance for any vertex!
โช We again have that dist(๐ข) is non-decreasing!
โช Can we have strong monotonicity?
Taking a closer lookโฆ
โช All the paths in ๐บ๐ฟ๐๐ with length dist๐(๐ก) are โblockedโ after
the ๐-th iteration.
โช Thus, a path in the (๐ + 1)-th iteration must use some edges that are not in ๐บ๐ฟ
๐๐.
Taking a closer lookโฆ
โช This new edge may be a โbackwardโ edge whose reverse was a critical edge in the previous iteration.
โช In this case, dist(๐ก) is increased by at least 2.
ts
๐บ๐ฟ๐๐
Taking a closer lookโฆ
โช Or, it may be an edge in ๐บ๐๐, but not in ๐บ๐ฟ๐๐.
โช In this case, dist(๐ก) is increased by at least 1.
ts
๐บ๐ฟ๐๐
Taking a closer lookโฆ
โช In both cases: dist๐+1 ๐ก > dist๐(๐ก)
โช Letโs prove it rigorously thenโฆ
Proving dist๐+1 ๐ก > dist๐(๐ก)
โช Consider an arbitrary ๐ -๐ก path ๐ in ๐บ๐ฟ๐๐+1 with length dist๐+1(๐ก).
โช We have dist๐+1 ๐ก โฅ dist๐(๐ก) by monotonicity.
โช Suppose for the sake of contraction that dist๐+1 ๐ก = dist๐(๐ก).
โช Case 1: all edges in ๐ also appear in ๐บ๐ฟ๐๐
โช Then ๐ is a shortest path containing no critical edges in ๐บ๐ฟ๐๐
โช Contracting to the definition of blocking flow!
Proving dist๐+1 ๐ก > dist๐(๐ก)
โช Case 2: ๐ contains an edge (๐ข, ๐ฃ) that is not in ๐บ๐ฟ๐๐
โช If (๐ข, ๐ฃ) was not in ๐บ๐๐, then (๐ฃ, ๐ข) was critical in the last iteration. We have dist๐ ๐ข = dist๐ ๐ฃ + 1.
โช If (๐ข, ๐ฃ) was in ๐บ๐๐ but not ๐บ๐ฟ๐๐, by the definition of level
graph, we have dist๐ ๐ข โฅ dist๐ ๐ฃ .
โช In both cases above, dist๐ ๐ข โฅ dist๐ ๐ฃ .
โช We have dist๐+1 ๐ข โฅ dist๐(๐ข) by monotonicity,
โช and we have dist๐+1 ๐ฃ, ๐ก โฅ dist๐(๐ฃ, ๐ก). (why?)
Proving dist๐+1 ๐ก > dist๐(๐ก)
โช Case 2: ๐ contains an edge (๐ข, ๐ฃ) that is not in ๐บ๐ฟ๐๐
โช Fact i: dist๐ ๐ข โฅ dist๐ ๐ฃ .
โช Fact ii: dist๐+1 ๐ข โฅ dist๐ ๐ข .
โช Fact iii: dist๐+1 ๐ฃ, ๐ก โฅ dist๐(๐ฃ, ๐ก).
Putting together:dist๐+1 ๐ก = dist๐+1 ๐ข + 1 + dist๐+1(๐ฃ, ๐ก)
โฅ dist๐ ๐ข + 1 + dist๐(๐ฃ, ๐ก)
โฅ dist๐ ๐ฃ + 1 + dist๐(๐ฃ, ๐ก)
โฅ dist๐ ๐ก + 1
(Fact ii and iii)
(Fact i)
(triangle inequality)
Putting Togetherโฆ
โช dist(๐ก) is increased by at least 1 after each iteration.
โช dist(๐ก) takes value from {0, 1, โฆ , ๐ ,โ}, so it can be increased for at most ๐ ๐ times.
โช Total number of iterations is at most ๐ ๐ .
Questions Remain
1. How many iterations do we need before termination?โ ๐ ๐
2. How do we find a blocking flow?
Finding a blocking flow in a level graphโฆ
Iteratively do the followings, until no path from ๐ to ๐ก:
โช Find an arbitrary maximal path in ๐บ๐ฟ๐ starting from ๐ :
โ At every vertex, find an arbitrary edge in ๐บ๐ฟ๐ and append it to the
path.
โช Two possibilities:โ End up at ๐ก: in this case, we update ๐ (by pushing flow along the
path) and remove the critical edge
โ End up at a dead-end, a vertex ๐ฃ with no out-going edges in ๐บ๐ฟ๐: in
this case, we remove all the incoming edges of ๐ฃ
Finding a blocking flow in a level graphโฆ
โช At least one edge is removed after each search.โ Total number of searches: ๐ ๐ธ
โช Each search takes at most |๐| steps.
โช Time complexity for each iteration of Dinicโs algorithm: ๐ ๐ โ ๐ธ .
Overall Time Complexity for Dinicโs Algorithm
โช Each iteration: ๐ ๐ โ ๐ธ .
โช We need at most ๐ ๐ iterations.
โช Overall time complexity for Dinicโs algorithm: ๐ ๐ 2 โ ๐ธ .
Other Algorithms for Max-Flow
โช Improvements to Dinicโs algorithm:โ [Malhotra, Kumar & Maheshwari, 1978]: ๐ ๐ 3
โ Dynamic tree: ๐ ๐ โ ๐ธ โ log ๐
โช Push-relabel algorithm [Goldberg & Tarjan, 1988]
โ ๐ ๐ 2 ๐ธ , later improved to ๐ ๐ 3 , ๐ ๐ 2 ๐ธ , ๐ ๐ ๐ธ log๐ 2
๐ธ
โช [King, Rao & Tarjan, 1994] and [Orlin, 2013]: ๐ ๐ โ ๐ธ
โช Interior-point-method-based algorithms:
โ [Kathuria, Liu & Sidford, 2020] ๐ธ4
3+๐(1)๐
1
3
โ [BLNPSSSW, 2020] [BLLSSSW, 2021] เทจ๐ ๐ธ + ๐3
2 log๐
โ [Gao, Liu & Peng, 2021] เทจ๐ ๐ธ3
2โ
1
328 log๐
HopcroftโKarpโKarzanov algorithm
โช Find a maximum bipartite matching in ๐ ๐ธ โ ๐ time.
โช Proposed independently by Hopcroft-Karp and Karzanov.
โช Can be viewed as a special case of Dinicโs algorithm.
Conversion to Max-Flow Problem
s
t
1111
1
1111111
1 11
11
11
1 1
1
Set the capacity to 1 for all edges.
Conversion to Max-Flow Problem
s
t
1111
1
1111111
1 11
11
11
1 1
1
Dinicโs algorithm runs in ๐ ๐ธ โ ๐ time for this special case.
Conversion to Max-Flow Problem
โช Integrality theorem also holds for Dinicโs algorithm:โ The flow output by Dinicโs algorithm in our case is integral.
โช We aim to show Dinicโs algorithm runs in ๐ ๐ธ โ ๐ time.
โช Step 1: Finding a blocking flow in a level graph takes ๐ ๐ธtime.
โช Step 2: Number of iterations is at most 2 ๐ .
Step 1: Finding a blocking flow in a level graph takes ๐ ๐ธ time.
Iteratively do the followings, until no path from ๐ to ๐ก:
โช Perform DFS from ๐
โช If we reach ๐ก, delete all edges on the ๐ -๐ก path (why can we do this?) and start over from ๐ .
โช If we ever go backward, delete the edge just travelled. (why can we do this?)
Step 1: Finding a blocking flow in a level graph takes ๐ ๐ธ time.
s t
An ๐ -๐ก path is found, remove all edges from the path.
Step 1: Finding a blocking flow in a level graph takes ๐ ๐ธ time.
s t
We have to go backward now; delete the edge just travelled.
Step 1: Finding a blocking flow in a level graph takes ๐ ๐ธ time.
s t
Again, we have to go backward; delete the edge just travelled.
Step 1: Finding a blocking flow in a level graph takes ๐ ๐ธ time.
s t
Again, we have to go backward; delete the edge just travelled.
Step 1: Finding a blocking flow in a level graph takes ๐ ๐ธ time.
s t
Again, we have to go backward; delete the edge just travelled.
Step 1: Finding a blocking flow in a level graph takes ๐ ๐ธ time.
s t
Find another ๐ -๐ก path; delete all edges on the path
Step 1: Finding a blocking flow in a level graph takes ๐ ๐ธ time.
s t
We have obtained a blocking flow!
Step 1: Finding a blocking flow in a level graph takes ๐ ๐ธ time.
Time complexity: ๐ ๐ธ
โช Each edge is visited at most once.
Step 2: Number of iterations is at most 2 ๐ .
โช If the algorithm terminates within ๐ iterations, we are already done!
โช Otherwise, let ๐ be the flow after ๐ iterations.
โช Claim: the maximum flow in ๐บ๐ has value at most ๐ .
Observation on ๐บ๐
โช In each iteration, for each ๐ฃ โ ๐ โ {๐ , ๐ก}, either its in-degree is 1, or its out-degree is 1.
โช Proof. By Inductionโฆ
โช At the beginning, this is clearly true.
t
s
1111
1
111111
1 11
11
11
1
1
in-degree = 1
out-degree = 1
Observation on ๐บ๐
โช In each iteration, for each ๐ฃ โ ๐ โ {๐ , ๐ก}, either its in-degree is 1, or its out-degree is 1.
โช Proof. At the beginning, this is clearly true.
โช For each iteration, the amount of flow going through ๐ฃ is either 0 or 1.
โช If it is 0, ๐ฃโs in-degree and out-degree are unchanged.
โช Otherwise, exactly one in-edge and one out-edge are flipped; the property is still maintained.
The maximum flow in ๐บ๐ has value at most ๐
โช Integrality Theorem: there exists a maximum integral flow ๐โฒ in ๐บ๐.
โช ๐โฒ consists of edge-disjoint paths.
โช Edge-disjointness implies vertex-disjointness by previous observation on ๐บ๐.
s t s t
violating flow conservation
The maximum flow in ๐บ๐ has value at most ๐
โช Max-flow on ๐บ๐, ๐โฒ, is integral and consists of edge-disjoint paths.
โช By our analysis to Dinicโs algorithm, dist๐บ๐๐ , ๐ก โฅ ๐ .
โช Each path in ๐โฒ has length at least ๐ .
โช There are at most ๐
๐= ๐ paths in ๐โฒ by vertex-disjointness.
โช ๐ฃ ๐โฒ โค ๐
Step 2: Number of iterations is at most 2 ๐ .
โช If the algorithm terminates within ๐ iterations, we are already done!
โช Otherwise, let ๐ be the flow after ๐ iterations.
โช Claim: the maximum flow in ๐บ๐ has value at most ๐ .
โช Each iteration increase the value of flow by at least 1.
โช Thus, the algorithm will terminates within at most another ๐ iterations.
โช Total number of iterations: 2 ๐ .
Putting Togetherโฆ
โช Step 1: Finding a blocking flow in a level graph takes ๐ ๐ธtime.
โช Step 2: Number of iterations is at most 2 ๐ .
โช Overall time complexity: ๐ ๐ธ โ ๐
Todayโs Lecture
Maximum Flow Problem:
โช Edmonds-Karp Algorithmโ Implement Ford-Fulkerson method by BFS
โ ๐ ๐ โ ๐ธ 2
โช Dinicโs Algorithmโ Push flow on multiple paths at one iterationโ Level graph and blocking flow
โ ๐ ๐ 2 โ |๐ธ|
Maximum Bipartite Matching Problem:
โช HopcroftโKarpโKarzanov algorithmโ Apply Dinicโs algorithm
โ ๐ ๐ธ โ ๐