Algorithm Design and Analysis (ADA) 242-535, Semester 1 2014-2015 12. Maxflow Networks v.2 Objectives describe the maxflow problem, explain and analyse the Ford-Fulkerson, Edmonds-Karp look at the mincut problem, bipartite matching and edge disjoint paths
Overview A Flow Network Ford-Fulkerson (FF) Algorithm Edmonds-Karp (EK) Algorithm The Mincut Problem Bipartite Matching Edge Disjoint Paths Making Maxflow Faster
1. A Flow Network 'pipe' capacity for 'water' flow x 20 10 30 s t source terminal (sink) 20 10 y What is the maximum flow f from s to t?
Answer flow / capacity x 20/20 10/10 10/30 s t 20/20 10/10 y flows must be integer 10/30 s t 20/20 10/10 y Maxflow f = 30 = ∑ outflow of s = ∑ inflow of t
Flow Rules Capacity constraint Flow conservation inflow at v = 0 ≤ edge's flow ≤ edge's capacity Flow conservation inflow = outflow at every vertex (except s and t) inflow at v = 5 + 5 + 0 = 10 outflow at v = 10 + 0 = 10
Another Example A maxflow f in G is 19
Many Applications of Maxflow Bipartite matching Data mining Network reliability Image processing Network connectivity Distributed computing many more ... blood flow analysis
2. Ford-Fulkerson (FF) Algorithm void FF(Graph G, Node s, Node t) { initialize flows on all edges in G to 0; change G into residual graph Gf; while (can find an augmented path P in Gf) { bottleneck b = minimum of edge weights in P; augment flow along P in G using b; regenerate Gf from updated G; } report maxflow f in G; }
Graph Residual Graph Every flow/capacity edge in the graph G becomes two edges in the residual graph Gf forward edge: max amount can increase flow flow / capacity Gf 11 G 6 / 17 u v u v 6 values on edges called residual capacities backward edge: max amount can decrease flow
2.1 Example: initial G Gf x x G to Gf s t s t y y x 0/10 x 0/20 20 10 G to Gf s t s t 0/30 30 10 0/10 0/20 20 y y 20 x 10 To make Gf less messy, 0 weighted edges are not drawn. s t 30 10 20 y
Find an Augmented Path P in Gf In FF, an augmented path is any path from s to t in the residual graph Gf forward and backward edges can be used (if not 0) 20 x One possible augmented path P: s x y t 10 s t 20 30 30 20 10 20 y Bottleneck b = min of edge weights in P = 20
Augment Flow along P in G using b The flow fe on each edge e in G can change in two ways: fe = fe + b in G if the edge in P is a forward edge i.e. increase the flow fe = fe – b in G if the edge in P is a backward edge i.e. decrease the flow
Update G x x G to Gf s t s t y y One augmented path P: s x y t x 0/10 0/20 20 x 10 G to Gf s t 0/30 s t 30 10 0/10 0/20 20 y y One augmented path P: s x y t x 0/10 20/20 augment flow s t 20 30 20/30 20 0/10 20/20 Bottleneck b = 20; s x and y t are the critical edges y
Regenerate Gf from Updated G x 10 x 0/10 20/20 G to Gf 20 s 10 t s t 20/30 20 10 20 0/10 20/20 y y remove 0 weight edges x 10 20 s 10 t 20 10 20 y
Find Augmented Path x One augmented path P: s y x t s t y 10 20 10 One augmented path P: s y x t s t 20 10 20 10 20 10 y Bottleneck b = 10; s y and x t are the critical edges
Update G x x G to Gf s t s t y y One augmented path P: s y x t x 0/10 x 20/20 10 G to Gf 20 s t s 10 t 20/30 20 10 20 0/10 20/20 y y One augmented path P: s y x t x 10/10 20/20 augment flow 10 20 10 s t 10/30 Bottleneck b = 10 10/10 20/20 y
Regenerate Gf from Updated G x x 10/10 20/20 G to Gf 20 s 20 10 t s t 10 10/30 20 10 10/10 20/20 y y remove 0 weight edges x 20 s 20 10 t 10 20 10 y
Find Augmented Path x There are no paths from s to t. The while loop in FF() exits, and the maximum flow f in G is reported. 20 s 20 10 t 10 20 10 y maxflow f = 30 = ∑ outflow of s = ∑ inflow of t = ∑ all bottlenecks b's = 20 + 10 = 30 x 10/10 20/20 s t 10/30 10/10 20/20 y
2.2 Another Example v1 12 v3 20 16 t s 4 7 9 13 v2 v4 4 14
Add Initial Flows of 0 v1 v3 t s v2 v4 0/12 0/20 0/16 0/4 0/7 0/9 0/13 0/14
Generate Initial Gf G to Gf s t s t remove 0 weight edges s t 12 v1 0/12 v3 v1 v3 0/20 16 20 0/16 0/9 9 s t s 4 7 t 0/4 0/7 14 0/13 v2 v4 13 4 0/14 v2 v4 0/4 remove 0 weight edges 12 v1 v3 16 20 9 s 4 7 t 14 13 v2 4 v4
Gf G b = 4 b = 4 b = 4
Gf G b = 7 2 b = 4 11 maxflow f = 23 (also = ∑ all b's) 2 11
2.3. FF Running Time dfs/bfs = O(V+E) = O(E) O(E) O(E) O(E) O(E) void FF(Graph G, Node s, Node t) { initialize flows on all edges in G to 0; change G into residual graph Gf; while (can find an augmented path P in Gf) { bottleneck b = minimum of edge weights in P; augment flow along P in G using b; regenerate Gf from updated G; } report maxflow f in G; } O(E) O(E) O(E) O(E) Total Running time: O(E ∙ |f|) no. of loops = | f |
Why no. of loops == |f|? The loop stops when no more b's can be found. The maxflow f = ∑ all b's In the worst case each time round loop produces the smallest b, which is 1 So it will take f loops before ∑ all b's == f So max no. of loops == |f|
In the worst case, the running time of FF is O(E · |f|) |f| might be very big depending on the problem. It might be much larger than the number of edges.
2.4. FF can be slow!
The Augmenting Paths First Second
many, many more Third Fourth
199th 200th (= maxflow)
Choosing Paths is Important Only two iterations are needed.
2.5. Choosing Augmenting Paths Use care when selecting augmenting paths Some choices lead to exponential algorithms Clever choices lead to polynomial algorithms If capacities are not integers, some algorithm are not guaranteed to terminate Goal: choose augmenting paths so that: Can find augmenting paths efficiently Few iterations Choose augmenting paths with: Max bottleneck capacity Sufficiently large bottleneck capacity Fewest number of edges
3. Edmonds-Karp (EK) Algorithm void EK(Graph G, Node s, Node t) { initialize flows on all edges in G to 0; change G into residual graph Gf; while (can find an augmented SHORTEST path P in Gf) { bottleneck b = minimum of edge weights in P; augment flow along P in G using b; regenerate Gf from updated G; } report maxflow f in G; } Running time: O(V· E2)
Finds an augmenting path using breadth-first search. The bfs algorithm is slightly changed: a weight of 1 is assigned to every forward and backward edge, not the edge's residual capacity i.e. count the no. of edges
3.1. First Example Again x 20 10 30 s t 20 10 y
Generate Initial Gf x x G to Gf s t s t y y remove 0 weight edges x s 0/10 x 0/20 20 10 G to Gf s t s t 0/30 30 10 0/10 0/20 20 y y remove 0 weight edges 20 x 10 s t 30 10 20 y
Gf G x x s t s t b = 10 y y x x s t s t b = 10 y y 10/10 20 10 10/20 0/30 30 10 b = 10 20 0/10 0/20 y y 10 x x 10/10 10 10/20 10 s t s t 0/30 30 10 b = 10 20 10/10 10/20 y y
Gf G x x s t s t b = 10 y y x maxflow f = 30 (also = ∑ all b's) s t 10/10 10 20/20 10 s 30 t s t 10/30 10 10 b = 10 10 10/10 20/20 y y x 10 maxflow f = 30 (also = ∑ all b's) 20 s 20 10 t 10 20 y
3.2. EK Running Time bfs = O(V+E) = O(E) O(E) O(E) O(E) O(E) void EK(Graph G, Node s, Node t) { initialize flows on all edges in G to 0; change G into residual graph Gf; while (can find an augmented SHORTEST path P in Gf) { bottleneck b = minimum of edge weights in P; augment flow along P in G using b; regenerate Gf from updated G; } report maxflow f in G; } O(E) O(E) O(E) O(E) Running time: O(V· E2) no. of loops = O(V · E)
Why no. of loops == O(V∙E)? The loop stops when no more b's can be found (i.e. when there are no more critical edges). So max no of loops == max no. of critical edges Remember: Smallest shortest path == 1 Longest shortest path == |V| - 1 Smallest b in a path == 1
Assume e is critical when path distance = p: What is the maximum number of times one edge e (u -- v) can be critical? Assume e is critical when path distance = p: assume path distance of s to u == d u s t 1 assume path distance of s to t == p v path s to v == d+1
The earliest that e can next be critical is when path distance = p+2 path distance of s to u == d + 2 u s t 1 smallest path distance of s to t == p + 2 v smallest path s to v == d+1
So the most times that one edge can be critcal is when p == 1, 3, 5, 7, ..., |V-1| = O(v/2) = O(V) times The most times that all e edges can be critical is: E * O(V) = O(E ∙ V) Max no. of critical edges == O(V ∙ E) == max no. of loops
4. The Mincut Problem A cut is a partition of a flow network's vertices into two disjoint sets, with the source (s) in one set A and the sink (t) in the other set B. A cut's capacity is the sum of the capacities of the edges from A to B. The minimum cut (mincut) problem: find a cut of minimum capacity.
find a cut of minimum capacity. Example don't count edges from B to A capacity = 10 + 5 + 15 = 30 capacity = 10 + 8 + 16 = 34 The minimum cut (mincut) problem: find a cut of minimum capacity.
Maxflow == Mincut The maxflow problem The mincut problem find a flow of maximum value The mincut problem find a cut of minimum capacity Two problems that appear very different but are actually two versions of the same question. Maxflow-mincut theorem: maxflow value = mincut capacity
5. Bipartite Matching Offers: Students apply for jobs. Each student gets several offers. Is there a way to match every student to a different job?
As a Bipartite Graph Alice Adobe Bob Amazon Carol Facebook Dave Google Eliza IBM Frank Yahoo
Maximize No. of 1-1 Edges Alice Adobe Bob Amazon Carol Facebook Dave Google A perfect match Eliza IBM Frank Yahoo
Changing Bipartite to Maxflow Direct edges from L to R, and set all capacities to 1. Add source s, and link s to each node in L. Add sink t, and link each node in R to t. 1 1 1 1 1
Conversion to Maxflow Alice Adobe Bob Amazon Carol Facebook s t Dave 1/1 1/1 1/1 Bob Amazon 1/1 1/1 1/1 1/1 Carol 1/1 Facebook 1/1 1/1 s t 1/1 1/1 Dave Google 1/1 1/1 1/1 Eliza IBM 1/1 1/1 1/1 Frank Yahoo Other edges are 0/1
6. Edge Disjoint Paths Find the max number of edge-disjoint s-t paths in a digraph. two paths are edge-disjoint if they have no edge in common
Solution Two edge-disjoint s-t paths:
Maxflow Formulation Assign capacities of 1 to every edge. Max no. of edge-disjoint s-to-t paths = maxflow 1/1 0/1 1/1 1/1 1/1 1/1 0/1 1/1 1/1 1/1 1/1 Other edges are 0/1
6.1. Network Connectivity Related Problem: Find the min. no. of edges whose removal disconnects t from s. A set of edges F disconnects t from s if each s-t paths uses at least one edge in F removing F would make t unreachable from s Removing two edges breaks the s-t link
The max no. of edge-disjoint s-t paths = min no The max no. of edge-disjoint s-t paths = min no. of edges whose removal disconnects t from s. utilizes the mincut
7. Making MaxFlow Faster year method worst case running time discovered by 1955 augmenting path O(E · |f|) Ford-Fulkerson 1970 shortest augmented path O(V · E2) Edmonds-Karp 1983 dynamic trees O(E2 ·log E) Sleator-Tarjan 1985 capacity scaling O(E2 · log C) Gabow 1997 length function O(E3/2 · log E · log C) Goldberg-Rao ? E C is the biggest capacity on an edge in the graph.
Analysis is Tricky Worst-case big-Oh is generally not useful for predicting or comparing real-world maxflow algorithm speeds. Best in practice: push-relabel method with gap relabeling: O(E3/2) (can be written as O(V3) very close to linear in E