maximum flow - cs.ust.hk€¦ · maximum flow 3/42. s v v v 4 t 12 16 20 13 10 v1 3 2 414 4 9 7 a...

42
Maximum Flow Revision of November 7, 2016 Maximum Flow 1 / 42

Upload: others

Post on 17-Apr-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Maximum Flow

Revision of November 7, 2016

Maximum Flow 1 / 42

Outline

IntroductionDefinitions

Multi-Source Multi-Sink

The Ford-Fulkerson Method

Residual Networks

Augmenting Paths

The Max-Flow Min-Cut Theorem

The Edmonds-Karp algorithm

Max Bipartite Matching

Odds and Ends

Maximum Flow 2 / 42

Maximum Flow

Main Reference: Sections 26.1-26.3 in CLRS.

Input: a directed graph G = (V,E) :(flow network)Source (producer) s and destination t.Internal Nodes are warehousesEdge costs are capacitiesMaximum amount that can be shipped over edgeNo storage at internal nodesAll goods shipped into warehouse must leave warehouse

Objective:Ship Maximum amount (flow) from s to t.

Maximum Flow 3 / 42

s

v

v

v

t4

12

2016

13

10

v1 3

2 4144

79

A Flow Network and its capacities

s

v

v

v

t

v1 3

2 4

10

5/16

6/13

1/4

6/12

4/9 6/7

3/49/14

8/20

A flow: value 11

s

v

v

v

t

v1 3

42

4/4

11/16

7/7

11/14

10

12/13

1/4 9

12/1219/20

A max-flow: value = 23

Maximum Flow 4 / 42

Flow Definition: I

A flow network is a graph G = (V,E) .Source s ∈ V, , sink t ∈ V .

Every edge (u, v) ∈ E has capacity , c(u, v) ≥ 0 .Assume that for every v ∈ V ,there is a path from s to v and from v to t.

Maximum Flow 5 / 42

Flow Definition: IIA FLOW is a function f : V × V → R satisfying:

Capacity Constraint:∀u, v,∈ V, f (u, v) ≤ c(u, v).

Skew Symmetry:∀u, v,∈ V, f (u, v) = −f (v, u).

Flow Conservation:∀u ∈ V − {s, t},

∑v∈V f (u, v) = 0.

The VALUE of flow f is |f | =∑

v∈V f (s, v) .

MAXIMUM-FLOW PROBLEM:Given G, c, s, t, find f that maximizes |f |.

Maximum Flow 6 / 42

Multi-Source Multi-Sink Problem

Max-Flow problem has only one source s, and one sink t.Suppose there are

multiple sources s1, s2, . . . , sk and multiple sinks t1, t2, . . . , tℓ.

Definition of a flow remains the same except thatFlow Conservation property now becomes∀u ∈ V − {s1, s2, . . . , sk, t1, t2, . . . , tℓ},

∑v∈V f (u, v) = 0

and our goal is to maximize

|f | =k∑

i=1

∑v∈V

f (si, v).

This problem can be reduced to the original one by in-troducing a supersource s0, a supersink t0 and edges∪i(s0, si) and ∪j(tj, t0), all of which have capacity ∞.

Maximum Flow 7 / 42

A multi-source multi-sink problem and its equivalent single-source single-sink version.

t1

2t

t3

3

15

6

20

13

18

s 1

s 2

10

12

5

83s

14

7s 4

112

s 5

t1

2t

t3

3

15

6

20

13

18

1

s 2

10

12

5

83s

14

7s 4

112

s 5

s t

s

Maximum Flow 8 / 42

Manipulating Flows

Let X,Y ⊆ V. We define

f (X,Y) =∑x∈X

∑y∈Y

f (x, y).

The flow-conservation constraint then just says

∀u ∈ V − {s, t}, f (u,V) = 0.

Maximum Flow 9 / 42

Lemma: (Proof in Homework)

∀X ⊆ V, f (X,X) = 0.

∀X,Y ⊆ V, f (X,Y) = −f (Y,X).

∀X,Y,Z ⊆ V with X ∩ Y = ∅f (X ∪ Y,Z) = f (X,Z) + f (Y, Z) and

f (Z,X ∪ Y) = f (Z,X) + f (Z, Y)

Maximum Flow 10 / 42

Flow f was defined asamount that leaves source s.

We now see that this is the same asamount that enters sink t.

|f | = f (s,V) definition= f (V,V)− f (V − s,V) previous page= −f (V − s,V) previous page= f (V,V − s) previous page= f (V, t) + f (V,V − s − t) previous page= f (V, t) flow conservation

Maximum Flow 11 / 42

All optimization problems must deal with the question:How to prove that solution is optimal (maximal/minimal)?

A common technique (for max problems) is to find a goodupper-bound on the cost of an optimal solution and thenshow that our solution satisfies that bound.

A CUT S, T of G is a partition of the verticesV = S ∪ T, S ∩ T = ∅, s ∈ S, and t ∈ T.

The flow across the cut is f (S,T).

The capacity of a cut is C(S,T) =∑

x∈S,y∈T c(x, y).

Note that for any cut, f (S, T) ≤ C(S, T).

Maximum Flow 12 / 42

s

v

v

v

t

1 3

2 4

10

11/16

8/13

12/1215/20

4/9 7/7

4/411/14S T

v

1/4

Cut (S,T): S = {s, v1, v2}, T = {v3, v4, t}.The flow value is |f | = 19 and C(S, T) = 26.Note that, in this example, |f | < C(S,T).

Maximum Flow 13 / 42

Lemma:If S,T is any cut, f any flow, then

|f | ≤ C(S,T).Proof:

|f | = f (s,V)

= f (s,V) + f (S − s,V)

= f (S,V)

= f (S,V)− f (S, S)= f (S,V − S)= f (S, T)≤ C(S, T)

We now develop the Ford-Fulkerson method for findingmax-flows. When FF terminates it provides a flow f and acut S, T such that |f | = C(S,T), so f is maximal.

Maximum Flow 14 / 42

The Ford-Fulkerson Method

Is iterative.Starts with flow f = 0, (∀u, v, f (u, v) = 0)At each step

Constructs a residual network Gf of f indicating howmuch capacity “remains” to be used .

Finds an augmenting path s-t path p in Gf along whichflow can be pushed.

pushes f ′ units of flow along p.Creates new flow f = f + f ′.

Stops when there is no s-t path in current Gf .

S = set of nodes reachable from s in Gf & T = V − S.

At end of algorithm: |f | = C(S, T) ⇒ f is optimalMaximum Flow 15 / 42

Residual networks

Given flow f , the residual network Gf consists of the edgesalong which we can (still) push more flow. The amountthat can (still) be pushed across (u, v) is called the residualcapacity cf (u, v).

cf (u, v) = c(u, v)− f (u, v).

If there is flow from u to v then f (u, v) > 0 and cf (u, v) is theremaining capacity on (u, v).

Maximum Flow 16 / 42

Residual Capacity: cf (u, v) = c(u, v)− f (u, v).

If there is flow from u to v then f (u, v) > 0and cf (u, v) is the remaining capacity on (u, v).

If there is flow from v to u then f (u, v) < 0,and cf (u, v) = c(u, v) + f (v, u) is the capacity of (u, v)plus amount of existing flow that can be pushedbackwards from u to v.

The Residual Network Gf is Gf = (V,Ef ) where

Ef = {(u, v) ∈ V × V : cf (u, v) > 0}

Maximum Flow 17 / 42

s

v

v

v

t

v1 3

2 4

10

11/16

12/12

7/7

4/411/14

19/20

12/13

91/4

A Flow

s

v

v

v

t

v1 3

2 4

3

115

11 7

4

11

12

119

112

9

3

Its residual network

Maximum Flow 18 / 42

Lemma:

Let f be a flow in G = (V,E) and Gf its residual network.Letf ′ be a flow in Gf .

Define f + f ′ as (f + f ′)(u, v) = f (u, v) + f ′(u, v).

Then f + f ′ is a flow in G with value|f + f ′| = |f |+ |f ′|.

Augmenting path p is a simple s-t path in Gf .The residual capacity of a.p. p is

cf (p) = min{cf (u, v) : (u, v) on p}.

Maximum Flow 19 / 42

Let p be an augmenting path in Gf and define

fp(u, v) =

cf (p) if (u, v) is on p−cf (p) if (v, u) is on p0 otherwise

Lemma: If f is a flow and p an a.p.in Gf then:fp is a flow in Gf with |fp| = cf (p) > 0.

f ′ = f + fp is a flow in G with |f ′| = |f |+ |fp| > |f |.

Maximum Flow 20 / 42

s

v

v

v

t

v1 3

2 4

1011

/16

8/13

12/1215/20

4/9 7/7

4/411/14

1/4 s

v

v

v

t

v1 3

2 4

3 4115

11 5 7

3 4

5

11

58

12

15

s

v

v

v

t

v1 3

2 4

10

11/16

12/127/

7

4/411/14

19/20

12/13

91/4 s

v

v

v

t

v1 3

2 4

3

115

11 7

4

11

12

119

112

9

3

An initial flow f .Its residual network Gf and an augmenting path f ′ in Gf .The flow f + f ′ and its residual network.

Maximum Flow 21 / 42

Optimality

Theorem: (Max-Flow Min-Cut Theorem)Let f be a flow.Then the following three conditions are equivalent:

1 f is a maximum flow in G.

2 Gf contains no augmenting paths

3 |f | = C(S,T) for some (S,T) cut.

Maximum Flow 22 / 42

Proof:

(1) ⇒ (2): If Gf contained an augmenting path p then|f + fp| > |f | so f could not be maximal.

(2) ⇒ (3): Let S = {u ∈ V : ∃ path from s to v in Gf}.T = V − S. Then

f (S,T) = f (S,V)−f (S, S) = f (S,V) = f (s,V)+f (S−s,V) = |f |.

Now note that ∀u ∈ S, v ∈ T, f (u, v) = c(u, v) sinceotherwise cf (u, v) > 0 and v ∈ S.Thus C(S,T) = f (S,T) = |f |.

(3) ⇒ (1): We previously saw that every flow f ′ mustsatisfy |f ′| ≤ C(S, T) so if |f | = C(S, T), f must be opti-mal.

Maximum Flow 23 / 42

Theorem Proof

The Ford-Fulkerson Method

Starts with flow f ≡ 0, (∀u, v, f (u, v) = 0)

Construct residual network Gf .If Gf contains no augmenting path, stop(f is optimal by MFMC theorem).Otherwise.

1 Find an augmenting path (s − t path) p in Gf

2 Let fp be the flow in Gf that pushes cf (p) units of flowalong p.

3 Let f = f + fp be new flow in G.

Maximum Flow 24 / 42

FF Example: Steps 1 & 2

s

v

v

v

4

12

79

4

20

13

10 t

v1 3

42

16

14

s

v

v

v

4 7

20

13

t

v1 3

42

4/16

10 4/9

4/12

4/4

4/14

s

v

v

v

13

t

v1 3

42

124

104

4

10 4 54

20

8

4

7 s

v

v

v

4 t

v1 3

42

4/9

4/4

11/16

7/7

11/14

13

7/20

7/10

4/12

Maximum Flow 25 / 42

FF Example: Steps 2 & 3

s

v

v

v

13

t

v1 3

42

124

104

4

10 4 54

20

8

4

7 s

v

v

v

4 t

v1 3

42

4/9

4/4

11/16

7/7

11/14

13

7/20

7/10

4/12

s

v

v

v

t

v1 3

42

45

4

137

13

3

511

48

11

311

7 s

v

v

v

1/4

t

v1 3

42

4/9

4/4

11/16

12/1215/20

7/7

10

8/1311/14

Maximum Flow 26 / 42

FF Example: Steps 3 & 4

s

v

v

v

t

v1 3

42

45

4

137

13

35

1148

11

311

7 s

v

v

v

1/4

t

v1 3

42

4/9

4/4

11/16

12/1215/20

7/7

10

8/1311/14

s

v

v

v

7 t

v1 3

42

54

511

515

85 11

12

113

43 s

v

v

v

t

v1 3

42

4/4

11/16

7/7

11/1410

12/13

1/4 9

12/1219/20

Maximum Flow 27 / 42

FF Example: Steps 4 & 5 (End)

s

v

v

v7 t

v1 3

42

54

511

515

85 11

12

113

43 s

v

v

v

t

v1 3

42

4/4

11/16

7/7

11/14

10

12/13

1/4 9

12/1219/20

s

v

v

v

7 t

v1 3

42

4

5

11

12

113

3

112

9

11911

Maximum Flow 28 / 42

Running Time & Finiteness

The FF method is not a completely defined algorithm sinceit doesn’t specify how to choose the augmenting paths.

In fact, if the capacities are irrational, it is possible that a“bad” way of choosing the a.p. will lead to a non-terminatingalgorithm that will never stop (it will keep on adding cheaperand cheaper augmenting paths).

Maximum Flow 29 / 42

If the capacities are all integers⇒ then each cp will be an integer ≥ 1⇒ the algorithm must terminate after |f ∗| steps,

where f ∗ is a max-flow.

Maintaining the graphs G and Gf and the flow f using ad-jacency lists, while using DFS or BFS to find a s-t path,the algorithm can then be implemented to run in O(|f ∗||E|)time.

Note: This can be normalized to work if the capacities arerational.

Maximum Flow 30 / 42

Running Time

Starts with flow f ≡ 0, O(|E|)

Construct residual network Gf . O(|E|)If Gf contains no augmenting path, stop(f is optimal by MFMC theorem).Otherwise. Can be repeated O(|f ∗|) times.

1 Find an augmenting s − t path p in Gf O(|E|)

2 Let fp be the flow in Gf that pushes cf (p) units of flowalong p.

3 Let f = f + fp be new flow in G. O(|E|)

Maximum Flow 31 / 42

A pathological example in which each augmenting pathonly increases flow value by 1 unit.

s 11,0

00,00

0

1,000,000

u

v 1,000

,000

1,000,000

t s 1

1,000,000

u

v

1,000,000

t

999,9

99

999,9

99

1

1

s

u

v

t

999,9

99

999,9

99

1

1

1

1

999,999

999,9991

s

u

v

t1

1

999,999

1

999,9

98

999,9

98999,999

2

2

s

u

v

t1999,998

999,998

999,9

98

999,9

98

2 2

22

Maximum Flow 32 / 42

The Edmonds-Karp Algorithm

Always choose an augmenting path of minimum-length inGf (where each edge has unit length). This can be done inO(E) time using BFS.

Theorem: The EK alg performs at most O(VE)path-augmentations, so the E.K. alg runs in O(VE2) time.

Let δf (u, v) denote shortest-path distance from u to v in Gf .

The proof of the Theorem is a consequence of the follow-ing two lemmas:

Maximum Flow 33 / 42

Lemma: ∀v ∈ V − {s, t}, δf (s, v) does not decrease after aflow augmentation.

Lemma:Edge (u, v) is critical on a.p. p if cf (u, v) = cf (p).Suppose when running the E.K. algorithm that (u, v) is crit-ical for a.p. p in Gf , and is later critical again for anothera.p. p′ in Gf ′ . Then

δf ′(s, u) ≥ δf (s, u) + 2.

Augmenting paths are simple and do not contain s,t internally, soδf (s, v) is always ≤ |V|−2 (as long as v is reachable). Combiningthe two lemmas therefore shows that no specific edge can be-come critical more than (|V| − 2)/2 = O(|V|) times. Some edgeis critical in each step, so there can be at most O(|V||E|) steps.

Maximum Flow 34 / 42

Application: Max Bipartite Matching

A graph G = (V,E) is bipartite if there exists partition V =L ∪ R with L ∩ R = ∅ and E ⊆ L × R.

A Matching is a subset M ⊆ E such that ∀v ∈ V at mostone edge in M is incident upon v.

The size of a matching is |M| , the number of edges in M.

A Maximum Matching is matching M such that every othermatching M′ satisfies |M′| ≤ M.

Problem: Given bipartite graph G, find a maximum match-ing.

Maximum Flow 35 / 42

A bipartite graph with 2 matchings

L R L R

Maximum Flow 36 / 42

Our approach will be to write the Max Bipartite Matchingproblem as a Max-Flow problem.

Our flow network will be G′ = (V ′,E′) whereV ′ = V ∪ {s, t} andE′ = {(s, u) : u ∈ L} ∪ {(u, v) : u ∈ L, v ∈ R and (u, v) ∈ E}

∪ {(v, t) : t ∈ R}

We also assign∀(u, v) ∈ E′, c(u, v) = 1.

Maximum Flow 37 / 42

Lemma: If f is an integer valued flow in G′ then there is amatching M of G with |f | = |M|.Similarly, if M is a matching of G then there is an integervalued flow f with |f | = |M|.

This almost tells us that Max-Flow solves our problem.The difficulty is that it’s possible that the max-flow mightnot have integer value (it is possible that |f | might be aninteger but some f (u, v) might not be integers).

Maximum Flow 38 / 42

A bipartite graph and its associated flow network.A matching and associated flow are illustrated

L R L R

s t

Maximum Flow 39 / 42

Theorem:

Let G′ = (V ′,E′) be a flow network in which c is integral.Then the max-flow f found by the F.F. method has the prop-erty that

∀u, v, f (u, v)is integer valued.

The proof is by induction on the steps in the FF method.

At each step the current flow f is integer so the residualcapacities are all integer.

This implies that the a.p. found has cf (p) integral, so thenew flow f + f ′ created is also integral.

Maximum Flow 40 / 42

The theorem guarantees that if G′ is the flow network cor-responding to a bipartite matching problem then max flowvalue |f | is the value of a maximum matching.

The flow found by the FF algorithm can be modified to yieldthe max matching.

The FF algorithm run on this special graph will take O(VE)time (why?).

Maximum Flow 41 / 42

Odds and Ends

A faster implementation of the FF method uses theidea of blocking flows developed by Dinic. This ap-proach finds many augmenting paths at once.

A totally different approach to the Max-Flow algorithmis the push-relabel method (see CLRS for details).This can run in O(|V|3) time as compared to the O(|V||E|2)of FF.

General Culture: The max-flow problem can be writtenas a linear program. The FF method is essentially aspecial case of the primal-dual algorithm for solvingcombinatorial Linear Programs.

Maximum Flow 42 / 42