TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
max_flow_with_ford_fulkerson_and_edmond_karp_algo.cpp
1/*
2 * Author: Amit Kumar
3 * Created: May 24, 2020
4 * Copyright: 2020, Open-Source
5 * Last Modified: May 25, 2020
6 */
7#include <algorithm>
8#include <bitset>
9#include <cstring>
10#include <iostream>
11#include <limits>
12#include <queue>
13#include <tuple>
14#include <utility>
15#include <vector>
16// std::max capacity of node in graph
17const int MAXN = 505;
18class Graph {
19 std::vector<std::vector<int> > residual_capacity, capacity;
20 int total_nodes = 0;
21 int total_edges = 0, source = 0, sink = 0;
22 std::vector<int> parent;
23 std::vector<std::tuple<int, int, int> > edge_participated;
24 std::bitset<MAXN> visited;
25 int max_flow = 0;
26 bool bfs(int source, int sink) { // to find the augmented - path
27 visited.reset();
28 std::queue<int> q;
29 q.push(source);
30 while (q.empty() == false) {
31 int current_node = q.front();
32 visited.set(current_node);
33 q.pop();
34 for (int i = 0; i < total_nodes; ++i) {
35 if (residual_capacity[current_node][i] > 0 && !visited[i]) {
36 visited.set(i);
37 parent[i] = current_node;
38 if (i == sink) {
39 return true;
40 }
41 q.push(i);
42 }
43 }
44 }
45 return false;
46 }
47
48 public:
49 void set_graph() {
50 std::cin >> total_nodes >> total_edges >> source >> sink;
51 parent = std::vector<int>(total_nodes, -1);
52 capacity = residual_capacity = std::vector<std::vector<int> >(
53 total_nodes, std::vector<int>(total_nodes));
54 for (int start = 0, destination = 0, capacity_ = 0, i = 0;
55 i < total_edges; ++i) {
56 std::cin >> start >> destination >> capacity_;
57 residual_capacity[start][destination] = capacity_;
58 capacity[start][destination] = capacity_;
59 }
60 }
61 void ford_fulkerson() {
62 while (bfs(source, sink)) {
63 int current_node = sink;
64 int flow = std::numeric_limits<int>::max();
65 while (current_node != source) {
66 int parent_ = parent[current_node];
67 flow = std::min(flow, residual_capacity[parent_][current_node]);
68 current_node = parent_;
69 }
70 current_node = sink;
71 max_flow += flow;
72 while (current_node != source) {
73 int parent_ = parent[current_node];
74 residual_capacity[parent_][current_node] -= flow;
75 residual_capacity[current_node][parent_] += flow;
76 current_node = parent_;
77 }
78 }
79 }
80 void print_flow_info() {
81 for (int i = 0; i < total_nodes; ++i) {
82 for (int j = 0; j < total_nodes; ++j) {
83 if (capacity[i][j] &&
84 residual_capacity[i][j] < capacity[i][j]) {
85 edge_participated.emplace_back(std::make_tuple(
86 i, j, capacity[i][j] - residual_capacity[i][j]));
87 }
88 }
89 }
90 std::cout << "\nNodes : " << total_nodes << "\nMax flow: " << max_flow
91 << "\nEdge present in flow: " << edge_participated.size()
92 << '\n';
93 std::cout << "\nSource\tDestination\tCapacity\total_nodes";
94 for (auto& edge_data : edge_participated) {
95 int source = 0, destination = 0, capacity_ = 0;
96 std::tie(source, destination, capacity_) = edge_data;
97 std::cout << source << "\t" << destination << "\t\t" << capacity_
98 << '\t';
99 }
100 }
101};
102int main() {
103 /*
104 Input Graph: (for testing )
105 4 5 0 3
106 0 1 10
107 1 2 1
108 1 3 1
109 0 2 1
110 2 3 10
111 */
112 Graph graph;
113 graph.set_graph();
114 graph.ford_fulkerson();
115 graph.print_flow_info();
116 return 0;
117}
int main()
Main function.
Graph Algorithms.