Ford–Fulkerson algorithm
The Ford-Fulkerson method or Ford–Fulkerson algorithm 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 to the sink, 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.
Algorithm
Let be a graph, and for each edge from to, let be the capacity and be the flow. We want to find the maximum flow from the source to the sink. After every step in the algorithm the following is maintained: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 to is allowed in the residual
network, though disallowed in the original network: if and then.
The path in step 2 can be found with for example a breadth-first search or a depth-first search in. If you use the former, the algorithm is called Edmonds–Karp.
When no more paths in step 2 can be found, will not be able to reach in the residual
network. If is the set of nodes reachable by in the residual network, then the total
capacity in the original network of edges from to the remainder of is on the one hand
equal to the total flow we found from to,
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 has capacity constraint, we replace this node with two nodes, and an edge, with capacity. Then apply the Ford-Fulkerson algorithm.
Complexity
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 runs forever, the flow might not even 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 , 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.
Integral 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.Notice how flow is "pushed back" from to when finding the path.
Non-terminating example
Consider the flow network shown on the right, with source, sink, capacities of edges, and respectively, 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.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 even converge to the maximum flow.
Another non-terminating example based on the Euclidean algorithm is given by, where they also show that the worst case running-time of the Ford-Fulkerson algorithm on a network in ordinal numbers is.
Python implementation of [Edmonds–Karp] algorithm
import collections
- This class represents a directed graph using adjacency matrix representation
def __init__:
self.graph = graph # residual graph
self.ROW = len
def bfs:
"""Returns true if there is a path from source 's' to sink 't' in
residual graph. Also fills parent to store the path """
# Mark all the vertices as not visited
visited = *
# Create a queue for BFS
queue = collections.deque
# Mark the source node as visited and enqueue it
queue.append
visited = True
# Standard BFS loop
while queue:
u = queue.popleft
# Get all adjacent vertices's of the dequeued vertex u
# If an adjacent has not been visited, then mark it
# visited and enqueue it
for ind, val in enumerate:
if and :
queue.append
visited = True
parent = u
# If we reached sink in BFS starting from source, then return
# true, else false
return visited
# Returns the maximum flow from s to t in the given graph
def edmonds_karp:
# This array is filled by BFS and to store path
parent = *
max_flow = 0 # There is no flow initially
# Augment the flow while there is path from source to sink
while self.bfs:
# Find minimum residual capacity of the edges along the
# path filled by BFS. Or we can say find the maximum flow
# through the path found.
path_flow = float
s = sink
while s != source:
path_flow = min
s = parent
# Add path flow to overall flow
max_flow += path_flow
# update residual capacities of the edges and reverse edges
# along the path
v = sink
while v != source:
u = parent
self.graph -= path_flow
self.graph += path_flow
v = parent
return max_flow