algorithms - princeton university computer science · max flow / min cut problem 8 s t 4 / 10 3 /...

79
ROBERT SEDGEWICK | KEVIN WAYNE FOURTH EDITION Algorithms http://algs4.cs.princeton.edu Algorithms R OBERT S EDGEWICK | K EVIN WAYNE 6.4 M AXIMUM F LOW introduction Ford-Fulkerson algorithm maxflow-mincut theorem running time analysis Java implementation applications

Upload: others

Post on 06-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

ROBERT SEDGEWICK | KEVIN WAYNE

F O U R T H E D I T I O N

Algorithms

http://algs4.cs.princeton.edu

Algorithms ROBERT SEDGEWICK | KEVIN WAYNE

6.4 MAXIMUM FLOW

‣ introduction

‣ Ford-Fulkerson algorithm

‣ maxflow-mincut theorem

‣ running time analysis

‣ Java implementation

‣ applications

Page 2: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

http://algs4.cs.princeton.edu

ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

‣ introduction

‣ Ford-Fulkerson algorithm

‣ Java implementation

‣ maxflow-mincut theorem

‣ running time analysis

‣ applications

6.4 MAXIMUM FLOW

Page 3: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Input. An edge-weighted digraph, source vertex s, and target vertex t.

Mincut problem

3

5s t

15

1015

16

9

15

6

8 10

154

4 10

10

each edge has a

positive capacity

capacity

Page 4: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Max flow / min cut problem

4

s t

0 / 50 /

100 / 15

0 / 10

0 / 1

0 / 3

0 / 120 / 10 0

value of flow

0 / 10

flow capacity

Page 5: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Max flow / min cut problem

5

s t

1 / 52 /

101 / 15

2 / 10

1 / 1

0 / 3

1 / 120 / 10 2

value of flow

0 / 10

flow capacity

Page 6: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Max flow / min cut problem

6

s t

/ 5 /

10 / 15

/ 10

/ 1

/ 3

/ 12 / 10 ?

value of flow

/ 9

flow capacity

Q: What is the value of the max flow?

A. 20 [191071] D. 11 [170148]B. 19 [175058] E. 10 [170215]C. 16 [170059]

Extra: Find a cut whose total capacity equals the max flow.

pollEv.com/jhug text to 37607

Page 7: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Max flow / min cut problem

7

s t

3 / 5 4

/ 10

0 / 15

10 / 10

1 / 1

0 / 3

10 / 127 / 10 11

value of flow

1 / 9

flow capacity

Q: What is the value of the max flow?

D. 11

Extra: Find a cut whose total capacity equals the max flow.

pollEv.com/jhug text to 37607

Page 8: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Max flow / min cut problem

8

s t

3 / 5 4

/ 10

0 / 15

10 / 10

1 / 1

0 / 3

10 / 127 / 10 11

value of flow

1 / 9

cut capacity = 1 + 10 = 11

Non Obvious Fact. There is always a cut such that:

・Set A contains the source (s).

・Set B contains the sink (t).

・The capacity of this cut is equal to the value of the max flow.

・All edges from A to B are full.

・All edges from B to A are empty.

Page 9: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Def. A st-cut (cut) is a partition of the vertices into two disjoint sets,

with s in one set A and t in the other set B.

Def. Its capacity is the sum of the capacities of the edges from A to B.

Mincut problem

9

5s

15

10

t

capacity = 10 + 5 + 15 = 30

Page 10: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Def. A st-cut (cut) is a partition of the vertices into two disjoint sets,

with s in one set A and t in the other set B.

Def. Its capacity is the sum of the capacities of the edges from A to B.

10

Mincut problem

10

8

don't count edges

from B to A

t

16capacity = 10 + 8 + 16 = 34

s

Page 11: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Def. A st-cut (cut) is a partition of the vertices into two disjoint sets,

with s in one set A and t in the other set B.

Def. Its capacity is the sum of the capacities of the edges from A to B.

Minimum st-cut (mincut) problem. Find a cut of minimum capacity.

10

Mincut problem

11

s

10

t

capacity = 10 + 8 + 10 = 28

8

Page 12: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Input. An edge-weighted digraph, source vertex s, and target vertex t.

Maxflow problem

12

4

4 15

10

105s t

6

10

9

8

15

1015

16

15

capacity

each edge has a

positive capacity

Page 13: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Def. An st-flow (flow) is an assignment of values to the edges such that:

・Capacity constraint: 0 ≤ edge's flow ≤ edge's capacity.

・Local equilibrium: inflow = outflow at every vertex (except s and t).

13

Maxflow problem

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 = 10

outflow at v = 10 + 0 = 10

flow capacity

0 / 15

Page 14: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

14

Maxflow problem

Def. An st-flow (flow) is an assignment of values to the edges such that:

・Capacity constraint: 0 ≤ edge's flow ≤ edge's capacity.

・Local equilibrium: inflow = outflow at every vertex (except s and t).

Def. The value of a flow is the inflow at t.

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

we assume no edges point to s or from t

Page 15: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

15

Maxflow problem

Def. An st-flow (flow) is an assignment of values to the edges such that:

・Capacity constraint: 0 ≤ edge's flow ≤ edge's capacity.

・Local equilibrium: inflow = outflow at every vertex (except s and t).

Def. The value of a flow is the inflow at t.

Maximum st-flow (maxflow) problem. Find a flow of maximum value.

0 / 4

10 / 10

10 / 105 / 5s

8 / 10

8 / 9

8 / 8

10 / 1013 / 15

0 / 15

value = 8 + 10 + 10 = 28

0 / 4

3 / 6

13 / 16

0 / 15

t

2 / 15

Page 16: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Input. A weighted digraph, source vertex s, and target vertex t. Mincut problem. Find a cut of minimum capacity.

Maxflow problem. Find a flow of maximum value.

Remarkable fact. These two problems are dual!

Summary

16

s

value of flow = 28

t

0 / 4

10 / 10

10 / 105 / 5

8 / 10

8 / 9

8 / 8

2 / 15

10 / 10

13 / 15

0 / 4

3 / 6

13 / 16

0 / 15

0 / 15

s

capacity of cut = 28

10

8

10

t

Page 17: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

http://algs4.cs.princeton.edu

ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

‣ introduction

‣ Ford-Fulkerson algorithm

‣ Java implementation

‣ maxflow-mincut theorem

‣ running time analysis

‣ applications

6.4 MAXIMUM FLOW

Page 18: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Initialization. Start with 0 flow.

Ford-Fulkerson algorithm

18

s t

0 / 4

0 / 40 / 15

0 / 10 0 / 15

0 / 15

0 / 10

0 / 10

0 / 9

0 / 15

0 / 8

0 / 6

0 / 16

initialization

0 / 5 0

value of flow

0 / 10

flow capacity

Page 19: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Augmenting path. Find an undirected path from s to t such that:

・Can increase flow on forward edges (not full).

・Can decrease flow on backward edge (not empty).

Idea: increase flow along augmenting paths

19

0 / 4

0 / 40 / 15

0 / 15

0 / 15 0 / 10

0 / 9

0 / 8

0 / 6

0 / 16

s t

0 / 10 0 / 15

0 / 10

0 / 10 0 / 15

0 / 10

10

10

10

—0 / 5

1st augmenting path

0 + 10 = 10

0 / 10

bottleneck capacity = 10

Page 20: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Augmenting path. Find an undirected path from s to t such that:

・Can increase flow on forward edges (not full).

・Can decrease flow on backward edge (not empty).

Idea: increase flow along augmenting paths

20

0 / 5

0 / 4

0 / 4

0 / 15

0 / 15

0 / 10

0 / 9

0 / 8

0 / 6

10 / 10 10 / 15

10 / 10s t

0 / 15 0 / 10

0 / 16

0 / 15 0 / 10

0 / 16

10

10

10

2nd augmenting path

10 + 10 = 20

Page 21: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Augmenting path. Find an undirected path from s to t such that:

・Can increase flow on forward edges (not full).

・Can decrease flow on backward edge (not empty).

Idea: increase flow along augmenting paths

21

0 / 4

0 / 4 0 / 150 / 6

10 / 10

10 / 10

10 / 15 10 / 10

10 / 16

s t0 / 5

0 / 10

0 / 9

0 / 8

10 / 15

0 / 5

0 / 10

0 / 9

0 / 8

10 / 15

3rd augmenting path

20 + 5 = 255

5

5—

5—

5—

0 / 15

backward edge

(not empty)

Page 22: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Augmenting path. Find an undirected path from s to t such that:

・Can increase flow on forward edges (not full).

・Can decrease flow on backward edge (not empty).

Idea: increase flow along augmenting paths

22

0 / 4

0 / 4 0 / 15

10 / 10

10 / 105 / 5s t

0 / 6

5 / 10

5 / 9

5 / 8

0 / 6

5 / 10

5 / 9

5 / 8

5 / 155 / 15

10 / 1010 / 15

10 / 15

10 / 1610 / 16

4th augmenting path

25 + 3 = 288

2—

8—

8—

13—

13

3—

0 / 15

backward edge

(not empty)

Page 23: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Termination. All paths from s to t are blocked by either a

・Full forward edge.

・Empty backward edge.

Idea: increase flow along augmenting paths

23

no more augmenting paths

0 / 4

0 / 4 0 / 15

10 / 10

10 / 105 / 5s t

3 / 6

8 / 10

8 / 9

8 / 8

2 / 15

10 / 1

013 / 15

28

13 / 16

0 / 15

full forward edge

empty backward edge

Page 24: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Ford-Fulkerson algorithm

Questions.

・How to find an augmenting path?

・If FF terminates, does it always compute a maxflow?

・How to compute a mincut?

・Does FF always terminate? If so, after how many augmentations?

24

Start with 0 flow.While there exists an augmenting path: - find an augmenting path - compute bottleneck capacity - increase flow on that path by bottleneck capacity

Ford-Fulkerson algorithm

Page 25: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

http://algs4.cs.princeton.edu

ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

‣ introduction

‣ Ford-Fulkerson algorithm

‣ Java implementation

‣ maxflow-mincut theorem

‣ running time analysis

‣ applications

6.4 MAXIMUM FLOW

Page 26: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Ford-Fulkerson algorithm

Questions.

・How to find an augmenting path?

・If FF terminates, does it always compute a maxflow?

・How to compute a mincut?

・Does FF always terminate? If so, after how many augmentations?

26

Start with 0 flow.While there exists an augmenting path: - find an augmenting path - compute bottleneck capacity - increase flow on that path by bottleneck capacity

Ford-Fulkerson algorithm

Page 27: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Flow network: Java implementation

27

public class FlowNetwork{ private final int V; private Bag<FlowEdge>[] adj; public FlowNetwork(int V) { this.V = V; adj = (Bag<FlowEdge>[]) new Bag[V]; for (int v = 0; v < V; v++) adj[v] = new Bag<FlowEdge>(); }

public void addEdge(FlowEdge e) { int v = e.from(); int w = e.to(); adj[v].add(e); adj[w].add(e); }

public Iterable<FlowEdge> adj(int v) { return adj[v]; }}

same as EdgeWeightedGraph,

but adjacency lists of

FlowEdges instead of Edges

add forward edge

add backward edge

Page 28: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Flow network: Java implementation

28

Ford-Fulkerson inspired details

・Both forward and backward edges are provided.

t4 / 94 / 7 4 / 4s

0

1

2

3

0 1 2 3

0 1 4.0 7.0

0 1 4.0 7.0 1 2 4.0 4.0

2 3 4.0 9.0 1 2 4.0 4.0

2 3 4.0 9.0

Page 29: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Flow network: Java implementation

29

Ford-Fulkerson inspired details

・Both forward and backward edges are provided.

・Edges can report their residual capacity.

t4 / 94 / 7 4 / 4s

0 1 2 3

0 1 4.0 7.0

0 1 4.0 7.0 1 2 4.0 4.0

2 3 4.0 9.0 1 2 4.0 4.0

2 3 4.0 9.0

e.edgeFrom(): 0 e.edgeTo(): 1

e.residualCapacityTo(1): 3

e.residualCapacityTo(0): 4

Forward edge, residual capacity = capacity - flow

Backward edge, residual capacity = flow

0

1

2

3

Page 30: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Flow network: Java implementation

30

Residual Network

・Edge weighted digraph representing how much spare (used) capacity is

available on a forward (backward) edge. If none, no edge.

・Represented IMPLICITLY by e.residualCapacityTo().

t4 / 94 / 7 4 / 4s

0 1 2 3

0 1 4.0 7.0

0 1 4.0 7.0 1 2 4.0 4.0

2 3 4.0 9.0 1 2 4.0 4.0

2 3 4.0 9.0

3

4

s

0 15

4

t

2 3

4

0

1

2

3

Page 31: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

0 / 1

Residual Networks - Questions to ponder

Draw the residual network corresponding to the graph below.

What is the result of the code below?

How can you find an augmenting path using the residual network graph?

31

t2 / 22 / 5

7 / 8

9 / 9

2 / 4

original network

s

7 / 7

2 / 10

2 / 2

for (FlowEdge e : G.adj(s)) { int v = e.from(); int w = e.to(); System.out.println(e.residualCapacityTo(w));}

Page 32: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Residual Networks

Draw the residual network corresponding to the graph below.

32

s t23

7

9

2

8

residual network

2

7 22 2

t2 / 22 / 5

7 / 8

9 / 9

2 / 4

original network

s

7 / 7

2 / 10

2 / 2

0 / 1

1

1

Page 33: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Residual Network

・The two FlowEdges adjacent to e have residual capacity of 0 and 3 when

examined in the forward direction.

– Prints 0 on a new line, and 3 on a new line.33

for (FlowEdge e : G.adj(s)) { int v = e.from(); int w = e.to(); System.out.println(e.residualCapacityTo(w));}

s t23

7

9

2

8

residual network

2

7 22 2 1

1

What is the result of the code below (s is the source vertex)?

Page 34: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Residual Networks

Draw the residual network corresponding to the graph below.

34

How can you find an augmenting path using the residual network graph?

・Find any path from s to t. Edge only exists if weight > 0.

s t23

7

9

2

8

residual network

2

7 22 2

t2 / 22 / 5

7 / 8

9 / 9

2 / 4

original network

s

7 / 7

2 / 10

2 / 2

0 / 1

1

1

Page 35: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

35

Finding a shortest augmenting path (cf. breadth-first search)

private boolean hasAugmentingPath(FlowNetwork G, int s, int t){

edgeTo = new FlowEdge[G.V()];marked = new boolean[G.V()];Queue<Integer> queue = new Queue<Integer>();queue.enqueue(s);while (!queue.isEmpty()) {

int v = queue.dequeue();for (FlowEdge e : G.adj(v)) { int w = e.other(v);

if (!marked[w] && e.residualCapacityTo(w) > 0) { marked[w] = true; queue.enqueue(w); edgeTo[w] = e;

}}//how do we know if a path exists to t?return marked[t];

} }

Page 36: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

36

Ford-Fulkerson: Java implementation

public class FordFulkerson{ private boolean[] marked; // true if s->v path in residual network private FlowEdge[] edgeTo; // last edge on s->v path private double value; // value of flow

public FordFulkerson(FlowNetwork G, int s, int t) { value = 0.0; while (hasAugmentingPath(G, s, t)) { double bottle = Double.POSITIVE_INFINITY; for (int v = t; v != s; v = edgeTo[v].other(v)) bottle = Math.min(bottle, edgeTo[v].residualCapacityTo(v));

for (int v = t; v != s; v = edgeTo[v].other(v)) edgeTo[v].addResidualFlowTo(v, bottle);

value += bottle; } } ...

walk backwards from t

and compute

bottleneck capacity

walk backwards from

t and augment flow

creates edgeTo[]

s t23

7

9

2

8

2

7 22 2 1

1

Page 37: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Residual Networks

Any path in residual network is an augmenting path in original network

37

s t23

7

9

2

8

residual network

2

7 22 2

t2 / 22 / 5

7 / 8

9 / 9

2 / 4

original network

s

7 / 7

2 / 10

2 / 2

0 / 1

1

1

Page 38: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

http://algs4.cs.princeton.edu

ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

‣ introduction

‣ Ford-Fulkerson algorithm

‣ Java implementation

‣ maxflow-mincut theorem

‣ running time analysis

‣ applications

6.4 MAXIMUM FLOW

Page 39: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Ford-Fulkerson algorithm

Questions.

・How to find an augmenting path? BFS (or other).

・If FF terminates, does it always compute a maxflow?

・How to compute a mincut?

・Does FF always terminate? If so, after how many augmentations?

39

Start with 0 flow.While there exists an augmenting path: - find an augmenting path - compute bottleneck capacity - increase flow on that path by bottleneck capacity

Ford-Fulkerson algorithm

Page 40: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Maxflow-mincut theorem

Augmenting path theorem. A flow f is a maxflow iff no augmenting paths.

Maxflow-mincut theorem. Value of the maxflow = capacity of mincut.

Pf. The following three conditions are equivalent for any flow f : i. There exists an st-cut cut whose capacity equals the value of the flow f. ii. f is a maxflow.

iii. There is no augmenting path with respect to f.

Overall Goal:

・Prove that i ⇒ ii. [Trivial]

・Prove that ii ⇒ iii. [Trivial]

・Prove that iii ⇒ i. [A little work]

40

Page 41: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Maxflow-mincut theorem

Augmenting path theorem. A flow f is a maxflow iff no augmenting paths.

Maxflow-mincut theorem. Value of the maxflow = capacity of mincut.

Pf. The following three conditions are equivalent for any flow f : i. There exists an st-cut whose capacity equals the value of the flow f. ii. f is a maxflow.

iii. There is no augmenting path with respect to f.

[ i ⇒ ii ]: Trivial by analogy with water flow [see book for technical proof].

41

2 / 2

2 / 2

2 / 2

2 / 22 / 2

0 / ?

Bucket can fill at no

faster than 10

gallons per second.

Page 42: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Maxflow-mincut theorem

Augmenting path theorem. A flow f is a maxflow iff no augmenting paths.

Maxflow-mincut theorem. Value of the maxflow = capacity of mincut.

Pf. The following three conditions are equivalent for any flow f : i. There exists an st-cut cut whose capacity equals the value of the flow f. ii. f is a maxflow.

iii. There is no augmenting path with respect to f.

[ ii ⇒ iii ] Trivial, we prove contrapositive: ~iii ⇒ ~ii.

・Suppose that there is an augmenting path with respect to f.

・Can improve flow f by sending flow along this path.

・Thus, f is not a maxflow.

42

Page 43: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Find an augmenting path.

Computing a mincut from a maxflow

43

0 / 4

0 / 15

10 / 10

10 / 105 / 5 t

8 / 10

8 / 9

8 / 8

2 / 15

10 / 10

16 / 16

0 / 15

ss

13 / 15

13 / 156 / 66 / 6

3 / 43 / 4

Page 44: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Find an augmenting path.

・Couldn’t find an augmenting path (some edges block us).

– These edges form a cut.

– There is no backward flow from t to s.

– All edges from s to t are full.

Computing a mincut from a maxflow

44

・We’ve found a cut whose capacity equals the value of the flow.

0 / 4

0 / 15

10 / 10

10 / 105 / 5 t

8 / 10

8 / 9

8 / 8

2 / 15

10 / 10

16 / 16

0 / 15

ss

13 / 15

13 / 156 / 66 / 6

3 / 43 / 4

Page 45: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Maxflow-mincut theorem

45

Augmenting path theorem. A flow f is a maxflow iff no augmenting paths.

Maxflow-mincut theorem. Value of the maxflow = capacity of mincut.

Pf. The following three conditions are equivalent for any flow f : i. There exists a cut whose capacity equals the value of the flow f. ii. f is a maxflow.

iii. There is no augmenting path with respect to f.

Overall Goal:

・Prove that i ⇒ ii. [Analogy with water, see book for technical proof]

・Prove that ii ⇒ iii. [Trivial by proving contrapositive]

・Prove that iii ⇒ i. [Constructive proof, see book for technical proof]

Page 46: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Ford-Fulkerson algorithm

Questions.

・How to find an augmenting path? BFS (or other).

・If FF terminates, does it always compute a maxflow?

– Yes, because non-existence of augmenting path implies max flow.

– iii ⇒ i ⇒ ii

・How to compute a mincut?

・Does FF always terminate? If so, after how many augmentations?

46

Start with 0 flow.While there exists an augmenting path: - find an augmenting path - compute bottleneck capacity - increase flow on that path by bottleneck capacity

Ford-Fulkerson algorithm

Page 47: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Find an augmenting path.

・Couldn’t find an augmenting path (some edges block us).

– These edges form a cut.

– There is no backward flow from t to s.

– All edges from s to t are full.

・We’ve found a cut whose capacity equals the value of the flow.

Computing a mincut from a maxflow

47

0 / 4

0 / 15

10 / 10

10 / 105 / 5 t

8 / 10

8 / 9

8 / 8

2 / 15

10 / 10

16 / 16

0 / 15

ss

13 / 15

13 / 156 / 66 / 6

3 / 43 / 4

Page 48: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

http://algs4.cs.princeton.edu

ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

‣ introduction

‣ Ford-Fulkerson algorithm

‣ Java implementation

‣ maxflow-mincut theorem

‣ running time analysis

‣ applications

6.4 MAXIMUM FLOW

Page 49: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Ford-Fulkerson algorithm

Questions.

・How to find an augmenting path? BFS (or other).

・If FF terminates, does it always compute a maxflow? Yes. ✔

・How to compute a mincut? Easy. ✔

・Does FF always terminate? If so, after how many augmentations?

49

Start with 0 flow.While there exists an augmenting path: - find an augmenting path - compute bottleneck capacity - increase flow on that path by bottleneck capacity

Ford-Fulkerson algorithm

yes, provided edge capacities are integers

(or augmenting paths are chosen carefully)

requires clever analysis

Page 50: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Ford-Fulkerson algorithm with integer capacities

Important special case. Edge capacities are integers between 1 and U.

Invariant. The flow is integer-valued throughout Ford-Fulkerson.

Pf. [by induction]

・Bottleneck capacity is an integer.

・Flow on an edge increases/decreases by bottleneck capacity.

Proposition. Number of augmentations ≤ the value of the maxflow.

Pf. Each augmentation increases the value by at least 1.

Integrality theorem. There exists an integer-valued maxflow.

Pf. Ford-Fulkerson terminates and maxflow that it finds is integer-valued.

50

flow on each edge is an integer

and FF finds one!important for some applications (stay tuned)

Page 51: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Bad news. Even when edge capacities are integers, number of augmenting

paths could be equal to the value of the maxflow.

Bad case for Ford-Fulkerson

51

t

s

0

1

00

0capacity

flow

100

100

100

100

0

initialize with 0 flow

Page 52: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Bad news. Even when edge capacities are integers, number of augmenting

paths could be equal to the value of the maxflow.

Bad case for Ford-Fulkerson

52

0

0

t

s

0

1

0

0

1st iteration

1

1

1

100

100

100

100

Page 53: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Bad news. Even when edge capacities are integers, number of augmenting

paths could be equal to the value of the maxflow.

Bad case for Ford-Fulkerson

53

1

1

1

2nd iteration

0

0

t

s

1

1

1

0

100

100

100

100

Page 54: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Bad news. Even when edge capacities are integers, number of augmenting

paths could be equal to the value of the maxflow.

Bad case for Ford-Fulkerson

54

1

1

t

s

1

1

1

0

3rd iteration

2

2

1

100

100

100

100

Page 55: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Bad news. Even when edge capacities are integers, number of augmenting

paths could be equal to the value of the maxflow.

Bad case for Ford-Fulkerson

55

2

2

1

4th iteration

1

1

t

s

1

2

2

0

100

100

100

100

Page 56: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Bad news. Even when edge capacities are integers, number of augmenting

paths could be equal to the value of the maxflow.

Bad case for Ford-Fulkerson

56

. . .

Page 57: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Bad news. Even when edge capacities are integers, number of augmenting

paths could be equal to the value of the maxflow.

Bad case for Ford-Fulkerson

57

t

s

99

1

99

0

199th iteration

100

100

1

99

99

100

100

100

100

Page 58: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Bad news. Even when edge capacities are integers, number of augmenting

paths could be equal to the value of the maxflow.

Bad case for Ford-Fulkerson

58

100

100

1

200th iteration

99

99

t

s

1

100

100

0

100

100

100

100

Page 59: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Bad news. Even when edge capacities are integers, number of augmenting

paths could be equal to the value of the maxflow.

Good news. This case is easily avoided. [use shortest/fattest path]

Bad case for Ford-Fulkerson

59

0

100

100

t

s

1

100

100

100

10010

0

100

can be exponential in input size

Page 60: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

How to choose augmenting paths?

FF performance depends on choice of augmenting paths.

60

augmenting path

with fewest

number of edges

augmenting path

with maximum

bottleneck capacity

shortest path fattest path

augmenting path number of paths implementation

shortest path ≤ ½ E V queue (BFS)

fattest path ≤ E ln(E U) priority queue

random path ≤ E U randomized queue

DFS path ≤ E U stack (DFS)

digraph with V vertices, E edges, and integer capacities between 1 and U

Page 61: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Non-integer weights (beyond scope of course)

If:

・e1 = 1

・e2 = (sqrt(5) - 1)/2

・e3 = 1

・Other edges of weight 2 or greater

Can show:

・Ford Fulkerson can get stuck in infinite loop.

・Does not converge even if it runs forever.

61

Page 62: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

http://algs4.cs.princeton.edu

ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

‣ introduction

‣ Ford-Fulkerson algorithm

‣ Java implementation

‣ maxflow-mincut theorem

‣ running time analysis

‣ applications

6.4 MAXIMUM FLOW

Page 63: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

"Free world" goals. Understand peak Soviet supply rate.

Cut supplies (if cold war turns into real war).

Figure 2From Harris and Ross [1955]: Schematic diagram of the railway network of the Western So-viet Union and Eastern European countries, with a maximum flow of value 163,000 tons fromRussia to Eastern Europe, and a cut of capacity 163,000 tons indicated as ‘The bottleneck’.

Mincut application (RAND Corporation - 1950s)

63

rail network connecting Soviet Union with Eastern European countries(map declassified by Pentagon in 1999)

flow

capacity

Page 64: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Soviet Union goal. Maximize flow of supplies to Eastern Europe.

・Originally studied by writer Alexei Tolstoi in the 1930s (ad hoc approach).

・Later considered by Ford & Fulkerson via min cut approach.

Maxflow application (1950s)

Гиперболоид инженера Гарина

Page 65: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Maxflow/mincut is a widely applicable problem-solving model.

・Data mining.

・Open-pit mining.

・Bipartite matching.

・Network reliability.

・Baseball elimination.

・Image segmentation.

・Network connectivity.

・Distributed computing.

・Security of statistical data.

・Egalitarian stable matching.

・Multi-camera scene reconstruction.

・Sensor placement for homeland security.

・Many, many, more.

Maxflow and mincut applications

65

liver and hepatic vascularization segmentation

Page 66: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Bipartite matching problem

66

N students apply for N jobs.

Each gets several offers.

Is there a way to match all students to jobs?

1

2

3

4

5

Alice

Bob

Carol

Dave

Eliza

AdobeAmazonGoogle

AdobeAmazon

AdobeFacebook Google

AmazonYahoo

AmazonYahoo

6

7

8

9

10

Adobe

Amazon

Facebook

Google

Yahoo

AliceBobCarol

AliceBobDaveEliza

Carol

AliceCarol

DaveEliza

bipartite matching problem

Page 67: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Given a bipartite graph, find a perfect matching.

Bipartite matching problem

67

bipartite graph

N students N companies

1

2

3

4

5

Alice

Bob

Carol

Dave

Eliza

AdobeAmazonGoogle

AdobeAmazon

AdobeFacebook Google

AmazonYahoo

AmazonYahoo

6

7

8

9

10

Adobe

Amazon

Facebook

Google

Yahoo

AliceBobCarol

AliceBobDaveEliza

Carol

AliceCarol

DaveEliza

bipartite matching problemperfect matching (solution)

Alice

Bob

Carol

Dave

Eliza

—— Google

—— Adobe

—— Facebook

—— Yahoo

—— Amazon 3

1

5

2

4

6

8

9

7

10

Page 68: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Network flow formulation of bipartite matching

・Create s, t, one vertex for each student, and one vertex for each job.

・Add edge from s to each student (capacity 1).

・Add edge from each job to t (capacity 1).

・Add edge from student to each job offered (infinite capacity).

68

1

2

3

4

5

Alice

Bob

Carol

Dave

Eliza

AdobeAmazonGoogle

AdobeAmazon

AdobeFacebook Google

AmazonYahoo

AmazonYahoo

6

7

8

9

10

Adobe

Amazon

Facebook

Google

Yahoo

AliceBobCarol

AliceBobDaveEliza

Carol

AliceCarol

DaveEliza

3

1

t

6

8

9

55

2

4

7

10

s

bipartite matching problem

N students N companies

flow network

also works if just 1

Page 69: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

1-1 correspondence between perfect matchings in bipartite graph and

integer-valued maxflows of value N.

Network flow formulation of bipartite matching

69

3

1

t

6

8

9

55

2

4

7

10

s

1

2

3

4

5

Alice

Bob

Carol

Dave

Eliza

AdobeAmazonGoogle

AdobeAmazon

AdobeFacebook Google

AmazonYahoo

AmazonYahoo

6

7

8

9

10

Adobe

Amazon

Facebook

Google

Yahoo

AliceBobCarol

AliceBobDaveEliza

Carol

AliceCarol

DaveEliza

bipartite matching problemflow network

N students N companies

Page 70: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Goal. When no perfect matching, explain why.

What the mincut tells us

70

5

3

2

4

10

6

8

7

9

5

2

4

10

7

1

S = { 2, 4, 5 }T = { 7, 10 }

student in Scan be matched

only tocompanies in T

| S | > | T |

no perfect matching exists

Page 71: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Mincut. Consider mincut (A, B).

・Let S = students on s side of cut.

・Let T = companies on s side of cut.

・Fact: | S | > | T |; students in S can be matched only to companies in T.

Bottom line. When no perfect matching, mincut explains why.

What the mincut tells us

71

3

1

t

6

8

9

55

2

4

7

10

s

2

4

7

10

s

5

no perfect matching exists

S = { 2, 4, 5 }T = { 7, 10 }

student in Scan be matched

only tocompanies in T

| S | > | T |

Page 72: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Q. Which teams have a chance of finishing the season with the most wins?

Montreal is mathematically eliminated.

・Montreal finishes with ≤ 80 wins.

・Atlanta already has 83 wins.

Baseball elimination problem

72

i team wins losses to play ATL PHI NYM MON

0 Atlanta 83 71 8 – 1 6 1

1 Philly 80 79 3 1 – 0 2

2 New York 78 78 6 6 0 – 0

3 Montreal 77 82 3 1 2 0 –

Page 73: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Q. Which teams have a chance of finishing the season with the most wins?

Philadelphia is mathematically eliminated.

・Philadelphia finishes with ≤ 83 wins.

・Either New York or Atlanta will finish with ≥ 84 wins.

Observation. Answer depends not only on how many games already won

and left to play, but on whom they're against.

Baseball elimination problem

73

i team wins losses to play ATL PHI NYM MON

0 Atlanta 83 71 8 – 1 6 1

1 Philly 80 79 3 1 – 0 2

2 New York 78 78 6 6 0 – 0

3 Montreal 77 82 3 1 2 0 –

Page 74: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Q. Which teams have a chance of finishing the season with the most wins?

Detroit is mathematically eliminated.

・Detroit finishes with ≤ 76 wins.

・Wins for R = { NYY, BAL, BOS, TOR } = 278.

・Remaining games among { NYY, BAL, BOS, TOR } = 3 + 8 + 7 + 2 + 7 = 27.

・Average team in R wins 305/4 = 76.25 games.

Baseball elimination problem

74

i team wins losses to play NYY BAL BOS TOR DET

0 New York 75 59 28 – 3 8 7 3

1 Baltimore 71 63 28 3 – 2 7 4

2 Boston 69 66 27 8 2 – 0 0

3 Toronto 63 72 27 7 7 0 – 0

4 Detroit 49 86 27 3 4 0 0 –

AL East (August 30, 1996)

Page 75: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Intuition. Remaining games flow from s to t. Build a flow network for EACH team. Below is graph for 4.

Fact. Team 4 not eliminated iff all edges pointing from s are full in maxflow.

Baseball elimination problem: maxflow formulation

75

s g12 t

game vertices

(each pair of teams other than 4)

team vertices

(each team other than 4)

w4 + r4 – w2

1

0

3

2

0–2

0–3

1–3

0–1

2–3

1–2

games left

between 1 and 2

team 2 can still win

this many more games

Page 76: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Maximum flow algorithms: theory

(Yet another) holy grail for theoretical computer scientists.

76

year method worst case discovered by

1951 simplex E3 U Dantzig

1955 augmenting path E2 U Ford-Fulkerson

1970 shortest augmenting path E3 Dinitz, Edmonds-Karp

1970 fattest augmenting path E2 log E log( E U ) Dinitz, Edmonds-Karp

1977 blocking flow E 5/2 Cherkasky

1978 blocking flow E 7/3 Galil

1983 dynamic trees E2 log E Sleator-Tarjan

1985 capacity scaling E2 log U Gabow

1997 length function E3/2 log E log U Goldberg-Rao

2012 compact network E2 / log E Orlin

? ? E ?

maxflow algorithms for sparse digraphs with E edges, integer capacities between 1 and U

Page 77: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Maximum flow algorithms: practice

Warning. Worst-case order-of-growth is generally not useful for predicting

or comparing maxflow algorithm performance in practice.

Best in practice. Push-relabel method with gap relabeling: E 3/2.

77

EUROPEAN JOURNAL

OF OPERATIONAL RESEARCH

E L S E V I E R European Journal of Operational Research 97 (1997) 509-542

T h e o r y a n d M e t h o d o l o g y

Computational investigations of maximum flow algorithms R a v i n d r a K . A h u j a a, M u r a l i K o d i a l a m b, A j a y K . M i s h r a c, J a m e s B . O r l i n d, .

a Department t~'lndustrial and Management Engineering. Indian Institute of Technology. Kanpur, 208 016, India b AT& T Bell Laboratories, Holmdel, NJ 07733, USA

c KA'F-Z Graduate School of Business, University of Pittsburgh, Pittsburgh, PA 15260, USA d Sloun School of Management, Massachusetts Institute of Technology. Cambridge. MA 02139. USA

Received 30 August 1995; accepted 27 June 1996

A b s t r a c t

The maximum flow algorithm is distinguished by the long line of successive contributions researchers have made in obtaining algorithms with incrementally better worst-case complexity. Some, but not all, of these theoretical improvements have produced improvements in practice. The purpose of this paper is to test some of the major algorithmic ideas developed in the recent years and to assess their utility on the empirical front. However, our study differs from previous studies in several ways. Whereas previous studies focus primarily on CPU time analysis, our analysis goes further and provides detailed insight into algorithmic behavior. It not only observes how algorithms behave but also tries to explain why algorithms behave that way. We have limited our study to the best previous maximum flow algorithms and some of the recent algorithms that are likely to be efficient in practice. Our study encompasses ten maximum flow algorithms and five classes of networks. The augmenting path algorithms tested by us include Dinic's algorithm, the shortest augmenting path algorithm, and the capacity-scaling algorithm. The preflow-push algorithms tested by us include Karzanov's algorithm, three implementations of Goldberg-Tarjan's algorithm, and three versions of Ahuja-Orlin-Tarjan's excess-scaling algorithms. Among many findings, our study concludes that the preflow-push algorithms are substantially faster than other classes of algorithms, and the highest-label preflow-push algorithm is the fastest maximum flow algorithm for which the growth rate in the computational time is O(n LS) on four out of five of our problem classes. Further, in contrast to the results of the worst-case analysis of maximum flow algorithms, our study finds that the time to perform relabel operations (or constructing the layered networks) takes at least as much computation time as that taken by augmentations and/or pushes. © 1997 Published by Elsevier Science B.V.

1. I n t r o d u c t i o n

The maximum flow problem is one of the most fundamental problems in network optimization. Its intuitive appeal, mathematical simplicity, and wide applicabil i ty has made it a popular research topic

* Corresponding author.

0377-2217/97/$17.00 © 1997 Published by Elsevier Science B.V. All PII S0377-2217(96)00269-X

among mathematicians, operations researchers and computer scientists.

The maximum flow problem arises in a wide variety of situations. It occurs directly in problems as diverse as the flow of commodit ies in pipeline net- works, parallel machine scheduling, distributed com- puting on multi-processor computers, matrix round- ing problems, the baseball el imination problem, and the statistical security of data. The maximum flow

rights reserved.

On Implement ing Push-Re labe l M e t h o d for the M a x i m u m Flow Problem

Boris V. Cherkassky 1 and Andrew V. Goldberg 2

1 Central Institute for Economics and Mathematics, Krasikova St. 32, 117418, Moscow, Russia

[email protected] 2 Computer Science Department, Stanford University

Stanford, CA 94305, USA goldberg ~cs. stanford, edu

Abst rac t . We study efficient implementations of the push-relabel method for the maximum flow problem. The resulting codes are faster than the previous codes, and much faster on some problem families. The speedup is due to the combination of heuristics used in our implementations. We also exhibit a family of problems for which the running time of all known methods seem to have a roughly quadratic growth rate.

1 I n t r o d u c t i o n

The rnaximum flow problem is a classical combinatorial problem that comes up in a wide variety of applications. In this paper we study implementations of the push-rdabel [13, 17] method for the problem.

The basic methods for the maximum flow problem include the network sim- plex method of Dantzig [6, 7], the augmenting path method of Ford and F~lker- son [12], the blocking flow method of Dinitz [10], and the push-relabel method of Goldberg and Tarjan [14, 17]. (An earlier algorithm of Cherkassky [5] has many features of the push-relabel method.) The best theoretical time bounds for the maximum flow problem, based on the latter method, are as follows. An algorithm of Goldberg and Tarjan [17] runs in O(nm log(n2/m)) time, an algo- r i thm of King et. al. [21] runs in O(nm + n TM) time for any constant e > 0, an algorithm of Cheriyan et. al. [3] runs in O(nm + (n logn) 2) time with high probability, and an algorithm of Ahuja et. al. [1] runs in O ( a m log (~ - -~ + 2 ) ) time.

Prior to the push-relabel method, several studies have shown that Dinitz' algorithm [10] is in practice superior to other methods, including the network simplex method [6, 7], Ford-giflkerson algorithm [11, 12], Karzanov's algorithm [20], and Tarjan's algorithm [23]. See e.g. [18]. Several recent studies (e.g. [2,

* Andrew V. Goldberg was supported in part by NSF Grant CCR-9307045 and a grant from Powell Foundation. This work was done while Boris V. Cherkassky was visiting Stanford University Computer Science Department and supported by the above-mentioned NSF and Powell Foundation grants.

Page 78: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

Summary

Mincut problem. Find an st-cut of minimum capacity.

Maxflow problem. Find an st-flow of maximum value.

Duality. Value of the maxflow = capacity of mincut.

Proven successful approaches.

・Ford-Fulkerson (various augmenting-path strategies).

・Preflow-push (various versions).

Open research challenges.

・Practice: solve real-word maxflow/mincut problems in linear time.

・Theory: prove it for worst-case inputs.

・Still much to be learned!

78

Page 79: Algorithms - Princeton University Computer Science · Max flow / min cut problem 8 s t 4 / 10 3 / 5 0 / 15 10 / 10 1 / 1 0 / 3 7 / 10 10 / 12 11 value of flow 1 / 9 cut capacity

ROBERT SEDGEWICK | KEVIN WAYNE

F O U R T H E D I T I O N

Algorithms

http://algs4.cs.princeton.edu

Algorithms ROBERT SEDGEWICK | KEVIN WAYNE

6.4 MAXIMUM FLOW

‣ introduction

‣ Ford-Fulkerson algorithm

‣ maxflow-mincut theorem

‣ running time analysis

‣ Java implementation

‣ applications