0% found this document useful (0 votes)
23 views

Max Flow

The document describes the max flow problem and the Ford-Fulkerson algorithm for finding the maximum flow in a flow network. The max flow problem involves finding the maximum amount of flow that can be sent from a source node s to a sink node t in a flow network with edge capacities. The Ford-Fulkerson algorithm iteratively finds augmenting paths in the residual graph to incrementally increase the flow from s to t until no more augmenting paths exist.

Uploaded by

ballechase
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Max Flow

The document describes the max flow problem and the Ford-Fulkerson algorithm for finding the maximum flow in a flow network. The max flow problem involves finding the maximum amount of flow that can be sent from a source node s to a sink node t in a flow network with edge capacities. The Ford-Fulkerson algorithm iteratively finds augmenting paths in the residual graph to incrementally increase the flow from s to t until no more augmenting paths exist.

Uploaded by

ballechase
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 74

CSE 202: Design and

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?

An s-t flow is a function: E !R such that:


- 0 <= f(e) <= c(e), for all edges e
- flow into node v = flow out of node v, for all nodes v except s and t,
X X
f (e) = f (e)
e into v e out of v

Size of flow f = Total flow out of s = total flow into 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

s 1 t Then we’d have to allow:

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

Find a path from s to t along which flow s 1 t


can be increased 1
1
Increase the flow along that path b

In any iteration, we have some flow f and we f: 1 a


are trying to improve it. How to do this?
s 1 t
1: Construct a residual graph Gf (“what’s left to take?”)
Gf = (V, Ef) where Ef ✓ E U ER 1
b
For any (u,v) in E, cf(u,v) = c(u,v) – f(u,v)
any (u,v) in ER, cf(u,v) = f(v,u)
Gf : 1 a 1
[ignore edges with zero cf: don’t put them in Ef]
s 1 t
2: Find a path from s to t in Gf
3: Increase flow along this path, as much as possible 1 1
b
Example: Round 1
f 2
Construct residual graph Gf = (V, Ef) a d
Ef ✓ E U ER 2 2
For any (u,v) in E or ER,
cf(u,v) = c(u,v) – f(u,v) + f(v,u) s b t
Find a path from s to t in Gf
Augment f along this path
c e

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

FF Algorithm: Start with zero flow


Repeat:
Find a path from s to t along which flow can be increased
Increase the flow along that path

A hillclimbing procedure How many iterations are needed to reach


! max flow
the maximum flow?
Flow size: Example:
106 a 106

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

0/1 b 0/1 0.5/1 b 0.5/1

An Integral Flow A Fractional Flow

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.

Note: All max flows are not necessarily integral flows!


How to improve the efficiency?

!
• 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:

FF Algorithm: Start with zero flow 106 a 106


Repeat: s 1 t
Find a path from s to t along which
flow can be increased !
106 b 106
Increase the flow along that path
Bad Path Sequence:
(s, a, b, t), (s, b, a, t), (s, a, b, t),...

Capacity Scaling: Find paths of high capacity first between s and t


Capacity Scaling
Cmax = max capacity edge. Start with D = Cmax
Start with zero flow
While D >= 1, repeat:
Gf(D) = D-residual graph

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
!
Example: For
! f=0 !
G 106 a 106 Gf(10) 106 a 106

s 1 t s t

106 b 106 106 b 106


Capacity Scaling: Correctness
Cmax = max capacity edge. Start with D = Cmax
Start with zero flow
While D >= 1, repeat:
Gf(D) = D-residual graph

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: 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|

Total Running Time: O(|E|2(1 + log2 Cmax))


( Recall: Time to find a flow path in a residual graph = O(|E|) )
How to improve the efficiency?

!
• 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

Idea: Route flow along blue edges


Preflows

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 e out of v
X X
Excess(v) = f (e) f (e)
e into v e out of v

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

Labeling h assigns a non-negative integer


label h(v) to all v in V

Push(v, w): Applies if excess(v) > 0, h(w) < h(v), (v, w) in Ef


q = min(excess(v), cf(v,w))
Add q to f(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

Start with labeling: h(s) = n, h(t) = 0, h(v) = 0, for all other v


Start with preflow f: f(e) = c(e) for e = (s, v), f(e) = 0, for all other edges e
!
While there is a node (other than t) with positive excess
Pick a node v with excess(v) > 0
If there is an edge (v, w) in Ef such that push(v, w) can be applied
Push(v, w)
Else
Relabel(v)

Push(v, w): Applies if excess(v) > 0, h(w) < h(v), (v, w) in Ef


q = min(excess(v), cf(v,w))
Add q to f(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: An Example

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

- Preflow-push outputs a flow on termination


Correctness: Compatible Pre-Flows

Preflow: A function f: E !R is a preflow if: 4 s


1. Capacity Constraints: 0 <= f(e) <= c(e)
2. Instead of X
conservationX
constraints: 3
f (e) f (e) 0 2 lnvalid
e into v e out of v
X X 1 v
Excess(v) = f (e) f (e)
w t
e into v e out of v 0

Preflow f and labeling h are compatible if:


1. h(s) = n, h(t) = 0
2. For all edges (v, w) in the residual graph Gf, h(v) <= h(w) + 1
PreFlow Push: Correctness

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

Preflow f and labeling l are compatible if:


1. h(s) = n, h(t) = 0 2. For all edges (v, w) in Gf, h(v) <= h(w) + 1

Invariant: Preflow f and labeling h are always compatible over the Preflow-Push algorithm

Proof: By induction. Initially, compatible, as Gf has no (s, v) edges 4 s


Suppose f and h are compatible at time t. At time t+1: 3
- Relabel: Labels increase only if no downward edges in Gf 2 lnvalid
- Push: Edges in Gf may be reversed. If so, as we push from v
1
high to low h a downwards edge will become an upwards edge
0 w t
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

- Preflow-push outputs a flow on termination


Properties of Compatible PreFlows
Preflow f and labeling h are compatible if:
1. h(s) = n, h(t) = 0
2. For all edges (v, w) in the residual graph Gf, h(v) <= h(w) + 1

Property 1: If preflow f and labeling h are


compatible, then there is no s-t path in Gf

Proof: Suppose there is an s-t path in Gf s


4
.....
3
s v1 v2 v3 vk-1 t= vk
2 lnvalid
Due to compatibility,
1 v
h(v1) >= h(s) - 1 = n - 1
h(v2) >= h(v1) - 1 >= n - 2 w t
0
...
h(t) = h(vk) - 1 >= n - k > 0 (as k < n)
Contradiction!
Properties of Compatible PreFlows

Preflow f and labeling h are compatible if:


1. h(s) = n, h(t) = 0
2. For all edges (v, w) in the residual graph Gf, h(v) <= h(w) + 1

Property 1: If preflow f and labeling h are


compatible, then there is no s-t path in Gf 4 s

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

- Preflow-push outputs a flow on termination


PreFlow Push: Correctness

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

Invariant: Preflow f and labeling h are always


compatible over the Preflow-Push algorithm Preflow f and labeling h are compatible if:
1. h(s) = n, h(t) = 0
Fact: When Preflow-push stops, f is a flow 2. For all edges (v, w) in Gf, h(v) <= h(w) + 1
Proof: Why does Preflow-push stop?
Property 1: If preflow f and labeling h are
- No valid push or relabel operation:
compatible, then there is no s-t path in Gf
We can always relabel or push if excess(v) > 0
for some v Property 2: If flow f and labeling h are
- No node v with excess(v) > 0: compatible, then f is a max flow
Then f is a flow!
PreFlow Push: Correctness

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

Preflow f and labeling h are compatible if:


Invariant: Preflow f and labeling h are always 1. h(s) = n, h(t) = 0
compatible over the Preflow-Push algorithm 2. For all edges (v, w) in Gf, h(v) <= h(w) + 1
Fact: When Preflow-push stops, f is a flow Property 1: If preflow f and labeling h are
From Property 2 of compatible flows, and compatible, then there is no s-t path in Gf
Invariant, f is a max flow Property 2: If flow f and labeling h are
Thus, Preflow-Push correctly outputs a maxflow compatible, then f is a max flow
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

- Preflow-push outputs a flow on termination


Pre-Flow Push

• Algorithm
• Correctness
• Running Time Analysis
Running Time Analysis: Outline

1. How many Push Ops? Relabel Ops?

2. How to implement Push and Relabel Ops efficiently?


Running Time Analysis: Outline

1. How many Relabel Ops?


!
Main Idea: Bound the maximum value of h(v) for any node v, and bound
#relabel ops through this
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

Property 1: In a preflow f, if excess(v) > 0, then Now, total excess of nodes in B =


X X X X
there is a path from v to s in Gf f (e) f (e) 0
v2B e into B v2B e out of B
x y Three types of edges e in the sum:
s
1. Both endpoints of e are in B: f(e) cancels out
A B 2. e = (u, v), u in A, v in B: f(e) = 0
A = all nodes v s.t. s is reachable from v in Gf 3. e = (v, u), u in A, v in B
XX
B = remaining nodes Total excess of nodes in B: f (v, u) 0
v2B u2A
Fact: Any e=(x, y) from A to B has f(x,y) = 0
As excess(v) is never <0, excess(v)=0 for v in B
If not, (y, x) is in Gf, so there is a y - s path
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

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

Property 1: In a preflow f, if excess(v) > 0, then


there is a path from v to s in Gf

Property 2: At any point in the algorithm,


for any v, h(v) <= 2n - 1 v
h
Property 3: Any node can be relabeled at most
w
2n times in the algorithm
Proof: Labels never decrease, start at 0, increase by at
least 1 per relabel, and can only go upto 2n - 1
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

Property 1: In a preflow f, if excess(v) > 0, then


there is a path from v to s in Gf

Property 2: At any point in the algorithm,


for any v, h(v) <= 2n - 1 v
h
Property 3: Any node can be relabeled at most
w
2n times in the algorithm

Total #relabel operations = O(n2)


Running Time Analysis: Outline

1. How many Push Ops? Relabel Ops?

2. How to implement Push and Relabel Ops efficiently?


Running Time Analysis: Outline

1. How many Relabel Ops? How many Push Ops?


!
Two types of Push Ops:
Saturating Pushes: (v, w) is saturated after push(v, w)
Same edge can’t be pushed on until a relabel (we will see why!)
!
Non-saturating Pushes: excess(v) = 0 after push(v, w)
!
Preflow Push: #Pushes
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

Two kinds of Pushes:


Saturating: (v,w) is not in Gf after push Labels
Nonsaturating: excess(v) becomes 0 after push v

Property 1: There are at most 2mn saturating pushes


w
Proof: For a fixed edge (v,w), after a saturating push, we can only v w v
push along (v, w) again once v is relabeled
w Time
#relabels of v <= 2n
#saturating pushes along (v, w) <= 2n (v,w) disappears from Gf after saturating
#saturating pushes along all m edges <= 2nm push, appears only after w to v push
Running Time Analysis: Outline

1. How many Relabel Ops? How many Push Ops?


!
Two types of Push Ops:
Saturating Pushes: (v, w) is saturated after push(v, w)
Same edge can’t be pushed on until a relabel
!
Non-saturating Pushes: excess(v) = 0 after push(v, w)
Harder to bound. Need to use a potential function argument
Preflow Push: #Pushes

Two kinds of Pushes: Push(v, w):


Saturating: (v,w) is not in Gf after push Applies if excess(v) > 0, h(w) < h(v)
Nonsaturating: excess(v) becomes 0 after push q = min(excess(v), cf(v,w))
Add q to f(v, w)
Property 0: There are <= 2n2 relabels
Property 1: There are <= 2mn saturating pushes
Relabel(v):
Property 2: There are <= 4mn2 non-saturating pushes
Applies if excess(v) > 0 and for all
Proof: Define a potential function w s.t (v, w) in Ef, h(w) >= h(v)
X G(f, h):
G(f, h) = h(v) Increase h(v) by 1
v:excess(v)>0
Initially, G(f, h) = 0
At any time, G(f, h) >= 0 Total increase from relabels <= 2n2
At a relabel operation, G(f, h) can increase by 1
Total increase from saturating pushes
At a saturating push operation, G(f, h) can increase if w pushes <= 2mn(2n - 1)
gets >0 excess. Total increase = h(w) <= 2n - 1
(#non-saturating pushes) x 1
At a non-saturating push operation, G(f, h) will decrease <= Total decrease from such pushes
by h(v), but may increase by h(w) if w gets >0 excess <= total increase from anything else
But h(v) > h(w), so G(f, h) will decrease by at least 1 <= 2n2 + 2mn(2n - 1) = 4mn2
#Non-saturating Pushes <= 4mn2
Preflow Push: #Pushes
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

Two kinds of Pushes:


Saturating: (v,w) is not in Gf after push
Nonsaturating: excess(v) becomes 0 after push
Property 0: There are at most 2n2 relabels
Property 1: There are at most 2mn saturating pushes
Property 2: There are at most 4mn2 non-saturating pushes

Total #pushes: O(mn2)


Running Time Analysis: Outline

1. How many Push Ops? Relabel Ops?

2. How to implement Push and Relabel Ops efficiently?


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

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

1. How many Push Ops? Relabel Ops?


#pushes = O(mn2), #relabels = O(n2)
!
2. How to implement Push and Relabel Ops efficiently?
Data structure which takes: O(1) per push, O(deg(v)) to relabel v once
Total running time = O(mn2)

You might also like