10 KiB
Lecture 4
Maximum Flow
Example 1: Ship cement from factory to building
Input s: source, t: destination
Graph with directed edges weights on each edge: capacity
Goal: Ship as much stuff as possible while obeying capacity constrains.
Graph: (V,E) directed and weighted
- Unique source and sink nodes
\to s, t - Each edge has capacity
c(e)[Integer]
A valid flow assignment assigns an integer f(e) to each edge s.t.
Capacity constraint: 0\leq f(e)\leq c(e)
Flow conservation:
\sum_{e\in E_{in}(v)}f(e)=\sum_{e\in E_{out}(v)}f(e),\forall v\in V-{s,t}
E_{in}(v): set of incoming edges to v
E_{out}(v): set of outgoing edges from v
Compute: Maximum Flow: Find a valid flow assignment to
Maximize |F|=\sum_{e\in E_{in}(t)}f(e)=\sum_{e\in E_{out}(s)}f(e) (total units received by end and sent by source)
Additional assumptions
shas no incoming edges,thas no outgoing edges- You do not have a cycle of 2 nodes
A proposed algorithm:
- Find a path from
stot - Push as much flow along the path as possible
- Adjust the capacities
- Repeat until we cannot find a path
Residual Graph: If there is an edge e=(u,v) in G, we will add a back edge \bar{e}=(v,u). Capacity of \bar{e}= flow on e. Call this graph G_R.
Algorithm:
- Find an "augmenting path"
P.Pcan contain forward or backward edges!
- Say the smallest residual capacity along the path is
k. - Push
kflow on the path (f(e) =f(e) + kfor all edges on pathP)- Reduce the capacity of all edges on the path
Pbyk - Increase the capacity of the corresponding mirror/back edges
- Reduce the capacity of all edges on the path
- Repeat until there are no augmenting paths
Formalize: Ford-Fulkerson (FF) Algorithm
- Initialize the residual graph
G_R=G - Find an augmenting path
Pwith capacityk(min capacity of any edge onP) - Fix up the residual capacities in
G_Rc(e)=c(e)-k,\forall e\in Pc(\bar{e})=c(\bar{e})+k,\forall \bar{e}\in P
- Repeat 2 and 3 until no augmenting path can be found in
G_R.
def ford_fulkerson_algo(G,n,s,t):
"""
Args:
G: is the graph for max_flow
n: is the number of vertex in the graph
s: start vertex of flow
t: end vertex of flow
Returns:
the max flow in graph from s to t
"""
# Initialize the residual graph $G_R=G$
GR=[defaultdict(int) for i in range(n)]
for i in range(n):
for v,_ in enumerate(G[i]):
# weight w is unused
GR[v][i]=0
path=set()
def augP(cur):
# Find an augumentting path $P$ with capacity $k$ (min capacity of any edge on $P$)
if cur==t: return True
# true for edge in residual path, false for edge in graph
for v,w in G[cur]:
if w==0 or (cur,v,False) in path: continue
path.add((cur,v,False))
if augP(v): return True
path.remove((cur,v,False))
for v,w in GR[cur]:
if w==0 or (cur,v,True) in path: continue
path.add((cur,v,True))
if augP(v): return True
path.remove((cur,v,True))
return False
while augP(s):
k=min([GR[a][b] if isR else G[a][b] for a,b,isR in path])
# Fix up the residual capacities in $G_R$
# - $c(e)=c(e)-k,\forall e\in P$
# - $c(\bar{e})=c(\bar{e})+k,\forall \bar{e}\in P$
for a,b,isR in path:
if isR:
GR[a][b]+=k
else:
G[a][b]-=k
return sum(GR[s].values())
Proof of Correctness: Valid Flow
Lemma 1: FF finds a valid flow
- Capacity and conservation constrains are not violated
- Capacity constraint:
0\leq f(e)\leq c(e) - Flow conservation:
\sum_{e\in E_{in}(v)}f(e)=\sum_{e\in E_{out}(v)}f(e),\forall v\in V-\{s,t\}
Proof: We proceed by induction on augmenting paths
Base Case
f(e)=0 on all edges
Inductive Case
By inductive hypothesis, we have a valid flow and the corresponding residual graph G_R.
Inductive Step:
Now we find an augmented path P in GR, pushed k (which is the smallest edge capacity on P). Argue that the constraints are not violated.
Capacity Constrains: Consider an edge e in P.
- If
eis an forward edge (in the original graph)- by construction of
G_R, it had left over capacities.
- by construction of
- If
eis an back edge with residual capacity\geq k- flow on real edge reduces, but the real capacity is still
\geq 0, no capacity constrains violation.
- flow on real edge reduces, but the real capacity is still
Conservation Constrains: Consider a vertex v on path P
- Both forward edges
- No violation, push
kflow intovand out.
- No violation, push
- Both back edges
- No violation, push
kless flow intovand out.
- No violation, push
- Redirecting flow
- No violation, change of
0byk-konv.
- No violation, change of
Proof of Correctness: Termination
Lemma 2: FF terminate
Proof:
Every time it finds an augmenting path that increases the total flow.
Must terminate either when it finds a max flow or before.
Each iteration we use \Theta(m+n) to find a valid path.
The number of iteration \leq |F|, the total is \Theta(|F|(m+n)) (not polynomial time)
Proof of Correctness: Optimality
From Lemma 1 and 2, we know that FF returns a feasible solution, but does it return the maximum flow?
Max-flow Min-cut Theorem
Given a graph G(V,E), a graph cut is a partition of vertices into 2 subsets.
S:s+ maybe some other verticesV-S:t+ maybe some other vertices
Define capacity of the cut be the sum of capacity of edges that go from a vertex in S to a vertex in T.
Lemma 3: For all valid flows f, |f|\leq C(S) for all cut S (Max-flow \leq Min-cut)
Proof: all flow must go through one of the cut edges.
Min-cut: cut of smallest capacity, S^*. |f|\leq C(S^*)
Lemma 4: FF produces a flow =C(S^*)
Proof: Let \hat{f} be the flow found by FF. Mo augmenting paths in G_R.
Let \hat{S} be all vertices that can be reached from s using edges with capacities >0.
and all the forward edges going out of the cut are saturated. Since back edges have capacity 0, no flow is going into the cut S.
If some flow was coming from V-\hat{S}, then there must be some edges with capacity >0. So, |f|\leq C(S^*)
Example 2: Bipartite Matching
input: Given n classes and n rooms; we want to match classes to rooms.
Bipartite graph G=(V,E) (unweighted and undirected)
- Vertices are either in set
LorR - Edges only go between vertices of different sets
Matching: A subset of edges M\subseteq E s.t.
- Each vertex has at most one edge from
Mincident on it.
Maximum Matching: matching of the largest size.
We will reduce the problem to the problem of finding the maximum flow
Reduction
Given a bipartite graph G=(V,E), construct a graph G'=(V',E') such that
|max-flow (G')|=|max-flow(G)|
Let s connects to all vertices in L and all vertex in R connects to t.
$G'=G+s+t+$added edges form S to T and added capacities.
Proof of correctness
Claim: G' has a flow of k iff G has a matching of size k
Proof: Two directions:
- Say
Ghas a matching of sizek, we want to proveG'has a flow of sizek. - Say
G'has a flow of sizek, we want to proveGhas a matching of sizek.
Conclusion: Maximum Flow
Problem input and target
Ford-Fulkerson Algorithm
- Execution: residual graph
- Runtime
FF correctness proof
- Max-flow Min-cut Theorem
- Graph Cut definition
- Capacity of cut
Reduction to Bipartite Matching
Example 3: Image Segmentation: (reduction from min-cut)
Given:
- Image consisting of an object and a background.
- the object occupies some set of pixels
A, while the background occupies the remaining pixelsB.
Required:
- Separate
AfromBbut if doesn't know which pixels are each. - For each pixel
i,p_iis the probability thati\in A - For each pair of adjacent pixels
i,j,c_{ij}is the cost of placing the object boundary between them. i.e. puttingiinAandjinB. - A segmentation of the image is an assignment of each pixel to
AorB. - The goal is to find a segmentation that maximizes
\sum_{i\in A}p_i+\sum_{i\in B}(1-p_i)-\sum_{i,j\ on \ boundary}c_{ij}
Solution:
- Let's turn our maximization into a minimization
- If the image has
Npixels, then we can rewrite the objective as
N-\sum_{i\in A}(1-p_i)-\sum_{i\in B}p_i-\sum_{i,j\ on \ boundary}c_{ij}
because N=\sum_{i\in A}p_i+\sum_{i\in A}(1-p_i)+\sum_{i\in B}p_i+\sum_{i\in B}(1-p_i) boundary
New maximization problem:
Max\left( N-\sum_{i\in A}(1-p_i)-\sum_{i\in B}p_i-\sum_{i,j\ on \ boundary}c_{ij}\right)
Now, this is equivalent ot minimizing
\sum_{i\in A}(1-p_i)+\sum_{i\in B}p_i+\sum_{i,j\ on \ boundary}c_{ij}
Second steps
- Form a graph with
nvertices,v_ion for each pixel - Add vertices
sandt - For each
v_i, add edgesS-Tcut ofGassigned eachv_ito eitherSside orTside. - The
Sside of anS-Tis theAside, while theTside of the cur is theBside. - Observer that if
v_igoes on theSside, it becomes part ofA, so the cut increases by1-p. Otherwise, it become part ofB, so the cut increases byp_iinstead. - Now add edges
v_i\to v_jwith capacityc_{ij}for all adjacent pixels pairsi,j - If
v_iandv_jend up on opposite sides of the cut (boundary), then the cut increases byc_{ij}. - Conclude that any
S-Tcut that assignsS\subseteq Vto theAside andV\backslash Sto theBside pays a total of1-p_ifor eachv_ion theAsidep_ifor eachv_ion theBsidec_{ij}for each adjacent pairi,jthat is at the boundary. i.e.i\in S\ and\ j\in V\backslash S
- Conclude that a cut with a capacity
cimplies a segmentation with objective valuecs. - The converse can (and should) be also checked: a segmentation with subjective value
cimplies aS-Tcut with capacityc.
Algorithm
- Given an image with
Npixels, build the graphGas desired. - Use the FF algorithm to find a minimum
S-Tcut ofG - Use this cut to assign each pixel to
AorBas described, i.e pixels that correspond to vertices on theSside are assigned toAand those corresponding to vertices on theTside toB. - Minimizing the cut capacity minimizes our transformed minimization objective function.
Running time
The graph G contains \Theta(N) edges, because each pixel is adjacent to a maximum of of 4 neighbors and S and T.
FF algorithm has running time O((m+n)|F|), where |F|\leq |n| is the size of set of min-cut. The edge count is m=6n.
So the total running time is O(n^2)