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
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.
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!
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
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
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'
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'
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)
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
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
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
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
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.
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.
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.
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
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
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
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
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
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
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
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
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