Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Princeton University • COS 226 • Algorithms and Data Structures • Spring 2004 • Kevin Wayne • http://www.Princeton.EDU/~cos226 2
2 9 5
10 4 15 15 10
source s 5 3 8 6 10 t sink
capacity 15 4 6 15 10
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
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.
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
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
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.
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.
S 15 4 10
4 0 6 15 0 10
15
15
4 30 7
17 18
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."
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
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
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?
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
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()) {
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.
not polynomial 1 0
s t
in input size!
Corollary: if U = 1, then algorithm runs in £ V iterations. 0 0
100 100
29 30
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
33 34
35 36
Shortest Augmenting Path Analysis Fattest Augmenting Path
12 X 10
9
v 10 w
residual capacity
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
39 40
An Application Bipartite Matching
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
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
47