review: augmenting flows compsci 311: introduction to ...marius/class/cs311-fa18/lec17-nup.pdflemma...

5
COMPSCI 311: Introduction to Algorithms Lecture 17: Network Flow Marius Minea University of Massachusetts Amherst slides credit: Dan Sheldon (adapted) Review: Augmenting Flows residual graph; edges: forward (difference), reverse (existing flow) augmenting path: s r in residual graph, bottleneck capacity t s u v 2/2 0/1 0/1 2/3 2/2 G t s u v 2 1 1 2 2 1 G f t s u v 2/2 1/1 1/1 1/3 2/2 new flow Review: Ford-Fulkerson Algorithm Augment flow as long as it is possible while there exists an s-t path P in G f do f = Augment(f , P ) update G f end while return f Relate maximum flow to minimum cut Clicker Question 1 What is the value of the flow across the black-white cut? A: 10 B: 15 C: 20 D: 25 0 / 4 10 / 10 10 / 10 5 / 5 s t 5 / 10 5 / 9 5 / 8 5 / 15 10 / 10 10 / 15 0 / 15 0 / 4 0 / 6 10 / 16 0 / 15 edges from B to A value of flow = 2 Flow Value Lemma First relationship between cuts and flows Lemma: let f be any flow and (A, B) be any s-t cut. Then v(f )= e out of A f (e) - e into A f (e) Proof (see book) use conservation of flow: all the flow out of s must leave A eventually. Rewrite flow as v(f )= vA f out (v) - f in (v) only nonzero difference is f (s) Consider cases: edge in A, leading out of A, leading into A Network flow: quiz 3 Which is the net flow across the given cut? A. 11 (20 + 25 8 11 9 6) B. 26 (20 + 22 8 4 4) C. 42 (20 + 22) D. 45 (20 + 25) 28 8 / 8 5 / 12 4 / 9 8 / 8 14 / 16 1 / 1 capacity s 4 / 8 0 / 6 22 / 25 t 4 / 10 20 / 20 1 / 6 4 / 11 flow slide credit: Kevin Wayne / Pearson

Upload: others

Post on 06-Oct-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Review: Augmenting Flows COMPSCI 311: Introduction to ...marius/class/cs311-fa18/lec17-nup.pdfLemma 2. Let f be the flow at the end of a %-scaling phase. ! Then, the max-flow value

COMPSCI 311: Introduction to AlgorithmsLecture 17: Network Flow

Marius Minea

University of Massachusetts Amherst

slides credit: Dan Sheldon (adapted)

Review: Augmenting Flows

residual graph; edges: forward (difference), reverse (existing flow)

augmenting path: s r in residual graph, bottleneck capacity

ts

u

v

2/2 0/1

0/1

2/3

2/2

G

ts

u

v

2

1

1

2

21

Gf

ts

u

v

2/2 1/1

1/1

1/3

2/2

new flow

Review: Ford-Fulkerson Algorithm

. Augment flow as long as it is possiblewhile there exists an s-t path P in Gf do

f = Augment(f , P )update Gf

end whilereturn f

Relate maximum flow to minimum cut

Clicker Question 1What is the value of the flow across the black-white cut?

A: 10

B: 15

C: 20

D: 25

Relationship between flows and cuts

Flow value lemma. Let f be any flow and let (A, B) be any cut. Then,the value of the flow f equals the net flow across the cut (A, B).

27

0 / 4

10 / 10

10 / 105 / 5s t

5 / 10

5 / 9

5 / 8

5 / 15

10 / 1010 / 15

0 / 150 / 4

0 / 6

10 / 16

0 / 15

net flow across cut = (10 + 10 + 5 + 10 + 0 + 0) – (5 + 5 + 0 + 0) = 25

edges from B to A

value of flow = 25

val(f) =�

e Qmi Q7 A

f(e) ��

e BM iQ A

f(e)

Flow Value Lemma

First relationship between cuts and flows

Lemma: let f be any flow and (A, B) be any s-t cut. Then

v(f) =∑

e out of A

f(e)−∑

e into A

f(e)

Proof (see book) use conservation of flow:all the flow out of s must leave A eventually.

Rewrite flow as v(f) = ∑v∈A fout(v)− f in(v)

only nonzero difference is f(s)

Consider cases: edge in A, leading out of A, leading into A

Network flow: quiz 3

Which is the net flow across the given cut?

A. 11 (20 + 25 − 8 − 11 − 9 − 6)

B. 26 (20 + 22 − 8 − 4 − 4)

C. 42 (20 + 22)

D. 45 (20 + 25)

28

8 / 85 / 12

4 / 9

8 / 8

14 / 161 / 1

capacity

s

4 / 80 /

6

22 / 25 t

4 / 1020 / 20

1 / 64 / 11

flow

slide credit: Kevin Wayne / Pearson

Page 2: Review: Augmenting Flows COMPSCI 311: Introduction to ...marius/class/cs311-fa18/lec17-nup.pdfLemma 2. Let f be the flow at the end of a %-scaling phase. ! Then, the max-flow value

Corollary: Cuts and Flows

Really important corollary of flow-value lemma

Corollary: Let f be any s-t flow and let (A, B) be any s-t cut.Then v(f) ≤ c(A, B).

Proof:v(f) =

e out of A

f(e)−∑

e into A

f(e)

≤∑

e out of A

f(e)

≤∑

e out of A

c(e)

= c(A, B)

Duality: Max Flow – Min Cut

Certificate of optimality

Corollary. Let f be a flow and let (A, B) be any cut.If val( f ) = cap(A, B), then f is a max flow and (A, B) is a min cut.

Pf.

・For any flow f ʹ: val( f ʹ) ≤ cap(A, B) = val( f ).

・For any cut (Aʹ, Bʹ): cap(Aʹ, Bʹ) ≥ val( f ) = cap(A, B). ▪

31

s t

0 / 4

10 / 10

10 / 105 / 5

8 / 10

8 / 9

8 / 8

2 / 15

10 / 10

13 / 15

0 / 4

3 / 6

13 / 16

0 / 15

0 / 15

s

10

8 t

10

weak duality

value of flow = 28 capacity of cut = 28=

weak duality

Claim If there is a flow f∗ and cut (A∗, B∗) such thatv(f∗) = c(A∗, B∗), thenI f∗ is a maximum flowI (A∗, B∗) is a minimum cut

F-F returns a maximum flowTheorem: The s-t flow f returned by F-F is a maximum flow.

I Since f is the final flow there are no residual paths in Gf .

I Let (A, B) be the s-t cut where A consists of all nodes reachablefrom s in the residual graph.I Any edge out of A must have f(e) = c(e) otherwise there

would be more nodes than just A that reachable from s.I Any edge into A must have f(e) = 0 otherwise there would

be more nodes than just A that reachable from s.

I Therefore v(f) =∑

e out ofAf(e)−

e intoA

f(e)

=∑

e out ofAc(e) = c(A, B)

F-F finds a minimum cutTheorem: The cut (A, B) where A is the set of all nodes reachablefrom s in the residual graph is a minimum-cut.

Gf

v1 v2

v3 v4

ts

11

5

1

12

319

3

11

71

19

4

12

G

v1 v2

v3 v4

ts

11/16

12/13

11/14

0/9

12/12

1/47/7

19/20

4/4

F-F finds a minimum cut

Capacity 163,000 tons per day [Harris and Ross 1955]

Ford-Fulkerson Running Time

I Flow increases at least one unit per iteration

I F-F terminates in at most Cs iterations, where Cs is sum ofcapacities leaving source.

I Cs ≤ n Cmax (in terms of maximum edge capacity)

I Running time: O(m n Cmax)

Is this polynomial? pseudo-polynomial (exponential in log Cmax)

Page 3: Review: Augmenting Flows COMPSCI 311: Introduction to ...marius/class/cs311-fa18/lec17-nup.pdfLemma 2. Let f be the flow at the end of a %-scaling phase. ! Then, the max-flow value

Ford–Fulkerson: exponential example

Q. Is generic Ford–Fulkerson algorithm poly-time in input size?

A. No. If max capacity is C, then algorithm can take ≥ C iterations.

・s→v→w→t

・s→w→v→t

・s→v→w→t

・s→w→v→t

・…

・s→v→w→t

・s→w→v→t

38

m, n, and log C

each augmenting path sends only 1 unit of flow

(# augmenting paths = 2C)

1

C

C

C

C

t

s

v w

slide credit: Kevin Wayne / Pearson

Improving Running Time

Choose good augmenting paths, withI Large enough bottleneck capacity

Maximum hard to find, but can cleverly search for good values

I Fewest edges (Edmonds-Karp, Dinitz)

Capacity-scaling algorithm

Overview. Choosing augmenting paths with “large” bottleneck capacity.

・Maintain scaling parameter Δ.

・Let Gf (Δ) be the part of the residual network containingonly those edges with capacity ≥ Δ.

・Any augmenting path in Gf (Δ) has bottleneck capacity ≥ Δ.

42Gf

t

s

1

122

102

170

110

Gf (Δ), Δ = 100

t

s

122

102

170

110

though not necessarily largest

slide credit: Kevin Wayne / Pearson

Capacity-scaling algorithm

43

CAPACITY-SCALING(G) __________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

FOREACH edge e ∈ E : f (e) ← 0.

Δ ← largest power of 2 ≤ C.

WHILE (Δ ≥ 1)

Gf (Δ) ← Δ-residual network of G with respect to flow f .WHILE (there exists an s↝t path P in Gf (Δ))

f ← AUGMENT( f, c, P).

Update Gf (Δ).

Δ ← Δ / 2.

RETURN f.__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Δ-scaling phase

slide credit: Kevin Wayne / Pearson

Capacity-Scaling: Running Time

How many scaling phases? log Cmax (precisely: 1 + blog Cmaxc)How much does the flow increase at every augmentation? ≥ ∆

How many augmentations per phase?

Intuition: at end of each ∆ phase, residual capacity on an edge inminimum cut less than ∆, else we would have augmented more.

Lemma 2. Let f be the flow at the end of a Δ-scaling phase.Then, the max-flow value ≤ val( f ) + m Δ.

Pf.

・We show there exists a cut (A, B) such that cap(A, B) ≤ val( f ) + m Δ.

・Choose A to be the set of nodes reachable from s in Gf (Δ).

・By definition of A: s ∈ A.

・By definition of flow f: t ∉ A.

t

Capacity-scaling algorithm: analysis of running time

46

original flow network

s

A B

edge e = (v, w) with v ∈ B, w ∈ A must have f(e) < Δ

edge e = (v, w) with v ∈ A, w ∈ B must have f(e) > c(e) – Δ

val(f) =�

e Qmi Q7 A

f(e) ��

e BM iQ A

f(e)

��

e Qmi Q7 A

(c(e) ��) ��

e BM iQ A

��

e Qmi Q7 A

c(e) ��

e Qmi Q7 A

� ��

e BM iQ A

� cap(A, B) � m�

flow value lemma �

e Qmi Q7 A

(c(e) ��) ��

e BM iQ A

slide credit: Kevin Wayne / Pearson

Page 4: Review: Augmenting Flows COMPSCI 311: Introduction to ...marius/class/cs311-fa18/lec17-nup.pdfLemma 2. Let f be the flow at the end of a %-scaling phase. ! Then, the max-flow value

Capacity-scaling algorithm: analysis of running time

Lemma 1. There are 1 + ⎣log2 C⎦ scaling phases.

Pf. Initially C / 2 < Δ ≤ C; Δ decreases by a factor of 2 in each iteration. ▪ Lemma 2. Let f be the flow at the end of a Δ-scaling phase. Then, the max-flow value ≤ val( f ) + m Δ.

Pf. Next slide.

Lemma 3. There are ≤ 2m augmentations per scaling phase.

Pf.

・Let f be the flow at the beginning of a Δ-scaling phase.

・Lemma 2 ⇒ max-flow value ≤ val( f ) + m (2 Δ).

・Each augmentation in a Δ-phase increases val( f ) by at least Δ. ▪ Theorem. The capacity-scaling algorithm takes O(m2 log C) time.

Pf.

・Lemma 1 + Lemma 3 ⇒ O(m log C) augmentations.

・Finding an augmenting path takes O(m) time. ▪ 45

or equivalently, at the end

of a 2Δ-scaling phase

slide credit: Kevin Wayne / Pearson

Shortest augmenting path

Q. How to choose next augmenting path in Ford–Fulkerson?

A. Pick one that uses the fewest edges.

48

SHORTEST-AUGMENTING-PATH(G) _________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

FOREACH e ∈ E : f (e) ← 0.

Gf ← residual network of G with respect to flow f.WHILE (there exists an s↝t path in Gf )

P ← BREADTH-FIRST-SEARCH(Gf ).

f ← AUGMENT( f, c, P).

Update Gf.

RETURN f._________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

can find via BFS

slide credit: Kevin Wayne / Pearson

Network flow: quiz 6

How to compute the level graph LG efficiently?

A. Depth-first search.

B. Breadth-first search.

C. Both A and B.

D. Neither A nor B.

70

C

D

A E F

B

0 1

1

2

3

3

source sink

slide credit: Kevin Wayne / Pearson

Shortest augmenting path: overview of analysis

Lemma 1. The length of a shortest augmenting path never decreases.

Pf. Ahead.

Lemma 2. After at most m shortest-path augmentations, the length of a

shortest augmenting path strictly increases.

Pf. Ahead.

Theorem. The shortest-augmenting-path algorithm takes O(m2 n) time.

Pf.

・O(m) time to find a shortest augmenting path via BFS.

・There are ≤ m n augmentations. - at most m augmenting paths of length k- at most n−1 different lengths ▪

49

Lemma 1 + Lemma 2

augmenting paths are simple paths

number of edges

slide credit: Kevin Wayne / Pearson

Shortest augmenting path: analysis

Def. Given a digraph G = (V, E) with source s, its level graph is defined by:

・ℓ(v) = number of edges in shortest s↝v path.

・LG = (V, EG) is the subgraph of G that contains only those edges (v, w) ∈ E

with ℓ(w) = ℓ(v) + 1.

50

s t

graph G

s t

level graph LG

ℓ= 0 ℓ= 1 ℓ= 2 ℓ= 3

slide credit: Kevin Wayne / Pearson

Network flow: quiz 5

Which edges are in the level graph of the following digraph?

A. D→F.

B. E→F.

C. Both A and B.

D. Neither A nor B.

51

C

D

A E F

B

0 1

1

2

3

3

source sink

slide credit: Kevin Wayne / Pearson

Page 5: Review: Augmenting Flows COMPSCI 311: Introduction to ...marius/class/cs311-fa18/lec17-nup.pdfLemma 2. Let f be the flow at the end of a %-scaling phase. ! Then, the max-flow value

Shortest augmenting path: analysis

Def. Given a digraph G = (V, E) with source s, its level graph is defined by:

・ℓ(v) = number of edges in shortest s↝v path.

・LG = (V, EG) is the subgraph of G that contains only those edges (v, w) ∈ E

with ℓ(w) = ℓ(v) + 1.

Key property. P is a shortest s↝v path in G iff P is an s↝v path in LG.

52

level graph LG

s t

ℓ= 0 ℓ= 1 ℓ= 2 ℓ= 3

slide credit: Kevin Wayne / Pearson

Shortest augmenting path: analysis

Lemma 1. The length of a shortest augmenting path never decreases.

・Let f and f ʹ be flow before and after a shortest-path augmentation.

・Let LG and LG ʹ be level graphs of Gf and Gf ʹ . ・Only back edges added to Gf ′

(any s↝t path that uses a back edge is longer than previous length) ▪

53s t

level graph LG′

ℓ= 0

level graph LG

ℓ= 1 ℓ= 2 ℓ= 3

s t

slide credit: Kevin Wayne / Pearson

Lemma 2. After at most m shortest-path augmentations, the length of a

shortest augmenting path strictly increases.

・At least one (bottleneck) edge is deleted from LG per augmentation.

・No new edge added to LG until shortest path length strictly increases. ▪

Shortest augmenting path: analysis

54

ℓ= 0

level graph LG

ℓ= 1 ℓ= 2 ℓ= 3

s t

level graph LG′

s t

slide credit: Kevin Wayne / Pearson

Shortest augmenting path: review of analysis

Lemma 1. Throughout the algorithm, the length of a shortest augmenting

path never decreases.

Lemma 2. After at most m shortest-path augmentations, the length of ashortest augmenting path strictly increases.

Theorem. The shortest-augmenting-path algorithm takes O(m2 n) time.

55

slide credit: Kevin Wayne / Pearson