network flow - cs.rit.eduzjb/courses/800/lec16.pdfslides based on kevin wayne / pearson-addison...

27
Slides based on Kevin Wayne / Pearson-Addison Wesley Network Flow

Upload: dongoc

Post on 02-Jun-2019

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley

Network Flow

Page 2: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 3

Flow network.   Abstraction for material flowing through the edges.

–  E.g. traffic on highway, pipes carrying liquid, computer networks   G = (V, E) = directed graph.   Two distinguished nodes: s = source, t = sink.   c(e) = capacity of edge e. c(e) ≥ 0.

Network Flow

s

2

3

4

5

6

7

t

15

5

30

15

10

8

15

9

6 10

10

10 15 4

4 capacity

source sink

Source generates flow and has no incoming edges

Sink absorbs flow and has no outgoing edges

Page 3: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 4

Def. An s-t flow is a function that satisfies:   For each e ∈ E: (capacity)   For each v ∈ V – {s, t}: (conservation)

Def. The value of a flow f is:

Flows

4

0

0

0

0 0

0 4 4

0 0

0

Value = 4 0

f (e)e in to v∑ = f (e)

e out of v∑

0 ≤ f (e) ≤ c(e)

capacity flow

s

2

3

4

5

6

7

t

15

5

30

15

10

8

15

9

6 10

10

10 15 4

4 0

v( f ) = f (e) e out of s∑ .

4

Page 4: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 5

Def. An s-t flow is a function that satisfies:   For each e ∈ E: (capacity)   For each v ∈ V – {s, t}: (conservation)

Def. The value of a flow f is:

Flows

10

6

6

11

1 10

3 8 8

0 0

0

11

capacity flow

s

2

3

4

5

6

7

t

15

5

30

15

10

8

15

9

6 10

10

10 15 4

4 0

Value = 24

f (e)e in to v∑ = f (e)

e out of v∑

0 ≤ f (e) ≤ c(e)

v( f ) = f (e) e out of s∑ .

4

Page 5: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 6

Max flow problem. Find s-t flow of maximum value.

Maximum Flow Problem

capacity

s

2

3

4

5

6

7

t

15

5

30

15

10

8

15

9

6 10

10

10 15 4

4

10

9

9

14

4 10

4 8 9

1

0 0

0

14 flow

0

Value = 28

Page 6: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 7

Towards a Max Flow Algorithm

Greedy algorithm.   Start with f(e) = 0 for all edge e ∈ E.   Find an s-t path P where each edge has f(e) < c(e).   Augment flow along path P.   Repeat until you get stuck.

s

1

2

t

10

10

0 0

0 0

0

20

20

30

Flow value = 0

Page 7: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 8

Towards a Max Flow Algorithm

Greedy algorithm.   Start with f(e) = 0 for all edge e ∈ E.   Find an s-t path P where each edge has f(e) < c(e).   Augment flow along path P.   Repeat until you get stuck.

s

1

2

t

20

Flow value = 20

10

10 20

30

0 0

0 0

0

X

X

X

20

20

20

Page 8: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 9

Towards a Max Flow Algorithm

Greedy algorithm.   Start with f(e) = 0 for all edge e ∈ E.   Find an s-t path P where each edge has f(e) < c(e).   Augment flow along path P.   Repeat until you get stuck.

greedy = 20

s

1

2

t

20 10

10 20

30

20 0

0

20

20

opt = 30

s

1

2

t

20 10

10 20

30

20 10

10

10

20

locally optimality ⇒ global optimality

Page 9: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 10

Residual Graph

Original edge: e = (u, v) ∈ E.   Flow f(e), capacity c(e).

Residual edge.   "Undo" flow sent.   e = (u, v) and eR = (v, u).   Residual capacity:

Residual graph: Gf = (V, Ef ).   Residual edges with positive residual capacity.   Ef = {e : f(e) < c(e)} ∪ {eR : f(e) > 0}.

u v 17

6

capacity

u v 11

residual capacity

6 residual capacity

flow

c f (e) =c(e)− f (e) if e ∈ Ef (e) if eR ∈ E

$ % &

Page 10: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 11

Ford-Fulkerson Algorithm

Basic Algorithm:   Identify an s-t path and augment the flow along that path

–  How to find a path? BFS or DFS –  Bottleneck along path determines amount flow can be augmented

  Still need to include concepts of residual graph and forward/backward edges

Ford-Fulkerson ( G=(V,E), c, s, t ) 1.   Initialize flow f to 0 2.   While exists augmenting path p do 3.   Augment flow f along p 4.   Return f

Page 11: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley

Using the residual edges

Start with our previous greedy flow but create the residual where appropriate…

12

s

1

2

t

10

10

20

20

30

Page 12: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 13

Ford-Fulkerson Algorithm

Complete Algorithm:   Compute residual graph including both forward and backward edges

Ford-Fulkerson ( G=(V,E), c, s, t ) 1.   For every edge e let f(e)=0 2. Construct the residual graph Gf 3.   While exists s-t path in Gf do 4.   Let p be an s-t path in Gf 5.   Let d=mine in p cf(e) 6.   For every e on p do 7.   If e is a forward edge then 8.   f(e)+=d 9.   else 10.   f(reverse(e))-=d 11.   Update Gf (construct new Gf) 12.  Return f

Page 13: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 14

Analysis of Ford-Fulkerson

Questions.   Does it terminate? Assumption. Flow capacities are all integer values. Invariant. Every flow value f(e) and every residual capacities cf (e) remains an integer throughout the algorithm. (Can be proved by induction over the iterations of the while loop) Theorem. Let Then the algorithm terminates in at most v(f*) ≤ C iterations. Pf. Each augmentation increase value by at least 1.

C = c(e)e out of s∑

Page 14: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 15

Analysis of Ford-Fulkerson

Questions.   What is the running time? How much work for each iteration?   Finding an s-t path in the residual graph Gf

–  Gf has at most 2m edges –  Use BFS/DFS: O(m+n) = O(m) since assume G is connected

  Augment flow along the path –  O(n) to update each edge along the path

  Update the residual graph –  O(m) to update each edge

  Total running time for Ford-Fulkerson is: O(mC) –  NOTE – this is pseudo-polynomial (similar to Knapsack problem) –  Running time grows exponentially with the length (number of bits)

in the parameter C

Page 15: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 16

Analysis of Ford-Fulkerson

Questions.   Does it generate the maximum flow? To see this we will introduce the concept of a graph cut

Page 16: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 17

Def. An s-t cut is a partition (A, B) of V with s ∈ A and t ∈ B. Def. The capacity of a cut (A, B) is:

Cuts

s

2

3

4

5

6

7

t

15

5

30

15

10

8

15

9

6 10

10

10 15 4

4

Capacity = 10 + 5 + 15 = 30

A

cap( A, B) = c(e)e out of A∑

Page 17: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 18

s

2

3

4

5

6

7

t

15

5

30

15

10

8

15

9

6 10

10

10 15 4

4 A

Cuts

Def. An s-t cut is a partition (A, B) of V with s ∈ A and t ∈ B.

Def. The capacity of a cut (A, B) is:

cap( A, B) = c(e)e out of A∑

Capacity = 9 + 15 + 8 + 30 = 62

Page 18: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 19

Min s-t cut problem. Find an s-t cut of minimum capacity.

Minimum Cut Problem

s

2

3

4

5

6

7

t

15

5

30

15

10

8

15

9

6 10

10

10 15 4

4 A

Capacity = 10 + 8 + 10 = 28

Page 19: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 20

Flow value lemma. Let f be any flow, and let (A, B) be any s-t cut. Then, the net flow sent across the cut is equal to the amount leaving s.

Flows and Cuts

10

6

6

11

1 10

3 8 8

0 0

0

11

s

2

3

4

5

6

7

t

15

5

30

15

10

8

15

9

6 10

10

10 15 4

4 0

Value = 24

f (e)e out of A∑ − f (e)

e in to A∑ = v( f )

4

A

Page 20: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 21

Flow value lemma. Let f be any flow, and let (A, B) be any s-t cut. Then, the net flow sent across the cut is equal to the amount leaving s.

Flows and Cuts

10

6

6

1 10

3 8 8

0 0

0

11

s

2

3

4

5

6

7

t

15

5

30

15

10

8

15

9

6 10

10

10 15 4

4 0

f (e)e out of A∑ − f (e)

e in to A∑ = v( f )

Value = 6 + 0 + 8 - 1 + 11 = 24

4

11

A

Page 21: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 22

Flow value lemma. Let f be any flow, and let (A, B) be any s-t cut. Then, the net flow sent across the cut is equal to the amount leaving s.

Flows and Cuts

10

6

6

11

1 10

3 8 8

0 0

0

11

s

2

3

4

5

6

7

t

15

5

30

15

10

8

15

9

6 10

10

10 15 4

4 0

f (e)e out of A∑ − f (e)

e in to A∑ = v( f )

Value = 10 - 4 + 8 - 0 + 10 = 24

4

A

Page 22: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 23

Flows and Cuts

Flow value lemma. Let f be any flow, and let (A, B) be any s-t cut. Then Pf.

f (e)e out of A∑ − f (e) = v( f )

e in to A∑ .

v( f ) = f (e)e out of s∑

=v ∈A∑ f (e)

e out of v∑ − f (e)

e in to v∑

%

& '

(

) *

= f (e)e out of A∑ − f (e).

e in to A∑

by flow conservation, all terms except v = s are 0

Page 23: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 24

Flows and Cuts

Weak duality. Let f be any flow, and let (A, B) be any s-t cut. Then the value of the flow is at most the capacity of the cut.

Cut capacity = 30 ⇒ Flow value ≤ 30

s

2

3

4

5

6

7

t

15

5

30

15

10

8

15

9

6 10

10

10 15 4

4

Capacity = 30

A

Page 24: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 25

Weak duality. Let f be any flow. Then, for any s-t cut (A, B) we have v(f) ≤ cap(A, B). Pf.

Flows and Cuts

v( f ) = f (e)e out of A∑ − f (e)

e in to A∑

≤ f (e)e out of A∑

≤ c(e)e out of A∑

= cap(A,B)s

t

A B

7

6

8 4

Page 25: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 26

Certificate of Optimality

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

Value of flow = 28 Cut capacity = 28 ⇒ Flow value ≤ 28

10

9

9

14

4 10

4 8 9

1

0 0

0

14

s

2

3

4

5

6

7

t

15

5

30

15

10

8

15

9

6 10

10

10 15 4

4 0 A

Page 26: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 27

Max-Flow Min-Cut Theorem

Max-flow min-cut theorem. [Ford-Fulkerson 1956] The value of the max flow is equal to the value of the min cut. The Ford-Fulkerson Algorithm generates a max flow To prove this we exhibit an s-t cut (A,B) with capacity equal to the flow generated by the algorithm

Page 27: Network Flow - cs.rit.eduzjb/courses/800/lec16.pdfSlides based on Kevin Wayne / Pearson-Addison Wesley 3 Flow network. Abstraction for material flowing through the edges. – E.g

Slides based on Kevin Wayne / Pearson-Addison Wesley 28

Proof of Max-Flow Min-Cut Theorem

Proof that Ford-Fulkerson generates a maximum flow   Let f be a flow with no augmenting paths.   Let A be set of vertices reachable from s in residual graph.   By definition of A, s ∈ A.   By definition of f, t ∉ A.

v( f ) = f (e)e out of A∑ − f (e)

e in to A∑

= c(e)e out of A∑

= cap(A,B)

original network

s

t

A B

All outgoing edges from A must be used at full capacity All incoming edges to A must be unused