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 nodesAssumptions
- No edge enters
s& no edge leavest - At least 1 edge incident to each node
cis int
- No edge enters
- Flow conditions & notations
f(e)satisfies:0 <= f(e) <= cefor 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
Pfromstot - Augment the path
Pwith 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
ftof' - Update residual graph
GftoGf'- 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/2since 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&Bs.t.s ∈ Aandt ∈ B. Any flow going fromstotmust cross fromAintoBat some point and use up some of the edge capacity fromAtoB. - 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
fbe 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
fbe 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
fbe an s-t flow s.t. no s-t path in the residual graphGf. Then there is an s-t cut(A*,B*)inGwherev(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
freturned 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 maximumGiven 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
fwhere 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 fRuntime
- Outer
Whileloop (scaling phase): at most1 + logCwhereC = sum_e_outof_s(ce) - During the scaling phase, each augmentation increases the flow by at least
sp v(f) >= max flow - m*spwheref = flow at the end of scaling phaseandm = |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
2mv(f*) <= v(f_prev) + m*sp = v(f_prev) + 2m*sp_prev Each augmentation increases the flow by >= sp_prev So at most 2m augmentationsAugmentation:
O(m)time;1 + logCscaling phases;2maugmentations 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:vwish to receivedvmore flow than it sends out (sink)dv < 0:vwish to send outdvmore flow than it receives (source)dv = 0: not source nor sink
- Conditions
0 <= f(e) <= cefor 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) <= cefor all edges, whereleis 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
Giff there is a feasible circulation inG', whereGis 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
swith capacity 1 - Connect nodes from another set to
twith capacity 1 - Find max flow
- Connect nodes from 1 set to
Image Segmentation
Separate the foreground and background of an image.
Goal
- Let
aibe likelihood to foreground for pixeli - Let
bibe likelihood to background for pixeli - Let
pijbe 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}, optimalOptimality 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 mAnalysis
- If both
x,ybelong toT, thenu_xy ∈ A (A, B)is a min cut:u_xy ∈ Aiff bothx, y ∈ TTcan 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