Network flow problems are crucial in areas like transportation, communication networks, water distribution, and computer networking. These problems involve determining the maximum flow that can be sent from a source node to a sink node in a network with limited capacity on edges.
One of the most fundamental algorithms for solving network flow problems is the Ford-Fulkerson Algorithm, which finds the maximum flow in a flow network using the concept of augmenting paths.
In this article, we will explore:
A flow network is a directed graph where:
✅ Capacity (c(u, v)) – The maximum amount of flow an edge (u, v)
can carry.
✅ Flow (f(u, v)) – The actual amount of flow passing through an edge.
✅ Residual Capacity – The remaining capacity after accounting for flow: residual capacity = c(u,v) − f(u,v)
✅ Augmenting Path – A path from the source to the sink with available residual capacity.
✅ Residual Graph – A modified graph that keeps track of unused capacities and allows backflows.
Ford-Fulkerson is a greedy, iterative approach that finds augmenting paths and pushes as much flow as possible through them until no more augmenting paths exist.
#include <iostream>
#include <vector>
#include <climits>
#include <cstring>
using namespace std;
#define V 6 // Number of vertices in the graph
// Perform Depth-First Search (DFS) to find an augmenting path
bool dfs(int rGraph[V][V], int u, int t, vector<int>& parent, vector<bool>& visited) {
visited[u] = true;
if (u == t) return true; // If we reach the sink, path found
for (int v = 0; v < V; v++) {
if (!visited[v] && rGraph[u][v] > 0) { // Check residual capacity
parent[v] = u;
if (dfs(rGraph, v, t, parent, visited))
return true;
}
}
return false;
}
// Ford-Fulkerson Algorithm for Maximum Flow
int fordFulkerson(int graph[V][V], int s, int t) {
int rGraph[V][V]; // Residual graph
memcpy(rGraph, graph, sizeof(rGraph));
vector<int> parent(V); // Store path
int maxFlow = 0;
while (true) {
vector<bool> visited(V, false);
// Find an augmenting path using DFS
if (!dfs(rGraph, s, t, parent, visited))
break; // No more augmenting paths
// Find the bottleneck (minimum residual capacity)
int pathFlow = INT_MAX;
for (int v = t; v != s; v = parent[v]) {
int u = parent[v];
pathFlow = min(pathFlow, rGraph[u][v]);
}
// Update residual graph
for (int v = t; v != s; v = parent[v]) {
int u = parent[v];
rGraph[u][v] -= pathFlow;
rGraph[v][u] += pathFlow; // Reverse flow
}
maxFlow += pathFlow; // Add path flow to total max flow
}
return maxFlow;
}
// Driver Code
int main() {
int 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}
};
int maxFlow = fordFulkerson(graph, 0, 5);
cout << "The Maximum Flow is: " << maxFlow << endl;
return 0;
}
🚀 1. Transportation and Logistics
🌍 2. Internet and Data Networks
⚡ 3. Power Grid Optimization
🏥 4. Healthcare Resource Allocation
📊 5. Bipartite Matching Problems
The Ford-Fulkerson Algorithm is a powerful technique for solving maximum flow problems in networks. It finds an augmenting path and iteratively increases flow until no more flow can be added.
🔹 Key Takeaways: