440 likes | 578 Views
Design & Analysis of Algorithms COMP 482 / ELEC 420 John Greiner. Flow networks. What if weights in a graph are maximum capacities of some flow of material? Pipe network to transport fluid/gas (e.g., water, oil, natural gas, CO 2 ) Edges – pipes Vertices – junctions of pipes
E N D
Design & Analysis of Algorithms COMP 482 / ELEC 420John Greiner
Flow networks What if weights in a graph are maximum capacities of some flow of material? • Pipe network to transport fluid/gas (e.g., water, oil, natural gas, CO2) • Edges – pipes • Vertices – junctions of pipes • Data communication network • Edges – network connections of different capacity • Vertices – routers (do not produce or consume data just move it) • Also used in resource planning, economics, ecosystem network analysis To do: [CLRS] 26 #7
Flow Network Definitions Directed graph G=(V,E) One source and one sink. 13 a b 15 19 5 10 s e t 4 9 5 14 3 c d 11 Each edge has capacity c(u,v) 0. Each vertex is on some path from s to t.
Flow Definitions How much is currently flowing – f : V V ® 2/13 a b 3/15 3/19 Must satisfy 3 properties: • 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}: f(u,V) = f(V,u) = 0 What goes in must go out. 1/5 0/10 s e t 1/4 0/9 1/5 2/14 2/3 c d Total value of flow f: |f| = f(s,V) = f(V,t) 3/11
Example flows Valid or invalid? Why? 0/13 1/13 2/13 a a a b b b 1/15 2/15 0/15 1/19 1/19 0/19 1/5 0/5 0/5 1/10 0/10 2/10 s s s e e e t t t 0/4 1/4 0/4 1/9 0/9 0/9 0/5 0/5 1/5 1/14 4/14 0/14 1/3 0/3 5/3 c c c d d d 0/11 5/11 1/11
Maximize the flow 13 a b 15 19 5 10 s e t 4 9 5 14 3 c d 11
Maximum flow: Algorithm idea • If we have some flow, … • …and can find an augmenting path can add a constant amount of flow along path: a>0, (u,v)p, f(u,v) + a £c(u,v) • Then just do it, to get a better flow! 2/13 a b 3/15 2/19 1/5 p 1/10 s e t 1/4 0/9 s t 1/5 1/14 2/3 c d 3/11
Ford-Fulkerson method Ford-Fulkerson(G,s,t) 1 initialize flow f to 0 everywhere 2 while there is an augmenting path p do 3 augment flow f along p 4 return f • How do we find/choose an augmenting path? • How much additional flow can we send through that path? • Does the algorithm always find the maximum flow?
Augmenting Augmenting path – any path in the residual network: • Residual network: Gf=(V,Ef) Ef = {(u,v) Î V V : cf(u,v) > 0} • Residual capacities: cf(u,v) = c(u,v) – f(u,v) • Residual capacity of path p: 2/13 a b 3/15 2/19 1/5 ? 1/10 s e t 1/4 0/9 Residual ? 1/5 1/14 2/3 c d 3/11 Residual capacity of path (s,c,d,t)? Observe – Edges in Ef are either edges in E or their reversals: |Ef| 2|E|.
Ford-Fulkerson method, with details Algorithms based on this method differ in how they choose p. Ford-Fulkerson(G,s,t) 1 for each edge (u,v)G.E do 2 f(u,v) = f(v,u) = 0 3 while path p from s to t in residual network Gfdo 4 cf = min{cf(u,v): (u,v)p} 5 for each edge (u,v) in p do 6 f(u,v) = f(u,v) + cf 7 f(v,u)=-f(u,v) 8 return f
Does it always find a maximum flow? Cut–a partition of V into S,T such that sÎS, tÎT Minimum cut– a cut with minimum capacity S T 2/13 |f| = f(S,T) a b 3/15 2/19 1/5 1/10 s e t 1/4 0/9 1/5 1/14 2/3 c d 3/11
Does it always find a maximum flow? 1. We will prove three parts: From this we have 2.1., which means that the Ford-Fulkerson method always correctly finds a maximum flow. 3. 2. Max-flow min-cut theorem: The following are equivalent statements: • f is a maximum flow in G. • The residual network Gf contains no augmenting paths. • |f| = c(S,T), for some cut (S,T) of G.
What is the worst-case running time? 100 a 100 s 1 t 100 100 b • Augmentation = ? • How many augmentations? • Let’s assume integer flows. • Each increases the value of the flow by some integer. • O(|f*|), where f* is the max-flow. • Total worst-case = O(E|f*|). • How an augmenting path is chosen is very important! O(E)
Edmonds-Karp Algorithm Use shortest augmenting path (in #edges). Run algorithm on our example: 13 a b 15 19 5 10 s e t 4 9 5 14 3 c d 11
Edmonds-Karp algorithm analysis: 1 Augmentation = O(E) – Breadth-first search Will prove: #augmentations = O(VE). Let d(v) be distance from s to v in residual network. Will prove: Every |E| iterations, d(t) increases by 1. d(t) can increase at most |V| times O(VE) iterations Total = O(VE2)
Edmonds-Karp algorithm analysis: 2 0 1 2 3 Will prove: Every |E| iterations, d(t) increases by 1. Consider the residual network in levels according to d(v): As long as d(t) doesn't change, the paths found will only use forward edges. • Each iteration saturates & removes at least 1 forward edge, and adds only backward edges (so no distance ever drops). • After removing |E| - d(t) +1 forward edges, t will be disconnected from s. So, within |E| iterations, either • t is disconnected, & algorithm terminates, or • A non-forward edge used, & d(t) increased. 11 a b 12 2 17 3 4 1 2 9 s e t 3 9 1 2/13 1 2 1 4 a b 1 3/15 2/19 1 3 13 1/5 c d 1/10 s e t 1/4 0/9 8 1/5 1/14 2/3 c d 3/11
Antiparallel edges Allow: Flows cancel. Residual (multi)graph can have parallel edges. Disallow: 5 9 5 9 5
What if we have multiple sources or sinks? 13 s1 t1 13 s1 t1 11 7 ¥ 11 ¥ 7 4 5 10 4 5 10 ¥ 11 ¥ s s2 t2 t 11 s2 t2 4 8 4 8 6 6 s3 s3
Maximum Flow: Another Algorithm Idea 13 a b 15 19 10 4 9 5 s t 14 3 c d 11 Greedily fill outgoing edges to capacity. Later edges’ capacity constraints can lead us to reduce those flows.
Push-Relabel Example – Goldberg & Tarjan (1986) s 15/15 14/14 0/5 0/13 0/10 0/11 0/3 d a b c t 0/4 0/9 0/19
Relabel a s 0/14 0/15 0/5 a 0/4 0/13 0/10 0/11 0/3 d b c t 0/9 0/19
Push Preflow from a to b & c s 15/15 14/14 0/5 a 2/4 13/13 0/10 0/11 0/3 d b c t 0/9 0/19
Relabel b s 15/15 14/14 0/5 13/13 a b 0/9 2/4 0/11 0/3 d c t 0/10 0/19
Push Preflow from b s 15/15 14/14 0/5 13/13 a b 0/9 2/4 0/11 0/3 d c t 0/10 13/19
Relabel c s 15/15 14/14 0/5 13/13 a b c 0/10 0/11 0/3 d t 0/9 2/4 13/19
Push Preflow from c to d s 15/15 14/14 0/5 13/13 a b c 0/10 11/11 0/3 d t 0/9 2/4 13/19
Relabel c s 15/15 14/14 c 2/4 0/10 11/11 13/13 a b 0/5 0/3 d t 0/9 13/19
Push Preflow from c to a s 15/15 14/14 c 0/4 0/10 11/11 13/13 a b 0/5 0/3 d t 0/9 13/19
Relabel a & Push Preflow from a to c s 14/14 15/15 a 2/4 c 13/13 0/10 11/11 b 0/5 0/3 d t 0/9 13/19
Relabel & Push c, Relabel & Push a, Relabel & Push c 14/14 s c 6 0/4 15/15 a 5 0/10 11/11 13/13 b 1 0/5 0/3 d t 0 0/9 13/19
Relabela & Push Preflow from a to s a 7 13/15 0/4 s c 6 14/14 13/13 0/10 11/11 b 1 0/5 0/3 d t 0 0/9 13/19
Relabel c & Push Preflow from c to s 0/4 a c 7 13/15 s 6 11/14 13/13 0/10 11/11 b 1 0/5 0/3 d t 0 0/9 13/19
Relabel d & Push Preflow from d to t 0/4 a c 7 13/15 s 6 11/14 13/13 0/10 11/11 0/5 b d 1 0/9 3/3 13/19 t 0
Relabel d & Push Preflow from d to b 0/4 a c 7 13/15 s 6 11/14 13/13 0/10 11/11 d 2 5/5 3/3 b 0/9 1 13/19 t 0
Push Preflow from b to t 0/4 a c 7 13/15 s 6 11/14 13/13 0/10 11/11 d 2 5/5 3/3 b 0/9 1 18/19 t 0
Relabel d & Push Preflow from d to c d 8 8/11 0/4 a c 7 13/15 s 6 5/5 11/14 13/13 0/10 0/9 3/3 b 1 18/19 t 0
Push Preflow from c to s d 8 8/11 0/4 a c 7 13/15 s 6 5/5 8/14 13/13 0/10 0/9 3/3 b 1 18/19 t 0
Correctness Overview d 8 8/11 0/4 a c 7 13/15 s 6 13/13 0/10 5/5 0/9 3/3 8/14 b 1 18/19 t 0 Is this a flow? Is it maximum? – What is the residual graph?
Correctness Depends on Height Invariant Invariant: Residual edges go down in height by at most one. But, height(s) – height(t) = |V|, longer than any s-t path.
Running Time Overview Max height = . Overall algorithm is . • Requires amortized analysis. • See text & homework. Choosing operation order wisely, can reduce to .
Two Other Approaches Blocking flows: • Each s-t path in blocking flow contains a saturated edge. • Dinitz/Dinic (1970) O(VE log V) • Goldberg & Rao (1997) O(min(V2/3,E1/2) E log(V2/E) log (max(u,v)E c(u,v))) Combinatorial game: • Cheriyan, Hagerup (1989) O(VE + V2 log2 V) expected • King, Rao, & Tarjan (1994) O(VE logE/(V log V) V)
An Application of Max-Flow Bipartite: V = L+R E L×R A maximum E.g., dating agency Maximum bipartite matching problem Matching in a graph is a subset M of edges such that each vertex has at most one edge of M incident on it. It puts vertices in pairs. E.g.?
Maximum Bipartite Matching • How can we reformulate as a max-flow problem? • What is the running time, using Edmonds-Karp? V = L+R E L×R