1 / 34

Max-flow min-cut

Max-flow min-cut. Overview of the Max-flow problem with sample code and example problem. . Georgi Stoyanov. Sofia University. http:// backtrack-it.blogspot.com. Student at. Table of Contents. Definition of the problem Where does it occur? Max-flow min-cut theorem Example

lou
Download Presentation

Max-flow min-cut

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Max-flow min-cut Overview of the Max-flow problemwith sample code and example problem. GeorgiStoyanov Sofia University http://backtrack-it.blogspot.com Student at

  2. Table of Contents • Definition of the problem • Where does it occur? • Max-flow min-cut theorem • Example • Max-flow algorithm • Run-time estimation • Questions

  3. Definition of the problem

  4. Definition of the problem • Maximum flow problems • Finding feasible flow • Through a single -source, -sink flow network • Flow is maximum • Many problems solved by Max-flow • The problem is often present at algorithmic competitions

  5. The Max-flow algorithm • Additional definitions • Edge capacity – maximum flow that can go through the edge • Residual edge capacity – maximum flow that can pass after a certain amount has passed • residualCapacity= edgeCapacity – alreadyPassedFlow • Augmented path – path starting from source to sink • Only edges with residual capacity above zero

  6. Where does it occur?

  7. Where does it occur? • In any kind of network with certain capacity • Network of pipes – how much water can pass through the pipe network per unit of time?

  8. Where does it occur? • Electricity network – how much electricity can go through the grid?

  9. Where does it occur? • The internet network– how much traffic can go through a local network or the internet?

  10. Where does it occur? • In other problems • Matching problem • Group of N guys and M girls • Every girl/guy likes a certain amount of people from the other group • What is the maximum number of couples, with people who like each other?

  11. Where does it occur? • Converting the matching problem to a max-flow problem: • We add an edge with capacity one for every couple that is acceptable • We add two bonus nodes – source and sink • We connect the source with the first group and the second group with the sink

  12. Max-flow min-cut theorem

  13. Max-flow min-cut theorem • Themax-flow min-cut theorem states that in a flow network, the maximum amount of flow passing from the source to the sink is equal to the minimum capacity that when removed in a specific way from the network causes the situation that no flow can pass from the source to the sink.

  14. Example

  15. Example • Example min( cf(A,D), cf(D,E), cf(E,G)) = min( 3 – 0, 2 – 0, 1 – 0) = min( 3, 2, 1) = 1 maxFlow = maxFlow + 1 = 1

  16. Example • Example min( cf(A,D), cf(D,F), cf(F,G)) = min( 3 – 1, 6 – 0, 9 – 0) = min( 2, 6, 9) = 2 maxFlow = maxFlow + 2 = 3

  17. Example • Example min( cf(A,B), cf(B,C), cf(C,D), cf(D,F), cf(F,G)) = min( 3 – 0, 4 – 0, 1 – 0, 6 – 2, 9 - 2) = min( 3, 4, 1, 4, 7) = 1 maxFlow = maxFlow + 1 = 4

  18. Example • The flow in the previous slide is not optimal! • Reverting some of the flow through a different path will achieve the optimal answer • To do that for each directed edge (u, v) we will add an imaginary reverse edge (v, u) • The new edge shall be used only if a certain amount of flow has already passed through the original edge!

  19. Example • Example min( cf(A,B), cf(B,C), cf(C,E), cf(E,D), cf(D,F), cf(F,g) ) = min( 3 – 1, 4 – 1, 2 – 0, 0 – -1, 6 – 3, 9 - 3) = min( 2, 3, 2, 1, 3, 6 ) = 1 maxFlow = maxFlow + 1 = 5 (which is the final answer)

  20. The Max-flow algorithm

  21. The Max-flow algorithm • The Edmonds-Karp algorithm • Uses a graph structure • Uses matrix of the capacities • Uses matrix for the passed flow

  22. The Max-flow algorithm • The Edmonds-Karp algorithm • Uses breadth-first search on each iteration to find a path from the source to the sink • Uses parent table to store the path • Uses path capacity table to store the value of the maximum flow to a node in the path

  23. The Max-flow algorithm - initialization #include<cstdio> #include<queue> #include<cstring> #include<vector> #include<iostream> #define MAX_NODES 100 // the maximum number of nodes in the graph #define INF 2147483646 // represents infity #define UNINITIALIZED -1 // value for node with no parent using namespace std; // represents the capacities of the edges int capacities[MAX_NODES][MAX_NODES]; // shows how much flow has passed through an edge intflowPassed[MAX_NODES][MAX_NODES]; // represents the graph. The graph must contain the negative edges too! vector<int> graph[MAX_NODES]; //shows the parents of the nodes of the path built by the BFS intparentsList[MAX_NODES]; //shows the maximum flow to a node in the path built by the BFS intcurrentPathCapacity[MAX_NODES];

  24. The Max-flow algorithm - core • The “heart” of the algorithm: int edmondsKarp(int startNode, int endNode) { intmaxFlow=0; while(true) { intflow=bfs(startNode, endNode); if(flow==0) break; maxFlow+=flow; intcurrentNode=endNode; while(currentNode!= startNode) { intpreviousNode = parentsList[currentNode]; flowPassed[previousNode][currentNode] += flow; flowPassed[currentNode][previousNode] -= flow; currentNode=previousNode; } } return maxFlow; }

  25. The Max-flow algorithm – Breadth-first search • Breadth-first search intbfs(intstartNode, intendNode) { memset(parentsList, UNINITIALIZED, sizeof(parentsList)); memset(currentPathCapacity, 0, sizeof(currentPathCapacity)); queue<int> q; q.push(startNode); parentsList[startNode]=-2; currentPathCapacity[startNode]=INF; . . .

  26. The Max-flow algorithm – Breadth-first search ... while(!q.empty()) { intcurrentNode = q.front(); q.pop(); for(inti=0; i<graph[currentNode].size(); i++) { intto = graph[currentNode][i]; if(parentsList[to] == UNINITIALIZED && capacities[currentNode][to] - flowPassed[currentNode][to] > 0) { parentsList[to] = currentNode; currentPathCapacity[to] = min(currentPathCapacity[currentNode], capacities[currentNode][to] - flowPassed[currentNode][to]); if(to == endNode) return currentPathCapacity[endNode]; q.push(to); } } } return 0; }

  27. Run-time estimation • Breaking down the algorithm: • The BFS will cost O(E) operations to find a path on each iteration • We will have total O(VE) path augmentations (proved with Theorem and Lemmas) • This gives us total run-time of O(VE*E)

  28. Run-time estimation • There are other algorithms that can run in O(V³) time but are far more complicated to implement • ! Note - this algorithm can also run in O(V³) time for sparse graphs

  29. The Max-flow algorithm • Perks of using the Edmonds-Karp algorithm • Runs relatively fast in sparse graphs • Represents a refined version of the Ford-Fulkerson algorithm • Unlike the Ford-Fulkerson algorithm, this will always terminate • It is relatively simple to implement

  30. Summary • Many problems can be transformed to a max-flow problem. So keep your eyes open! • The Edmonds-Karp algorithm is: • fairly fast for sparse graphs – O(V³) • easy to implement • runs in O(VE²) time

  31. Summary • Don’t forget to add the reverse edges to your graph! • The algorithm • Looks for augmenting path from source to sink on each iteration • Maximum flow == smallest residual capacity of an edge in that path

  32. Resources • Video lectures (in kind of English) • http://www.youtube.com/watch?v=J0wzih3_5Wo • http://en.wikipedia.org/wiki/Maximum_flow_problem • http://en.wikipedia.org/wiki/Edmonds%E2%80%93Karp_algorithm • http://en.wikipedia.org/wiki/Matching_(graph_theory) • Nakov’s book: Programming = ++Algorithms;

  33. Combinatorics http://algoacademy.telerik.com

  34. Free Trainings @ Telerik Academy • “C# Programming @ Telerik Academy • csharpfundamentals.telerik.com • Telerik Software Academy • academy.telerik.com • Telerik Academy @ Facebook • facebook.com/TelerikAcademy • Telerik Software Academy Forums • forums.academy.telerik.com

More Related