Advertisement
Sourav_CSE

Max Flow Cpp

Dec 3rd, 2019
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. // geekforgeeks
  2. // C++ program for implementation of Ford Fulkerson algorithm
  3. #include <iostream>
  4. #include <limits.h>
  5. #include <string.h>
  6. #include <queue>
  7. using namespace std;
  8.  
  9. // Number of vertices in given graph
  10. #define V 6
  11.  
  12. /* Returns true if there is a path from source 's' to sink 't' in
  13. residual graph. Also fills parent[] to store the path */
  14. bool bfs(int rGraph[V][V], int s, int t, int parent[])
  15. {
  16. // Create a visited array and mark all vertices as not visited
  17. bool visited[V];
  18. memset(visited, 0, sizeof(visited));
  19.  
  20. // Create a queue, enqueue source vertex and mark source vertex
  21. // as visited
  22. queue <int> q;
  23. q.push(s);
  24. visited[s] = true;
  25. parent[s] = -1;
  26.  
  27. // Standard BFS Loop
  28. while (!q.empty())
  29. {
  30. int u = q.front();
  31. q.pop();
  32.  
  33. for (int v=0; v<V; v++)
  34. {
  35. if (visited[v]==false && rGraph[u][v] > 0)
  36. {
  37. q.push(v);
  38. parent[v] = u;
  39. visited[v] = true;
  40. }
  41. }
  42. }
  43.  
  44. // If we reached sink in BFS starting from source, then return
  45. // true, else false
  46. return (visited[t] == true);
  47. }
  48.  
  49. // Returns the maximum flow from s to t in the given graph
  50. int fordFulkerson(int graph[V][V], int s, int t)
  51. {
  52. int u, v;
  53.  
  54. // Create a residual graph and fill the residual graph with
  55. // given capacities in the original graph as residual capacities
  56. // in residual graph
  57. int rGraph[V][V]; // Residual graph where rGraph[i][j] indicates
  58. // residual capacity of edge from i to j (if there
  59. // is an edge. If rGraph[i][j] is 0, then there is not)
  60. for (u = 0; u < V; u++)
  61. for (v = 0; v < V; v++)
  62. rGraph[u][v] = graph[u][v];
  63.  
  64. int parent[V]; // This array is filled by BFS and to store path
  65.  
  66. int max_flow = 0; // There is no flow initially
  67.  
  68. // Augment the flow while tere is path from source to sink
  69. while (bfs(rGraph, s, t, parent))
  70. {
  71. // Find minimum residual capacity of the edges along the
  72. // path filled by BFS. Or we can say find the maximum flow
  73. // through the path found.
  74. int path_flow = INT_MAX;
  75. for (v=t; v!=s; v=parent[v])
  76. {
  77. u = parent[v];
  78. path_flow = min(path_flow, rGraph[u][v]);
  79. }
  80.  
  81. // update residual capacities of the edges and reverse edges
  82. // along the path
  83. for (v=t; v != s; v=parent[v])
  84. {
  85. u = parent[v];
  86. rGraph[u][v] -= path_flow;
  87. rGraph[v][u] += path_flow;
  88. }
  89.  
  90. // Add path flow to overall flow
  91. max_flow += path_flow;
  92. }
  93.  
  94. // Return the overall flow
  95. return max_flow;
  96. }
  97.  
  98. // Driver program to test above functions
  99. int main()
  100. {
  101. // Let us create a graph shown in the above example
  102. int graph[V][V] = { {0, 16, 13, 0, 0, 0},
  103. {0, 0, 10, 12, 0, 0},
  104. {0, 4, 0, 0, 14, 0},
  105. {0, 0, 9, 0, 0, 20},
  106. {0, 0, 0, 7, 0, 4},
  107. {0, 0, 0, 0, 0, 0}
  108. };
  109.  
  110. cout << "The maximum possible flow is " << fordFulkerson(graph, 0, 5);
  111.  
  112. return 0;
  113. }
  114.  
  115. Output:
  116. The maximum possible flow is 23
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement