Divide and Conquer - jhc.sjtu.edu.cn

Post on 14-Mar-2022

3 views 0 download

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

Step 1: Finding a blocking flow in a level graph takes 𝑂 𝐸 time.

s t

Step 1: Finding a blocking flow in a level graph takes 𝑂 𝐸 time.

s t

Step 1: Finding a blocking flow in a level graph takes 𝑂 𝐸 time.

s t

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

Start over…

Step 1: Finding a blocking flow in a level graph takes 𝑂 𝐸 time.

s t

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

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

Step 1: Finding a blocking flow in a level graph takes 𝑂 𝐸 time.

s t

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 are done!

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

– 𝑂 𝐸 β‹… 𝑉