Sei sulla pagina 1di 21

CS 170:Algorithms

Prof Elchanan Mossel.


Based on: Algorithms by Dasgupta Papadimitriou and Vazirani.
Slides edited from a version created by Prof. Satish Rao.
For UC-Berkeley CS170 Spring 2014 students use only.
Do not re-post or distribute
Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 1 / 16
CS 170: Algorithms
S
H
H
H
H
.
.
.
.
.
Linear Programming today!!!!
No laptops please.
Thank you !!!
Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 2 / 16
Outline
Maximum Flow.
Problem.
Linear Program
Upper bound: Cut!
Residual Network
Algorithm
Minimum Cut.
Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 3 / 16
Maximum ow
Flow network G = (V, E), source s, sink t V, capacities c
e
>0.
a
c
d
e
s
t
4
4
3
1
4
2
1
1
1
3
1
2
2
2
1
Find Flow: f
e
1
0 f
e
c
e
. Capacity constraints. 3 =f
s,c
c
s,c
= 4.
2
If u is not s or t

(w,u)E
f
wu
=

(u,w)E
f
uw
. 3 =f
s,c
=f
c,d
+f
c,e
= 2+1.
Maximize: size(f ) =

(s,u)E
f
su
. f
sa
+f
sc
= 1+3 = 4
Optimal? c
ad
+c
cd
+c
et
= 1+1+2 = 4.
The capacity of any s t cut gives an upper bound.
Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 4 / 16
S-T cut.
An s t cut is a partition of V into S and T where s S and t T. Its
capacity is the total capacity of edges from S to T.
Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 5 / 16
Do you know the denition?
Find Flow: f
e
1
0 f
e
c
e
. Capacity constraints.
2
If u is not s or t

(w,u)E
f
wu
=

(u,w)E
f
uw
.
Valid or Invalid?
s
b
c
d
t
3
2
1
1
2
2
1
1
2
3
2
3
s
b
c
d
t
3
2
1
1
2
2
1
1
2
2
2+1 = 2
Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 6 / 16
Algorithms.
FindFlow: f
e
1
0 f
e
c
e
. Capacity constraints.
2

(w,u)E
f
wu
=

(u,w)E
f
uw
.
3
maximize

su
f
su
.
Linear program!
Variables f
e
, linear constraints, linear optimization function.
Cool!
Note...
Integer? (Given integer capacities.)
Yes. There is an integer vertex solution!
Constraint matrix has every subdeterminant being 1, 0, 1.
Vertex solution to linear program must be integral!
Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 7 / 16
Ford-Fulkerson.
Simplex method.
Find s to t path with remaining capacity.
Add to ow variables along path.
Update remaining capacity.
Repeat.
a
c
d
e
s
t
4
4
3
1
4
2
1
1
4 4 X 3
1
1 1 X 0
1
1 1 X 0 1
2 2 X 1
1
Uh oh...
Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 8 / 16
Residual Capacity.
Find s to t path with remaining capacity.
Add to ow along path. Or reduce ow on reverse edge.
Update remaining capacity.
Reduce r
e
=c
e
f
e
and add reverse r
uv
=f
vu
Repeat.
s
a
b
t
1
1
1
1
1
1 1 X 0
1
1 1 X 0
1
1 1 X 0 1
1
1
1
1
1
1 X 0
1 X 0
1
1
No remaining path. Uh oh! Optimal is 2! (At most 2 due to cut.)
Add reverse arcs to indicate reverse capacity.
Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 9 / 16
Bigger Example.
Find s to t path with remaining capacity.
Add to ow along path.
Update residual capacities: : r
e
=c
e
f
e
; r
uv
=f
vu
.
Repeat.
a
c
d
e
s
t
4
4
3
1
4
2
1
1
4 4 X 3
1 1
1 1 X 0
11
1 1 X 0 1 1
2 2 X 1
1 1
4 4 X 3
1 1
3 3 X 2
11
2 X 1 1 X 0
12 2
4 4 X 3
1 1
1 X 0 0 X 1 1
4 X 3 3 X 2
12
3 X 2 2 X 1
12
1 1 X 0
11
3 X 2 2 X 1
23 3
4 X 3 3 X 2
12 2
Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 10 / 16
Check Result...
a
c
d
e
s
t
4
4
3
1
4
2
1
1
1
3
1
2
2
2
1
Find Flow: f
e
1
0 f
e
c
e
. Capacity constraints. 3 =f
s,c
c
s,c
= 4.
2
If u is not s or t

(w,u)E
f
wu
=

(u,w)E
f
uw
. 3 =f
s,c
=f
c,d
+f
c,e
= 2+1.
Maximize: size(f ) =

(s,u)E
f
su
. f
sa
+f
sc
= 1+3 = 4
Optimal? c
ad
+c
cd
+c
et
= 1+1+2 = 4.
Any s t cut gives an upper bound.
Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 11 / 16
Correctness.
1. Capacity Constraints: 0 f
e
c
e
.
Only increase ow to c
e
.
Or use reverse arcs decrease to 0.
Flow values to be between 0 and c
e
.
2. Conservation Constraints:
ow into v = ow out of v (if not s or t .)
Algorithm adds ow, say f , to path from s to t .
Each internal node has f in, and f out.
Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 12 / 16
Optimality: upper bound.
s-t Cut: V =ST and s S and t T.
s
t
Lemma: Capacity of any s t cut is an upper bound on the ow.
C(S, T) - sum of capacities of all arcs from S to T
C(S, T) =

e=(u,v):uS,VT
c
e
For valid ow:
Flow out of (S) = Flow out of s.
Flow into (T) = Flow into t .
For any valid ow, f : E Z+, the ow out of S (into T)

eST
f
e

eTS
f
e

eST
c
e

eTS
0 =C(S, T).
The value of any valid ow is at most C(S, T)!
Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 13 / 16
Optimality: max ow = min cut.
At termination of augmenting path algorithm.
No path with residual capacity!
Depth rst search only starting at s does not reach t .
s
t
f
e
=c
e
f
e
= 0
S be reachable nodes.
No arc with positive residual capacity
leaving S
= All arcs leaving S are full.
= No arcs into S have ow.
Total ow leaving S is C(S, T).
Valid ow = all that ow from source.
Value of ow equals value of C(S, T). and Optimal is C(S, T).
Flow is maximum!!
Cut is minimum s t cut too!
any ow any cut and this ow = this cut.
Maximum ow and minimum s t cut!
Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 14 / 16
Celebrated max ow -minimum cut theorem.
Theorem: In any ow network, the maximum s-t ow is equal to the
minimum cut.
Celebrate!
Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 15 / 16
Back to business: Algorithm Terminates?
It will!!
Flow keeps increasing.
How long?
One more unit every step!
O(mF) time where F is size of ow.
Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 16 / 16
Efciency.
s
a
b
t
100000
1
100000 100000
100000 99999
1
99999
1
1
99999
1
99999
1
99999
1
99999
1
1
Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 17 / 16
Edmonds-Karp
Augment along shortest path.
Breadth rst search!
O(|V||E|) augmentations.
Analysis idea.
d(v) is distance to sink.
Only route ow on (u, v) if d(u) d(v).
Only reverse ow on (u, v) if d(v) d(u).
Maximum d(v) is |V|.
Distances only go up. (To prove!)
Every augment removes edge at a distance.
O(|V||E|) removals.
O(|V||E|
2
) time.
Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 18 / 16
Bipartite Matching
Given a bipartite graph: B = (L, R, E) where E LR.
Cindy
Jin
Shelby
Katya
Chen
Ollie
Ivan
Jacque
Find largest subset of edges (matches) which are one to one.
Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 19 / 16
Bipartite Matching
Algorithm by Reduction.:
From matching problem produce ow problem.
From ow solution produce matching solution.
C
J
S
K
C
O
I
J
s
t
Max ow = Max Matching Size.
Flow is not integer necessarily....
Augmenting path algorithm gives integer ow.
Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 20 / 16
Review.
Maximum ow.
Greedy augment path...
Except reverse old decisions ..
Reverse residual capacities.
Optimality?
Upper bound on optimal: s t cut!
Find ow and s t cut with equal value!
Optimal plus proof of optimality.
Application to bi-partite matchings.
Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 21 / 16

Potrebbero piacerti anche