Maximum flow: The push/relabel method of Goldberg and ...haimk/adv-alg-2013/preflow2013.pdf ·...

115
Maximum flow: The push/relabel method of Goldberg and Tarjan (87)

Transcript of Maximum flow: The push/relabel method of Goldberg and ...haimk/adv-alg-2013/preflow2013.pdf ·...

  • Maximum flow: The push/relabel method of Goldberg and Tarjan (87)

  • Distance labels

    • Defined with respect to residual capacities

    • d(t) = 0, d(s) = n

    • d(v) ≤ d(w) + 1 if r(v,w) > 0

  • Example (distance labels)

    3 4

    3 3

    3 4

    2

    1

    1 3

    1 2

    3 1

    1

    3 3

    2 2

    3 3

    2

    1

    1

    1 1

    1

    A flow The residual network

  • Example (distance labels)

    3 4

    3 3

    3 4

    2

    1

    1 3

    1 2

    3 1

    1

    3 3

    2 2

    3 3

    2

    1

    1

    1 1

    1

    A flow The residual network

    0

    1 1

    2 2

    6

    Is this a valid distance labeling ?

  • Distance labels – basic lemma

    Lemma: d(v) is a lower bound on the length of the shortest path from v to the sink

    Proof: Let the s.p. to the sink be:

    v v1 v2 t

    d(v) ≤ d(v1) + 1 ≤ d(v2) + 2 ..... ≤ d(t) + k = k

  • Preflow (definition)

    A preflow is a function on the edges which satisfies the following requirements

    • f(v,w) = -f(w,v) skew symmetry

    • f(v,w) u(v,w)

    • For every w, except s and t, vf(v,w) ≥ 0

    Let e(w) = vf(v,w) be the excess at the node v

    (we’ll also have e(t) ≥ 0, and e(s) ≤ 0)

  • Example (preflow)

    Nodes with positive excess are called active.

    s t

    3

    3

    3

    2

    2

    2

    2

    1

    2

    1

    0

    0

    The preflow push algorithm will try to push flow from active nodes towards the sink, relying on d( ).

  • Initialization (preflow)

    3 4

    3 3

    3 4

    2

    1 4 3

    0 0

    0 0

    0

    3 4

    3 3

    3 4

    2

    1 3 4 0

  • Initialization (distance labels)

    3 4

    3 3

    3 4

    2

    1 4 3

    0 0

    0 0

    0

    3 4

    3 3

    3 4

    2

    1

    6

    0

    0

    0

    0 0 3 4

    Recall: s must be disconnected from t when d(s) = n, and the labeling is valid…

    0

  • Admissible arc in the residual graph

    w

    v

    d(v) = d(w) + 1

  • The push/relabel algorithm

    While there is an active node {

    pick an active node v and push/relabel(v)

    }

    Push/relabel(v) {

    If there is an admissible arc (v,w) then {

    push = min {e(v) , r(v,w)} flow from v to w

    } else {

    d(v) := min{d(w) + 1 | r(v,w) > 0} (relabel)

    }

  • Correctness

    Lemma 1: The source is reachable from every active vertex in the residual network

    Proof:

    Which means that no flow enters S --

    A contradiction

    v s

    S

    Assume that’s not the case:

  • Correctness (cont)

    Corollary: There is an outgoing residual arc incident with every active vertex

    Corollary: So we can push-relabel as long as there is an active vertex

  • Correctness (cont)

    Lemma 2: Distance labels only increase and remain valid at all times

    Proof:

    By induction on the number of push and relabel operations.

    For relabel this is clear by the definition of relabel

    For push:

    v w

    d(v) = d(w) + 1 so even if we add (w,v) to the residual network then it is still a valid labeling

  • Correctness (cont)

    Lemma 3: When (and if) the algorithm stops the preflow is a maximum flow

    Proof:

    It is a flow since there is no active vertex.

    It is maximum since the sink is not reachable from the source in the residual network. (d(s) = n, and the labeling is valid)

  • Complexity analysis

  • Another example

  • Complexity analysis

    Observation: d(v) increases when we relabel v !

    Lemma: d(v) ≤ 2n-1

    Proof:

    v v1 v2 s

    d(v) ≤ d(v1) + 1 ≤ d(v2) + 2 ..... ≤ d(s) + (n-1) = 2n-1

  • Complexity analysis (cont)

    Lemma: The # of relabelings is (2n-1)(n-2) < 2n2

    Proof:

    At most 2n-1 per each node other than s and t

  • Complexity analysis (cont)

    Def: Call a push saturating if min{e(v), r(v,w)} = r(v,w)

    Lemma: The # of saturating pushes is at most 2nm

    Proof: Before another saturating push on (v,w), we must push from w to v.

    d(w) must increase by at least 2

    Since d(w) ≤ 2n-1, this can happen at most n times

  • Nonsaturating pushes

    Lemma: The # of nonsaturating pushes is at most 4n2m

    Proof:

    Let Φ = Σv active d(v)

    • Decreases (by at least one) by every nonsaturating push

    • Increases by at most 2n-1 by a saturating push : total increase (2n-1)2nm

    • Increases by each relabeling: total increase < (2n-1)(n-2)

  • Implementation

    Maintain a list of active nodes, so finding an active node is easy

    Given an active node v, we need to decide if there is an admissible arc (v,w) to push on ?

    v

    current edge

    All edges, not only those in R

  • Current edge

    v

    current edge

    Push/relabel(v): If the current edge (v,w) is admissible, push on it (updating the list of active vertices)

    Otherwise, advance the current edge pointer

    if you are on the last edge, relabel v and set the current edge to be the first one.

  • Is this implementation correct?

    Lemma: When we relabel v there is no admissible arc (v,w)

    Proof: After we scanned (v,w) either (v,w) dropped off the residual network or d(v) ≤ d(w)

    If d(v) ≤ d(w) then this must be the case now since v has not been relabeled.

    If (v,w) became residual since it was scanned then when that happened d(w) = d(v) + 1 d(v) ≤ d(w) and this must be the case now

  • Analysis

    Lemma: The total time spent at v between two relabelings of v is Δv plus O(1) per push out of v

    Summary: Since we relabel v at most (2n-1) times we get that the total work at v is O(nΔv) + O(1) per push out of v.

    Summing over all vertices we get that the total time is O(nm) + #of pushes

    O(n2m)

  • Maintain the list of active vertices as a FIFO queue (Q)

    Discharge the first vertex of the queue:

    Discharge(v) {

    While v is active and hasn’t been relabeled then push/relabel(v).

    (If the loop stops because v is relabeled then add v to the end of Q)

    }

    Reducing the # of nonsaturating pushes

  • 4

    1

    2

    2

    2

    4

    2

    Example (FIFO order)

  • 0

    4

    1

    2

    2

    2

    4

    2 0

    0

    6 0

    0 4

    2

  • 0

    4

    1

    2

    2

    2

    4

    2 0

    0

    6 0

    0 4

    2

    0

    4

    1

    2

    2

    2 0

    0

    6 0

    0 4

    2

    relabel

    x y

    z

    u v

    w

    Q: z y

    4

    2

  • 0

    4

    1

    2

    2

    2

    4

    2 0

    1

    6 0

    0 4

    2

    0

    4

    1

    2

    2

    2 0

    1

    6 0

    0 4

    2

    x y

    z

    u v

    w

    Q: y z relabel

    4

    2

  • 0

    4

    1

    2

    2

    2

    4

    2 0

    1

    6 2

    0 4

    2

    0

    4

    1

    2

    2

    2 0

    1

    6 2

    0 4

    2

    x y

    z

    u v

    w

    Q: z y

    push

    push

    4

    2

  • 0

    4

    1

    2

    2

    2

    4

    2 0

    1

    6 2

    0 4

    2

    0

    4

    1

    2

    2

    2 0

    1

    6 2

    0 4

    2

    2

    2

    push

    x y

    z

    u v

    w

    Q: y u

    2

    2

  • 0

    4

    1

    2

    2

    2

    4

    2 0

    1

    6 2

    0 4

    2

    0

    4

    1

    2

    2

    2 0

    1

    6 2

    0 4

    2

    2

    2

    2

    x y

    z

    u v

    w

    Q: u z

    relabel

    2

    2

  • 1

    4

    1

    2

    2

    2

    4

    2 0

    1

    6 2

    0 4

    2

    1

    4

    1

    2

    2

    2 0

    1

    6 2

    0 4

    2

    2

    2

    2

    x y

    z

    u v

    w

    Q: z u

    relabel

    2

    2

  • 1

    4

    1

    2

    2

    2

    4

    2 0

    3

    6 2

    0 4

    2

    0

    4

    1

    2

    2

    2 0

    3

    6 2

    0 4

    2

    2

    2

    2

    x y

    z

    u v

    w

    Q: u z

  • Passes

    Pass 1: Until you finish discharging all vertices initially in Q

    Pass i: Until you finish discharging all vertices added to Q in pass (i-1)

  • 0

    4

    1

    2

    2

    2

    4

    2 0

    0

    6 0

    0 4

    2

    0

    4

    1

    2

    2

    2 0

    0

    6 0

    0 4

    2

    relabel

    x y

    z

    u v

    w

    Q: z y

    4

    2

  • 0

    4

    1

    2

    2

    2

    4

    2 0

    1

    6 0

    0 4

    2

    0

    4

    1

    2

    2

    2 0

    1

    6 0

    0 4

    2

    x y

    z

    u v

    w

    Q: y z relabel

    4

    2

  • 0

    4

    1

    2

    2

    2

    4

    2 0

    1

    6 2

    0 4

    2

    0

    4

    1

    2

    2

    2 0

    1

    6 2

    0 4

    2

    x y

    z

    u v

    w

    Q: z y

    4

    2

    End of pass 1

  • 0

    4

    1

    2

    2

    2

    4

    2 0

    1

    6 2

    0 4

    2

    0

    4

    1

    2

    2

    2 0

    1

    6 2

    0 4

    2

    x y

    z

    u v

    w

    Q: z y

    push

    push

    4

    2

  • 0

    4

    1

    2

    2

    2

    4

    2 0

    1

    6 2

    0 4

    2

    0

    4

    1

    2

    2

    2 0

    1

    6 2

    0 4

    2

    2

    2

    push

    x y

    z

    u v

    w

    Q: y u

    2

    2

  • 0

    4

    1

    2

    2

    2

    4

    2 0

    1

    6 2

    0 4

    2

    0

    4

    1

    2

    2

    2 0

    1

    6 2

    0 4

    2

    2

    2

    2

    x y

    z

    u v

    w

    Q: u z

    2

    2 End of pass 2

  • 0

    4

    1

    2

    2

    2

    4

    2 0

    1

    6 2

    0 4

    2

    0

    4

    1

    2

    2

    2 0

    1

    6 2

    0 4

    2

    2

    2

    2

    x y

    z

    u v

    w

    Q: u z

    relabel

    2

    2

  • 1

    4

    1

    2

    2

    2

    4

    2 0

    1

    6 2

    0 4

    2

    1

    4

    1

    2

    2

    2 0

    1

    6 2

    0 4

    2

    2

    2

    2

    x y

    z

    u v

    w

    Q: z u

    relabel

    2

    2

  • 1

    4

    1

    2

    2

    2

    4

    2 0

    3

    6 2

    0 4

    2

    0

    4

    1

    2

    2

    2 0

    3

    6 2

    0 4

    2

    2

    2

    2

    x y

    z

    u v

    w

    Q: u z

    End of pass 3

  • Analysis

    Note that we still have the O(n2m) bound

    How many passes are there ?

    Let Φ = maxactive vd(v) 1) If the algorithm does not relabel during a pass then Φ decreases by at least 1 (each active node at the beginning of a pass moved its excess to a vertex with lower label)

    2) If we relabel then Φ may increase by at most the maximum increase of a distance label

    There are at most O(n2) passes of the second kind.

    These passes increase Φ by at most O(n2)

    There are at most O(n2) passes of the first kind

  • Analysis (Cont)

    So we have O(n2) passes

    In each pass we have at most one nonsaturating push per vertex

    O(n3) nonsaturating pushes

    O(n3) total running time

  • A faster implementation

    Maintain a (dynamic) forest of some of the admissible current edges

  • Reminder: Admissible arc in the residual graph

    w

    v

    d(v) = d(w) + 1

  • A faster implementation

    Maintain a (dynamic) forest of some of the admissible current edges

  • A faster implementation

    Maintain a (dynamic) forest of some of the admissible current edges

    Active guys are among the roots

  • At a high level the algorithm is almost the same

    While there is an active node in Q {

    Let v be the first in Q

    discharge(v)

    }

    discharge(v) {

    While v is active and hasn’t been relabeled then Treepush/relabel(v).

    (If the loop stops because v is relabeled then add v to the end of Q)

    }

  • A faster implementation

    Q: v….. discharge(v) Treepush/relabel(v)

    v

    w

  • Case 1: (v,w) is admissible

    v

    w

    link(v,w,rf(v,w)),

    (v,c) = findmin(v), c = min(c,e(v)), addcost(v,-c)

    Let (u,c) = findmin(v) If c=0 cut(u) and repeat If e(v) > 0 and v is not a root then repeat

  • Case 2: (v,w) is not admissible

    v

    w

    a) If (v,w) is not the last edge then advance the current edge

    b) If (v,w) is the last edge we relabel v and perform cut(u) for every child u of v

  • Treepush/relabel Analysis

    O(1) work per link/cut or we advance the current arc

    How many cuts do we do ?

    O(mn) (each charged to a saturating push or a relabel)

    O(nm) links

    How many timesTreepush/relabel advance the current arc?

    O(mn)

  • Analysis (Cont)

    Summary: The running time is determined by O(mn) dynamic tree operations

    O(mnlog n)

  • Can we improve on that ?

    Notice that we have not really used the fact that Q is a queue, any list would do !

  • Idea: Don’t let the trees to grow too large

    Case 1: (v,w) is admissible

    v

    w

    link(v,w,rf(v,w)),

    (v,c) = findmin(v), c = min(c,e(v)), addcost(v,-c)

    Let (u,c) = findmin(v) If c=0 cut(u) and repeat If e(v) > 0 and v is not a root then repeat

    We won’t do the link if we are about to create a too large tree (say larger than k)

  • If we are about to create a tree with at least k vertices

    v

    w

    Push from v to w min{e(v),rf(v,w)} flow

    (w,c) = findmin(w), c = min(c,e(w)), addcost(w,-c)

    Let (u,c) = findmin(w) If c=0 cut(u) and repeat If e(w) > 0 and w is not a root then repeat

  • What collapses in our analysis ?

    There are calls to Treepush/relabel that do we cannot charge to links (or cuts) and do not advance current arc!

    v

    r

    w

  • How do we recover ?

    v

    r

    w

    May assume that the push from v is not saturating..(there are only O(nm) saturating ones)

    v is not active after such Treepush/relabel

    We are going to bound the # of such Treepush/relabels by bounding the # of times a node becomes active

  • Activating nodes

    When does a node become active ?

    v

    r

    w

    • In Treepush/relabel. We know that there are O(mn) of them except for nonsaturating ones, that do not link/cut.

    So we can further focus on those problematic Treepush/relabels that make r active

  • Concluding

    v

    r

    w

    May assume that v becomes inactive and r becomes active and the push is not saturating and does not link

    Tv

    Tr

    Either Tv or Tr is large: ≥ k/2

    We charge the large tree.

  • v

    r

    w

    Since the root of the tree that we charge either becomes active or inactive each tree is charged at most twice in a phase

    Tv

    Tr

    If the tree did not exist at the beginning of the phase then further deliver the charge it to the link or cut that created it

  • Each link is charged once, a cut is charged twice O(mn) such charges over all phases.

    At the beginning of a phase we have O(n/k) large trees, each charged once O(n3/k)

    So we get that nodes get activated at most O(mn + n3/k) times

    For k=n2/m we get the bound of O(mnlog(n2/m))

    This also bounds the # of Treepush/relabels and the # of dynamic tree operations