Sei sulla pagina 1di 12

Maximum Flow and Minimum Cut

Max Flow, Min Cut Max flow and min cut.


n Two very rich algorithmic problems.
n Cornerstone problems in combinatorial optimization.
n Beautiful mathematical duality.

Minimum cut Nontrivial applications / reductions.


Network connectivity. Network reliability.
Maximum flow
n
n

Max-flow min-cut theorem n Bipartite matching. n Security of statistical data.


Ford-Fulkerson augmenting path algorithm n Data mining. n Distributed computing.
Open-pit mining. Egalitarian stable matching.
Edmonds-Karp heuristics
n
n

Bipartite matching n Airline scheduling. n Distributed computing.


n Image processing. n Many many more . . .
n Project selection.
n Baseball elimination.

Princeton University • COS 226 • Algorithms and Data Structures • Spring 2004 • Kevin Wayne • http://www.Princeton.EDU/~cos226 2

Soviet Rail Network, 1955 Minimum Cut Problem

Network: abstraction for material FLOWING through the edges.


n Directed graph.
n Capacities on edges.
n Source node s, sink node t.

Min cut problem. Delete "best" set of edges to disconnect t from s.

2 9 5

10 4 15 15 10

source s 5 3 8 6 10 t sink

capacity 15 4 6 15 10

Source: On the history of the transportation and maximum flow problems.


4 30 7
Alexander Schrijver in Math Programming, 91: 3, 2002.
3 4
Cuts Cuts

A cut is a node partition (S, T) such that s is in S and t is in T. A cut is a node partition (S, T) such that s is in S and t is in T.
n capacity(S, T) = sum of weights of edges leaving S. n capacity(S, T) = sum of weights of edges leaving S.

2 9 5 2 9 5

10 15 15 10 10 15 15 10
4 4

s 5 3 8 6 10 t s 5 3 8 6 10 t
S
S
4 6 15 10 4 6 15 10
15 15

Capacity = 30 Capacity = 62
4 30 7 4 30 7
5 6

Minimum Cut Problem Maximum Flow Problem

A cut is a node partition (S, T) such that s is in S and t is in T. Network: abstraction for material FLOWING through the edges.
n capacity(S, T) = sum of weights of edges leaving S. n Directed graph.
same input as min cut problem
n Capacities on edges.
Min cut problem. Find an s-t cut of minimum capacity. n Source node s, sink node t.

Max flow problem. Assign flow to edges so as to:


n Equalize inflow and outflow at every intermediate vertex.
2 9 5 n Maximize flow sent from s to t.

10 15 15 10 2 9 5
4

10 4 15 15 10
s 5 3 8 6 10 t
source s 5 3 8 6 10 t sink
4 6 15 10
S 15 capacity 15 4 6 15 10
Capacity = 28
4 30 7 4 30 7
7 8
Flows Flows

A flow f is an assignment of weights to edges so that: A flow f is an assignment of weights to edges so that:
n Capacity: 0 £ f(e) £ u(e). n Capacity: 0 £ f(e) £ u(e).
n Flow conservation: flow leaving v = flow entering v. n Flow conservation: flow leaving v = flow entering v.

except at s or t except at s or t

0 6
2 9 5 2 9 5

4 0 10 6
0 0
10 15 15 0 10 10 15 15 0 10
4 4 4 4

0 4 4 3 8 8
s 5 3 8 6 10 t s 5 3 8 6 10 t

0 0 1 10
4 0 6 15 0 10 4 0 6 15 0 10
capacity 15 capacity 15
flow 0 0 flow 11 11
Value = 4 Value = 24
4 30 7 4 30 7
9 10

Maximum Flow Problem Flows and Cuts

Max flow problem: find flow that maximizes net flow into sink. Observation 1. Let f be a flow, and let (S, T) be any s-t cut. Then, the
net flow sent across the cut is equal to the amount reaching t.

9 6
2 9 5 2 9 5

10 9 10 6
1 0
10 15 15 0 10 10 15 15 0 10
4 0 4 4

4 8 9 4 8 8
s 5 3 8 6 10 t s 5 3 8 6 10 t
S
4 10 10 0 10
4 0 6 15 0 10 4 0 6 15 0 10
capacity 15 15
flow 14 14 10
Value = 28 Value = 24
4 30 7 4 30 7
11 12
Flows and Cuts Flows and Cuts

Observation 1. Let f be a flow, and let (S, T) be any s-t cut. Then, the Observation 1. Let f be a flow, and let (S, T) be any s-t cut. Then, the
net flow sent across the cut is equal to the amount reaching t. net flow sent across the cut is equal to the amount reaching t.

6 6
2 9 5 2 9 5

10 6 10 6
0 0
10 15 15 0 10 10 15 15 0 10
4 4 4 4

4 8 8 4 8 8
s 5 3 8 6 10 t s 5 3 8 6 10 t
S S
10 0 10 10 0 10
4 0 6 15 0 10 4 0 6 15 0 10
15 15
10 Value = 24 10 Value = 24
4 30 7 4 30 7
13 14

Flows and Cuts Max Flow and Min Cut

Observation 2. Let f be a flow, and let (S, T) be any s-t cut. Then the Observation 3. Let f be a flow, and let (S, T) be an s-t cut whose capacity
value of the flow is at most the capacity of the cut. equals the value of f. Then f is a max flow and (S, T) is a min cut.

Cut capacity = 28 Þ Flow value £ 28


Cut capacity = 30 Þ Flow value £ 30

9
Flow value = 28
2 9 5
2 9 5
10 9
1
10 15 15 0 10
4 0
10 15 15 10
4
4 8 9
s 5 3 8 6 10 t
s 5 3 8 6 10 t
S 15 4 10
S 15 0
4 0 6 10
4 15 15
6 10
15
15
4 30 7
4 30 7
15 16
Max-Flow Min-Cut Theorem Towards an Algorithm

Max-flow min-cut theorem. (Ford-Fulkerson, 1956): In any network, Find s-t path where each arc has f(e) < u(e) and "augment" flow along it.
the value of max flow equals capacity of min cut.
n Proof IOU: we find flow and cut such that Observation 3 applies.

flow Flow value = 0


Min cut capacity = 28 Û Max flow value = 28
4 5
0 capacity
9
2 9 5 0 4 0
4
10 0 4
1 9
10 15 0 0 4
4 0 15 10 0 0
s 10 2 13 3 10 t
4 8 9
s 5 3 8 6 10 t

S 15 4 10
4 0 6 15 0 10
15
15
4 30 7

17 18

Towards an Algorithm Towards an Algorithm

Find s-t path where each arc has f(e) < u(e) and "augment" flow along it. Find s-t path where each arc has f(e) < u(e) and "augment" flow along it.
n Greedy algorithm: repeat until you get stuck. n Greedy algorithm: repeat until you get stuck.
n Fails: need to be able to "backtrack."

flow Flow value = 10 flow Flow value = 10

4 5 4 5
0 capacity 0 capacity
0 4 0 0 4 0
4 4
0 4 0 4
X
0 10 4 X X
0 10 4 X
X
0 10 0 10 X
0 10 0 10
s 10 2 13 3 10 t s 10 2 13 3 10 t

4 5 Flow value = 14
Bottleneck capacity of path = 10 4
4 4 4
4
4 4
10 4
6 10
s 10 2 13 3 10 t
19 20
Residual Graph Augmenting Paths

Augmenting path = path in residual graph.


flow = f(e)
Original graph. n Increase flow along forward edges.
n Flow f(e). 6 n Decrease flow along backward edges.
n Edge e = v-w v 17 w
capacity = u(e)
4 5

4
residual 4
Residual edge. 4
4
n Edge e = v-w or w-v.
"Undo" flow sent. s 10 2 10 3 10 t
n
residual capacity = u(e) – f(e)
3

Residual graph. v 11 w
6 4 5
n All the edges that have 4 X
0
strictly positive residual capacity. 4 0
X
residual capacity = f(e) 4 0 4
X
original 4 4X
0 4
10 4 X 6
10 10
s 10 2 13 3 10 t
21 22

Augmenting Paths Ford-Fulkerson Augmenting Path Algorithm

Observation 4. If augmenting path, then not yet a max flow. Ford-Fulkerson algorithm. Generic method for solving max flow.
Q. If no augmenting path, is it a max flow?

while (there exists an augmenting path) {


4 5 Find augmenting path P
4 Compute bottleneck capacity of P
residual 4
4 Augment flow along P
4 }
s 10 2 6 3 10 t
7
Questions.
Flow value = 14 Does this lead to a maximum flow? yes
4 5
n

4 X
0
4 0
X
n How do we find an augmenting path? s-t path in residual graph
4 0 4
X
original 4
n How many augmenting paths does it take?
4X
0 4
How much effort do we spending finding a path?
4
n

10 X 6
10 10
s 10 2 13 3 10 t
23 24
Max-Flow Min-Cut Theorem Proof of Max-Flow Min-Cut Theorem

Augmenting path theorem. A flow f is a max flow if and only if there (ii) Þ (iii). If there is no augmenting path relative to f, then there
are no augmenting paths. exists a cut whose capacity equals the value of f.
Proof.
Max-flow min-cut theorem. The value of the max n Let f be a flow with no augmenting paths.
flow is equal to the capacity of the min cut. n Let S be set of vertices reachable from s in residual graph.
– S contains s; since no augmenting paths, S does not contain t
We prove both simultaneously by showing the following are equivalent: – all edges e leaving S in original network have f(e) = u(e)
(i) f is a max flow. – all edges e entering S in original network have f(e) = 0
(ii) There is no augmenting path relative to f.
(iii) There exists a cut whose capacity equals the value of f.
S T
f = å f (e ) - å f (e )
(i) Þ (ii) equivalent to not (ii) Þ not (i), which was Observation 4 e out of S e in to S
t
(ii) Þ (iii) next slide = å u (e )
e out of S
(iii) Þ (i) this was Observation 3
= capacity (S, T)
s

residual network
25 26

Max Flow Network Implementation Ford-Fulkerson Algorithm: Implementation

Edge in original graph may correspond to 1 or 2 residual edges. Ford-Fulkerson main loop.
n May need to traverse edge e = v-w in forward or reverse direction.
n Flow = f(e), capacity = u(e).
n Insert two copies of each edge, one in adjacency list of v and one in w. // while there exists an augmenting path, use it
while (augpath()) {

// compute bottleneck capacity


public class Edge { int bottle = INFINITY;
private int v, w; // from, to for (int v = t; v != s; v = ST(v))
private int cap; // capacity from v to w bottle = Math.min(bottle, pred[v].capRto(v));
private int flow; // flow from v to w
// augment flow
public Edge(int v, int w, int cap) { ... }
for (int v = t; v != s; v = ST(v))
public int cap() { return cap; }
pred[v].addflowRto(v, bottle);
public int flow() { return flow; }
// keep track of total flow sent from s to t
public boolean from(int v) { return this.v == v; } value += bottle;
public int other(int v) { return from(v) ? this.w : this.v; } }
public int capRto(int v) { return from(v) ? flow : cap - flow; }
public void addflowRto(int v, int d) { flow += from(v) ? -d : d; }
}

27 28
Ford-Fulkerson Algorithm: Analysis Choosing Good Augmenting Paths

Assumption: all capacities are integers between 1 and U. Use care when selecting augmenting paths.

Invariant: every flow value and every residual capacities remain an


integer throughout the algorithm.
4
0 0
Theorem: the algorithm terminates in at most | f * | £ V U iterations. 100 100

not polynomial 1 0
s t
in input size!
Corollary: if U = 1, then algorithm runs in £ V iterations. 0 0
100 100

Integrality theorem: if all arc capacities are integers, then there 2


exists a max flow f for which every flow value is an integer. Original Network

29 30

Choosing Good Augmenting Paths Choosing Good Augmenting Paths

Use care when selecting augmenting paths. Use care when selecting augmenting paths.

1 4 4
0
X 0 1 0
100 100 100 100
1
s 1 X
0 t s 1 1 t
0 1 X
0 0 1
100 100 100 100

2 2
Original Network Original Network

31 32
Choosing Good Augmenting Paths Choosing Good Augmenting Paths

Use care when selecting augmenting paths. Use care when selecting augmenting paths.

4 4
1 0 1
X 1 1
100 100 100 100
0
s 1 X
1 t s 1 0 t
X
0 1 1 1 1
100 100 100 100

2 2
Original Network Original Network

200 iterations possible!

33 34

Choosing Good Augmenting Paths Shortest Augmenting Path

Use care when selecting augmenting paths. Shortest augmenting path.


n Some choices lead to exponential algorithms. n Easy to implement with BFS.
n Clever choices lead to polynomial algorithms. n Finds augmenting path with fewest number of arcs.
n Optimal choices for real world problems ???
while (!q.isEmpty()) {
int v = q.dequeue();
IntIterator i = G.neighbors(v);
Design goal is to choose augmenting paths so that:
while(i.hasNext()) {
n Can find augmenting paths efficiently. Edge e = i.next();
n Few iterations. int w = e.other(v);
if (e.capRto(w) > 0) { // is v-w a residual edge?
if (wt[w] > wt[v] + 1) {
Choose augmenting path with: Edmonds-Karp (1972) wt[w] = wt[v] + 1;
pred[w] = e; // keep track of shortest path
n Fewest number of arcs. (shortest path)
q.enqueue(w);
n Max bottleneck capacity. (fattest path) }
}
}
}
return (wt[t] < INFINITY); // is there an augmenting path?

35 36
Shortest Augmenting Path Analysis Fattest Augmenting Path

Length of shortest augmenting path increases monotonically. Fattest augmenting path.


n Strictly increases after at most E augmentations. n Finds augmenting path whose bottleneck capacity is maximum.
n At most E V total augmenting paths. n Delivers most amount of flow to sink.
n O(E2 V) running time. n Solve using Dijkstra-style (PFS) algorithm.

12 X 10
9

v 10 w
residual capacity

if (wt[w] < Math.min(wt[v], e.capRto(w)) {


wt[w] = Math.min(wt[v], e.capRto(w));
pred[w] = v;
}

Finding a fattest path. O(E log V) per augmentation with binary heap.
Fact. O(E log U) augmentations if capacities are between 1 and U.

37 38

Choosing an Augmenting Path History of Worst-Case Running Times

Choosing an augmenting path.


Year Discoverer Method Asymptotic Time
n Any path will do Þ wide latitude in implementing Ford-Fulkerson.
1951 Dantzig Simplex E V2 U †
n Generic priority first search.
1955 Ford, Fulkerson Augmenting path EVU †
n Some choices lead to good worst-case performance.
1970 Edmonds-Karp Shortest path E2 V
– shortest augmenting path
1970 Edmonds-Karp Max capacity E log U (E + V log V) †
– fattest augmenting path
1970 Dinitz Improved shortest path E V2
– variation on a theme: PFS
1972 Edmonds-Karp, Dinitz Capacity scaling E2 log U †
n Average case not well understood.
1973 Dinitz-Gabow Improved capacity scaling E V log U †

1974 Karzanov Preflow-push V3


Research challenges.
1983 Sleator-Tarjan Dynamic trees E V log V
n Practice: solve max flow problems on real networks in linear time.
1986 Goldberg-Tarjan FIFO preflow-push E V log (V2 / E)
n Theory: prove it for worst-case networks.
... ... ... ...
E3/2log (V2 / E) log U †
1997 Goldberg-Rao Length function
EV2/3 log (V2 / E) log U †

† Arc capacities are between 1 and U.

39 40
An Application Bipartite Matching

Jon placement. Bipartite matching.


n Companies make job offers. n Input: undirected and bipartite graph G.
n Students have job choices. n Set of edges M is a matching if each vertex appears at most once.
n Max matching: find a max cardinality matching.
Can we fill every job?

Can we employ every student? 1 A

2 B Matching M
Alice-Adobe
Bob-Yahoo 1-B, 3-A, 4-E
Carol-HP 3 C
Dave-Apple
Eliza-IBM
4 D
Frank-Sun

L 5 E R
41 42

Bipartite Matching Bipartite Matching

Bipartite matching. Reduces to max flow.


n Input: undirected and bipartite graph G. n Create a directed graph G'.
n Set of edges M is a matching if each vertex appears at most once. n Direct all arcs from L to R, and give infinite (or unit) capacity.
n Max matching: find a max cardinality matching. n Add source s, and unit capacity arcs from s to each node in L.
n Add sink t, and unit capacity arcs from each node in R to t.

L R
1 A
1 A 1 ¥ A

2 B Matching M 1 1
2 B 2 B
1-A, 2-B, 3-C, 4-D
3 C 3 C s 3 C t

4 D 4 D 4 D

5 E 5 E
L 5 E R
G G'
43 44
Bipartite Matching: Proof of Correctness Bipartite Matching: Proof of Correctness

Claim. Matching in G of cardinality k induces flow in G' of value k. Claim. Flow f of value k in G' induces matching of cardinality k in G.
n Given matching M = { 1-B, 3-A, 4-E } of cardinality 3. n By integrality theorem, there exists 0/1 valued flow f of value k.
n Consider flow f that sends 1 unit along each of 3 paths: n Consider M = set of edges from L to R with f(e) = 1.
s-1-B-t s-3-A-t s-4-E-t. – each node in L and R incident to at most one edge in M
n f is a flow, and has cardinality 3. – |M| = k

L R L R
1 A 1 ¥ A 1 A 1 ¥ A

2 B 1 2 B
1 2 B 1 2 B
1

3 C s 3 C t 3 C s 3 C t

4 D 4 D 4 D 4 D

5 E 5 E 5 E 5 E
G G' G G'
45 46

Reduction

Reduction.
n Given an instance of bipartite matching.
n Transform it to a max flow problem.
n Solve max flow problem.
n Transform max flow solution to bipartite matching solution.

Issues.
n How expensive is transformation? O(E + V)
n Is it better to solve problem directly? O(E V1/2) bipartite matching

Bottom line: max flow is an extremely rich problem-solving model.


n Many important practical problems reduce to max flow.
n We know good algorithms for solving max flow problems.

47

Potrebbero piacerti anche