Lecture 4
Maximum Flow
Example 1: Ship cement from factory to building
Input : source, : destination
Graph with directed edges weights on each edge: capacity
Goal: Ship as much stuff as possible while obeying capacity constrains.
Graph: directed and weighted
- Unique source and sink nodes
- Each edge has capacity [Integer]
A valid flow assignment assigns an integer to each edge s.t.
Capacity constraint:
Flow conservation:
: set of incoming edges to : set of outgoing edges from
Compute: Maximum Flow: Find a valid flow assignment to
Maximize (total units received by end and sent by source)
Additional assumptions
- has no incoming edges, has no outgoing edges
- You do not have a cycle of 2 nodes
A proposed algorithm:
- Find a path from to
- 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 in , we will add a back edge . Capacity of flow on . Call this graph .
Algorithm:
- Find an "augmenting path" .
- can contain forward or backward edges!
- Say the smallest residual capacity along the path is .
- Push flow on the path ( for all edges on path )
- Reduce the capacity of all edges on the path by
- Increase the capacity of the corresponding mirror/back edges
- Repeat until there are no augmenting paths
Formalize: Ford-Fulkerson (FF) Algorithm
- Initialize the residual graph
- Find an augmenting path with capacity (min capacity of any edge on )
- Fix up the residual capacities in
- Repeat 2 and 3 until no augmenting path can be found in .
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:
- Flow conservation:
Proof: We proceed by induction on augmenting paths
Base Case
on all edges
Inductive Case
By inductive hypothesis, we have a valid flow and the corresponding residual graph .
Inductive Step:
Now we find an augmented path in , pushed (which is the smallest edge capacity on ). Argue that the constraints are not violated.
Capacity Constrains: Consider an edge in .
- If is an forward edge (in the original graph)
- by construction of , it had left over capacities.
- If is an back edge with residual capacity
- flow on real edge reduces, but the real capacity is still , no capacity constrains violation.
Conservation Constrains: Consider a vertex on path
- Both forward edges
- No violation, push flow into and out.
- Both back edges
- No violation, push less flow into and out.
- Redirecting flow
- No violation, change of by on .
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 to find a valid path.
The number of iteration , the total is (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 , a graph cut is a partition of vertices into 2 subsets.
- : + maybe some other vertices
- : + maybe some other vertices
Define capacity of the cut be the sum of capacity of edges that go from a vertex in to a vertex in .
Lemma 3: For all valid flows , for all cut (Max-flow Min-cut)
Proof: all flow must go through one of the cut edges.
Min-cut: cut of smallest capacity, .
Lemma 4: FF produces a flow
Proof: Let be the flow found by FF. Mo augmenting paths in .
Let be all vertices that can be reached from using edges with capacities .
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 .
If some flow was coming from , then there must be some edges with capacity . So,
Example 2: Bipartite Matching
input: Given classes and rooms; we want to match classes to rooms.
Bipartite graph (unweighted and undirected)
- Vertices are either in set or
- Edges only go between vertices of different sets
Matching: A subset of edges s.t.
- Each vertex has at most one edge from incident 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 , construct a graph such that
Let connects to all vertices in and all vertex in connects to .
added edges form to and added capacities.
Proof of correctness
Claim: has a flow of iff has a matching of size
Proof: Two directions:
- Say has a matching of size , we want to prove has a flow of size .
- Say has a flow of size , we want to prove has a matching of size .
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 , while the background occupies the remaining pixels .
Required:
- Separate from but if doesn't know which pixels are each.
- For each pixel is the probability that
- For each pair of adjacent pixels is the cost of placing the object boundary between them. i.e. putting in and in .
- A segmentation of the image is an assignment of each pixel to or .
- The goal is to find a segmentation that maximizes
Solution:
- Let's turn our maximization into a minimization
- If the image has pixels, then we can rewrite the objective as
because boundary
New maximization problem:
Now, this is equivalent ot minimizing
Second steps
- Form a graph with vertices, on for each pixel
- Add vertices and
- For each , add edges cut of assigned each to either side or side.
- The side of an is the side, while the side of the cur is the side.
- Observer that if goes on the side, it becomes part of , so the cut increases by . Otherwise, it become part of , so the cut increases by instead.
- Now add edges with capacity for all adjacent pixels pairs
- If and end up on opposite sides of the cut (boundary), then the cut increases by .
- Conclude that any cut that assigns to the side and to the side pays a total of
- for each on the side
- for each on the side
- for each adjacent pair that is at the boundary. i.e.
- Conclude that a cut with a capacity implies a segmentation with objective value .
- The converse can (and should) be also checked: a segmentation with subjective value implies a cut with capacity .
Algorithm
- Given an image with pixels, build the graph as desired.
- Use the FF algorithm to find a minimum cut of
- Use this cut to assign each pixel to or as described, i.e pixels that correspond to vertices on the side are assigned to and those corresponding to vertices on the side to .
- Minimizing the cut capacity minimizes our transformed minimization objective function.
Running time
The graph contains edges, because each pixel is adjacent to a maximum of of 4 neighbors and and .
FF algorithm has running time , where is the size of set of min-cut. The edge count is .
So the total running time is