Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004...

24
Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley http://www.cs.Princeton.EDU/cos423 Max Flow: Shortest Augmenting Path

Transcript of Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004...

Page 1: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

Algorithm Design by Éva Tardos and Jon Kleinberg • Lecture Notes by Kevin Wayne • Copyright © 2004 Addison Wesley • http://www.cs.Princeton.EDU/cos423

Max Flow: Shortest Augmenting Path

Page 2: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

2

Choosing Good Augmenting Paths

Use care when selecting augmenting paths. Some choices lead to exponential algorithms. Clever choices lead to polynomial algorithms. If capacities are irrational, algorithm not guaranteed to terminate!

Goal: choose augmenting paths so that: Can find augmenting paths efficiently. Few iterations.

Choose augmenting paths with: (Edmonds-Karp, 1972) Max bottleneck capacity. Sufficiently large capacity. Fewest number of edges.

Page 3: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

3

Shortest Augmenting Path

Shortest augmenting path. Find augmenting path with fewest number of edges. BFS is an efficient implementation.

Shortest-Augmenting-Path(G, s, t, c) { foreach e E f(e) 0 Gf residual graph

while (there exists an augmenting path) { find such a shortest such path P using BFS f Augment(f, c, P) update Gf

} return f}

may implement by accident!

Page 4: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

4

Shortest Augmenting Path: Overview of Analysis

Proof outline.

L1. Throughout the algorithm, the length of the shortest path never decreases.

L2. After at most m shortest path augmentations, the length of the shortest augmenting path strictly increases.

Theorem. The shortest augmenting path algorithm performs at most O(mn) augmentations. It can be implemented in O(m2n) time.

O(m) time to find shortest augmenting path via BFS. O(m) augmentations for paths of exactly k edges. If there is an augmenting path, there is a simple one.

1 k < n O(mn) augmentations. ▪

proof ahead

proof ahead

Page 5: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

5

Shortest Augmenting Path: Analysis

Level graph. G = (V, E), s For each vertex v, define (u) to be the length of shortest s-u

path. L = (V, F) is subgraph of G that contains only those edges (u, v)

E with (v) = (u) + 1. Compute in O(m+n) time using BFS, deleting back and side

edges. P is a shortest s-u path in G iff it is an s-u path LG.

s

2

3

5

6 t

= 0

= 1

= 2

= 3

L:

number of edges

Page 6: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

6

Shortest Augmenting Path: Analysis

L1. Throughout the algorithm, the length of the shortest path never decreases.

Let f and f' be flow before and after a shortest path augmentation.

Let L and L' be level graphs of Gf and Gf '

Only back edges added to Gf '

Path with back edge has length greater than previous length.

s

2

3

5

6 t

= 0 = 1 = 2 = 3

s

2

3

5

6 t

L

L'

Page 7: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

7

Shortest Augmenting Path: Analysis

L2. After at most m shortest path augmentations, the length of the shortest augmenting path strictly increases.

At least one edge (the bottleneck edge) is deleted from L after each augmentation.

No new edges added to L until length of shortest path strictly increases.

s

2

3

5

6 t

= 0 = 1 = 2 = 3

s

2

3

5

6 t

L

L'

Page 8: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

8

Shortest Augmenting Path: Review of Analysis

L1. Throughout the algorithm, the length of the shortest path never decreases.

L2. After at most m shortest path augmentations, the length of the shortest augmenting path strictly increases.

Theorem. The shortest augmenting path algorithm performs at most O(mn) augmentations. It can be implemented in O(m2n) time.

Note: (mn) augmentations necessary on some networks. Try to decrease time per augmentation instead. Dynamic trees O(mn log n) Sleator-Tarjan, 1983 Simple idea O(mn2)

Page 9: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

9

Shortest Augmenting Path: Improved Version

Two types of augmentations. Normal augmentation: length of shortest path doesn't change. Special augmentation: length of shortest path strictly

increases.

L3. Group of normal augmentations takes O(mn) time. Explicitly maintain level graph - it changes by at most 2n edges

after each normal augmentation. Start at s, advance along an edge in L until reach t or get stuck.

– if reach t, augment and delete at least one edge– if get stuck, delete node

s

2

3

5

6 t

= 0 = 1 = 2 = 3

L

Page 10: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

10

Shortest Augmenting Path: Improved Version

s

2

3

5

6 t

L

s

2

3

5

6 t

L

s

2 5

6 t

L

augment

augment

delete 3

bottleneck edge

got stuck,delete node

bottleneck edge

Page 11: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

11

Shortest Augmenting Path: Improved Version

s

2 5

6 t

L

s

2 5

6 t

L

Stop: length of shortest path must have strictly increased.

bottleneck edge

augment

Page 12: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

12

Shortest Augmenting Path: Improved Version

Advance-Retreat(V, E, f, s, t) { array pred[u V] L level graph of Gf

u s, pred[v] nil

repeat { while (there exists (u, v) L) { pred[v] u, u v if (u = t) { P path defined by pred[] f Augment(f, P) update L u s, pred[u] nil } } delete u from L } until (u = s)

return f}

advance

retreat

Page 13: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

13

Shortest Augmenting Path: Improved Version

Two types of augmentations. Normal augmentation: length of shortest path doesn't change. Special augmentation: length of shortest path strictly

increases.

L3. Group of normal augmentations takes O(mn) time. At most n advance steps before you either

– get stuck: delete a node from level graph– reach t: augment and delete an edge from level graph

Theorem. Algorithm runs in O(mn2) time. O(mn) time between special augmentations. At most n special augmentations.

Page 14: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

14

Choosing Good Augmenting Paths: Summary

Method Augmentations

Augmenting path nU

Max capacity m log C

Capacity scaling m log C

Shortest path mn

Asymptotic time

mnC †

m log C (m + n log n) †

m2 log C †

m2n

Improved shortest path mn mn2

Improved capacity scaling m log C mn log C †

† Edge capacities are between 1 and C.

Page 15: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

15

History of Worst-Case Running Times

Dantzig

Discoverer

Simplex

Method Asymptotic Time

m n2 C †1951

Year

Ford, Fulkerson Augmenting path m n C †1955

Edmonds-Karp Shortest path m2 n1970

Dinitz Improved shortest path m n21970

Edmonds-Karp, Dinitz Capacity scaling m2 log C †1972

Dinitz-Gabow Improved capacity scaling m n log C †1973

Karzanov Preflow-push n31974

Sleator-Tarjan Dynamic trees m n log n1983

Goldberg-Tarjan FIFO preflow-push m n log (n2 / m)1986

. . . . . . . . .. . .

Goldberg-Rao Length function m3/2 log (n2 / m) log C † mn2/3 log (n2 / m) log C †1997

Edmonds-Karp Fattest path m log U (m log n) †1970

† Edge capacities are between 1 and C.

Page 16: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

16

Unit Capacity Simple Networks

Unit capacity simple network. Every edge capacity is one. Every node has either:

(i) at most one incoming edge, or(ii) at most one outgoing edge.

If G is simple unit capacity, then so isGf, assuming f is 0-1 flow.

Shortest augmenting path algorithm. Normal augmentation: length of shortest path doesn't change. Special augmentation: length of shortest path strictly increases.

Theorem. Shortest augmenting path algorithm runs in O(m n1/2) time.

L1. Each phase of normal augmentations takes O(m) time. L2. After at most n1/2 phases, | f | | f *| - n1/2. L3. After at most n1/2 additional augmentations, flow is optimal.

1

1

1

Page 17: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

17

Unit Capacity Simple Networks

Level graph

Augment

Lemma 1. Phase of normal augmentations takes O(m) time. Start at s, advance along an arc in LG until reach t or get stuck.

– if reach t, augment and delete ALL arcs on path– if get stuck, delete node and go to previous node

Page 18: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

18

Unit Capacity Simple Networks

Level graph

Delete node and retreat

Lemma 1. Phase of normal augmentations takes O(m) time. Start at s, advance along an arc in LG until reach t or get stuck.

– if reach t, augment and delete ALL arcs on path– if get stuck, delete node and go to previous node

Page 19: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

19

Unit Capacity Simple Networks

Level graph

Augment

Lemma 1. Phase of normal augmentations takes O(m) time. Start at s, advance along an arc in LG until reach t or get stuck.

– if reach t, augment and delete ALL arcs on path– if get stuck, delete node and go to previous node

Page 20: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

20

Unit Capacity Simple Networks

Level graph

STOPLength of shortest path has increased.

Lemma 1. Phase of normal augmentations takes O(m) time. Start at s, advance along an arc in LG until reach t or get stuck.

– if reach t, augment and delete ALL arcs on path– if get stuck, delete node and go to previous node

Page 21: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

21

Unit Capacity Simple Networks

Lemma 1. Phase of normal augmentations takes O(m) time. Start at s, advance along an arc in LG until reach t or get stuck.

– if reach t, augment and delete ALL arcs on path– if get stuck, delete node and go to previous node

O(m) running time.– O(m) to create level graph– O(1) per arc, since each arc traversed at most once– O(1) per node deletion

Page 22: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

22

Unit Capacity Simple Networks

ARRAY pred[v V]LG level graph of Gfv s, pred[v] nil

REPEAT WHILE (there exists (v,w) LG) pred[w] v, v w IF (v = t) P path defined by pred[] f augment(f, P) update LG v s, pred[v] nil delete v from LG UNTIL (v = s)

RETURN f

AdvanceRetreat(V, E, f, s, t)

advance

retreat

augment

Page 23: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

23

Unit Capacity Simple Networks

Lemma 2. After at most n1/2 phases, | f | | f *| - n1/2. After n1/2 phases, length of shortest augmenting path is > n1/2. Level graph has more than n1/2 levels. Let 1 h n1/2 be layer with min number of nodes: |Vh| n1/2.

VhV0 Vn1/2

Level graph

V1

Page 24: Algorithm Design by Éva Tardos and Jon Kleinberg Lecture Notes by Kevin Wayne Copyright © 2004 Addison Wesley  Max Flow:

24

Unit Capacity Simple Networks

Lemma 2. After at most n1/2 phases, | f | | f *| - n1/2. After n1/2 phases, length of shortest augmenting path is > n1/2. Level graph has more than n1/2 levels. Let 1 h n1/2 be layer with min number of nodes: |Vh| n1/2. S := {v : (v) < h} {v : (v) = h and v has 1 outgoing

residual arc}. capf (S, T) |Vh| n1/2 | f | | f *| - n1/2.

VhV0 Vn1/2 V1

Level graphResidual arcs