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.