Max Flow
Max Flow
Analysis of Algorithms
Lecture 8
!
Next: Network Flows
Oil Through Pipelines
Problem: Given directed graph G=(V,E), source s, sink t, edge capacities c(e),
how much oil can we ship from s to t?
u
2/2 1/3
s 1/2 t Size of f = 3
1/1 2/5
v
The Max Flow Problem: Given directed graph G=(V,E), source s, sink t,
edge capacities c(e), find an s-t flow of maximum size
Flows and Cuts
The Max Flow Problem: Given directed graph G=(V,E), source s, sink t,
edge capacities c(e), find an s-t flow of maximum size
u
2/2 1/3 An s-t Cut partitions nodes into groups = (L, R)
L
s.t. s in L, t in R
s 1/2 t X
R Capacity of a cut (L, R) = c(u, v)
(u,v)2E,u2L,v2R
1/1 2/5 X X
v Flow across (L,R) = f (u, v) f (v, u)
(u,v)2E,u2L,v2R (v,u)2E,u2L,v2R
Size of f = 3
Property: For any flow f, any s-t cut (L, R), size(f) <= capacity(L, R)
Proof: For any cut (L,R), Flow Across (L,R) cannot exceed capacity(L,R)
From flow conservation constraints, size(f) = flow across(L,R) <= capacity(L,R)
Max-Flow <= Min-Cut
Flows and Cuts
The Max Flow Problem: Given directed graph G=(V,E), source s, sink t,
edge capacities c(e), find an s-t flow of maximum size
u R*
2/2 1/3 An s-t Cut partitions nodes into groups = (L, R)
s.t. s in L, t in R
L* s 1/2 t X
Capacity of a cut (L, R) = c(u, v)
(u,v)2E,u2L,v2R
1/1 2/5 X X
v Flow across (L,R) = f (u, v) f (v, u)
(u,v)2E,u2L,v2R (v,u)2E,u2L,v2R
Size of f = 3
Property: For any flow f, any s-t cut (L, R), size(f) <= capacity(L, R)
Proof: For any cut (L,R), Flow Across (L,R) cannot exceed capacity(L,R)
From flow conservation constraints, size(f) = flow across(L,R) <= capacity(L,R)
Max-Flow <= Min-Cut Cuts
In our example: Size of f = 3, Capacity of Cut (s,V - s) = 3.
Flows
Thus, a Min Cut is a certificate of optimality for a flow
Ford-Fulkerson algorithm
FF Algorithm: Start with zero flow But what if we first chose:
Repeat:
Find a path from s to t along which flow 1 a
can be increased 1
Increase the flow along that path s t
1
b
Example a
1 1
1 1 a
b 1
s 1 t
First choose:
1 a 1
1 cancels out
b
existing flow
s t
Next choose:
s t
1 1
b
Ford-Fulkerson, continued
Example
FF Algorithm: Start with zero flow a
Repeat: G: 1 1
G Gf 2
2
a d a d
3 5 1 3
2 2
2
2 2 2
1 1
s b t s b t
10 10
4 1 4 1
c e c e
6 6
Example: Round 2
f 2
Construct residual graph Gf = (V, Ef) a d
Ef ✓ E U ER 2 4
For any (u,v) in E or ER, 2
cf(u,v) = c(u,v) – f(u,v) + f(v,u) s b t
2
Find a path from s to t in Gf
Augment f along this path
c e
Gf (before) 2 Gf (after) 2
a d a d
1 3 1 1
2 2
2 2
2 2 2 4
1 8 1
s b t s b t
10 2
4 1 4 1
c e c e
6 6
Example: Round 3
f 2
Construct residual graph Gf = (V, Ef) a d
Ef ✓ E U ER 2 4
For any (u,v) in E or ER, 2
cf(u,v) = c(u,v) – f(u,v) + f(v,u) s b t
2
Find a path from s to t in Gf
1 1
Augment f along this path
c e
1
Gf (before) 2 Gf (after) 2
a d a d
1 1 1 1
2 2
2 2
2 4 2 4
8 1 8 1
s b t s b t
2 2
1 3 1
4 1
1 c e
c e
6 5
Example: Round 3
f 2
Construct residual graph Gf = (V, Ef) a d
Ef ✓ E U ER 2 4
For any (u,v) in E or ER, 2
cf(u,v) = c(u,v) – f(u,v) + f(v,u) s b t
2
Find a path from s to t in Gf
1 1
Augment f along this path
c e
1
G 2 Gf (after) 2
a d a d
3 5 1 1
2 2
2
2 2 4
1 8 1
s b t s b t
10 2
1 3 1
4 1
1 c e
c e
6 5
Analysis: Correctness
FF algorithm gives us a valid flow. But is it the maximum possible flow?
!
Consider final residual graph Gf = (V, Ef)
Let L = nodes reachable from s in Gf and let R = rest of nodes = V – L
So s 2L and t 2R
L R
x y
s w t
z
Edges from L to R must be at full capacity Recall: for any flow and any cut,
Edges from R to L must be empty size(flow) <= capacity(cut)
Therefore, flow across cut (L,R) is !
X
c(u, v) Therefore f is the max flow and (L,R) is
(u,v)2E,u2L,v2R the min cut! Cuts
Thus, size(f) = capacity(L,R) Thus, Max Flow = Min Cut
Flows
Analysis: efficiency
FF Algorithm: Start with zero flow How many iterations are needed to reach
Repeat: the maximum flow?
Find a path from s to t along which Example: a
10
6 6 10
flow can be increased 1
Increase the flow along that path s t
106 b 106
A hillclimbing procedure 1 a
! max flow 1
s t
Flow size:
1
b
a 1
s 1 t
0
1 b
Each iteration is fast (O(|E|) time).
#iterations can be Max Capacity
Analysis: efficiency
s 1 t
106 b 106
0
#iterations can be Max Capacity (with
Each iteration is fast (O(|E|) time).
integer capacities)
!
An Observation: Integrality
Integral Flows: A flow f is integral if f(e) is an integer for all e
Example:
1/1 a 1/1 0.5/1 a 0.5/1
1/1 1/1
s v t s v t
Property: If all edge capacities are integers, then, there is a max flow f which is integral.
Proof: If the edge capacities are integers, then, the FF algorithm always finds an integral flow
The FF algorithm also always finds a max flow.
!
• Ford-Fulkerson Style Algorithms:
• Edmonds Karp
• Capacity Scaling
!
• Preflow-Push
!
Edmonds Karp
!
Bad Example for FF:
FF Algorithm: Start with zero flow
Repeat: 106 a 106
Find a path from s to t along which s 1 t
flow can be increased
Increase the flow along that path 106 b 106
!
Bad Path Sequence:
(s, a, b, t), (s, b, a, t), (s, a, b, t),...
EK Path Selection: Find the shortest path along which flow can be increased
(shortest path = shortest in terms of #edges)
It can be shown that this requires only O(|V||E|) iterations (Proof not in this class)
Running Time: O(|V| |E|2)
Edmonds Karp
!
EK Algorithm: Start with zero flow Bad Example for FF:
Repeat: 106 a 106
Find the shortest path from s to t
along which flow can be increased s 1 t
Increase the flow along that path 106 106
b
! !
f 106 a Gf 106 a 106
! 106
Iteration 1 s t s 1 t
b 106 b 106
! !
f Gf
a 106 a 106
!
Iteration 2 s t s 1 t
106 106 106 106
b b
How to improve the efficiency?
!
• Ford-Fulkerson Style Algorithms:
• Edmonds Karp
• Capacity Scaling
!
• Preflow-Push
Capacity Scaling
!
Bad Example:
D-Residual Graph: Subgraph of residual graph with only edges with capacity >= D
!
Example: For
! f=0 !
G 106 a 106 Gf(10) 106 a 106
s 1 t s t
D-Residual Graph: Subgraph of residual graph with only edges with capacity >= D
Property: If all edge capacities are integers, algorithm outputs a max flow
Proof: At D=1, Gf(D) = Gf. So on termination, Gf(D) has no more paths from s to t
Capacity Scaling: Running Time
Cmax = max capacity edge. Start with D = Cmax
Start with zero flow
1 While D >= 1, repeat:
Gf(D) = D-residual graph D scaling phase
2
While there is a path from s to t in Gf(D) along which flow can be increased
Increase flow along that path
Update Gf(D)
D = D/2
D-Residual Graph: Subgraph of residual graph with only edges with capacity >= D
Property 1: While loop 1 is executed 1 + log2 Cmax times
Property 2: At the end of a D-scaling phase, size(max flow) <= size(current flow) + D|E|
Proof: Let L = nodes reachable from s in Gf(D) and let R = rest of nodes = V – L max
#edges in Gf(D) in the (L, R) cut = 0 flow
#edges in G in the (L,R) cut <= |E| D|E|
s t f
Capacity of each such edge < D curr-
L R Thus, size(max flow) <= capacity(L,R) <= size(f) + D|E| ent
Capacity Scaling: Running Time
Cmax = max capacity edge. Start with D = Cmax
Start with zero flow
1 While D >= 1, repeat:
Gf(D) = D-residual graph D scaling phase
2
While there is a path from s to t in Gf(D) along which flow can be increased
Increase flow along that path
Update Gf(D)
D = D/2
D-Residual Graph: Subgraph of residual graph with only edges with capacity >= D
Property 1: While loop 1 is executed 1 + log2 Cmax times
Property 2: At the end of a D-scaling phase, size(max flow) <= size(current flow) + D|E|
Property 3: For any D, #iterations of loop 2 in the D-scaling phase <= 2|E|
!
• Ford-Fulkerson Style Algorithms:
• Edmonds Karp
• Capacity Scaling
!
• Preflow-Push
!
Preflow-Push
Main Idea:
- Each node has a label, which is a potential
- Route flow from high to low potential
v
Labels
w
Example excess = 1
1 a 1 1 a 0
G 1 0
s t f s t
1 1 1 0
b b
excess = 1
Preflow-Push: Two Operations
Preflow: A function f: E ! R is a preflow if:
1. Capacity Constraints: 0 <= f(e) <= c(e)
2. Instead of conservation constraints:
X X
f (e) f (e) 0
e into v
X
e out of v
X v
Excess(v) = f (e) f (e) h
e into v e out of v
w
Relabel(v): Applies if excess(v) > 0, for all w s.t (v, w) in Ef, h(w) >= h(v)
Increase h(v) by 1
Pre-Flow Push: The Algorithm
G f, h 06 Gf
6 a 5 6 a 6 a 5
s 1 t 04 s t 00 s 1 t
6 b 5 6 b 6 b 5
06
Labels
Excesses
Pre-Flow Push: An Example
Gf (before) f, h Gf
16
6 a 5 6 a 6 a 5
s 1 t 04 s t 00 s 1 t
6 b 5 6 b 6 5
b
06
Labels
Excesses
Pre-Flow Push: An Example
Gf (before) f, h Gf
11
6 a 5 6 a 6 a 5
5
s 1 t 04 s t 05 s 1 t
6 b 5 6 b 6 b 5
06
Labels
Excesses
Pre-Flow Push: An Example
Gf (before) f, h Gf
10
6 a 5 6 a 6 a 5
5
s 1 t 04 s 1 t 05 s 1 t
6 b 5 6 b 6 b 5
07
Labels
Excesses
Pre-Flow Push: An Example
Gf (before) f, h Gf
10
6 a 5 6 a 6 a 5
5
s 1 t 04 s 1 t 05 s 1 t
6 b 5 6 b 6 b 5
17
Labels
Excesses
Pre-Flow Push: An Example
Gf (before) f, h Gf
10
6 a 5 6 a 6 a 5
5
s 1 t 04 s 1 t 0 10 s 1 t
6 b 5 6 b 5 6 b 5
12
Labels
Excesses
Pre-Flow Push: An Example
Gf (before) f, h Gf
10
6 a 5 6 a 6 a 5
5
s 1 t 04 s 1 t 0 10 s 1 t
6 b 5 6 b 5 6 b 5
22
Labels
Excesses
Pre-Flow Push: An Example
Gf (before) f, h Gf
11
6 a 5 6 a 6 a 5
5
s 1 t 04 s t 0 10 s 1 t
6 b 5 6 b 5 6 b 5
21
Labels
Excesses
Pre-Flow Push: An Example
Gf (before) f, h Gf
11
6 a 5 6 a 6 a 5
5
s 1 t 04 s t 0 10 s 1 t
6 b 5 6 5 6 5
b b
31
Labels
Excesses
Pre-Flow Push: An Example
Gf (before) f, h Gf
11
6 a 5 6 a 6 a 5
5
s 1 t 04 s t 0 10 s 1 t
6 b 5 6 5 6 5
b b
41
Labels
Excesses
Pre-Flow Push: An Example
Gf (before) f, h Gf
11
6 a 5 6 a 6 a 5
5
s 1 t 04 s t 0 10 s 1 t
6 b 5 6 5 6 5
b b
51
Labels
Excesses
Pre-Flow Push: An Example
Gf (before) f, h Gf
11
6 a 5 6 a 6 a 5
5
s 1 t 04 s t 0 10 s 1 t
1
6 b 5 5 b 5 5 b 5
50
Labels
Excesses
Pre-Flow Push: An Example
Gf (before) f, h Gf
21
6 a 5 6 a 6 a 5
5
s 1 t 04 s t 0 10 s 1 t
1 1
5 b 5 5 b 5 5 b 5
50
Labels
Excesses
Pre-Flow Push: An Example
Gf (before) f, h Gf
31
6 a 5 6 a 6 a 5
5
s 1 t 04 s t 0 10 s 1 t
1 1
5 b 5 5 b 5 5 b 5
50
Labels
Excesses
Pre-Flow Push: An Example
Gf (before) f, h Gf
41
6 a 5 6 a 6 a 5
5
s 1 t 04 s t 0 10 s 1 t
1 1
5 b 5 5 b 5 5 b 5
50
Labels
Excesses
Pre-Flow Push: An Example
Gf (before) f, h Gf
51
6 a 5 6 a 6 a 5
5
s 1 t 04 s t 0 10 s 1 t
1 1
5 b 5 5 b 5 5 b 5
50
Labels
Excesses
Pre-Flow Push: An Example
Gf (before) f, h Gf
50
6 a 5 5 a 5 a 5
5
1 1 1
s t 04 s t 0 10 s t
1 1
5 b 5 5 b 5 5 b 5
50
Labels
Excesses
Pre-Flow Push
• Algorithm
• Correctness
• Running Time Analysis
Correctness: Proof Outline
Three Steps:
- Compatibility: Show that the preflow f and the labeling h maintained by the
algorithm always obeys a compatibility property
!
- If a flow f is compatible with some labeling, then f is a max-flow
Invariant: Preflow f and labeling h are always compatible over the Preflow-Push algorithm
Three Steps:
- Compatibility: Show that the preflow f and the labeling h maintained by the
algorithm always obeys a compatibility property
!
- If a flow f is compatible with some labeling, then f is a max-flow
3
Property 2: If flow f and labeling h are 2 lnvalid
compatible, then f is a max flow
1 v
Proof: From Property 1 and properties w t
of max flow 0
Correctness: Proof Outline
Three Steps:
- Compatibility: Show that the preflow f and the labeling h maintained by the
algorithm always obeys a compatibility property
!
- If a flow f is compatible with some labeling, then f is a max-flow
Three Steps:
- Compatibility: Show that the preflow f and the labeling h maintained by the
algorithm always obeys a compatibility property
!
- If a flow f is compatible with some labeling, then f is a max-flow
• Algorithm
• Correctness
• Running Time Analysis
Running Time Analysis: Outline
Compatibility of f and h:
Property 1: In a preflow f, if excess(v) > 0, then 1. h(s) = n, h(t) = 0
there is a path from v to s in Gf 2. For all edges (v, w) in Gf,
Property 2: At any point, for any v, h(v) <= 2n - 1
Proof: If excess(v) > 0, there is a v-s path in Gf If excess(v) = 0, then h(v) has not changed
Let v = v1, ..., vk = s be the path since the last time v had excess > 0
By compatibility: !
h(s) = n, h(vk-1) <= n + 1,h(v1) <= n+k-1 <= 2n - 1 Thus, h(v) <= 2n - 1 also
! .....
! v=v1 v2 v3 vk-1 s= vk
Preflow Push: #Relabels
Start with labeling:h(s) = n,h(t) = 0,h(v) = 0, for other v Push(v, w):
Start with preflow f: f(e) = c(e) for e = (s, v), f(e) = 0, ow Applies if excess(v) > 0, h(w) < h(v)
! q = min(excess(v), cf(v,w))
While there is a node (other than t) with positive excess Add q to f(v, w)
Pick a node v with excess(v) > 0
If there is an edge (v, w) in Ef s. t. push(v, w) applies Relabel(v):
Push(v, w) Applies if excess(v) > 0 and for all
Else w s.t (v, w) in Ef, h(w) >= h(v)
Relabel(v) Increase h(v) by 1
Label Lists
1. For each label, use a list to maintain nodes with excess > 0 h=0
Time to select a v with excess(v) > 0: O(1)
Time to insert or delete: O(1) h=1
!
2. For each v, maintain all (v,w) in Ef in an adjacency list
....
Keep a pointer P(v) to the next edge we can push on
If excess(v) = 0, P(v) stays on the current edge P(v)
Move P(v) by 1 when current edge is saturated
[Recall: If we push(v,w) and saturate it, then, we cannot adj-list(v)
push(v,w) again until v is relabeled]
Update P(v) and the list when v is relabeled
Preflow Push: Data Structures
Start with labeling:h(s) = n,h(t) = 0,h(v) = 0, for other v Push(v, w):
Start with preflow f: f(e) = c(e) for e = (s, v), f(e) = 0, ow Applies if excess(v) > 0, h(w) < h(v)
! q = min(excess(v), cf(v,w))
While there is a node (other than t) with positive excess Add q to f(v, w)
Pick a node v with excess(v) > 0
If there is an edge (v, w) in Ef s. t. push(v, w) applies Relabel(v):
Push(v, w) Applies if excess(v) > 0 and for all
Else w s.t (v, w) in Ef, h(w) >= h(v)
Relabel(v) Increase h(v) by 1
1. For each label, use a list to maintain nodes with excess > 0 Time per relabel = O(1)
Time to select a v with excess(v) > 0: O(1)
Time per push = O(1)
Time to insert or delete: O(1)
! Time to maintain list after
2. For each v, maintain all (v,w) in Ef in an adjacency list
relabeling v = O(deg(v))
Keep a pointer P(v) to the next edge we can push on
If excess(v) = 0, P(v) stays on the current edge Total running time
Move P(v) by 1 when current edge is saturated = O(m) x #relabels/node +
[Recall: If we push(v,w) and saturate it, then, we cannot O(#pushes + #relabels)
push(v,w) again until v is relabeled] = O(mn) + O(mn2) = O(mn2)
Update P(v) and the list when v is relabeled !
Running Time Analysis: Outline