Geeks Ford-Fulkerson Algorithm for maximum flow problem maximum Network Flow Problem

Source: Internet
Author: User

A long time ago, I tried to overcome the problem of network streams. I think this part of content seems very advanced, and I think there are still many other algorithms to learn, third, I think it would be better to supplement the relevant algorithms first.

However, although this is a relatively advanced graph theory, it is still not difficult to build a foundation, and there are not many related algorithms. You can learn it after you are familiar with graph theory, the algorithm can be learned without bipartite graphs.

The Ford-Fulkerson Algorithm is used here. The implementation method is Edmonds-Karp Algorithm.

In fact, the former is the basic algorithm idea, and the latter is the specific implementation method.

The two figures are very clear:

Figure 1: source image, find the maximum network flow of this image


Figure 2: 19 + 4 = 23 shows the maximum network flow.



Image Source: http://www.geeksforgeeks.org/ford-fulkerson-algorithm-for-maximum-flow-problem/


#include <stdio.h>#include <iostream>#include <string.h>#include <queue>using namespace std;const int V = 6;bool bfs(int rGraph[V][V], int s, int t, int parent[]){bool vis[V] = {0};queue<int> qu;qu.push(s);vis[s] = true;while (!qu.empty()){int u = qu.front();qu.pop();for (int v = 0; v < V; v++){if (!vis[v] && rGraph[u][v] > 0){qu.push(v);parent[v] = u;vis[v] = true;}}}return vis[t];}// Returns tne maximum flow from s to t in the given graphint fordFulkerson(int graph[V][V], int s, int t){int rGraph[V][V];for (int u = 0; u < V; u++)for (int v = 0; v < V; v++)rGraph[u][v] = graph[u][v];int parent[V];int maxFlow = 0;while (bfs(rGraph, s, t, parent)){int pathFlow = INT_MAX;for (int v = t; v != s; v = parent[v])pathFlow = min(pathFlow, rGraph[parent[v]][v]);for (int v = t; v != s; v = parent[v])rGraph[parent[v]][v] -= pathFlow;maxFlow += pathFlow;}return maxFlow;}int main(){// Let us create a graph shown in the above exampleint graph[V][V] = { {0, 16, 13, 0, 0, 0},{0, 0, 10, 12, 0, 0},{0, 4, 0, 0, 14, 0},{0, 0, 9, 0, 0, 20},{0, 0, 0, 7, 0, 4},{0, 0, 0, 0, 0, 0}};cout << "The maximum possible flow is " << fordFulkerson(graph, 0, 5)<<'\n';return 0;}

The result is 23.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.