500 likes | 512 Views
10. MaxFlow. Describe the maxflow problem, explain the Ford-Fulkerson, Edmonds-Karp algorithms. Look at the mincut problem, bipartite matching and edge disjoint paths. 1. A Flow Network. 'pipe' capacity for 'water' flow. 1. 10. 20. 30. 0. 3. source. terminal (sink). 20. 10.
E N D
10. MaxFlow Describe the maxflow problem, explain the Ford-Fulkerson, Edmonds-Karp algorithms. Look at the mincut problem, bipartite matching and edge disjoint paths Contest Algorithms: 10. MaxFlow
1. A Flow Network 'pipe' capacity for 'water' flow 1 10 20 30 0 3 source terminal (sink) 20 10 2 What is the maximum flow f from s to t?
Answer flow / capacity 1 20/20 10/10 flows must be integer 10/30 0 3 t s 20/20 10/10 2 Maxflow f = 30 = ∑ outflow of s = ∑ inflow of t
Flow Rules • Capacity constraint 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
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) {bottleneckb = 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 0/10 1 1 20 0/20 10 G to Gf 0 0 0 3 0 3 0/30 0 30 10 0 0/10 0/20 0 20 2 2 1 20 10 To make Gf less messy, 0 weighted edges are not drawn. 0 3 30 10 20 2
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) 1 One possible augmented path P: 0 1 2 3 20 10 0 3 20 30 20 30 10 20 2 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 0/10 1 1 0/20 20 10 G to Gf 0 3 3 0/30 0 30 10 0/10 0/20 20 2 2 0/10 1 One augmented path P: 0 1 2 3 20/20 augment flow 20 30 0 3 20 20/30 0/10 Bottleneck b = 20; 0 1 and 2 3 are the critical edges 20/20 2
Regenerate Gf from Updated G 1 0 10 0/10 1 20/20 G to Gf 20 0 10 0 3 0 3 20 20/30 10 20 0 0 0/10 20/20 2 2 remove 0 weight edges 1 10 20 10 3 0 20 10 20 2
Find Augmented Path 1 10 20 10 0 3 One augmented path P: 0 2 1 3 20 10 20 10 20 10 2 Bottleneck b = 10; 0 2 and 1 3 are the critical edges
Update G 0/10 1 1 10 20/20 G to Gf 20 10 3 0 3 0 20/30 20 10 20 0/10 20/20 2 2 One augmented path P: 0 2 1 3 10/10 1 20/20 augment flow 10 20 10 0 3 10/30 Bottleneck b = 10 10/10 20/20 2
Regenerate Gf from Updated G 1 0 0 10/10 1 20/20 G to Gf 20 10 20 0 3 10 0 3 10/30 0 20 10 0 10/10 2 20/20 2 remove 0 weight edges 1 20 10 20 0 3 10 20 10 2
Find Augmented Path 1 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 20 0 3 10 20 10 2 maxflow f = 30 = ∑ outflow of 0 = ∑ inflow of 3 = ∑ all bottlenecks b's = 20 + 10 = 30 10/10 1 20/20 0 3 10/30 10/10 20/20 2
2.2 Another Example v1 12 v3 20 16 t s 4 7 9 v2 v4 13 4 14
Add Initial Flows of 0 v1 0/12 v3 0/20 0/16 t s 0/4 0/7 0/9 v2 v4 0/13 0/4 0/14
Generate Initial Gf G to Gf 12 0/12 v1 v3 v1 v3 0/20 0 16 20 0/16 0/9 0 9 0 s t 0 s t 7 4 0/4 0/7 0 0 14 4 v2 v4 13 0/13 0 v2 v4 0/14 0/4 0 remove 0 weight edges 12 v1 v3 16 20 9 s t 7 4 14 4 13 v2 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
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.
3. Edmonds-Karp (EK) Algorithm 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) {bottleneckb = minimum of edge weights in P;augment flow along P in Gf using b; maxflow += bottleneck; }print maxflow;} Running time: O(V· E2)
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 1 10 20 30 0 3 20 10 2
Generate Initial Gf 0/10 1 1 20 0/20 10 G to Gf 0 0 0 3 0 3 0/30 0 30 10 0 0/10 0/20 0 20 2 2 remove 0 weight edges 1 20 10 0 3 30 10 20 2
Gf Gf 1 20 10 0 3 30 10 20 2 b = 10 1 10 10 bfs prevents loops in the augmented path between s and t 10 0 3 30 10 20 2 b = 10
Gf Gf 1 10 10 10 30 0 3 10 10 10 2 b = 10 1 10 20 20 0 3 10 20 10 2 maxflow f = 30 = ∑ all b's
Input Data Format • 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 4 0 3 2 1 20 2 10 2 2 30 3 10 1 3 20 0 ekData1.txt Contest Algorithms:10. MaxFlow
Data Structures • 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 Contest Algorithms:10. MaxFlow
Code see EdmondsKarp.java public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println("Usage: java EdmondsKarp <data-file>"); 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() Contest Algorithms:10. MaxFlow
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<IPair> 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. MaxFlow
public static ArrayList<IPair> 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<Integer> queue = new LinkedList<Integer>(); queue.add(s); visited[s] = true; parentOf[s] = -1; : Contest Algorithms:10. MaxFlow
// 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<IPair> path = new ArrayList<IPair>(); 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. MaxFlow
Example Execution 4 0 3 2 1 20 2 10 2 2 30 3 10 1 3 20 0 ekData1.txt Contest Algorithms:10. MaxFlow
Example (Fig 4.24, CP) ekData.txt 4 0 1 2 2 70 3 30 2 2 25 3 70 3 0 70 3 5 1 25 3 0 30 2 5 1 70 Contest Algorithms:10. MaxFlow
Execution Contest Algorithms:10. MaxFlow
4. The Mincut Problem • A cutis 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.
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 maxflowproblem • find a flow of maximum value • The mincutproblem • 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
Offers: 5. Bipartite Matching • 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 Adobe Alice Amazon Bob Facebook Carol Google Dave IBM Eliza Yahoo Frank
Maximize No. of 1-1 Edges Adobe Alice Amazon Bob Facebook Carol Google Dave A perfect match IBM Eliza Yahoo Frank
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 Adobe Alice 1/1 1/1 1/1 Amazon Bob 1/1 1/1 1/1 1/1 Facebook Carol 1/1 1/1 1/1 s t 1/1 1/1 Google Dave 1/1 1/1 1/1 IBM Eliza 1/1 1/1 1/1 Yahoo Frank 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 1/1 1/1 1/1 0/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. of edges whose removal disconnects t from s. utilizes the mincut