max-flow (ford-fulkerson) - ecology...

18
Max-Flow (Ford-Fulkerson) Emil Thomas 04/25/2019 https://www.cs.princeton.edu/~wayne/kleinberg-tardos/pearson/07MaximumFlow.pdf Slides From

Upload: others

Post on 25-Jun-2020

16 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Max-Flow (Ford-Fulkerson) - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/labs/...Ford–Fulkerson algorithm Ford–Fulkerson augmenting path algorithm. rì Start with

Max-Flow (Ford-Fulkerson)

Emil Thomas04/25/2019

https://www.cs.princeton.edu/~wayne/kleinberg-tardos/pearson/07MaximumFlow.pdfSlides From

Page 2: Max-Flow (Ford-Fulkerson) - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/labs/...Ford–Fulkerson algorithm Ford–Fulkerson augmenting path algorithm. rì Start with

Flow network

A flow network is a tuple G = (V, E, s, t, c).Digraph (V, E) with source s ∈ V and sink t ∈ V.

Capacity c(e) > 0 for each e ∈ E.

Intuition. Material flowing through a transportation network;material originates at source and is sent to sink.

3

s t5

15

1015

16

9

15

6

8 10

154

4 10

10

capacity

assume all nodes are reachable from s

Page 3: Max-Flow (Ford-Fulkerson) - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/labs/...Ford–Fulkerson algorithm Ford–Fulkerson augmenting path algorithm. rì Start with

Maximum-flow problem

Def. An st-flow (flow) f is a function that satisfies:

For each e ∈ E : [capacity]

For each v ∈ V – {s, t} : [flow conservation]

8

0 / 4

0 / 4 0 / 15

10 / 10

10 / 105 / 5 vs t

0 / 6

5 / 10

5 / 9

5 / 8

5 / 15

10 / 1010 / 15

10 / 16

inflow at v = 5 + 5 + 0 = 10outflow at v = 10 + 0 = 10

flow capacity

0 / 15

0 � f(e) � c(e)�

e v

f(e) =�

e v

f(e)

Page 4: Max-Flow (Ford-Fulkerson) - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/labs/...Ford–Fulkerson algorithm Ford–Fulkerson augmenting path algorithm. rì Start with

Maximum-flow problem

Def. An st-flow (flow) f is a function that satisfies:

For each e ∈ E : [capacity]

For each v ∈ V – {s, t} : [flow conservation]

Def. The value of a flow f is:

9

0 / 4

10 / 10

10 / 105 / 5s t

5 / 10

5 / 9

5 / 8

5 / 15

10 / 1010 / 15

0 / 15

value = 5 + 10 + 10 = 25

0 / 4

0 / 6

10 / 16

0 / 15

0 � f(e) � c(e)

val(f) =�

e s

f(e) ��

e s

f(e)

e v

f(e) =�

e v

f(e)

Page 5: Max-Flow (Ford-Fulkerson) - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/labs/...Ford–Fulkerson algorithm Ford–Fulkerson augmenting path algorithm. rì Start with

Maximum-flow problem

Def. An st-flow (flow) f is a function that satisfies:

For each e ∈ E : [capacity]

For each v ∈ V – {s, t} : [flow conservation]

Def. The value of a flow f is:

Max-flow problem. Find a flow of maximum value.

10

0 / 4

10 / 10

10 / 105 / 5s

8 / 10

8 / 9

8 / 8

10 / 1013 / 15

0 / 15

value = 10 + 5 + 13 = 28

0 / 4

3 / 6

13 / 16

0 / 15

t

2 / 15

0 � f(e) � c(e)

val(f) =�

e s

f(e) ��

e s

f(e)

e v

f(e) =�

e v

f(e)

Page 6: Max-Flow (Ford-Fulkerson) - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/labs/...Ford–Fulkerson algorithm Ford–Fulkerson augmenting path algorithm. rì Start with

Toward a max-flow algorithm

Greedy algorithm.

Start with f (e) = 0 for each edge e ∈ E.

Find an s↝t path P where each edge has f (e) < c(e). Augment flow along path P.

Repeat until you get stuck.

12

s t

0 / 20 /

10 0 / 6

0 / 10

0 / 4

0 / 8

0 / 9

flow network G and flow f

0 / 10 0

value of flow

0 / 10

flow capacity

Page 7: Max-Flow (Ford-Fulkerson) - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/labs/...Ford–Fulkerson algorithm Ford–Fulkerson augmenting path algorithm. rì Start with

Toward a max-flow algorithm

Greedy algorithm.

Start with f (e) = 0 for each edge e ∈ E.

Find an s↝t path P where each edge has f (e) < c(e). Augment flow along path P.

Repeat until you get stuck.

13

0 / 20 /

10 0 / 6

0 / 10

0 / 4

0 / 8

0 / 90 / 10 0

0 / 10

flow network G and flow f

s t

Page 8: Max-Flow (Ford-Fulkerson) - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/labs/...Ford–Fulkerson algorithm Ford–Fulkerson augmenting path algorithm. rì Start with

Toward a max-flow algorithm

Greedy algorithm.

Start with f (e) = 0 for each edge e ∈ E.

Find an s↝t path P where each edge has f (e) < c(e). Augment flow along path P.

Repeat until you get stuck.

14

0 / 20 /

10 0 / 6

0 / 10

0 / 4

0 / 8

0 / 90 / 10 0

0 / 10

+ 8 = 8—

8—

—8

8

flow network G and flow f

s t

Page 9: Max-Flow (Ford-Fulkerson) - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/labs/...Ford–Fulkerson algorithm Ford–Fulkerson augmenting path algorithm. rì Start with

+ 2 = 10

Toward a max-flow algorithm

Greedy algorithm.

Start with f (e) = 0 for each edge e ∈ E.

Find an s↝t path P where each edge has f (e) < c(e). Augment flow along path P.

Repeat until you get stuck.

15

0 / 6

0 / 4

8 / 8

0 / 10 8

0 / 100 / 2

8 / 10

8 / 100 / 9

—10 2 —

2—

10—

flow network G and flow f

s t

Page 10: Max-Flow (Ford-Fulkerson) - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/labs/...Ford–Fulkerson algorithm Ford–Fulkerson augmenting path algorithm. rì Start with

Toward a max-flow algorithm

Greedy algorithm.

Start with f (e) = 0 for each edge e ∈ E.

Find an s↝t path P where each edge has f (e) < c(e). Augment flow along path P.

Repeat until you get stuck.

16

0 / 4

8 / 8

10

2 / 210 /

10

10 / 10

0 / 6

0 / 10

0 / 10

2 / 9

6 —

8—

6— + 6 = 16

6—

flow network G and flow f

s t

Page 11: Max-Flow (Ford-Fulkerson) - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/labs/...Ford–Fulkerson algorithm Ford–Fulkerson augmenting path algorithm. rì Start with

Toward a max-flow algorithm

Greedy algorithm.

Start with f (e) = 0 for each edge e ∈ E.

Find an s↝t path P where each edge has f (e) < c(e). Augment flow along path P.

Repeat until you get stuck.

17

0 / 4

8 / 8

16

2 / 210 /

10

10 / 10

6 / 6

6 / 10

6 / 10

8 / 9

ending flow value = 16

flow network G and flow f

s t

Page 12: Max-Flow (Ford-Fulkerson) - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/labs/...Ford–Fulkerson algorithm Ford–Fulkerson augmenting path algorithm. rì Start with

Toward a max-flow algorithm

Greedy algorithm.

Start with f (e) = 0 for each edge e ∈ E.

Find an s↝t path P where each edge has f (e) < c(e). Augment flow along path P.

Repeat until you get stuck.

18

3 / 4

7 / 8

19

0 / 210 /

10

10 / 10

6 / 6

9 / 10

9 / 10

9 / 9

but max-flow value = 19

flow network G and flow f

s t

Page 13: Max-Flow (Ford-Fulkerson) - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/labs/...Ford–Fulkerson algorithm Ford–Fulkerson augmenting path algorithm. rì Start with

Q. Why does the greedy algorithm fail?

A. Once greedy algorithm increases flow on an edge, it never decreases it.

Ex. Consider flow network G .

The unique max flow has f *(v, w) = 0.

Greedy algorithm could choose s→v→w→t as first augmenting path.

Bottom line. Need some mechanism to “undo” a bad decision.

Why the greedy algorithm fails

19

s

t

w

v

1

2

2

22

flow network G

Page 14: Max-Flow (Ford-Fulkerson) - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/labs/...Ford–Fulkerson algorithm Ford–Fulkerson augmenting path algorithm. rì Start with

Residual network

Original edge. e = (u, v) ∈ E.

Flow f (e). Capacity c(e).

Reverse edge. ereverse = (v, u).

“Undo” flow sent.

Residual capacity.

Residual network. Gf = (V, Ef , s, t, cf ).

Ef = {e : f (e) < c(e)} ∪ {ereverse : f (e) > 0}.

Key property: f ʹ is a flow in Gf iff f + f ʹ is a flow in G.

20

u v

u v

residual capacity

flow

6 / 17

capacity

original flow network G

residual network Gf

11

6

where flow on a reverse edge negates flow on

corresponding forward edge

cf (e) =

�c(e) � f(e) e � E

f(e) e � Ereverse edge

edges with positive residual capacity

Page 15: Max-Flow (Ford-Fulkerson) - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/labs/...Ford–Fulkerson algorithm Ford–Fulkerson augmenting path algorithm. rì Start with

Augmenting path

Def. An augmenting path is a simple s↝t path in the residual network Gf . Def. The bottleneck capacity of an augmenting path P is the minimum residual capacity of any edge in P.

Key property. Let f be a flow and let P be an augmenting path in Gf . Then, after calling f ʹ ← AUGMENT( f, c, P), the resulting f ʹ is a flow and val( f ʹ) = val( f ) + bottleneck(Gf, P).

21

AUGMENT( f, c, P) ________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

δ ← bottleneck capacity of augmenting path P.

FOREACH edge e ∈ P :

IF (e ∈ E) f (e) ← f (e) + δ.

ELSE f (ereverse) ← f (ereverse) – δ.

RETURN f.________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Page 16: Max-Flow (Ford-Fulkerson) - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/labs/...Ford–Fulkerson algorithm Ford–Fulkerson augmenting path algorithm. rì Start with

Short Exercise & Discussion of solution in Lab

Page 17: Max-Flow (Ford-Fulkerson) - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/labs/...Ford–Fulkerson algorithm Ford–Fulkerson augmenting path algorithm. rì Start with

Ford–Fulkerson algorithm

Ford–Fulkerson augmenting path algorithm.

Start with f (e) = 0 for each edge e ∈ E.

Find an s↝t path P in the residual network Gf .

Augment flow along path P.

Repeat until you get stuck.

23

FORD–FULKERSON(G) _________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

FOREACH edge e ∈ E : f (e) ← 0.

Gf ← residual network of G with respect to flow f.

WHILE (there exists an s↝t path P in Gf )

f ← AUGMENT( f, c, P).

Update Gf.

RETURN f.

augmenting path

Page 18: Max-Flow (Ford-Fulkerson) - ecology labfaculty.cse.tamu.edu/slupoli/notes/DataStructures/labs/...Ford–Fulkerson algorithm Ford–Fulkerson augmenting path algorithm. rì Start with

Exercise Handout and Survey in Ecampus

• Survey in ecampus->Lab1 makefile