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