Ford Fulkerson Algorithm
Ford Fulkerson Algorithm
It is based
on the idea of finding augmenting paths in a residual graph and adding the flow along those
paths until no more augmenting paths can be found.
Example 1
Consider the following directed graph where s is the source and t is the sink. Each edge has a
capacity:
From To Capacity
S A 10
S B 5
A B 15
A T 10
B T 10
Goal
Step-by-Step Execution
Step 1: Initialize Flows
1. Initially, all flows are set to 0. The residual capacity for each edge is equal to its original
capacity.
An augmenting path in a flow network is a path from the source to the sink in the residual
graph where every edge has available capacity greater than zero.
In simpler terms, an augmenting path is a path along which we can still push more flow from
source to sink.
1
We repeatedly find augmenting paths from S to T in the residual graph, and augment the flow
along those paths.
Iteration 1
Find Augmenting Path: S→A→T
Residual capacities:
o S→A:10
o A→T:10
S A 0
S B 5
A B 15
A T 0
B T 10
Iteration 2
Find Augmenting Path: S→B→T
Residual capacities:
o S→B:5
o B→T:10
2
Update Flows and Residual Capacities:
Flow along S→B: 0+5 = 5, residual capacity = 5−5 = 0.
Flow along B→T: 0+5 = 5, residual capacity = 10−5 = 5.
Updated residual graph:
S A 0
S B 0
A B 15
A T 0
B T 5
Iteration 3
No more augmenting paths exist from S to T in the residual graph.
Final Result
Maximum Flow: 15
Flow Distribution:
o S→A:10, S→B:5.
o A→T:10, B→T:5.
This demonstrates the Ford-Fulkerson Algorithm in finding the maximum flow through a
network.
3
Python Code
import networkx as nx
def create_graph():
"""Create a directed graph with given capacities."""
G = nx.DiGraph()
return G
visited.add(source)
return None
while True:
visited = set()
path = dfs_find_augmenting_path(residual_graph, source, sink, [],
visited)
if not path:
break # No more augmenting paths
4
# Update the residual graph
for u, v in path:
residual_graph[u][v]['capacity'] -= bottleneck # Reduce forward
edge capacity
if residual_graph.has_edge(v, u):
residual_graph[v][u]['capacity'] += bottleneck # Increase
reverse edge capacity
else:
residual_graph.add_edge(v, u, capacity=bottleneck) # Create
reverse edge if not exists
total_flow += bottleneck
print(f"Augmenting Path Found: {' → '.join([u for u, v in path])} →
{sink}, Flow Added: {bottleneck}")
Expected Output
yaml
CopyEdit
Augmenting Path Found: S → A → T, Flow Added: 10
Augmenting Path Found: S → B → T, Flow Added: 5
Maximum Flow: 15
5
Example 2
Find the maximum flow from source S to sink T in the following flow network:
Graph Representation:
Nodes: S,A,B,C,D,T
S→A 10
S→C 10
A→B 4
A→C 2
A→D 8
B→T 10
C→D 9
D→B 6
D→T 10
Initial flow = 0.
6
Inflow at S is 8 + 2 = 10 and outflow at T is also 8 + 2 = 10.
7
B – T: 3/4, residual flow: 1
Termination
Final Result
Python Code
python
CopyEdit
from collections import deque, defaultdict
max_flow = 0
while True:
# Find an augmenting path using BFS
parent = {} # To store the path: child -> parent
queue = deque([source])
parent[source] = None # source has no parent
while queue and sink not in parent:
u = queue.popleft()
for v in residual[u]:
# If there is available capacity and v is not visited
if residual[u][v] > 0 and v not in parent:
parent[v] = u
queue.append(v)
if v == sink:
break
8
# If we didn't reach the sink, no more augmenting paths exist
if sink not in parent:
break
max_flow += path_flow
return max_flow
# Define the graph with capacities
graph = {
'S': {'A': 10, 'C': 10},
'A': {'B': 4, 'C': 2, 'D': 8},
'B': {'T': 10},
'C': {'D': 9},
'D': {'B': 6, 'T': 10},
'T': {}
}
source = 'S'
sink = 'T'
max_flow = edmonds_karp(graph, source, sink)
print("The maximum flow from {} to {} is: {}".format(source, sink, max_flow))
Explanation
9
Python Output
css
CopyEdit
The maximum flow from S to T is: 19
Conclusion
The Ford-Fulkerson algorithm is a powerful technique for solving the maximum flow
problem in a flow network. The algorithm uses augmenting paths to iteratively increase the flow
until no more augmenting paths can be found.
10