cse 421 - courses.cs.washington.edu...•orient all edges from x to y, andassign infinite (or unit)...

41
CSE 421 Network Flows, Matching Shayan Oveis Gharan 1

Upload: others

Post on 04-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

CSE 421

Network Flows, Matching

Shayan Oveis Gharan

1

Page 2: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Network Flows

Page 3: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Residual Graph

Original edge: e = (u, v) Î E.• Flow f(e), capacity c(e).

Residual edge.• "Undo" flow sent.• e = (u, v) and eR = (v, u).• Residual capacity:

𝑐! 𝑒 = $𝑐 𝑒 − 𝑓 𝑒 𝑖𝑓 𝑒 ∈ 𝐸𝑓 𝑒 𝑖𝑓 𝑒" ∈ 𝐸

Residual graph: Gf = (V, Ef ).• Residual edges with positive residual capacity.• 𝐸! = 𝑒 ∶ 𝑓 𝑒 < 𝑐 𝑒 ∪ {𝑒 ∶ 𝑓(𝑒") > 0}.

3

u v17

6

capacity

u v11

residual capacity

6residual capacity

flow

Page 4: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Ford-Fulkerson Alg: Greedy on Gf

4

s

2

3

4

5 t10

10

9

8

4

10

1062G:

capacity0

0

0 0

00

00

0

s

2

3

4

5 t10 9

8

10

62Gf:

10

4

10

Find Path

Page 5: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Ford-Fulkerson Alg: Greedy on Gf

5

s

2

3

4

5 t10

10

9

8

4

10

1062G:

capacity4

4

0 0

00

04

0

s

2

3

4

5 t10 9

8

10

62Gf:

10

4

10

Update Flow

Page 6: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Ford-Fulkerson Alg: Greedy on Gf

6

s

2

3

4

5 t10

10

9

8

4

10

1062G:

capacity4

4

0 0

00

04

0

s

2

3

4

5 t10 9

8

10

62Gf:

6 6

Update Residual

4

4

4

Page 7: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Ford-Fulkerson Alg: Greedy on Gf

7

s

2

3

4

5 t10

10

9

8

4

10

1062G:

capacity4

4

0 0

00

04

0

s

2

3

4

5 t10

8 6Gf:

6

Find Path

4

4

4

9 10

6 2

Page 8: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Ford-Fulkerson Alg: Greedy on Gf

8

s

2

3

4

5 t10

10

9

8

4

10

1062G:

capacity6

4

0 2

20

04

2

s

2

3

4

5 t10

8 6Gf:

64

4

4

9 10

6 2

Update Flow

Page 9: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Ford-Fulkerson Alg: Greedy on Gf

9

s

2

3

4

5 t10

10

9

8

4

10

1062G:

capacity6

4

0 2

20

04

2

s

2

3

4

5 t10

8 6Gf:

66

4

4

7 8

4 2

Update Residual

2 2

Page 10: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Ford-Fulkerson Alg: Greedy on Gf

10

s

2

3

4

5 t10

10

9

8

4

10

1062G:

capacity6

4

0 2

20

04

2

s

2

3

4

5 t10

6Gf:

66

4

4

7

2

2 2

8

8

4

Find Path

Page 11: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Ford-Fulkerson Alg: Greedy on Gf

11

s

2

3

4

5 t10

10

9

8

4

10

1062G:

capacity10

4

0 2

24

04

6

s

2

3

4

5 t10

6Gf:

66

4

4

7

2

2 2

8

8

4

Update Flow

Page 12: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Ford-Fulkerson Alg: Greedy on Gf

12

s

2

3

4

5 t10

10

9

8

4

10

1062G:

capacity10

4

0 2

24

04

6

s

2

3

4

5 t10

6Gf:

610

4

4

7

2

2 6

4

4

Update Residual

4

Page 13: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Ford-Fulkerson Alg: Greedy on Gf

13

s

2

3

4

5 t10

10

9

8

4

10

1062G:

capacity10

4

0 2

24

04

6

s

2

3

4

5 t

Gf: 10

4

4

72 6

4

Find Path

4

6642

10

Page 14: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Ford-Fulkerson Alg: Greedy on Gf

14

s

2

3

4

5 t10

10

9

8

4

10

1062G:

capacity10

4

2 2

06

26

6

s

2

3

4

5 t

Gf: 10

4

4

72 6

4

Update Flow

4

6642

10

Page 15: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Ford-Fulkerson Alg: Greedy on Gf

15

s

2

3

4

5 t10

10

9

8

4

10

1062G:

capacity10

4

2 2

06

26

6

s

2

3

4

5 t

Gf: 10

4

6

72 6

4

Update Residual

6

422

8

24

2

Page 16: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Ford-Fulkerson Alg: Greedy on Gf

16

s

2

3

4

5 t10

10

9

8

4

10

1062G:

capacity10

4

2 2

06

26

6

s

2

3

4

5 t

Gf: 10

4

6

2 6

Find Path

6

422 24

2

8 7 4

Page 17: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Ford-Fulkerson Alg: Greedy on Gf

17

s

2

3

4

5 t10

10

9

8

4

10

1062G:

capacity10

4

6 6

06

26

10

s

2

3

4

5 t

Gf: 10

4

6

2 6

Update Flow

6

422 24

2

8 7 4

Page 18: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Ford-Fulkerson Alg: Greedy on Gf

18

s

2

3

4

5 t10

10

9

8

4

10

1062G:

capacity10

4

6 6

06

26

10

s

2

3

4

5 t

Gf: 10

4

6

6 10

Update Residual

6

422 24

6

4 3

Page 19: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Ford-Fulkerson Alg: Greedy on Gf

19

s

2

3

4

5 t10

10

9

8

4

10

1062G:

capacity10

4

6 6

06

26

10

s

2

3

4

5 t

Gf: 10

4

6

6 10

Find Path

6

222

6

44

34

Page 20: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Ford-Fulkerson Alg: Greedy on Gf

20

s

2

3

4

5 t10

10

9

8

4

10

1062G:

capacity10

4

9 9

06

59

10

s

2

3

4

5 t

Gf: 10

4

6

6 10

Update FLow

6

222

6

44

34

Page 21: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Ford-Fulkerson Alg: Greedy on Gf

21

s

2

3

4

5 t10

10

9

8

4

10

1062G:

capacity10

4

9 9

06

59

10

s

2

3

4

5 t

Gf: 10

4

9

9 10

Find Path

6

225

9

11

1

Page 22: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Augmenting Path Algorithm

22

Augment(f, c, P) {b ¬ bottleneck(P) foreach e Î P {

if (e Î E) f(e) ¬ f(e) + bc(e) ¬ c(e) – bc(eR) ¬ c(eR) + b

else f(eR) ¬ f(eR) – bc(eR) ¬ c(eR) + bc(e) ¬ c(e) - b

}return f

}

Ford-Fulkerson(G, s, t, c) {foreach e Î E f(e) ¬ 0. Gf is residual graphwhile (there exists augmenting path P) {

f ¬ Augment(f, c, P)}

return f}

Smallest capacity edge on P

Forward edge

Reverse edge

Page 23: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Max Flow Min Cut Theorem

Augmenting path theorem. Flow f is a max flow iff there are no augmenting paths. Max-flow min-cut theorem. [Ford-Fulkerson 1956] The value of the max s-t flow is equal to the value of the min s-t cut.Proof strategy. We prove both simultaneously by showing the TFAE:

(i) There exists a cut (A, B) such that v(f) = cap(A, B).(ii) Flow f is a max flow.(iii) There is no augmenting path relative to f.

(i) Þ (ii) This was the corollary to weak duality lemma.

(ii) Þ (iii) We show contrapositive.Let f be a flow. If there exists an augmenting path, then we can improve f by sending flow along that path.

23

Page 24: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Pf of Max Flow Min Cut Theorem

(iii) => (i)No augmenting path for f => there is a cut (A,B): v(f)=cap(A,B)

• Let f be a flow with no augmenting paths.• Let A be set of vertices reachable from s in residual graph.• By definition of A, s Î A.• By definition of f, t Ï A.

24

𝑣 𝑓 = 5# $%& $' (

𝑓 𝑒 − 5# )* &$ (

𝑓(𝑒)

= 5# $%& $' (

𝑐 𝑒

= 𝑐𝑎𝑝(𝐴, 𝐵)

Page 25: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Running Time

Assumption. All capacities are integers between 1 and C.

Invariant. Every flow value 𝑓(𝑒) and every residual capacities 𝑐𝑓 (𝑒) remains an integer throughout the algorithm.

Theorem. The algorithm terminates in at most 𝑣(𝑓∗) £ 𝑛𝐶 iterations, if 𝑓∗ is optimal flow.Pf. Each augmentation increase value by at least 1.

Corollary. If C = 1, Ford-Fulkerson runs in O(mn) time.

Integrality theorem. If all capacities are integers, then there exists a max flow f for which every flow value f(e) is an integer.Pf. Since algorithm terminates, theorem follows from invariant.

25

Page 26: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Applications of Max Flow:Bipartite Matching

Page 27: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Maximum Matching Problem

Given an undirected graph G = (V, E).A set 𝑀 ⊆ 𝐸 is a matching if each node appears in at most one edge in M.Goal: find a matching with largest cardinality.

27

Page 28: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Bipartite Matching Problem

Given an undirected bibpartite graph 𝐺 = (𝑋 ∪ 𝑌, 𝐸)A set 𝑀 ⊆ 𝐸 is a matching if each node appears in at most one edge in M.Goal: find a matching with largest cardinality.

28

1

3

5

1'

3'

5'

2

4

2'

4'X Y

Page 29: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Bipartite Matching using Max Flow

Create digraph H as follows:• Orient all edges from X to Y, and assign infinite (or unit) capacity.• Add source s, and unit capacity edges from s to each node in L.• Add sink t, and unit capacity edges from each node in R to t.

29

s

1

3

5

1'

3'

5'

t

2

4

2'

4'

1 1

YX

H

Page 30: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Bipartite Matching: Proof of Correctness

Thm. Max cardinality matching in G = value of max flow in H.Pf. £Given max matching M of cardinality k.Consider flow f that sends 1 unit along each of k edges of M.f is a flow, and has cardinality k. ▪

30

1

3

5

1'

3'

5'

2

4

2'

4'

G

4 4'

s

1

3

5

1'

3'

5'

t

2 2'1 1

∞ H

Page 31: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Bipartite Matching: Proof of Correctness

Thm. Max cardinality matching in G = value of max flow in H.Pf. (of ≥) Let f be a max flow in H of value k.Integrality theorem Þ k is integral and we can assume f is 0-1.Consider M = set of edges from X to Y with f(e) = 1.• each node in X and Y participates in at most one edge in M• |M| = k: consider s-t cut (𝑠 ∪ 𝑋, 𝑡 ∪ 𝑌)

31

s

1

3

5

1'

3'

5'

t

2

4

2'

4'

1 1

∞ H 1

3

5

1'

3'

5'

2

4

2'

4'

G

Page 32: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Perfect Bipartite Matching

Page 33: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Perfect Bipartite Matching

Def. A matching M Í E is perfect if each node appears in exactly one edge in M.

Q. When does a bipartite graph have a perfect matching?

Structure of bipartite graphs with perfect matchings:• Clearly we must have |X| = |Y|.• What other conditions are necessary?• What conditions are sufficient?

33

Page 34: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Perfect Bipartite Matching: N(S)

Def. Let S be a subset of nodes, and let N(S) be the set of nodes adjacent to nodes in S.

Observation. If a bipartite graph G has a perfect matching, then |N(S)| ³ |S| for all subsets S ⊆ 𝑋.Pf. Each 𝑣 ∈ 𝑆 has to be matched to a unique node in N(S).

34

S

N(S)

SN(S)

Page 35: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Marriage Theorem

Thm: [Frobenius 1917, Hall 1935] Let 𝐺 = (𝑋 ∪ 𝑌, 𝐸) be a bipartite graph with |X| = |Y|. Then, G has a perfect matching iff 𝑁 𝑆 ≥ 𝑆 for all subsets 𝑆 ⊆ 𝑋.

Pf. ÞThis was the previous observation.If |N(S)| < |S| for some S, then there is no perfect matching.

35

Page 36: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Marriage Theorem

Pf. ∃𝑆 ⊆ 𝑋 s.t., |𝑁 𝑆 | < |𝑆| ⇐ G does not a perfect matchingFormulate as a max-flow and let (𝐴, 𝐵) be the min s-t cutG has no perfect matching => 𝑣 𝑓∗ < |𝑋|. So, 𝑐𝑎𝑝 𝐴, 𝐵 < |𝑋|Define 𝑋" = 𝑋 ∩ 𝐴, 𝑋# = 𝑋 ∩ 𝐵, 𝑌" = 𝑌 ∩ 𝐴Then, 𝑐𝑎𝑝 𝐴, 𝐵 = 𝑋# + |𝑌"|Since min-cut does not use ∞ edges, 𝑁 𝑋" ⊆ 𝑌"𝑁 𝑋" ≤ 𝑌" = 𝑐𝑎𝑝 𝐴, 𝐵 − 𝑋# = 𝑐𝑎𝑝 𝐴, 𝐵 − 𝑋 + 𝑋" < |𝑋"|

36

s t

𝑋(

𝑋, 𝑌,

𝑌(

Page 37: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

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: 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]

37

Page 38: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Edge Disjoint Paths

Page 39: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Edge Disjoint Paths Problem

Given a digraph G = (V, E) and two nodes s and t, find the max number of edge-disjoint s-t paths.

Def. Two paths are edge-disjoint if they have no edge in common.

Ex: communication networks.

39

s

2

3

4

5

6

7

t

Page 40: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Max Flow Formulation

Assign a unit capacitary to every edge. Find Max flow from s to t.

Thm. Max number edge-disjoint s-t paths equals max flow value.Pf. £Suppose there are k edge-disjoint paths 𝑃$, … , 𝑃%.Set f(e) = 1 if e participates in some path 𝑃& ; else set f(e) = 0.Since paths are edge-disjoint, f is a flow of value k. ▪

40

s t

1

1

1

1

1

1

11

1

1

1

1

1

1

Page 41: CSE 421 - courses.cs.washington.edu...•Orient all edges from X to Y, andassign infinite (or unit) capacity. •Add source s, and unitcapacity edges from s to each node in L. •Add

Max Flow Formulation

Thm. Max number edge-disjoint s-t paths equals max flow value.Pf. ≥ Suppose max flow value is kIntegrality theorem Þ there exists 0-1 flow f of value k.Consider edge (s, u) with f(s, u) = 1.• by conservation, there exists an edge (u, v) with f(u, v) = 1• continue until reach t, always choosing a new edgeThis produces k (not necessarily simple) edge-disjoint paths. ▪

41

s t

1

1

1

1

1

1

11

1

1

1

1

1

1

We can return to u so we can have cycles. But we can eliminate cycles if desired