maxflow - university of california, davislrademac/sp16_2331/maxflow.pdf · hhh hh 0/9 y||y yyy yyy...

60
CSE 2331 Maximum Flow 10.1

Upload: others

Post on 23-Nov-2019

18 views

Category:

Documents


0 download

TRANSCRIPT

CSE 2331

Maximum Flow

10.1

CSE 2331

Flow Network

v112 // v3

20

��???

????

??

9

������

����

����

����

���

s

16

??~~~~~~~~~

13

��@@@

@@@@

@@t

v214

//

4

OO

v4

4

??���������

7

OO

c(u, v) = capacity of edge (u, v).

10.2

CSE 2331

Flow

v1

12/12 // v3

15/20

��???

????

??

0/9

������

����

����

����

���

s

11/16

??~~~~~~~~~

8/13��@

@@@@

@@@@

t

v27/14

//

1/4

OO

v4

4/4

??���������

3/7

OO

c(u, v) = capacity of edge (u, v).

f(u, v) = flow along edge (u, v).

10.3

CSE 2331

Capacity Constraint

v1

12/12 // v3

15/20

��???

????

??

0/9

������

����

����

����

���

s

11/16

??~~~~~~~~~

8/13��@

@@@@

@@@@

t

v27/14

//

1/4

OO

v4

4/4

??���������

3/7

OO

Capacity constraint: 0 ≤ f(u, v) ≤ c(u, v).

c(u, v) = capacity of edge (u, v).

f(u, v) = flow along edge (u, v).

10.4

CSE 2331

Flow Conservation

v1

12/12 // v3

15/20

##HHHHHHH

0/9

||yyyyy

yyyyyy

yyyyy

s

11/16;;vvvvvvv

8/13 ##HHHHHHH t

v27/14

//

1/4

OO

v4

4/4

;;vvvvvvv

3/7

OO

Flow conservation: For all u ∈ G.V − {s, t},

flow in to u = flow out from u, or∑

v∈V

f(v, u) =∑

v∈V

f(u, v).

f(u, v) = flow along edge (u, v).

10.5

CSE 2331

Flow

v1

12/12 // v3

15/20

##HHHHHHH

0/9

||yyyyy

yyyyyy

yyyyy

s

11/16;;vvvvvvv

8/13 ##HHHHHHH t

v27/14

//

1/4

OO

v4

4/4

;;vvvvvvv

3/7

OO

A flow is a function f : G.E → R where

1. Capacity constraint: 0 ≤ f(u, v) ≤ c(u, v);

2. Flow conservation:∑

v∈V f(v, u) =∑

v∈V f(u, v).

10.6

CSE 2331

Flow

v1

12/12 // v3

15/20

##FFF

FFFF

F

0/9

}}{{{{

{{{{

{{{{

{{{{

{

s

11/16;;xxxxxxxx

8/13 ##FFF

FFFF

F t

v27/14

//

1/4

OO

v4

4/4

;;xxxxxxxx

3/7

OO

The value of the flow, |f |, is the amount flowing out of node s:

|f | =∑

v∈G.V

f(s, v).

10.7

CSE 2331

Flow

v1

12/12 // v3

15/20

##FFF

FFFF

F

0/9

}}{{{{

{{{{

{{{{

{{{{

{

s

11/16;;xxxxxxxx

8/13 ##FFF

FFFF

F t

v27/14

//

1/4

OO

v4

4/4

;;xxxxxxxx

3/7

OO

Lemma:

flow out of node s = flow in to node t, or

|f | =∑

v∈G.V

f(s, v) =∑

v∈G.V

f(v, t).

10.8

CSE 2331

Max Flow Problem

v1

12/12 // v3

15/20

$$JJJJJJJ

0/9

{{wwwwwwwwwwwwwww

s

11/1699ttttttt

8/13 %%JJJJJJJ t

v27/14

//

1/4

OO

v4

4/4

::ttttttt

3/7

OO

The value of the flow, |f |, is the amount flowing out of node s:

|f | =∑

v∈G.V

f(s, v).

max flow problem: Given a flow network G,

find max |f | over all flows f in G.

10.9

CSE 2331

Flow

v1

0/12 // v3

0/20

��???

????

??

0/9

������

����

����

����

���

s

0/16

??~~~~~~~~~

0/13��@

@@@@

@@@@

t

v20/14

//

0/4

OO

v4

0/4

??���������

0/7

OO

10.10

CSE 2331

Flow: Example 2

v1

0/7

AAA

AAAA

AA

0/5

��s

0/9 ++

0/3

33

0/4 // v2

0/7 //

0/5

>>}}}}}}}}}

0/2 A

AAAA

AAAA

v3

0/4 // t

v4

0/3

>>}}}}}}}}}

0/8

CC

10.11

CSE 2331

Flow: Example 2

v1

0/7

AAA

AAAA

AA

5/5

��s

5/9 ++

3/3

33

4/4 // v2

4/7 //

0/5

>>}}}}}}}}}

0/2 A

AAAA

AAAA

v3

4/4 // t

v4

0/3

>>}}}}}}}}}

3/8

CC

10.12

CSE 2331

Residual Capacity

Flow network (part): Residual capacity:

v1

0/7

��777

7777

s

5/9 ++

4/4 // v2

4/7 //

0/5

CC�������v3

v1

s v2 v3

Residual capacity:

cf (u, v) =

c(u, v)− f(u, v) if (u, v) ∈ G.E,

f(v, u) if (v, u) ∈ G.E,

0 otherwise.

10.13

CSE 2331

Residual NetworkFlow network:

v10/7

''NNNNNN

5/5

��s

5/9

((

3/3

66

4/4 // v2

4/7 //

0/5 88pppppp

0/2 ''NNNNNN v3

4/4 // t

v40/3

88pppppp

3/8

AA

Residual network:

v17

''NNNNNNww

5

5

��s

4

((

\\

3

oo 4 v2

3 //oo4

588pppppp

2 ''NNNNNN v3oo 4

t

v4

3

88pppppp

5

AA

gg3

10.14

CSE 2331

Augmenting Path

Residual network:

v1

7

AAA

AAAA

AAss 5

5

��s

4 ++

[[

3

oo 4 v2

3 //oo4

5

>>}}}}}}}}}

2

AAA

AAAA

AAv3

oo 4t

v4

3

>>}}}}}}}}}

5

CC

kk 3

An augmenting path is a path from s to t in the residual network.

10.15

CSE 2331

Residual NetworkFlow network:

v1

12/12 // v315/20

''OOOOOO

0/9

xxqqqqqqqqqqqqqq

s

11/16 77oooooo

8/13 ''OOOOOO t

v27/14

//

1/4

OO

v44/4

77oooooo

3/7

OO

Residual capacity:

cf (u, v) =

c(u, v)− f(u, v) if (u, v) ∈ G.E,

f(v, u) if (v, u) ∈ G.E,

0 otherwise.

Assume that G.E never contains both (u, v) and (v, u).

10.16

CSE 2331

What is the residual network?

v1

PPPPPPP 2/7

''PPPPPPP

5/5

��s

7/9

((

3/3

66

4/4 // v2

2/7 //

0/5 77ooooooo

PPPPPPP

2/2 ''PPPPPPP v3

4/4 // t

v40/3

77ooooooo

5/8

??

v1

s v2 v3 t

v4

10.17

CSE 2331

What is the residual network?

v1

0/12 // v30/20

&&LLLLLLL

0/9

zzttttttttttttttt

s

0/1688rrrrrrr

0/13 &&LLLLLLL t

v20/14

//

0/4

OO

v4

0/4

88rrrrrrr

0/7

OO

v1 v3

s t

v2 v4

10.18

CSE 2331

What is the residual network?

v1

12/12 // v3

LLLLLLL

12/20

&&LLLLLLL

0/9

zzttttttttttttttt

s

rrrrrrr

12/1688rrrrrrr

LLLLLLL

4/13 &&LLLLLLL t

v24/14

//

0/4

OO

v4

rrrrrrr 4/4

88rrrrrrr

0/7

OO

v1 v3

s t

v2 v4

10.19

CSE 2331

What is the residual network?

v1

12/12 // v3

LLLLLLL

19/20

&&LLLLLLL

0/9

zzttttttttttttttt

s

rrrrrrr

12/1688rrrrrrr

LLLLLLL

11/13 &&LLLLLLL t

v211/14

//

0/4

OO

v4

rrrrrrr 4/4

88rrrrrrr

7/7

OO

v1 v3

s t

v2 v4

10.20

CSE 2331

Residual Network

G is a directed graph where G.E never contains both (u, v) and

(v, u).

f is a flow network on G.

Residual capacity:

cf (u, v) =

c(u, v)− f(u, v) if (u, v) ∈ G.E,

f(v, u) if (v, u) ∈ G.E,

0 otherwise.

Gf is the residual network whose edges have capacities cf (u, v).

10.21

CSE 2331

Ford-Fulkerson Max Flow Algorithm

procedure FFMaxFlow(G)

1 foreach edge (u, v) ∈ E(G) do f(u, v)← 0;

2 Compute residual network Gf ;

3 Search for path P in residual network Gf ;

4 while there exists a path P from s to t in Gf do

5 x← min{cf (u, v)|(u, v) ∈ P};

6 Increase flow in G by x along path P ;

7 Compute residual network Gf ;

8 Search for path P in residual network Gf ;

9 end

10.22

CSE 2331

Ford-Fulkerson Max Flow Algorithm (Detailed)

procedure FFMaxFlow(G)

1 foreach edge (u, v) ∈ E(G) do f(u, v)← 0;

2 Compute residual network Gf ;

3 Search for path P in residual network Gf ;

4 while there exists a path P from s to t in Gf do

5 x← min{cf (u, v)|(u, v) ∈ P};

/* Increase flow in G by x along path P */

6 foreach edge (u, v) ∈ P do

7 if (u, v) ∈ E(G) then f(u, v)← f(u, v) + x;

8 else f(v, u)← f(v, u)− x ; /* (v, u) ∈ E(G) */

9 end

10 Compute residual network Gf ;

11 Search for path P in residual network Gf ;

12 end

10.23

CSE 2331

Max Flow Example

v1

6

$$IIIIIIIIIII 4

##s

7

::vvvvvvvvvvv 5 // v29 //

8

::v32 // t

10.24

CSE 2331

Max Flow Example

v1

6

$$IIIIIIIIIII 4

##s

7

::vvvvvvvvvvv 5 // v29 //

8

::v32 // t

v1

s v2 v3 t

10.25

CSE 2331

Max Flow Example

v1

6

$$IIIIIIIIIII 4

##s

7

::vvvvvvvvvvv 5 // v29 //

8

::v32 // t

v1

s v2 v3 t

10.26

CSE 2331

Minimum Cut

10.27

CSE 2331

Cut

v112 // v3

20

��???

????

??

9

������

����

����

����

���

s

16

??~~~~~~~~~

13

��@@@

@@@@

@@t

v214

//

4

OO

v4

4

??���������

7

OO

A cut (S, T ) of a flow network G is a partition of G.V into S and

T = G.V − S such that s ∈ S and t ∈ T .

10.28

CSE 2331

Cut

v112 // v3

20

��???

????

??

9

������

����

����

����

���

s

16

??~~~~~~~~~

13

��@@@

@@@@

@@t

v214

//

4

OO

v4

4

??���������

7

OO

A cut (S, T ) of a flow network G is a partition of G.V into S and

T = G.V − S such that s ∈ S and t ∈ T .

The capacity of the cut (S, T ) is:

c(S, T ) =∑

u∈S

v∈T

c(u, v).

10.29

CSE 2331

Cut

v112 // v3

20

��???

????

??

9

������

����

����

����

���

s

16

??~~~~~~~~~

13

��@@@

@@@@

@@t

v214

//

4

OO

v4

4

??���������

7

OO

10.30

CSE 2331

Minimum Cut

v112 // v3

20

��???

????

??

9

������

����

����

����

���

s

16

??~~~~~~~~~

13

��@@@

@@@@

@@t

v214

//

4

OO

v4

4

??���������

7

OO

The capacity of the cut (S, T ) is c(S, T ) =∑

u∈S

v∈T c(u, v).

A mininum cut of G is a cut whose capacity is minimum

over all cuts of G.

10.31

CSE 2331

Minimum Cut

v1

6

""FFFFFFFFFFFF

4

""s

7

<<yyyyyyyyyyyy 5 // v29 //

8

::v32 // t

10.32

CSE 2331

Minimum Cut

v1

7

AAA

AAAA

AA

5

��s

9 ++

333

4 // v27 //

5

>>}}}}}}}}}

2

AAA

AAAA

AAv3

4 // t

v4

3

>>}}}}}}}}}

8

CC

10.33

CSE 2331

Flows and Cuts

v1

6

$$IIIIIIIIIII 4

##s

7

::vvvvvvvvvvv 5 // v29 //

8

::v32 // t

|f | =∑

v∈G.V f(s, v) is the value of flow f .

c(S, T ) =∑

u∈S

v∈T c(u, v) is the capacity of cut (S, T ).

Lemma (Cut Lemma). For any flow f and any cut (S, T ),

|f | ≤ c(S, T ).

10.34

CSE 2331

max-flow min-cut theorem

Theorem. For any flow network G,

max flow of G = min cut of G!

10.35

CSE 2331

Max-flow min-cut theorem

Proof: The following three conditions are equivalent:

1. f is a maximum flow of G.

2. There are no augmenting paths in the residual network Gf .

3. |f | = c(S, T ) for some cut (S, T ) of G.

10.36

CSE 2331

Max-flow min-cut theorem: (1)⇒ (2).

The following three conditions are equivalent:

1. f is a maximum flow of G.

2. There are no augmenting paths in the residual network Gf .

3. |f | = c(S, T ) for some cut (S, T ) of G.

(1)⇒ (2): If Gf had an augmenting path P , then we could

increase |f | by adding flow along P to f .

10.37

CSE 2331

Max-flow min-cut theorem: (2)⇒ (3).

The following three conditions are equivalent:

1. f is a maximum flow of G.

2. There are no augmenting paths in the residual network Gf .

3. |f | = c(S, T ) for some cut (S, T ) of G.

(2)⇒ (3): Assume Gf has no augmenting path.Let S = {v ∈ G.V : there is a path from s to v in Gf}.Let T = G.V − S.

Since there is no edge in Gf from any u ∈ S to any v ∈ T :

• the flow from S to T is∑

u∈S

v∈T c(u, v) = c(S, T );

• there is no flow from T to S.

Thus |f | = c(S, T ).

10.38

CSE 2331

Max-flow min-cut theorem: (3)⇒ (1).

The following three conditions are equivalent:

1. f is a maximum flow of G.

2. There are no augmenting paths in the residual network Gf .

3. |f | = c(S, T ) for some cut (S, T ) of G.

(3)⇒ (1): Assume |f | = c(S, T ).

By the cut lemma (slide 10.34), |f ′| ≤ c(S, T ) for any flow f ′ in G.

Thus, |f ′| ≤ c(S, T ) = |f | so |f | is a maximum flow.

10.39

CSE 2331

Ford-Fulkerson Max Flow Algorithm

Running Time Analysis

10.40

CSE 2331

Ford-Fulkerson Max Flow Algorithm: Time

procedure FFMaxFlow(G)

1 foreach edge (u, v) ∈ E(G) do f(u, v)← 0;

2 Compute residual network Gf ;

3 Search for path P in residual network Gf ;

4 while there exists a path P from s to t in Gf do

5 x← min{cf (u, v)|(u, v) ∈ P};

6 Increase flow in G by x along path P ;

7 Compute residual network Gf ;

8 Search for path P in residual network Gf ;

9 end

10.41

CSE 2331

Ford-Fulkerson Max Flow Algorithm: Time

Lemma. If all capacities are integers, then FFMaxFlow increases

the flow value by a positive integer at each iteration.

10.42

CSE 2331

Ford-Fulkerson Max Flow Algorithm: Time

Lemma. If all capacities are integers, then FFMaxFlow increasesthe flow value by a positive integer at each iteration.

Idea of proof. Initially, flow is zero so all flows are integers.

If all capacities and flows are integers:⇓

Capacities in the residual network Gf are integers.⇓

x = min{cf (u, v)|(u, v) ∈ P} is an integer.⇓

New flow in G is an integer.⇓

All capacities and flows are integers.

Apply induction for formal proof.

10.43

CSE 2331

FF Max Flow Algorithm: Time Analysis

procedure FFMaxFlow(G)

1 foreach edge (u, v) ∈ E(G) do f(u, v)← 0;

2 Compute residual network Gf ;

3 Search for path P in residual network Gf ;

4 while there exists a path P from s to t in Gf do

5 x← min{cf (u, v)|(u, v) ∈ P};

6 Increase flow in G by x along path P ;

7 Compute residual network Gf ;

8 Search for path P in residual network Gf ;

9 end

10.44

CSE 2331

Ford-Fulkerson Max Flow Algorithm: Time

m = # graph edges.

Proposition. If all capacities are integers, then the Ford-Fulkerson

Algorithm runs in O(m|f∗|) time where f∗ is the max flow.

10.45

CSE 2331

Multi-Source/Sink Max-Flow

10.46

CSE 2331

Multiple Sources and Sinks

v13 // v2

5

��

s24

oo 7 // v3

3

��s1

8

OO

4 //

3

��

v46 // v5

4

��

2

>>}}}}}}}}}v6

9

��v7

1 // t1 v82

oo 3 // t2

Sources: s1 and s2.

Sinks: t1 and t2.

Flow value |f | =∑

si

vjf(si, vj).

10.47

CSE 2331

Reduction

Multi-Source/Sink Max-Flow Problem: Given a flow network G

with multiple sources and sinks, find max |f | over all flows f in G.

Single Source/Sink Max-Flow Problem: Given a flow network G

with one source and sink, find max |f | over all flows f in G.

Reduce the Multi-Source/Sink Max-Flow Problem to the Single

Source Max Flow Problem.

Reduce P to Q: Turn problem P into Q such that the solution to

Q gives the solution to P .

10.48

CSE 2331

Multi-Source/Sink Max-Flow Problem

Reduce Multi-Source/Sink Max-Flow Problem to Single

Source/Sink Max-Flow Problem:

v13 // v2

5

��

s24

oo 7 // v3

3

��s1

8

OO

4 //

3

��

v46 // v5

4

��

2

>>}}}}}}}}}v6

9

��v7

1 // t1 v82

oo 3 // t2

10.49

CSE 2331

Multi-Source/Sink Max-Flow Problem

Reduce the Multi-Source/Sink Max-Flow Problem to the Single

Source Max Flow Problem:

Let G be a flow network with multiple sources si and sinks ti.

Create flow network G′ from G with a single source and sink as

follows:

• Add new source s∗ and new sink t∗;

• Add directed edges from s∗ to each si.

Set capacity of each new edge to ∞.

• Add directed edges from each ti to t∗.

Set capacity of each new edge to ∞.

G′ has flow with value F from s∗ to t∗ if and only if G has flow

with value F from the si to the ti.

10.50

CSE 2331

Bipartite Matching

10.51

CSE 2331

Bipartite Graph

u1

YYYYYYYYYYYYYYYYYYYYYYYYY

RRRRRRRRRRRRRRRRRRRRRRRRRR w1

u2

eeeeeeeeeeeeeeeeeeeeeeeee

RRRRRRRRRRRRRRRRRRRRRRRRRR w2

u3

eeeeeeeeeeeeeeeeeeeeeeeee

RRRRRRRRRRRRRRRRRRRRRRRRRR w3

u4

llllllllllllllllllllllllllw4

w5

Definition. An undirected graph G is bipartite if its vertices can

be partitioned into two sets U and W such that every graph edge

e ∈ G.E has one endpoint ui in U and one endpoint wj in W .

10.52

CSE 2331

Bipartite Matching

u1

YYYYYYYYYYYYYYYYYYYYYYYYY

RRRRRRRRRRRRRRRRRRRRRRRRRR w1

u2

eeeeeeeeeeeeeeeeeeeeeeeee

RRRRRRRRRRRRRRRRRRRRRRRRRR w2

u3

eeeeeeeeeeeeeeeeeeeeeeeee

RRRRRRRRRRRRRRRRRRRRRRRRRR w3

u4

llllllllllllllllllllllllllw4

w5

Definition. A matching of a bipartite graph G is a subset M of

the edges G.E of G such that no two edges share an endpoint.

10.53

CSE 2331

Bipartite Matching

u1

YYYYYYYYYYYYYYYYYYYYYYYYY w1

u2 w2

u3

llllllllllllllllllllllllll

eeeeeeeeeeeeeeeeeeeeeeeee w3

u4

llllllllllllllllllllllllllw4

u5

llllllllllllllllllllllllll

eeeeeeeeeeeeeeeeeeeeeeeee

Definition. A matching of a bipartite graph G is a subset M of

the edges G.E of G such that no two edges share an endpoint.

10.54

CSE 2331

Maximum Bipartite Matching

u1

YYYYYYYYYYYYYYYYYYYYYYYYY w1

u2 w2

u3

llllllllllllllllllllllllll

eeeeeeeeeeeeeeeeeeeeeeeee w3

u4

llllllllllllllllllllllllllw4

u5

llllllllllllllllllllllllll

eeeeeeeeeeeeeeeeeeeeeeeee

Definition. A maximum matching of a bipartite graph G is a

matching with greatest number of edges.

Note: A maximum matching has at most min(|U |, |W |) edges. (Why?)

10.55

CSE 2331

Maximum Bipartite Matching

u1

YYYYYYYYYYYYYYYYYYYYYYYYY w1

u2 w2

u3

llllllllllllllllllllllllll

eeeeeeeeeeeeeeeeeeeeeeeee w3

u4

llllllllllllllllllllllllllw4

u5

llllllllllllllllllllllllll

eeeeeeeeeeeeeeeeeeeeeeeee

Bipartite Matching Problem: Given a bipartite graph G, find a

maximum matching of G.

10.56

CSE 2331

Reduction

Bipartite Matching Problem: Given a bipartite graph G, find a

maximum matching of G.

(Single Source/Sink) Max-Flow Problem: Given a flow network G

with one source and sink, find max |f | over all flows f in G.

Reduce the Bipartite Matching Problem to the (Single Source)

Max Flow Problem.

Reduce P to Q: Turn problem P into Q such that the solution to

Q gives the solution to P .

10.57

CSE 2331

Bipartite Matching Problem

Reduce the Bipartite Matching Problem to the (Single Source)

Max Flow Problem.

Let G be the bipartite graph whose edges connect U ⊂ G.V to

W ⊂ G.V .

Create a flow network G′ from G as follows:

• Add source node s and sink node t;

• Replace each undirected edge (ui, wi) of G.E with a directed

edge (ui, wi);

• Add directed edges from s to each ui ∈ U ;

• Added directed edges from each wi ∈W to t;

• Set the capacity of every edge to 1.

G′ has flow with value F from s to t if and only if G has a

matching of size F .

10.58

CSE 2331

Ford-Fulkerson Max Flow Algorithm: Time

m = # graph edges.

Proposition. If all capacities are integers, then the Ford-Fulkerson

Algorithm runs in O(m|f∗|) time where f∗ is the max flow.

10.59

CSE 2331

Bipartite Matching: Time

m = # graph edges.

Proposition. If all capacities are integers, then the Ford-Fulkerson

Algorithm runs in O(m|f∗|) time where f∗ is the max flow.

In the reduction of bipartite matching to max flow:

• All edge capacities are 1;

• The max flow |f∗| ≤ n.

Proposition. Reducing bipartite matching to max flow and

applying the Ford-Fulkerson Algorithm takes O(nm) time.

10.60