Skip to Content
CSE347Analysis of Algorithms (Lecture 4)

CSE347 Analysis of Algorithms (Lecture 4)

Maximum Flow

Example 1: Ship cement from factory to building

Input ss: source, tt: destination

Graph with directed edges weights on each edge: capacity

Goal: Ship as much stuff as possible while obeying capacity constrains.

Graph: (V,E)(V,E) directed and weighted

  • Unique source and sink nodes s,t\to s, t
  • Each edge has capacity c(e)c(e) [Integer]

A valid flow assignment assigns an integer f(e)f(e) to each edge s.t.

Capacity constraint: 0f(e)c(e)0\leq f(e)\leq c(e)

Flow conservation:

eEin(v)f(e)=eEout(v)f(e),vVs,t\sum_{e\in E_{in}(v)}f(e)=\sum_{e\in E_{out}(v)}f(e),\forall v\in V-{s,t}

Ein(v)E_{in}(v): set of incoming edges to vv Eout(v)E_{out}(v): set of outgoing edges from vv

Compute: Maximum Flow: Find a valid flow assignment to

Maximize F=eEin(t)f(e)=eEout(s)f(e)|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

  1. ss has no incoming edges, tt has no outgoing edges
  2. You do not have a cycle of 2 nodes

A proposed algorithm:

  1. Find a path from ss to tt
  2. Push as much flow along the path as possible
  3. Adjust the capacities
  4. Repeat until we cannot find a path

Residual Graph: If there is an edge e=(u,v)e=(u,v) in GG, we will add a back edge eˉ=(v,u)\bar{e}=(v,u). Capacity of eˉ=\bar{e}= flow on ee. Call this graph GRG_R.

Algorithm:

  • Find an “augmenting path” PP.
    • PP can contain forward or backward edges!
  • Say the smallest residual capacity along the path is kk.
  • Push kk flow on the path (f(e)=f(e)+kf(e) =f(e) + k for all edges on path PP)
    • Reduce the capacity of all edges on the path PP by kk
    • Increase the capacity of the corresponding mirror/back edges
  • Repeat until there are no augmenting paths

Formalize: Ford-Fulkerson (FF) Algorithm

  1. Initialize the residual graph GR=GG_R=G
  2. Find an augmenting path PP with capacity kk (min capacity of any edge on PP)
  3. Fix up the residual capacities in GRG_R
    • c(e)=c(e)k,ePc(e)=c(e)-k,\forall e\in P
    • c(eˉ)=c(eˉ)+k,eˉPc(\bar{e})=c(\bar{e})+k,\forall \bar{e}\in P
  4. Repeat 2 and 3 until no augmenting path can be found in GRG_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: 0f(e)c(e)0\leq f(e)\leq c(e)
  • Flow conservation: eEin(v)f(e)=eEout(v)f(e),vV{s,t}\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)=0f(e)=0 on all edges

Inductive Case

By inductive hypothesis, we have a valid flow and the corresponding residual graph GRG_R.

Inductive Step:

Now we find an augmented path PP in GRGR, pushed kk (which is the smallest edge capacity on PP). Argue that the constraints are not violated.

Capacity Constrains: Consider an edge ee in PP.

  • If ee is an forward edge (in the original graph)
    • by construction of GRG_R, it had left over capacities.
  • If ee is an back edge with residual capacity k\geq k
    • flow on real edge reduces, but the real capacity is still 0\geq 0, no capacity constrains violation.

Conservation Constrains: Consider a vertex vv on path PP

  1. Both forward edges
    • No violation, push kk flow into vv and out.
  2. Both back edges
    • No violation, push kk less flow into vv and out.
  3. Redirecting flow
    • No violation, change of 00 by kkk-k on vv.

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 Θ(m+n)\Theta(m+n) to find a valid path.

The number of iteration F\leq |F|, the total is Θ(F(m+n))\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)G(V,E), a graph cut is a partition of vertices into 2 subsets.

  • SS: ss + maybe some other vertices
  • VSV-S: tt + maybe some other vertices

Define capacity of the cut be the sum of capacity of edges that go from a vertex in SS to a vertex in TT.

Lemma 3: For all valid flows ff, fC(S)|f|\leq C(S) for all cut SS (Max-flow \leq Min-cut)

Proof: all flow must go through one of the cut edges.

Min-cut: cut of smallest capacity, SS^*. fC(S)|f|\leq C(S^*)

Lemma 4: FF produces a flow =C(S)=C(S^*)

Proof: Let f^\hat{f} be the flow found by FF. Mo augmenting paths in GRG_R.

Let S^\hat{S} be all vertices that can be reached from ss using edges with capacities >0>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 SS.

If some flow was coming from VS^V-\hat{S}, then there must be some edges with capacity >0>0. So, fC(S)|f|\leq C(S^*)

Example 2: Bipartite Matching

input: Given nn classes and nn rooms; we want to match classes to rooms.

Bipartite graph G=(V,E)G=(V,E) (unweighted and undirected)

  • Vertices are either in set LL or RR
  • Edges only go between vertices of different sets

Matching: A subset of edges MEM\subseteq E s.t.

  • Each vertex has at most one edge from MM 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 G=(V,E)G=(V,E), construct a graph G=(V,E)G'=(V',E') such that

maxflow(G)=maxflow(G)|max-flow (G')|=|max-flow(G)|

Let ss connects to all vertices in LL and all vertex in RR connects to tt.

G=G+s+t+G'=G+s+t+added edges form SS to TT and added capacities.

Proof of correctness

Claim: GG' has a flow of kk iff GG has a matching of size kk

Proof: Two directions:

  1. Say GG has a matching of size kk, we want to prove GG' has a flow of size kk.
  2. Say GG' has a flow of size kk, we want to prove GG has a matching of size kk.

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 AA, while the background occupies the remaining pixels BB.

Required:

  • Separate AA from BB but if doesn’t know which pixels are each.
  • For each pixel i,pii,p_i is the probability that iAi\in A
  • For each pair of adjacent pixels i,j,ciji,j,c_{ij} is the cost of placing the object boundary between them. i.e. putting ii in AA and jj in BB.
  • A segmentation of the image is an assignment of each pixel to AA or BB.
  • The goal is to find a segmentation that maximizes
iApi+iB(1pi)i,j on boundarycij\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 NN pixels, then we can rewrite the objective as
NiA(1pi)iBpii,j on boundarycijN-\sum_{i\in A}(1-p_i)-\sum_{i\in B}p_i-\sum_{i,j\ on \ boundary}c_{ij}

because N=iApi+iA(1pi)+iBpi+iB(1pi)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(NiA(1pi)iBpii,j on boundarycij)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

iA(1pi)+iBpi+i,j on boundarycij\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 nn vertices, viv_i on for each pixel
  • Add vertices ss and tt
  • For each viv_i, add edges STS-T cut of GG assigned each viv_i to either SS side or TT side.
  • The SS side of an STS-T is the AA side, while the TT side of the cur is the BB side.
  • Observer that if viv_i goes on the SS side, it becomes part of AA, so the cut increases by 1p1-p. Otherwise, it become part of BB, so the cut increases by pip_i instead.
  • Now add edges vivjv_i\to v_j with capacity cijc_{ij} for all adjacent pixels pairs i,ji,j
  • If viv_i and vjv_j end up on opposite sides of the cut (boundary), then the cut increases by cijc_{ij}.
  • Conclude that any STS-T cut that assigns SVS\subseteq V to the AA side and V\SV\backslash S to the BB side pays a total of
    1. 1pi1-p_i for each viv_i on the AA side
    2. pip_i for each viv_i on the BB side
    3. cijc_{ij} for each adjacent pair i,ji,j that is at the boundary. i.e. iS and jV\Si\in S\ and\ j\in V\backslash S
  • Conclude that a cut with a capacity cc implies a segmentation with objective value cscs.
  • The converse can (and should) be also checked: a segmentation with subjective value cc implies a STS-T cut with capacity cc.

Algorithm

  • Given an image with NN pixels, build the graph GG as desired.
  • Use the FF algorithm to find a minimum STS-T cut of GG
  • Use this cut to assign each pixel to AA or BB as described, i.e pixels that correspond to vertices on the SS side are assigned to AA and those corresponding to vertices on the TT side to BB.
  • Minimizing the cut capacity minimizes our transformed minimization objective function.

Running time

The graph GG contains Θ(N)\Theta(N) edges, because each pixel is adjacent to a maximum of of 4 neighbors and SS and TT.

FF algorithm has running time O((m+n)F)O((m+n)|F|), where Fn|F|\leq |n| is the size of set of min-cut. The edge count is m=6nm=6n.

So the total running time is O(n2)O(n^2)

Last updated on