๐ŸŽ‰ ไปŽ330่ฝฌไธบWashUๅธฎๅŠฉๆ‰‹ๅ†Œๅ•ฆ!

CSE347
ๆจกๅ—
Cse347 L4

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: 0โ‰คf(e)โ‰คc(e)0\leq f(e)\leq c(e)

Flow conservation:

โˆ‘eโˆˆEin(v)f(e)=โˆ‘eโˆˆEout(v)f(e),โˆ€vโˆˆVโˆ’s,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โˆฃ=โˆ‘eโˆˆEin(t)f(e)=โˆ‘eโˆˆEout(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,โˆ€eโˆˆPc(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: 0โ‰คf(e)โ‰คc(e)0\leq f(e)\leq c(e)
  • Flow conservation: โˆ‘eโˆˆEin(v)f(e)=โˆ‘eโˆˆEout(v)f(e),โˆ€vโˆˆVโˆ’{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 kโˆ’kk-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
  • Vโˆ’SV-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, โˆฃfโˆฃโ‰คC(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, Sโˆ—S^*. โˆฃfโˆฃโ‰คC(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 Vโˆ’S^V-\hat{S}, then there must be some edges with capacity >0>0. So, โˆฃfโˆฃโ‰คC(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 MโŠ†EM\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

โˆฃmaxโˆ’flow(Gโ€ฒ)โˆฃ=โˆฃmaxโˆ’flow(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: Gโ€ฒG' 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 Gโ€ฒG' has a flow of size kk.
  2. Say Gโ€ฒG' 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 iโˆˆAi\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
โˆ‘iโˆˆApi+โˆ‘iโˆˆB(1โˆ’pi)โˆ’โˆ‘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
Nโˆ’โˆ‘iโˆˆA(1โˆ’pi)โˆ’โˆ‘iโˆˆBpiโˆ’โˆ‘i,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=โˆ‘iโˆˆApi+โˆ‘iโˆˆA(1โˆ’pi)+โˆ‘iโˆˆBpi+โˆ‘iโˆˆB(1โˆ’pi)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(Nโˆ’โˆ‘iโˆˆA(1โˆ’pi)โˆ’โˆ‘iโˆˆBpiโˆ’โˆ‘i,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

โˆ‘iโˆˆA(1โˆ’pi)+โˆ‘iโˆˆBpi+โˆ‘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 Sโˆ’TS-T cut of GG assigned each viv_i to either SS side or TT side.
  • The SS side of an Sโˆ’TS-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 1โˆ’p1-p. Otherwise, it become part of BB, so the cut increases by pip_i instead.
  • Now add edges viโ†’vjv_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 Sโˆ’TS-T cut that assigns SโŠ†VS\subseteq V to the AA side and V\SV\backslash S to the BB side pays a total of
    1. 1โˆ’pi1-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. iโˆˆSย andย jโˆˆV\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 Sโˆ’TS-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 Sโˆ’TS-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 โˆฃFโˆฃโ‰คโˆฃnโˆฃ|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)