Contest Algorithms January 2016 Describe the maxflow problem, explain the Ford-Fulkerson, Edmonds-Karp algorithms. Look at the mincut problem, bipartite matching and edge disjoint paths 10. MaxFlow 1Contest Algorithms: 10. MaxFlow
1. A Flow Network source terminal (sink) 'pipe' capacity for 'water' flow What is the maximum flow f from s to t?
Answer /20 10/10 20/20 10 /10 10/30 flow / capacity Maxflow f = 30 = ∑ outflow of s = ∑ inflow of t flows must be integer s t
Capacity constraint 0 ≤ edge's flow ≤ edge's capacity Flow conservation inflow = outflow at every vertex (except s and t) Flow Rules inflow at v = = 10 outflow at v = = 10
A maxflow f in G is 19 Another Example
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; } 2. Ford-Fulkerson (FF) Algorithm
Every flow/capacity edge in the graph G becomes two edges in the residual graph Gf Graph Residual Graph uv 6 / 17 flow / capacity G uv 11 6 values on edges called residual capacities Gf forward edge: max amount can increase flow backward edge: max amount can decrease flow
2.1 Example: initial G Gf /20 0/10 0/20 0/10 0/ To make Gf less messy, 0 weighted edges are not drawn G to 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) Find an Augmented Path P in Gf One possible augmented path P: 0 1 2 Bottleneck b = min of edge weights in P = 20
The flow f e on each edge e in G can change in two ways: f e = f e + b in G if the edge in P is a forward edge i.e. increase the flow f e = f e – b in G if the edge in P is a backward edge i.e. decrease the flow Augment Flow along P in G using b
Update G /20 0/10 0/20 0/10 0/ One augmented path P: 0 1 2 Bottleneck b = 20; 0 1 and 2 3 are the critical edges G to Gf /20 0/10 20 /20 0/10 20 /30 augment flow
Regenerate Gf from Updated G /20 0/10 20/20 0/10 20/30 G to Gf remove 0 weight edges
Find Augmented Path One augmented path P: 0 2 1 Bottleneck b = 10; 0 2 and 1 3 are the critical edges
Update G /20 0/10 20/20 0/10 20/30 G to Gf One augmented path P: 0 2 1 Bottleneck b = /20 10 /10 20/20 10 /10 10 /30 augment flow
Regenerate Gf from Updated G /20 10/10 20/20 10/10 10/30 G to Gf remove 0 weight edges
Find Augmented Path There are no paths from 0 to 3 The while loop in FF() exits, and the maximum flow f in G is reported /20 10/10 20/20 10 /10 10/30 maxflow f = 30 = ∑ outflow of 0 = ∑ inflow of 3 = ∑ all bottlenecks b's = = 30
2.2 Another Example s v3 v4 t 16 v1 v
Add Initial Flows of 0 s v3 v4 t 0/16 v1 v2 0/13 0/4 0/9 0/12 0/14 0/4 0/7 0/20
Generate Initial Gf s v3 v4 t 0/1 6 v1 v2 0/1 3 0/4 0/9 0/1 2 0/1 4 0/4 0/7 0/2 0 G to Gf remove 0 weight edges s v3 v4 t v1 v s v3 v4 t v1 v
GfG b = 4
maxflow f = 23 (also = ∑ all b's) GfG b = 7 b =
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.
void EK(Graph G, Node s, Node t) { change G into residual graph Gf; maxflow = 0; while (can find an augmented SHORTEST path P in Gf) { bottleneck b = minimum of edge weights in P; augment flow along P in Gf using b; maxflow += bottleneck; } print maxflow; } 3. Edmonds-Karp (EK) Algorithm Running time: O(V· E 2 )
It's not necessary to keep regenerating Gf from G. All the flow changes are applied to Gf directly. Finds an augmenting path using breadth-first search bfs implemented using a queue and a loop
First Example Again
Generate Initial Gf /20 0/10 0/20 0/10 0/ G to Gf remove 0 weight edges
Gf Gf b = 10 bfs prevents loops in the augmented path between s and t
Gf Gf maxflow f = 30 = ∑ all b's b = 10
no. of vertices, source vertex, sink vertex multiple lines, each line a list of neighbours no. neighbours for u, followed by a series of (v, weight) pairs Input Data Format Contest Algorithms:10. MaxFlow ekData1.txt
An adjacency matrix for a directed, weighted graph which is treated as the initial residual graph A path is stored as a list of (u,v) pairs The bfs code uses: a queue of vertices a Boolean visited[] array, to prevent cycles a parentOf[] array of vertices which store the parents of node going from t to s in a path Data Structures Contest Algorithms:10. MaxFlow30
public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println("Usage: java EdmondsKarp "); return; } Scanner sc = new Scanner(new File(args[0])); int numVs = sc.nextInt(); int source = sc.nextInt(); int sink = sc.nextInt(); // build residual graph without creating an ordinary graph first int[][] rGraph = new int[numVs][numVs]; for (int u = 0; u < numVs; u++) { int numNeighbors = sc.nextInt(); for (int j = 0; j < numNeighbors; j++) rGraph[u][sc.nextInt()] = sc.nextInt(); // (u, v) = weight } System.out.println("The maximum possible flow is " + edmondsKarp(rGraph, source, sink)); } // end of main() Code Contest Algorithms:10. MaxFlow31 see EdmondsKarp.java
public static int edmondsKarp(int[][] rGraph, int s, int t) { int maxFlow = 0; // Augment the flow while there is a path from source to sink ArrayList path = null; while ((path = findShortestPath(rGraph, s, t)) != null) { System.out.print("Found: "); for (IPair p : path) System.out.print(p.getX() + " -> "); System.out.println(t); // Find minimum residual capacity of the edges along the path int bottleneck = Integer.MAX_VALUE; for (IPair p : path) bottleneck = Math.min(bottleneck, rGraph[p.getX()][p.getY()]); System.out.println(" Bottlneck: " + bottleneck); // update residual capacities along the path for (IPair p : path) { int u = p.getX(); int v = p.getY(); rGraph[u][v] -= bottleneck; // forward edge rGraph[v][u] += bottleneck; // backward edge } maxFlow += bottleneck; } return maxFlow; } // end of edmondsKarp() Contest Algorithms:10. MaxFlow32
public static ArrayList findShortestPath(int[][] rGraph, int s, int t) /* Returns the shortest augmented path from source s to sink t in the residual graph. */ { // set all of visited array as not visited int numVs = rGraph.length; boolean visited[] = new boolean[numVs]; // false by default int parentOf[] = new int[numVs]; // enqueue source and mark as visited LinkedList queue = new LinkedList (); queue.add(s); visited[s] = true; parentOf[s] = -1; : Contest Algorithms:10. MaxFlow33
// BFS implemented using a queue and a loop while (queue.size() != 0) { int u = queue.poll(); for (int v = 0; v < numVs; v++) { if ((!visited[v]) && (rGraph[u][v] > 0)) { /* there's some residual capacity available for visiting the unvisited v */ queue.add(v); parentOf[v] = u; visited[v] = true; } if (visited[t]) { // have we got from source to sink? ArrayList path = new ArrayList (); for (int v = t; v != s; v = parentOf[v]) // from t to s path.add(0, new IPair(parentOf[v],v)); // add (u,v) so in rev order return path; } else return null; } // end of findShortestPath() Contest Algorithms:10. MaxFlow34
Example Execution Contest Algorithms:10. MaxFlow ekData1.txt
Example (Fig 4.24, CP) Contest Algorithms:10. MaxFlow ekData.txt
Execution Contest Algorithms:10. MaxFlow37
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. 4. The Mincut Problem
Example capacity = = 34 don't count edges from B to A capacity = = 30 The minimum cut ( mincut ) problem: find a cut of minimum capacity.
The maxflow 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 Maxflow == Mincut
Students apply for jobs. Each student gets several offers. Is there a way to match every student to a different job? 5. Bipartite Matching Offers:
As a Bipartite Graph Alice Bob Carol Dave Eliza Frank Adobe Amazon Facebook Google IBM Yahoo
Maximize No. of 1-1 Edges Alice Bob Carol Dave Eliza Frank Adobe Amazon Facebook Google IBM Yahoo A perfect match
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. Changing Bipartite to Maxflow
Conversion to Maxflow Alice Bob Carol Dave Eliza Frank Adobe Amazon Facebook Google IBM Yahoo s t 1/1 Other edges are 0/1 1/1
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 6. Edge Disjoint Paths
Two edge-disjoint s-t paths: Solution
Assign capacities of 1 to every edge. Max no. of edge-disjoint s-to-t paths = maxflow Maxflow Formulation 1/1 Other edges are 0/1 0/1
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 6.1. Network Connectivity Removing two edges breaks the s-t link
The max no. of edge-disjoint s-t paths = min no. of edges whose removal disconnects t from s. utilizes the mincut