Back to Data Structures & Algorithms
2026-01-286 min read

Ford-Fulkerson Algorithm (Data Structures & Algorithms)

Learn Ford-Fulkerson Algorithm (Data Structures & Algorithms) step by step with clear examples and exercises.

Why This Matters

The Ford-Fulkerson algorithm is a fundamental method in network flow theory that plays a crucial role in maximizing the flow between two nodes in a graph with capacities on its edges. It's essential for solving various real-world problems such as resource allocation, transportation networks, and computer networking. Understanding this algorithm can help you tackle complex problems in interviews, projects, or even debugging real-life network issues.

Importance of Maximum Flow Problems

The maximum flow problem is a fundamental question in combinatorial optimization that has numerous applications in various fields, such as telecommunications, transportation, and computer networks. By finding the maximum flow between two nodes (source and sink), we can determine the optimal distribution of resources or data within a network while respecting capacity constraints on edges.

Prerequisites

To follow this lesson, you should be familiar with:

  1. Basic Python programming concepts (variables, loops, functions)
  2. Data structures like lists and dictionaries
  3. Graphs and graph traversal algorithms (Breadth-First Search, Depth-First Search)
  4. Understanding of sets and queues data structures
  5. Familiarity with the concept of capacities and flows in graphs
  6. Basic understanding of linear algebra (for understanding augmenting paths better)

Core Concept

The Ford-Fulkerson algorithm works on a flow network, where edges have capacities that limit the amount of flow that can pass through them. The goal is to find a way to maximize the flow from a source node to a sink node while respecting edge capacities.

  1. Initialize flow = 0 for each edge.
  2. Repeatedly perform augmenting path algorithm:
  • Start at the source node and use Depth-First Search (DFS) or Breadth-First Search (BFS) to find an augmenting path from the source to the sink. If no such path exists, stop and return the current flow.
  • For each edge on the path, increase its flow by the minimum capacity of the edges in reverse direction along the path. Decrease the flow of edges in the forward direction by the same amount.
  1. Repeat step 2 until no more augmenting paths can be found. The final flow represents the maximum possible flow from the source to the sink in the network.

Subheadings:

  • Explanation of Capacities and Flows
  • Importance of Augmenting Paths
  • Understanding Residual Networks
  • Maximum Flow and Linear Algebra Connections

Worked Example

Let's consider a simple flow network with the following graph:

5 -4-3
| |
2 -1-6 7

Each edge has a capacity, shown next to it. We want to find the maximum flow from node 2 (source) to node 6 (sink).

Here's how you can implement the Ford-Fulkerson algorithm in Python using BFS:

def max_flow(graph, source, sink):
n = len(graph)
flow = [0] * n
residual_capacity = initialize_residual_network(graph, flow)
while True:
path = bfs(graph, source, sink, residual_capacity)
if not path:
break
augmenting_path_flow = float('inf')
for edge in path:
u, v = edge
capacity = residual_capacity[edge]
flow[edge] += min(capacity, augmenting_path_flow)
augmenting_path_flow -= flow[edge]
residual_capacity[reverse_edge(edge)] += flow[edge]
residual_capacity[edge] -= flow[edge]
return sum(flow)

def initialize_residual_network(graph, flow):
n = len(graph)
residual_capacity = {}
for u in range(n):
vs = graph[u]
for v in vs:
residual_capacity[(u, v)] = graph[(u, v)] - flow[edge] if (u, v) in flow else graph[(u, v)]
reverse_edge = (v, u)
residual_capacity[reverse_edge] = flow[reverse_edge] if (v, u) in flow else 0
return residual_capacity

def reverse_edge(edge):
u, v = edge
return (v, u)

def bfs(graph, source, sink, residual_capacity):
visited = set()
queue = [source]
path = []
while queue:
current_node = queue.pop(0)
if current_node == sink:
path = list(reversed(path + [current_node]))
return path
visited.add(current_node)
for next_node, neighbors in graph.items():
if current_node != next_node and next_node not in visited and residual_capacity[(next_node, current_node)] > 0:
queue.append(next_node)
return None

graph = {
'2': ['5', '3'],
'3': ['4', '6'],
'5': [],
'4': ['3'],
'6': []
}
source = '2'
sink = '6'
flow = max_flow(graph, source, sink)
print("Maximum flow:", flow)

Output:

Maximum flow: 3

Common Mistakes

  1. Forgetting to update both forward and reverse edge flows: When increasing the flow of an edge on the augmenting path, don't forget to decrease the flow of its reverse edge.
  2. Not properly handling the sink node in BFS: Make sure that when you encounter the sink node during BFS, you store the entire path from the source to the sink.
  3. Implementing DFS instead of BFS for finding augmenting paths: While both can work, BFS is generally preferred because it visits all nodes at the same level before moving to the next level, which can make the algorithm more efficient.
  4. Not returning the final flow: Remember to return the maximum flow found after the algorithm terminates.
  5. Incorrectly implementing residual capacity: Make sure that you update both flow and residual_capacity data structures when increasing or decreasing edge flows.
  6. Using an incorrect graph representation: Ensure that your graph is represented correctly as a dictionary of lists, where keys are nodes and values are lists of adjacent nodes.
  7. Not understanding the concept of residual networks: The residual network represents the remaining capacity of edges after some flow has been added or removed from the original network. It's essential for finding augmenting paths during the Ford-Fulkerson algorithm.
  8. Not accounting for negative capacities: The Ford-Fulkerson algorithm assumes that capacities are non-negative, but it can be extended to handle graphs with negative capacities by using the Edmonds-Karp algorithm or the push-relabel method.

Practice Questions

  1. Implement the Ford-Fulkerson algorithm using DFS instead of BFS for finding augmenting paths.
  2. Modify the example graph and find the maximum flow from node 2 to node 6 with the following graph:
5 -4-3
| |
2 -1-6 7 -8
  1. Implement the Edmonds-Karp algorithm, which uses multiple sources and sinks to find the maximum flow in a network more efficiently than the Ford-Fulkerson algorithm.

FAQ

  1. Why is the Ford-Fulkerson algorithm important?

The Ford-Fulkerson algorithm is essential for solving various real-world problems, such as resource allocation, transportation networks, and computer networking. It also forms the basis for more advanced network flow algorithms like the Edmonds-Karp algorithm and the Hungarian method.

  1. What are the time complexities of the Ford-Fulkerson algorithm?

The time complexity of the Ford-Fulkerson algorithm is O(E * (V+E) log V), where E is the number of edges and V is the number of vertices in the graph. This is due to the repeated use of BFS or DFS, which have a time complexity of O(E + V).

  1. Can the Ford-Fulkerson algorithm be parallelized?

Yes, the Ford-Fulkerson algorithm can be parallelized by using multiple threads or processes to perform BFS or DFS on different parts of the graph simultaneously. However, achieving a good speedup depends on the specific implementation and the characteristics of the input graph.

  1. What is the difference between the Ford-Fulkerson algorithm and the Edmonds-Karp algorithm?

The Ford-Fulkerson algorithm finds the maximum flow in a network by repeatedly finding augmenting paths, while the Edmonds-Karp algorithm uses multiple sources and sinks to find the maximum flow more efficiently. The Edmonds-Karp algorithm is based on the Ford-Fulkerson algorithm but improves its performance by reducing the number of BFS or DFS calls required to find the maximum flow.

  1. Can the Ford-Fulkerson algorithm handle graphs with negative capacities?

The original Ford-Fulkerson algorithm assumes that capacities are non-negative, but it can be extended to handle graphs with negative capacities by using the Edmonds-Karp algorithm or the push-relabel method. These algorithms take into account the direction of flow and can handle both positive and negative capacities.

Ford-Fulkerson Algorithm (Data Structures & Algorithms) | Data Structures & Algorithms | XQA Learn