Implementation of Ford Fulkerson (Edmonds-Karp)
Learn how to implement the Ford-Fulkerson method.
We'll cover the following
In this lesson, we’ll implement the Ford-Fulkerson method to solve maximum flow problems. We’ll select a variant of Ford-Fulkerson called Edmonds-Karp, which has a better worst-case runtime.
Implementation Notes
In principle, we’ll follow the general Ford-Fulkerson method:
- Initialize a flow of value .
- Construct the residual network .
- Try to find an --path in .
-
- If there is such a path, augment the flow with it, then go to step .
- If there is no such path, return the flow .
Edmonds-Karp’s only addition is that for step , a breadth-first search is used to find an --path in the residual network.
Our input flow network will be represented using the weighted adjacency list data structure, where the edge weights are the capacities. To simplify the implementation, we won’t explicitly construct the residual network. Instead, we’ll keep track of the flow going through each edge. This is sufficient to compute residual capacities on the fly using the formula
Another technicality is that the residual network might contain edges that are not in the original graph. Here is a minimal example:
Get hands-on with 1400+ tech skills courses.