Overview of the Max-flow problem with sample code and example problem. Georgi Stoyanov Sofia University Student at.

Slides:



Advertisements
Similar presentations
Windows Basic and Dynamic Disk Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator Marian Marinov CEO of 1H Ltd.
Advertisements

Make swiftly iOS development Telerik Academy Telerik Academy Plus.
Amazon S 3, App Engine Blobstore, Google Cloud Storage, Azure Blobs Svetlin Nakov Telerik Software Academy academy.telerik.com.
RPN and Shunting-yard algorithm Ivaylo Kenov Telerik Software Academy academy.telerik.com Technical Assistant
Shortest paths in edge-weighted digraph Krasin Georgiev Technical University of Sofia g.krasin at gmail com Assistant Professor.
Telerik Software Academy Telerik School Academy.
Asynchronous Programming with C# and WinRT
Unleash the Power of JavaScript Tooling Telerik Software Academy End-to-end JavaScript Applications.
Telerik School Academy ASP.NET MVC.
Character sequences, C-strings and the C++ String class, Working with Strings Learning & Development Team Telerik Software Academy.
Hybrid or Native?! Doncho Minkov Telerik Software Academy Senior Technical Trainer
Done already for your convenience! Telerik School Academy Unity 2D Game Development.
Processing Sequences of Elements Telerik School Academy C# Fundamentals – Part 1.
C# Fundamentals – Part I
NoSQL Concepts, Redis, MongoDB, CouchDB
The Business Plan and the Business Model Margarita Antonova Volunteer Telerik Academy academy.telerik.com Business System Analyst Telerik Corporation.
Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training About the Course.
What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team Telerik Software Academy.
Making JavaScript code by template! Learning & Development Team Telerik Software Academy.
Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training Who, What, Why?
Marketing Mix, SWOT Analysis and Stages of Developing an Idea Margarita Antonova Volunteer Telerik Academy academy.telerik.com Business System.
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Processing Matrices and Multidimensional Tables Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Learning & Development Telerik Software Academy.
Reading and Writing Text Files Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Telerik Software Academy ASP.NET Web Forms.
Classical OOP in JavaScript Classes and stuff Telerik Software Academy
Optimization problems, Greedy Algorithms, Optimal Substructure and Greedy choice Learning & Development Team Telerik Software.
Using Selenium for Mobile Web Testing Powered by KendoUI Telerik QA Academy Atanas Georgiev Senior QA Engineer KendoUI Team.
NoSQL Concepts, Redis, MongoDB, CouchDB Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
New features: classes, generators, iterators, etc. Telerik Academy Plus JavaScript.Next.
Creating E/R Diagrams with SQL Server Management Studio and MySQL Workbench Svetlin Nakov Telerik Software Academy Manager Technical.
Throwing and Catching Exceptions Tran Anh Tuan Edit from Telerik Software Academy
Loops, Conditional Statements, Functions Tran Anh Tuan Edit from Telerik Academy
Private/Public fields, Module, Revealing Module Learning & Development Team Telerik Software Academy.
Building Data-Driven ASP.NET Web Forms Apps Telerik Software Academy ASP.NET Web Forms.
Course Introduction Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
General and reusable solutions to common problems in software design Vasil Dininski Telerik Software Academy academy.telerik.com Intern at Telerik Academy.
Planning and Tracking Software Quality Yordan Dimitrov Telerik Corporation Team Leader, Team Pulse, Team Leader, Team Pulse, Telerik Corporation,
What you need to know Ivaylo Kenov Telerik Corporation Telerik Academy Student.
Data binding concepts, Bindings in WinJS George Georgiev Telerik Software Academy academy.telerik.com Technical Trainer itgeorge.net.
Pavel Kolev Telerik Software Academy Senior.Net Developer and Trainer
Objects, Properties, Primitive and Reference Types Learning & Development Team Telerik Software Academy.
When and How to Refactor? Refactoring Patterns Alexander Vakrilov Telerik Corporation Senior Developer and Team Leader.
Free Training and Job for Software Engineers Svetlin Nakov, PhD Manager Technical Training Telerik Corp. Telerik Software Academy.
Free Training and Job for Software Engineers Svetlin Nakov, PhD Manager Technical Training Telerik Corp. Telerik Software Academy.
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Doing the Canvas the "easy way"! Learning & Development Telerik Software Academy.
Creating and Running Your First C# Program Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Course Overview Doncho Minkov Telerik Software Academy Technical Trainer
Subroutines in Computer Programming Telerik School Academy C# Fundamentals – Part 1.
Correctly Formatting the Source Code Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer
Data Types, Primitive Types in C++, Variables – Declaration, Initialization, Scope Telerik Software Academy academy.telerik.com Learning and Development.
The past, the present, the future Learning & Development Team Telerik Software Academy.
Learn to Design Error Steady Code Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Connecting, Queries, Best Practices Tran Anh Tuan Edit from Telerik Software Academy
Processing Sequences of Elements Telerik Software Academy C# Fundamentals – Part 2.
Telerik JavaScript Framework Telerik Software Academy Hybrid Mobile Applications.
Building Rock-Solid Software Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer
Telerik Software Academy Databases.
Things start to get serious Telerik Software Academy JavaScript OOP.
Learning & Development Mobile apps for iPhone & iPad.
Processing Matrices and Multidimensional Tables Telerik Software Academy C# Fundamentals – Part 2.
Nikolay Kostov Telerik Software Academy academy.telerik.com Team Lead, Senior Developer and Trainer
Functions and Function Expressions Closures, Function Scope, Nested Functions Telerik Software Academy
Implementing Control Logic in C# Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical trainer
Inheritance, Abstraction, Encapsulation, Polymorphism Telerik Software Academy Mobile apps for iPhone & iPad.
Mocking tools for easier unit testing Telerik Software Academy High Quality Code.
What why and how? Telerik School Academy Unity 2D Game Development.
Windows Security Model Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator
Presentation transcript:

Overview of the Max-flow problem with sample code and example problem. Georgi Stoyanov Sofia University Student at

1. Definition of the problem 2. Where does it occur? 3. Max-flow min-cut theorem 4. Example 5. Max-flow algorithm 6. Run-time estimation 7. Questions 2

  Maximum flow problems   Finding feasible flow   Through a single -source, -sink flow network flow network   Flow is maximum  Many problems solved by Max-flow  The problem is often present at algorithmic competitions

 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 5

 In any kind of network with certain capacity  Network of pipes – how much water can pass through the pipe network per unit of time? 7

 Electricity network – how much electricity can go through the grid? 8

 The internet network – how much traffic can go through a local network or the internet? 9

 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? 10

 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 11

  The max-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.flow networksourcesink 13

 Example 15 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

 Example 16 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

 Example 17 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

 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! 18

 Example 19 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)

 The Edmonds-Karp algorithm  Uses a graph structure  Uses matrix of the capacities  Uses matrix for the passed flow 21

 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 22

23 #include<cstdio>#include<queue>#include<cstring>#include<vector>#include<iostream> #define MAX_NODES 100// the maximum number of nodes in the graph #define INF // 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 int flowPassed[MAX_NODES][MAX_NODES]; // represents the graph. The graph must contain the negative edges too! vector graph[MAX_NODES]; //shows the parents of the nodes of the path built by the BFS int parentsList[MAX_NODES]; //shows the maximum flow to a node in the path built by the BFS int currentPathCapacity[MAX_NODES];

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

 Breadth-first search 25 int bfs(int startNode, int endNode) { memset(parentsList, UNINITIALIZED, sizeof(parentsList)); memset(parentsList, UNINITIALIZED, sizeof(parentsList)); memset(currentPathCapacity, 0, sizeof(currentPathCapacity)); memset(currentPathCapacity, 0, sizeof(currentPathCapacity)); queue q; queue q; q.push(startNode); q.push(startNode); parentsList[startNode]=-2; parentsList[startNode]=-2; currentPathCapacity[startNode]=INF; currentPathCapacity[startNode]=INF;...

26... while(!q.empty()) { while(!q.empty()) { int currentNode = q.front(); q.pop(); int currentNode = q.front(); q.pop(); for(int i=0; i<graph[currentNode].size(); i++) { for(int i=0; i<graph[currentNode].size(); i++) { int to = graph[currentNode][i]; int to = graph[currentNode][i]; if(parentsList[to] == UNINITIALIZED if(parentsList[to] == UNINITIALIZED && capacities[currentNode][to] - flowPassed[currentNode][to] > 0) { && capacities[currentNode][to] - flowPassed[currentNode][to] > 0) { parentsList[to] = currentNode; currentPathCapacity[to] = min(currentPathCapacity[currentNode], capacities[currentNode][to] - flowPassed[currentNode][to]); capacities[currentNode][to] - flowPassed[currentNode][to]); if(to == endNode) return currentPathCapacity[endNode]; q.push(to); } } } return 0; return 0;}

 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) 27

 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 28

 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 29

 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 30

 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 31

 Video lectures (in kind of English)    p_algorithm p_algorithm p_algorithm   Nakov’s book: Programming = ++Algorithms; 32

форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен SEO курс - оптимизация за търсачки уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop уроци по програмиране и уеб дизайн за ученици ASP.NET MVC курс – HTML, SQL, C#,.NET, ASP.NET MVC безплатен курс "Разработка на софтуер в cloud среда" BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране, книги – безплатно от Наков безплатен курс "Качествен програмен код" алго академия – състезателно програмиране, състезания ASP.NET курс - уеб програмиране, бази данни, C#,.NET, ASP.NET курсове и уроци по програмиране – Телерик академия курс мобилни приложения с iPhone, Android, WP7, PhoneGap free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране

 “C# Telerik Academy  csharpfundamentals.telerik.com csharpfundamentals.telerik.com  Telerik Software Academy  academy.telerik.com academy.telerik.com  Telerik Facebook  facebook.com/TelerikAcademy facebook.com/TelerikAcademy  Telerik Software Academy Forums  forums.academy.telerik.com forums.academy.telerik.com