Content
- Flow Network Overview
- Maximum-Flow Problem
- Residual Graph
- Implementation (Ford-Fulkerson Algorithm)
- Analysis
- Max-Flow Min-Cut Theorem
- Choosing Good Augmenting Paths
- Extensions to Max-Flow Problem
- Circulations with Demands
- Circulations with Demands & Lower Bounds
- Bipartite Matching
- Image Segmentation
- Project Selection
- Baseball Elimination
Flow Network Overview
- Components
- Capacities
- Source nodes
- Sink nodes
- Traffic
Graph representation
G = (V,E) edge ce = capacity where ce >= 0 source node s ∈ V sink node t ∈ V internal nodes = all other nodes
Assumptions
- No edge enters
s
& no edge leavest
- At least 1 edge incident to each node
c
is int
- No edge enters
- Flow conditions & notations
f(e)
satisfies:0 <= f(e) <= ce
for all edgessum_e_into_v(f(e)) = sum_e_outof_v(f(e))
for all internal nodes
v(f)
denotes amount of flow generated at sourcesum_e_outof_s(f(e))
f_out(v)
=sum_e_outof_v(f(e))
;f_in(v)
=sum_e_into_v(f(e))
Maximum-Flow Problem
Given a flow network, find a flow of max possible value.
Residual Graph
- A graph indicating additional possible flow. If there is a path from source to sink in residual graph, then it is possible to add flow.
- Every edge of a residual graph has a value
residual capacity = original capacity of the edge - current flow
. Residual capacity is basically the current capacity of the edge.
Implementation (Ford-Fulkerson Algorithm)
- Initial flow
f
= 0 - Find an augmenting path
P
froms
tot
- Augment the path
P
with flowf
, return new flowf'
- Let
b = bottleneck(P,f)
- For each edge
e = (u,v)
ofP
:- If forward edge,
f(e) += b
- If backward edge,
f(e=(v,u)) -= b
- If forward edge,
- return updated f
- Let
- Update
f
tof'
- Update residual graph
Gf
toGf'
- Node set the same
- For each edge
e = (u,v)
ofGf
,f(e) < ce
:- Push
e = (u,v)
with capacityce - f(e)
(forward edges) - Push
e' = (v,u)
with capacityf(e)
(backward edges)
- Push
- Repeat until no path found
augment(f,P):
Let b = bottleneck(P,f)
For each edge (u,v) ∈ P:
If e = (u,v) is a forward edge:
increase f(e) in G by b
Else: # ((u, v) is a backward edge, and let e = (v, u))
decrease f(e) in G by b
Return f
Max-Flow():
Initially f(e) = 0 for all e in G
While there is an s-t path in the residual graph Gf:
Let P be a simple s-t path in Gf
f' = augment(f,P)
Update f to be f'
Update the residual graph Gf to be Gf'
Return f
Analysis
- Termination
- At every intermediate stage of the Ford-Fulkerson Algorithm, the flow values {
f(e)
} and the residual capacities in Gf are integers. - The flow value strictly increases when we apply an augmentation, since we add
bottleneck > 0
. - Upper bound:
f_out(s)
. - Hence the algorithm runs for at most
f_out(s)
iterations.
- At every intermediate stage of the Ford-Fulkerson Algorithm, the flow values {
- Runtime
- Let
m = |E|
,n = |V|
,C = f_out(s)
m >= n/2
since all nodes have at least 1 incident edge, soO(m+n) = O(m)
- BFS/DFS:
O(m+n) = O(m)
augment
:O(n)
- Build new residual graph:
O(m)
- Overall
O(mC)
- Let
Max-Flow Min-Cut Theorem
In every flow network, the maximum value of an s-t flow is equal to the minimum capacity of an s-t cut.
- Cut
- Divide nodes into 2 sets
A
&B
s.t.s ∈ A
andt ∈ B
. Any flow going froms
tot
must cross fromA
intoB
at some point and use up some of the edge capacity fromA
toB
. - Cut puts bound on max flow
- Minimum cut: the minimum capacity of any division, which equals the max flow
- Divide nodes into 2 sets
Facts
Let
f
be any s-t flow, and(A, B)
any s-t cut. Then
v(f) = f_out(A) − f_in(A)
and
v(f) = f_in(B) − f_out(B)
v(f) = f_out(s) - f_in(s) = sum_v_in_A(f_out(v) - f_in(v)) # f_out(v) - f_in(v) = 0 for internal nodes = sum_e_outof_A(f(e)) - sum_e_into_A(f(e)) = f_out(A) - f_in(A) f_out(A) = f_in(B); f_in(A) = f_out(B)
Let
f
be any s-t flow, and(A,B)
any s-t cut. Then
v(f) <= c(A,B)
, wherec(A,B) = sum_e_outof_A(ce)
v(f) = f_out(A) − f_in(A) ≤ f_out(A) # f_in(A) >= 0 = sum_e_outof_A(f(e)) ≤ sum_e_outof_A(ce) = c(A,B)
=> Any flow is upper-bounded by the capacity of every cut
Let
f
be an s-t flow s.t. no s-t path in the residual graphGf
. Then there is an s-t cut(A*,B*)
inG
wherev(f) = c(A*,B*)
.Let A* be a set of nodes in G where there is a s-v path in Gf Let B* = V - A* v(f) = f_out(A*) − f_in(A*) = sum_e_outof_A*(f(e)) − sum_e_into_A*_f(e) = sum_e_outof_A*(ce) - 0 # f(e) = ce = c(A*,B*)
The flow
f
returned by Ford-Fulkerson is max flow.Let f* be max flow, (A*,B*) be min cut Then there exists a flow f s.t. v(f) = c(A,B) by 3. And by 2., v(f) = c(A,B) <= c(A*,B*) Hence A = A*, B = B* because c(A*,B*) should be minimum And by 2., v(f*) <= c(A*,B*) = v(f) Hence f = f* because v(f*) should be maximum
Given a max flow, we can compute an s-t min cut in
O(m)
time by constructing the residual graph and perform BFS/DFS to findA*
&B*
.- If all capacities in the flow network are integers, then there is a max flow
f
where every flow valuef(e)
is an integer.
Notes
- With rational numbers:
- Multiply all by least common multiple of all capacities
- With real numbers:
- May not terminate, since the progress we make at each iteration can be small
- Max-flow min-cut theorm still holds
Choosing Good Augmenting Paths
Idea 1
Find the path with large bottleneck capacity
.
Maintain a scaling parameter sp
and look for paths having bottleneck of at least sp
.
Implementation
Scaling Max-Flow(): Initially f(e) = 0 for all e in G Initially set sp = largest power of 2 <= max_e_outof_s(ce) While sp >= 1: While there is an s-t path in the graph Gf(sp): Let P be a simple s-t path in Gf(sp) f' = augment(f,P) Update f to be f' Update Gf(sp) sp /= 2 Return f
Runtime
- Outer
While
loop (scaling phase): at most1 + logC
whereC = sum_e_outof_s(ce)
- During the scaling phase, each augmentation increases the flow by at least
sp
v(f) >= max flow - m*sp
wheref = flow at the end of scaling phase
andm = |E|
f(e) > ce - sp for e = (u,v) which u ∈ A and v ∈ B f(e) ≥ sp for e = (u,v) which u ∈ B and v ∈ A v(f) = sum_e_outof_A(f(e)) − sum_e_into_A(f(e)) ≥ sum_e_outof_A(ce - sp) - sum_e_into_A(sp) = c(A,B) - m*sp
# of augmentations in a scaling phase is at most
2m
v(f*) <= v(f_prev) + m*sp = v(f_prev) + 2m*sp_prev Each augmentation increases the flow by >= sp_prev So at most 2m augmentations
Augmentation:
O(m)
time;1 + logC
scaling phases;2m
augmentations each phase =>O(m^2 logC)
- Scaling: polynomial in size of input (# of edges & numerical representation of capacities)
Original: polynomial in magnatude of capacities
- Outer
Idea 2
Choose path with fewest number of edges.
Edmond-Karp
- Runtime:
O(|V||E|^2)
Extensions to Max-Flow Problem
Circulations with Demands
Multiple sources & sinks with fixed supply/demand values.
- Demands
dv > 0
:v
wish to receivedv
more flow than it sends out (sink)dv < 0
:v
wish to send outdv
more flow than it receives (source)dv = 0
: not source nor sink
- Conditions
0 <= f(e) <= ce
for all edgessum_e_into_v(f(e)) = sum_e_outof_v(f(e))
for all internal nodes- All demands satisfied
- Total supply = total demand
sum_v(dv) = sum_v(f_in(v)) - sum_v(f_out(v)) = 0
sum_v_dv>0(dv) = sum_v_dv<0(-dv)
- Conversion to Max-Flow Problem
- Create super-source
s*
connecting each node inS
; create super-sinkt*
connecting each node inT
- For each node in
S
(dv < 0
), add(s*,v)
with capacity-dv
; for each node inT
(dv > 0
), add(v,t*)
with capacitydv
- A feasible circulation is found iff max s-t flow has value
D
, whereD = max capaxity from s* to t*
(saturating edges connected tos*
andt*
) - Graph G has a feasible circulation with demands
{dv}
if and only if for all cuts (A,B):
sum_v_in_B(dv) <= c(A,B)
- Create super-source
Circulations with Demands & Lower Bounds
Enforce flow to use certain edges - place lower bounds on edges.
- Conditions
le <= f(e) <= ce
for all edges, wherele
is the lower boundsum_e_into_v(f(e)) = sum_e_outof_v(f(e))
for all internal nodes- All demands satisfied
- Total supply = total demand
sum_v(dv) = sum_v(f_in(v)) - sum_v(f_out(v)) = 0
sum_v_dv>0(dv) = sum_v_dv<0(-dv)
- Conversion to Max-Flow Problem
- Let capacities of edges be
ce - le
- Let demands of nodes be
dv - Lv
, whereLv = sum_e_into_v(le) - sum_e_outof_v(le)
- There is a feasible circulation in
G
iff there is a feasible circulation inG'
, whereG
is the original graph,G'
is the converted one
- Let capacities of edges be
Bipartite Matching
- Max-flow problem property: if all edge capacities are integers, then the optimal flow found by our algorithm is integral.
- Idea
- Connect nodes from 1 set to
s
with capacity 1 - Connect nodes from another set to
t
with capacity 1 - Find max flow
- Connect nodes from 1 set to
Image Segmentation
Separate the foreground and background of an image.
Goal
- Let
ai
be likelihood to foreground for pixeli
- Let
bi
be likelihood to background for pixeli
- Let
pij
be separation penalty - Maximize
q(A,B) = sum_i_in_A(ai) + sum_j_in_B(bi) - sum_(i,j)_in_diff_set(pij)
- Let
3 problems
- The goal is seeking to maximize an objective
- Maximize
q(A,B) = sum_i_in_A(ai) + sum_j_in_B(bi) - sum_(i,j)_in_diff_set(pij)
<=> Minimizeq'(A,B) = sum_i_in_A(bi) + sum_j_in_B(ai) + sum_(i,j)_in_diff_set(pij)
- Maximize
- No source & sink
- Create super-source
s*
for foreground & super-sinkt*
for background
- Create super-source
- Undirected graph
- Model neighboring pair with 2 directed edges
- The goal is seeking to maximize an objective
Graph representation
Edges (s,j), where j ∈ B; capacity = aj Edges (i,t), where i ∈ A; capacity = bi Edges (i,j), where i ∈ A and j ∈ B; capacity = p_ij c(A,B) = q'(A,B)
Project Selection
A set of projects P
has revenue pi
being either positive or negative. Certain projects have prerequisites for other projects. Find a set of projects that maximizes the profits.
Graph representation
Edges (s,i), where pi > 0; capacity = pi Edges (i,t), where pi < 0; capacity = -pi Edges (i,j), where i depends on j; capacity = inf Max flow C = sum_i_pi>0(pi) Min cut (A',B'), where if i ∈ A has edge (i,j), j ∈ A Set of projects: A'-{s}, optimal
Optimality analysis
Prove the min cut in
G'
determines the optimum set of projects.- Capacity of cut
(A',B')
isc(A',B') = C − sum_i_in_A(pi)
- If
(A', B')
is a cut with capacity <= C, then setA = A' − {s}
satisfies the precedence constraints - Hence
c(A',B') = C - profit(A)
and small cuts imply big profits
- Capacity of cut
Baseball Elimination
A set of teams S
, each has wi
wins. Some games g_xy
left. Check if team z
has been eliminated.
Averaging argument
Suppose z has indeed been eliminated. Then: - z can finish with at most m wins. - There is a set of teams T ⊆ S s.t. sum_x_in_T(wx) + sum_x,y_in_T(gxy) > m|T| Hence one of the teams in T must end with > m wins.
Graph representation
Let u_xy be some games left between x & y Let m be w_z + remaining_games_of_z Let g* be total number of games left Edges (s,u_xy), capacity = g_xy Edges (u_xy,vx), capacity = inf Edges (vx,t), capacity = m - wx Eliminate x iff max flow in G < g* # else after g* games every team will have wins not exceeding m
Analysis
- If both
x
,y
belong toT
, thenu_xy ∈ A
(A, B)
is a min cut:u_xy ∈ A
iff bothx, y ∈ T
T
can be used in the averaging argumentc(A,B) = sum_x_in_T(m-wx) + sum_{x,y}_!in_T(g_xy) = m|T| - sum_x_in_T(wx) + (g* - sum_x,y_in_T(g_xy)) m|T| - sum_x_in_T(wx) - sum_x,y_in_T(g_xy) < 0 # c(A,B) = g' < g* sum_x_in_T(wx) + sum_x,y_in_T(g_xy) > m|T|
- If both