Ford-Fulkerson Algorithm
Ford-Fulkerson Algorithm
Ford-Fulkerson algorithm is a greedy approach to calculating the maximum possible flow in a graph or a network.
Terminologies Used
Augmenting Path
Augmenting path is the path available in a flow network.
Residual Graph
Residual graph represents the flow network and it has additional possible flow.
Residual Capacity
Residual capacity is the capacity of the edge after subtracting the flow from maximum capacity.
Flow Network
Flow network is used to describe a network of edges and vertices with a source (S) and a sink (T). Every vertex, except S and T, can receive and send an same amount of stuff through it. Here , S can only send and T can only receive stuff.
Flow netwrok
How Ford Fulkerson Algorithm works?
- Firstly, Initialize the flow in all edges to 0.
- While there is an augmenting path in between the source and sink, adding this path to the flow.
- After that update the residual graph.
Example of Ford-Fulkerson Algorithm
Consider the following graph
The maximum flow in the above graph is 23.
Program for Ford-Fulkerson algorithm
#include <stdio.h> #define A 0 #define B 1 #define C 2 #define MAX_NODES 1000 #define O 1000000000 int n; int e; int capacity[MAX_NODES][MAX_NODES]; int flow[MAX_NODES][MAX_NODES]; int color[MAX_NODES]; int pred[MAX_NODES]; int min(int x, int y) { return x < y ? x : y; } int head, tail; int q[MAX_NODES + 2]; void enqueue(int x) { q[tail] = x; tail++; color[x] = B; } int dequeue() { int x = q[head]; head++; color[x] = C; return x; } int bfs(int start, int target) { int u, v; for (u = 0; u < n; u++) { color[u] = A; } head = tail = 0; enqueue(start); pred[start] = -1; while (head != tail) { u = dequeue(); for (v = 0; v < n; v++) { if (color[v] == A && capacity[u][v] - flow[u][v] > 0) { enqueue(v); pred[v] = u; } } } return color[target] == C; } // Apply fordfulkerson algorithm int fordFulkerson(int source, int sink) { int i, j, u; int max_flow = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { flow[i][j] = 0; } } while (bfs(source, sink)) { int increment = O; for (u = n - 1; pred[u] >= 0; u = pred[u]) { increment = min(increment, capacity[pred[u]][u] - flow[pred[u]][u]); } for (u = n - 1; pred[u] >= 0; u = pred[u]) { flow[pred[u]][u] += increment; flow[u][pred[u]] -= increment; } max_flow += increment; } return max_flow; } int main() { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { capacity[i][j] = 0; } } n = 6; e = 7; capacity[0][1] = 8; capacity[0][4] = 3; capacity[1][2] = 9; capacity[2][4] = 7; capacity[2][5] = 2; capacity[3][5] = 5; capacity[4][2] = 7; capacity[4][3] = 4; int s = 0, t = 5; printf("Max Flow: %d\n", fordFulkerson(s, t)); }
Output
Max Flow: 6
Ford-Fulkerson Applications
- Water distribution pipeline
- Bipartite matching problem
- Circulation with demands
Read more, Greedy algorithm