Objecves Review: Maximum Flow Problemsprenkle/cs211/winter16/lectures/29-networkflow.pdf · 4/4/16...

6
4/4/16 1 Objec&ves Network Flow Ø Mo&va&on Ø Max flow Ø Min cut Apr 4, 2016 1 CSCI211 - Sprenkle Review: Maximum Flow Problem Make network most efficient Ø Use most of available capacity Apr 4, 2016 CSCI211 - Sprenkle 2 10 9 9 14 4 10 4 8 9 1 0 0 0 14 capacity flow s 2 3 4 5 6 7 t 15 5 30 15 10 8 15 9 6 10 10 10 15 4 4 0 Value = 28 Goal: Find s-t flow of maximum value Review: Residual Graph: G f Original edge: e = (u, v) E Ø Flow f(e), capacity c(e) Residual edge Ø e = (u, v) w/ capacity c(e) - f(e) Ø e R = (v, u) with capacity f(e) To undo flow Residual graph:G f = (V, E f ) Ø Residual edges with posi(ve residual capacity Ø E f = {e : f(e) < c(e)} {e R : f(e) > 0} Apr 4, 2016 CSCI211 - Sprenkle 3 u v 11 residual capacity 6 residual capacity Forward edges Backward edges u v 17 6 capacity flow Augmen&ng Path Algorithm Apr 4, 2016 CSCI211 - Sprenkle 4 Ford-Fulkerson(G, s, t, c) foreach e E f(e) = 0 # initially no flow G f = residual graph while there exists augmenting path P f = Augment(f, c, P) # change the flow update G f # build a new residual graph return f Augment(f, c, P) b = bottleneck(P) # edge on P with least capacity foreach e P if (e E) f(e) = f(e) + b # forward edge, é flow else f(e R ) = f(e) - b # forward edge, ê flow return f c=capacity Ford-Fulkerson Algorithm Apr 4, 2016 CSCI211 - Sprenkle 5 s 2 3 4 5 t 10 10 9 8 4 10 10 6 2 0 0 0 0 0 0 0 0 G: Flow value = 0 0 flow capacity Ford-Fulkerson Algorithm Apr 4, 2016 CSCI211 - Sprenkle 6 s 2 3 4 5 t 10 10 9 8 4 10 10 6 2 0 0 0 0 0 0 0 0 G: Flow value = 0 0 flow What does the residual graph look like? capacity

Transcript of Objecves Review: Maximum Flow Problemsprenkle/cs211/winter16/lectures/29-networkflow.pdf · 4/4/16...

Page 1: Objecves Review: Maximum Flow Problemsprenkle/cs211/winter16/lectures/29-networkflow.pdf · 4/4/16 1 Objecves • Network Flow Ø Mo&vaon Ø Max flow Ø Min cut Apr 4, 2016 CSCI211

4/4/16

1

Objec&ves• NetworkFlow

Ø Mo&va&onØ MaxflowØ Mincut

Apr4,2016 1CSCI211-Sprenkle

Review:MaximumFlowProblem• Makenetworkmostefficient

Ø Usemostofavailablecapacity

Apr4,2016 CSCI211-Sprenkle 2

10

9

9

14

4 10

4 8 9

1

0 0

0

14

capacityflow

s

2

3

4

5

6

7

t

15

5

30

15

10

8

15

9

6 10

10

10 15 4

4 0

Value = 28

Goal:Finds-tflowofmaximumvalue

Review:ResidualGraph:Gf• Originaledge:e=(u,v)∈E

Ø Flowf(e),capacityc(e)• Residualedge

Ø e=(u,v)w/capacityc(e)-f(e)Ø eR=(v,u)withcapacityf(e)

• Toundoflow• Residualgraph:Gf=(V,Ef)

Ø Residualedgeswithposi(veresidualcapacityØ Ef={e:f(e)<c(e)}∪{eR:f(e)>0}

Apr4,2016 CSCI211-Sprenkle 3

u v 11

residual capacity

6

residual capacity

Forward edges Backward edges

u v 17

6

capacity

flow

Augmen&ngPathAlgorithm

Apr4,2016 CSCI211-Sprenkle 4

Ford-Fulkerson(G, s, t, c) foreach e ∈ E f(e) = 0 # initially no flow Gf = residual graph

while there exists augmenting path P f = Augment(f, c, P) # change the flow update Gf # build a new residual graph

return f

Augment(f, c, P) b = bottleneck(P) # edge on P with least capacity foreach e ∈ P if (e ∈ E) f(e) = f(e) + b # forward edge, é flow else f(eR) = f(e) - b # forward edge, ê flow return f

c=capacity

Ford-FulkersonAlgorithm

Apr4,2016 CSCI211-Sprenkle 5

s

2

3

4

5 t 10

10

9

8

4

10

10 6 2

0

0

0

0 0 0

0

0

G:

Flow value = 0

0

flow

capacity

Ford-FulkersonAlgorithm

Apr4,2016 CSCI211-Sprenkle 6

s

2

3

4

5 t 10

10

9

8

4

10

10 6 2

0

0

0

0 0 0

0

0

G:

Flow value = 0

0

flow

What does the residual graph look like?

capacity

Page 2: Objecves Review: Maximum Flow Problemsprenkle/cs211/winter16/lectures/29-networkflow.pdf · 4/4/16 1 Objecves • Network Flow Ø Mo&vaon Ø Max flow Ø Min cut Apr 4, 2016 CSCI211

4/4/16

2

Ford-FulkersonAlgorithm

Apr4,2016 CSCI211-Sprenkle 7

s

2

3

4

5 t 10

10

9

8

4

10

10 6 2

0

0

0

0 0 0

0

0

G:

Flow value = 0

0

flow

s

2

3

4

5 t

Gf:

capacity

Ford-FulkersonAlgorithm

Apr4,2016 CSCI211-Sprenkle 8

s

2

3

4

5 t 10

10

9

8

4

10

10 6 2

0

0

0

0 0 0

0

0

G:

Flow value = 0

0

flow

s

2

3

4

5 t 10 9

4

10 6 2

Gf:

10 8

10

residual capacity

Bottleneck

capacity

Ford-FulkersonAlgorithm

Apr4,2016 CSCI211-Sprenkle 9

s

2

3

4

5 t 10

10

9

8

4

10

10 6 2

8

0

0

0 0 8

8

0 0

G:

s

2

3

4

5 t 10

4

10 6

Gf:

8

8

8

9

22

2

10

210

X

X

X2X

Flow value = 8

Ford-FulkersonAlgorithm

Apr4,2016 CSCI211-Sprenkle 10

0

s

2

3

4

5 t 10

10

9

8

4

10

10 6 2

10

0

0

0 2 10

8

2

G:

s

2

3

4

5 t

4

2

Gf:

10

810

2

10 7

10 6

X

66

6

X

X

8X

Flow value = 10

Ford-FulkersonAlgorithm

Apr4,2016 CSCI211-Sprenkle 11

s

2

3

4

5 t 10

10

9

8

4

10

10 6 2

10

0

6

6 8 10

8

2

G:

s

2

3

4

5 t1

6

Gf:

10

8 10

8

6

6

6

4

4

4

2

X

8

2

8

X

X

0X

Flow value = 16

Ford-FulkersonAlgorithm

Apr4,2016 CSCI211-Sprenkle 12

s

2

3

4

5 t 10

10

9

8

4

10

10 6 2

10

2

8

8 8 10

8

0

G:

s

2

3

4

5 t

6 2

Gf:

10

10

8

6

8

8

2

2 1

2

8 2

X

9

7 9

X

X

9X

X 3

Flow value = 18

Page 3: Objecves Review: Maximum Flow Problemsprenkle/cs211/winter16/lectures/29-networkflow.pdf · 4/4/16 1 Objecves • Network Flow Ø Mo&vaon Ø Max flow Ø Min cut Apr 4, 2016 CSCI211

4/4/16

3

Ford-FulkersonAlgorithm

Apr4,2016 CSCI211-Sprenkle 13

s

2

3

4

5 t 10

10

9

8

4

10

10 6 2

10

3

9

9 9 10

7

0

G:

s

2

3

4

5 t 1 9

1

1 6 2

Gf:

10

7 10

6

9

9

3

1

Flow value = 19

How do we know we’re done?

Ford-FulkersonAlgorithm

Apr4,2016 CSCI211-Sprenkle 14

s

2

3

4

5 t 10

10

9

8

4

10

10 6 2

10

3

9

9 9 10

7

0

G:

s

2

3

4

5 t 1 9

1

1 6 2

Gf:

10

7 10

6

9

9

3

1

What is reachable from s

Flow value = 19Cut capacity = 19

AnalyzingAugmen&ngPathAlgorithm

Apr4,2016 CSCI211-Sprenkle 15

Ford-Fulkerson(G, s, t, c) foreach e ∈ E f(e) = 0 # initially no flow Gf = residual graph

while there exists augmenting path P f = Augment(f, c, P) # change the flow update Gf # build a new residual graph

return f

Augment(f, c, P) b = bottleneck(P) # edge on P with least capacity foreach e ∈ P if (e ∈ E) f(e) = f(e) + b # forward edge, é flow else f(eR) = f(e) - b # forward edge, ê flow return f

Why does alg work? What is happening at each iteration?What is the running time? Need more analysis …

MINIMUMCUTS

Apr4,2016 CSCI211-Sprenkle 16

Cuts• Ans-tcutisapar&&on(A,B)ofVwiths∈Aandt∈B

• Thecapacityofacut(A,B)is

Apr4,2016 CSCI211-Sprenkle 17

s

2

3

4

5

6

7

t

15

5

30

15

10

8

15

9

6 10

10

10 15 4

4 A

cap( A, B) = c(e)e out of A∑

Capacity = 9 + 15 + 8 + 30� = 62

BWhat is the capacity of this cut?

MinimumCutProblem• Findans-tcutofminimumcapacity

Ø Putsupperboundonmaximumflow

Apr4,2016 CSCI211-Sprenkle 18

s

2

3

4

5

6

7

t

15

5

30

15

10

8

15

9

6 10

10

10 15 4

4

A Capacity = 10 + 8 + 10� = 28

B

Same graph, different cut

Page 4: Objecves Review: Maximum Flow Problemsprenkle/cs211/winter16/lectures/29-networkflow.pdf · 4/4/16 1 Objecves • Network Flow Ø Mo&vaon Ø Max flow Ø Min cut Apr 4, 2016 CSCI211

4/4/16

4

Recall• Thevalueofaflowfisv(f)=∑eoutofsf(e)

Apr4,2016 CSCI211-Sprenkle 19

4

0

0

0

0 0

0 4 4

00

0

Value = 40

capacityflow

0

4

s

2

3

4

5

6

7

t

15

5

30

15

10

8

15

9

6 10

10

10 15 4

4

FlowValueLemma• Letfbeanyflow,andlet(A,B)beanys-tcut.Then,thevalueoftheflowis=fout(A)–fin(A).

Apr4,2016 CSCI211-Sprenkle 20

10

6

6

11

1 10

3 8 8

0

0

0

11

s

2

3

4

5

6

7

t

15

5

30

15

10

8

15

9

6 10

10

10 15 4

4 0

Value = 24

f (e)e out of A∑ − f (e)

e in to A∑ = v( f )

4

A

B

What is the value of this flow?

FlowValueLemma

Apr4,2016 CSCI211-Sprenkle 21

10

6

6

1 10

3 8 8

0

0

0

11

s

2

3

4

5

6

7

t

15

5

30

15

10

8

15

9

6 10

10

10 15 4

4 0

Value = 6 + 0 + 8 - 1 + 11� = 24

4

11

A

B

• Letfbeanyflow,andlet(A,B)beanys-tcut.Then,thevalueoftheflowis=fout(A)–fin(A).

f (e)e out of A∑ − f (e)

e in to A∑ = v( f )

CSCI211-Sprenkle

Possibilities for edge e:•  Both ends in A (0)•  Points out from A (+), Points in to A (-)

FlowValueLemma(FVL)• Letfbeanyflow,andlet(A,B)beanys-tcut.• Then• Pf.

Apr4,201622

by flow conservation, �all terms except v = s are 0

By definition

A B

WeakDuality• Letfbeanyflowandlet(A,B)beanys-tcut.➜ Thenthevalueoftheflowisatmostthecut’scapacity

Apr4,2016 CSCI211-Sprenkle 23

Cut capacity = 30 ⇒ Flow value ≤ 30

s

2

3

4

5

6

7

t

15

5

30

15

10

8

15

9

6 10

10

10 15 4

4

Capacity = 30

A

WeakDuality• Letfbeanyflow.Then,foranys-tcut(A,B)v(f)≤cap(A,B).

• Pf.

Apr4,2016 CSCI211-Sprenkle 24

v( f ) = f (e)e out of A∑ − f (e)

e in to A∑

≤ f (e)e out of A∑

≤ c(e)e out of A∑

= cap(A,B) s

t

A B

7

6

84By FVL

Page 5: Objecves Review: Maximum Flow Problemsprenkle/cs211/winter16/lectures/29-networkflow.pdf · 4/4/16 1 Objecves • Network Flow Ø Mo&vaon Ø Max flow Ø Min cut Apr 4, 2016 CSCI211

4/4/16

5

Cer&ficateofOp&mality• Corollary.Letfbeanyflow,andlet(A,B)beanycut.Ifv(f)=cap(A,B),thenfisamaxflowand(A,B)isamincut.

Apr4,2016 CSCI211-Sprenkle 25

Value of flow = 28�Cut capacity = 28 ⇒�

Flow value ≤ 2810

9

9

14

4 10

4 8 9

1

0 0

0

14

s

2

3

4

5

6

7

t

15

5

30

15

10

8

15

9

6 10

10

10 15 4

4 0A

B

Recall:ResidualGraphGf• Originaledge:e=(u,v)∈E

Ø Flowf(e),capacityc(e)• Residualedge

Ø e=(u,v)w/capacityc(e)-f(e)Ø eR=(v,u)withcapacityf(e)

• Toundoflow• Residualgraph:Gf=(V,Ef)

Ø Residualedgeswithposi(veresidualcapacityØ Ef={e:f(e)<c(e)}∪{eR:f(e)>0}

Apr4,2016 CSCI211-Sprenkle 26

u v 11

residual capacity

6

residual capacity

Forward edges Backward edges

u v 17

6

capacity

flow

Recall:Augmen&ngPathAlgorithm

Apr4,2016 CSCI211-Sprenkle 27

Ford-Fulkerson(G, s, t, c) foreach e ∈ E f(e) = 0 # initially no flow Gf = residual graph

while there exists augmenting path P f = Augment(f, c, P) # change the flow update Gf # build a new residual graph

return f

Augment(f, c, P) b = bottleneck(P) # edge on P with least capacity foreach e ∈ P if (e ∈ E) f(e) = f(e) + b # forward edge, é flow else f(eR) = f(e) - b # forward edge, ê flow return f

Intui&onBehindCorrectnessofF-FAlgorithm

• LetAbesetofver&cesreachablefromsinresidualgraphatendofF-Falgexecu&on

• Bydefini&onofA,s∈A• Bydefini&onoftheF-Falgorithm’sresul&ngflow,t∉A

Apr4,2016 CSCI211-Sprenkle 28

Ford-FulkersonAlgorithm

Apr4,2016 CSCI211-Sprenkle 29

s

2

3

4

5 t 10

10

9

8

4

10

10 6 2

10

3

9

9 9 10

7

0

G:

s

2

3

4

5 t 1 9

1

1 6 2

Gf:

10

7 10

6

9

9

3

1

Flow value = 19Cut capacity = 19

A

• What do we know about the flow out of A?• What do we know about the flow into A?

A: nodes reachable from s

A

Ford-FulkersonAlgorithm

Apr4,2016 CSCI211-Sprenkle 30

s

2

3

4

5 t 10

10

9

8

4

10

10 6 2

10

3

9

9 9 10

7

0

G:

s

2

3

4

5 t 1 9

1

1 6 2

Gf:

10

7 10

6

9

9

3

1

Flow value = 19Cut capacity = 19

• What do we know about the flow out of A?• What do we know about the flow into A?

A

• All edges out of A are completely saturated• All edges into A are completely unused

A

Page 6: Objecves Review: Maximum Flow Problemsprenkle/cs211/winter16/lectures/29-networkflow.pdf · 4/4/16 1 Objecves • Network Flow Ø Mo&vaon Ø Max flow Ø Min cut Apr 4, 2016 CSCI211

4/4/16

6

Max-FlowMin-CutTheorem

• Proofstrategy.Provebothsimultaneouslybyshowingthefollowingareequivalent:(i)Thereexistsacut(A,B)suchthatv(f)=cap(A,B).(ii)Flowfisamaxflow.(iii)Thereisnoaugmen&ngpathrela&vetof. 31Apr4,2016 CSCI211-Sprenkle

Max-flow min-cut theorem. [Ford-Fulkerson 1956] �The value of the max flow is equal to the value of the min cut.

Augmenting path theorem. �Flow f is a max flow iff there are no augmenting paths.

See formal proof in book

AnalyzingAugmen&ngPathAlgorithm

Apr4,2016 CSCI211-Sprenkle 32

Ford-Fulkerson(G, s, t, c) foreach e ∈ E f(e) = 0 # initially no flow Gf = residual graph

while there exists augmenting path P f = Augment(f, c, P) # change the flow update Gf # build a new residual graph

return f

Augment(f, c, P) b = bottleneck(P) # edge on P with least capacity foreach e ∈ P if (e ∈ E) f(e) = f(e) + b # forward edge, é flow else f(eR) = f(e) - b # forward edge, ê flow return f

Costs?

AnalyzingAugmen&ngPathAlgorithm

Apr4,2016 CSCI211-Sprenkle 33

Ford-Fulkerson(G, s, t, c) foreach e ∈ E f(e) = 0 # initially no flow Gf = residual graph

while there exists augmenting path P f = Augment(f, c, P) # change the flow update Gf # build a new residual graph

return f

Augment(f, c, P) b = bottleneck(P) # edge on P with least capacity foreach e ∈ P if (e ∈ E) f(e) = f(e) + b # forward edge, é flow else f(eR) = f(e) - b # forward edge, ê flow return f

O(m)

O(m)

O(m)

O(m)

O(n)O(n)

O(1)O(1)

Total:O(n)àO(m),sincen≤2m

Total:O(Fm)

Find path: O(m); Iterations: O(F) iterations, where F = max flow

RunningTime•  Assump&on.Allcapaci&esareintegersbetween1andF.•  Invariant.Everyflowvaluef(e)andeveryresidualcapacity’s

cf(e)remainsanintegerthroughoutalgorithm.

•  Theorem.Algorithmterminatesinatmostv(f*)≤nFitera&ons.•  Pf.Eachaugmenta&onincreasesvaluebyatleast1.•  Corollary.IfF=1,Ford-FulkersonrunsinO(mn)&me.

•  Integralitytheorem.Ifallcapaci&esareintegers,thenthereexistsamaxflowfforwhicheveryflowvaluef(e)isaninteger.

•  Pf.Sincealgorithmterminates,theoremfollowsfrominvariant.

34Apr4,2016 CSCI211-Sprenkle

LookingAhead

• Wiki:Duetonight(7.1-7.2,7.5,7.7)Ø 7.5won’tbediscussedinclass

• ProblemSet9dueFriday

Apr4,2016 CSCI211-Sprenkle 35