CS 170 Lecture 20 Handout

Embed Size (px)

Citation preview

  • CS 170:Algorithms

    Prof Elchanan Mossel.Based on: Algorithms by Dasgupta Papadimitriou and Vazirani.Slides edited from a version created by Prof. Satish Rao.For UC-Berkeley CS170 Spring 2014 students use only.Do not re-post or distribute

    Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 1 / 16

  • CS 170: Algorithms

    S

    HH

    HH

    .

    .

    .

    ..

    Linear Programming today!!!!

    No laptops please.

    Thank you !!!Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 2 / 16

  • Outline

    Maximum Flow.

    Problem.

    Linear Program

    Upper bound: Cut!

    Residual Network

    Algorithm

    Minimum Cut.

    Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 3 / 16

  • Maximum flowFlow network G = (V ,E), source s, sink t V , capacities ce > 0.

    a

    c

    d

    e

    s t

    4

    4 3

    1 4

    2

    111

    31

    2

    2

    2

    1

    Find Flow: fe

    1 0 fe ce. Capacity constraints. 3 = fs,c cs,c = 4.2 If u is not s or t(w ,u)E fwu = (u,w)E fuw . 3 = fs,c = fc,d + fc,e = 2+1.

    Maximize: size(f ) = (s,u)E fsu. fsa+ fsc = 1+3 = 4Optimal? cad +ccd +cet = 1+1+2 = 4.The capacity of any s t cut gives an upper bound.

    Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 4 / 16

  • S-T cut.

    An s t cut is a partition of V into S and T where s S and t T . Itscapacity is the total capacity of edges from S to T .

    Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 5 / 16

  • Do you know the definition?

    Find Flow: fe

    1 0 fe ce. Capacity constraints.2 If u is not s or t(w ,u)E fwu = (u,w)E fuw .

    Valid or Invalid?

    s

    b

    c

    d

    t

    3211

    2211

    2323

    s

    b

    c

    d

    t

    3211

    2211

    22

    2+1 6= 2

    Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 6 / 16

  • Algorithms.FindFlow: fe

    1 0 fe ce. Capacity constraints.2 (w ,u)E fwu = (u,w)E fuw .

    3 maximize su fsu.

    Linear program!

    Variables fe, linear constraints, linear optimization function.

    Cool!

    Note...

    Integer? (Given integer capacities.)

    Yes. There is an integer vertex solution!

    Constraint matrix has every subdeterminant being 1, 0, 1.Vertex solution to linear program must be integral!

    Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 7 / 16

  • Ford-Fulkerson.

    Simplex method.

    Find s to t path with remaining capacity.

    Add to flow variables along path.

    Update remaining capacity.

    Repeat.

    a

    c

    d

    e

    s t

    4

    4 3

    1 4

    2

    11

    44X 31

    11X 01

    11X 0 1

    22X 11

    Uh oh...

    Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 8 / 16

  • Residual Capacity.Find s to t path with remaining capacity.Add to flow along path. Or reduce flow on reverse edge.Update remaining capacity.

    Reduce re = ce feand add reverse ruv = fvu

    Repeat.

    s

    a

    b

    t

    1

    1

    1

    1

    1

    11X 01

    11X 01

    11X 0 1

    1

    11

    1

    1

    1X 0

    1X 0

    1

    1

    No remaining path. Uh oh! Optimal is 2! (At most 2 due to cut.)Add reverse arcs to indicate reverse capacity.

    Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 9 / 16

  • Bigger Example.

    Find s to t path with remaining capacity.

    Add to flow along path.

    Update residual capacities: : re = ce fe; ruv = fvu.Repeat.

    a

    c

    d

    e

    s t

    4

    4 3

    1 4

    2

    11

    44X 311

    11X 011

    11X 0 11

    22X 111

    44X 311

    33X 211

    2X 11X 0122

    44X 311

    1X 00X 1 1

    4X 33X 212

    3X 22X 112

    11X 011

    3X 22X 1233

    4X 33X 2122

    Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 10 / 16

  • Check Result...

    a

    c

    d

    e

    s t

    4

    4 3

    1 4

    2

    111

    31

    2

    2

    2

    1

    Find Flow: fe

    1 0 fe ce. Capacity constraints. 3 = fs,c cs,c = 4.2 If u is not s or t(w ,u)E fwu = (u,w)E fuw . 3 = fs,c = fc,d + fc,e = 2+1.

    Maximize: size(f ) = (s,u)E fsu. fsa+ fsc = 1+3 = 4Optimal? cad +ccd +cet = 1+1+2 = 4.Any s t cut gives an upper bound.

    Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 11 / 16

  • Correctness.

    1. Capacity Constraints: 0 fe ce.Only increase flow to ce.Or use reverse arcs decrease to 0.Flow values to be between 0 and ce.

    2. Conservation Constraints:flow into v = flow out of v (if not s or t .)Algorithm adds flow, say f , to path from s to t .Each internal node has f in, and f out.

    Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 12 / 16

  • Optimality: upper bound.s-t Cut: V = ST and s S and t T .

    s t

    Lemma: Capacity of any s t cut is an upper bound on the flow.C(S,T ) - sum of capacities of all arcs from S to TC(S,T ) = e=(u,v):uS,VT ceFor valid flow:Flow out of (S) = Flow out of s.Flow into (T ) = Flow into t .

    For any valid flow, f : E Z+, the flow out of S (into T )eST feeTS fe eST ceeTS 0 = C(S,T ). The value of any valid flow is at most C(S,T )!

    Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 13 / 16

  • Optimality: max flow = min cut.At termination of augmenting path algorithm.

    No path with residual capacity!

    Depth first search only starting at s does not reach t .

    s tfe = ce

    fe = 0

    S be reachable nodes.

    No arc with positive residual capacityleaving S

    = All arcs leaving S are full.= No arcs into S have flow.Total flow leaving S is C(S,T ).

    Valid flow = all that flow from source.Value of flow equals value of C(S,T ). and Optimal is C(S,T ). Flow is maximum!!Cut is minimum s t cut too!any flow any cut and this flow = this cut. Maximum flow and minimum s t cut!

    Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 14 / 16

  • Celebrated max flow -minimum cut theorem.

    Theorem: In any flow network, the maximum s-t flow is equal to theminimum cut.

    Celebrate!

    Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 15 / 16

  • Back to business: Algorithm Terminates?

    It will!!

    Flow keeps increasing.

    How long?

    One more unit every step!

    O(mF ) time where F is size of flow.

    Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 16 / 16

  • Efficiency.

    s

    a

    b

    t

    100000

    1

    100000100000

    100000999991

    999991

    1

    999991

    999991

    999991

    999991

    1

    Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 17 / 16

  • Edmonds-Karp

    Augment along shortest path.

    Breadth first search!

    O(|V ||E |) augmentations.Analysis idea.

    d(v) is distance to sink.

    Only route flow on (u,v) if d(u) d(v).Only reverse flow on (u,v) if d(v) d(u).Maximum d(v) is |V |.Distances only go up. (To prove!)

    Every augment removes edge at a distance.

    O(|V ||E |) removals.O(|V ||E |2) time.

    Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 18 / 16

  • Bipartite Matching

    Given a bipartite graph: B = (L,R,E) where E LR.

    Cindy

    Jin

    Shelby

    Katya

    Chen

    Ollie

    Ivan

    Jacque

    Find largest subset of edges (matches) which are one to one.

    Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 19 / 16

  • Bipartite MatchingAlgorithm by Reduction.:

    From matching problem produce flow problem.From flow solution produce matching solution.

    C

    J

    S

    K

    C

    O

    I

    J

    s t

    Max flow = Max Matching Size.Flow is not integer necessarily....

    Augmenting path algorithm gives integer flow.

    Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 20 / 16

  • Review.

    Maximum flow.

    Greedy augment path...

    Except reverse old decisions ..Reverse residual capacities.

    Optimality?

    Upper bound on optimal: s t cut!Find flow and s t cut with equal value!Optimal plus proof of optimality.

    Application to bi-partite matchings.

    Elchanan Mossel (UC Berkeley) CS 170:Spring 2014 April 3, 2014 21 / 16