Ford-Fulkerson algorithm
Algorithm to compute the maximum flow in a network
The Ford-Fulkerson method or Ford-Fulkerson algorithm (FFA) is a greedy algorithm that computes the maximum flow in a flow network. It is sometimes called a "method" instead of an "algorithm" as the approach to finding augmenting paths in a residual graph is not fully specified or it is specified in several implementations with different running times. It was published in 1956 by L. R. Ford Jr. and D. R. Fulkerson. The name "Ford, Fulkerson" is often also used for the Edmonds-Karp algorithm, which is a fully defined implementation of the Ford, Fulkerson method.
The idea behind the algorithm is as follows: as long as there is a path from the source (start node) to the sink (end node), with available capacity on all edges in the path, we send flow along one of the paths. Then we find another path, and so on. A path with available capacity is called an augmenting path.
01Algorithm
Let be a graph, and for each edge from u to v, let
be the capacity and
be the flow. We want to find the maximum flow from the source s to the sink t. After every step in the algorithm the following is maintained:
| Capacity constraints | The flow along an edge cannot exceed its capacity. | |
|---|---|---|
| Skew symmetry | The net flow from u to v must be the opposite of the net flow from v to u (see example). | |
| Flow conservation | The net flow to a node is zero, except for the source, which "produces" flow, and the sink, which "consumes" flow. | |
| Value(f) | The flow leaving from s must be equal to the flow arriving at t. |
This means that the flow through the network is a legal flow after each round in the algorithm. We define the residual network to be the network with capacity
and no flow. Notice that it can happen that a flow from v to u is allowed in the residual
network, though disallowed in the original network: if
and
then
.
- Inputs Given a Network
with flow capacity c, a source node s, and a sink node t
- Output Compute a flow f from s to t of maximum value
for all edges
- While there is a path p from s to t in
, such that
for all edges
:
- Find
- For each edge
(Send flow along the path)
(The flow might be "returned" later)
- Find
- "←" denotes assignment. For instance, "largest ← item" means that the value of largest changes to the value of item.
- "return" terminates the algorithm and outputs the following value.
The path in step 2 can be found with, for example, breadth-first search (BFS) or depth-first search in . The former is known as the Edmonds-Karp algorithm.
When no more paths in step 2 can be found, s will not be able to reach t in the residual network. If S is the set of nodes reachable by s in the residual network, then the total capacity in the original network of edges from S to the remainder of V is on the one hand equal to the total flow we found from s to t, and on the other hand serves as an upper bound for all such flows. This proves that the flow we found is maximal. See also Max-flow Min-cut theorem.
If the graph has multiple sources and sinks, we act as follows:
Suppose that
and
. Add a new source
with an edge
from
to every node
, with capacity
. And add a new sink
with an edge
from every node
to
, with capacity
. Then apply the Ford, Fulkerson algorithm.
Also, if a node u has capacity constraint , we replace this node with two nodes
, and an edge
, with capacity
. We can then apply the Ford, Fulkerson algorithm.
02Complexity
By adding the flow augmenting path to the flow already established in the graph, the maximum flow will be reached when no more flow augmenting paths can be found in the graph. However, there is no certainty that this situation will ever be reached, so the best that can be guaranteed is that the answer will be correct if the algorithm terminates. In the case that the algorithm does not terminate, the flow might not converge towards the maximum flow. However, this situation only occurs with irrational flow values. When the capacities are integers, the runtime of Ford-Fulkerson is bounded by (see big O notation), where
is the number of edges in the graph and
is the maximum flow in the graph. This is because each augmenting path can be found in
time and increases the flow by an integer amount of at least
, with the upper bound
.
A variation of the Ford, Fulkerson algorithm with guaranteed termination and a runtime independent of the maximum flow value is the Edmonds-Karp algorithm, which runs in time.
03Integer flow example
The following example shows the first steps of Ford-Fulkerson in a flow network with 4 nodes, source and sink
. This example shows the worst-case behaviour of the algorithm. In each step, only a flow of
is sent across the network. If breadth-first-search were used instead, only two steps would be needed.
| Step | Flow network |
|---|---|
| Initial flow network. | |
| Flow is sent along the augmenting path |
|
| Here, one unit of flow is sent along the augmenting path |
|
| 1998 intermediate steps are omitted here. | |
| The final flow network, with a total flow of 2000 units. | |
04Non-terminating example
Consider the flow network shown on the right, with source , sink
, capacities of edges
,
and
, and the capacity of all other edges some integer
. The constant
was chosen so, that
. We use augmenting paths according to the following table, where
,
and
.
| Step | Augmenting path | Sent flow | Residual capacities | ||
|---|---|---|---|---|---|
| 0 | |||||
| 1 | |||||
| 2 | |||||
| 3 | |||||
| 4 | |||||
| 5 | |||||
Note that after step 1 as well as after step 5, the residual capacities of edges ,
and
are in the form
,
and
, respectively, for some
. This means that we can use augmenting paths
,
,
and
infinitely many times and residual capacities of these edges will always be in the same form. Total flow in the network after step 5 is
. If we continue to use augmenting paths as above, the total flow converges to
. However, note that there is a flow of value
, by sending
units of flow along
, 1 unit of flow along
, and
units of flow along
. Therefore, the algorithm never terminates and the flow does not converge to the maximum flow.
Another non-terminating example based on the Euclidean algorithm is given by Backman & Huynh (2018), where they also show that the worst case running-time of the Ford-Fulkerson algorithm on a network in ordinal numbers is
.
05Python implementation of the Edmonds-Karp algorithm
Sources and credits
This article is adapted from the Wikipedia article “Ford-Fulkerson algorithm”, written by its contributors and licensed under CC BY-SA 4.0. Fathomly has changed the layout, removed citation markers, navigation and maintenance notices, and adjusted punctuation. This adapted version is shared under the same license. For references, see the original article.
Fathomly is not affiliated with or endorsed by the Wikimedia Foundation. Spotted a problem? Tell us.