Ford–Fulkerson algorithm
The Ford-Fulkerson algorithm (named for L. R. Ford, Jr. and D. R. Fulkerson) computes the maximum flow in a flow network. It was published in 1956. The name "Ford-Fulkerson" is often also used for the Edmonds-Karp algorithm, which is a specialization of Ford-Fulkerson.
The idea behind the algorithm is very simple: 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 these paths. Then we find another path, and so on. A path with available capacity is called an augmenting path.
Algorithm
Given is a graph , with capacity and flow for the edge from to . We want to find the maximum flow from the source to the sink . After every step in the algorithm the following is maintained:
- . The flow from to does not exceed the capacity.
- . Maintain the net flow between and . If in reality units are going from to , and units from to , maintain and .
- for all nodes , except and . The amount of flow into a node equals the flow out of the node.
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 .
Algorithm Ford-Fulkerson
- Inputs Graph with flow capacity , a source node , and a sink node
- Output A flow from to which is a maximum
- for all edges
- While there is a path from to in , such that for all edges :
- Find
- For each edge
- (Send flow along the path)
- (The flow might be "returned" later)
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.
Complexity
By adding the augmenting path flow 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 O(E*f), where E is the number of edges in the graph and f is the maximum flow in the graph. This is because each augmenting path can be found in O(E) time and increases the flow by an integer amount which is at least 1.
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 O(VE2) time.
Example
The following example shows the first steps of Ford-Fulkerson in a flow network with 4 nodes, source A and sink D. This example shows the worst-case behaviour of the algorithm. In each step, only a flow of 1 is sent across the network. If you used breadth-first-search instead, you would only need two steps.
| Path | Capacity | Resulting flow network |
|---|---|---|
| Initial flow network | ||
|
||
|
||
| After 1998 more steps … | ||
| Final flow network | ||
Notice how flow is "pushed back" from C to B when finding the path A,C,B,D.
Python implementation (using depth first search)
class Graph:
# adj: adjacency list, f: flow function, c: capacity function
def __init__(self):
self.adj, self.f, self.c = {},{},{}
def add_v (self, vs):
for v in vs:
self.adj[v]=[]
def e_from (self, v):
return self.adj[v]
def vtx (self, e):
return e[0]
def cap (self, e):
return e[1]
def add_e (self, u,v,w=0):
# add edge in both directions
self.adj[u].append((v,w)),self.adj[v].append((u,-w))
self.f[(u,v)] = self.f[(v,u)] = 0
self.c[(u,v)],self.c[(v,u)] = w,0
# Returns a path of the form: [ (src_vtx, dst_vtx, residual), ... ]
def _path (self, s, t, path):
if s == t:
return path
edges = self.e_from(s)[:]
while len(edges) > 0:
cur = edges.pop()
r = self.cap(cur) - self.f[(s,self.vtx(cur))]
if r > 0:
p = self._path(self.vtx(cur), t, path + [(s, self.vtx(cur),r)])
if p != None:
return p
return None
def _update_f (self, p, c):
for e in p:
self.f[(e[0],e[1])] += c
self.f[(e[1],e[0])] -= c
def max_flow (self, s, t):
p = self._path(s,t,[])
while p != None:
self._update_f (p, min([e[2] for e in p]))
p = self._path(s,t,[])
return sum ([self.f[(s, self.vtx(e))] for e in self.e_from(s)])
Usage example
For the example flow network in maximum flow problem we do:
g=Graph()
g.add_v(['s','o','p','q','r','t'])
g.add_e('s','o',3)
g.add_e('s','p',3)
g.add_e('o','p',2)
g.add_e('o','q',3)
g.add_e('p','r',2)
g.add_e('r','t',3)
g.add_e('q','r',4)
g.add_e('q','t',2)
print g.max_flow('s','t')
Output: 5
External links
References
- Cormen, Thomas H.; Leiserson, Charles E.; Rivest, Ronald L.; Stein, Clifford (2001). "Section 26.2: The Ford-Fulkerson method". Introduction to Algorithms (Second ed.). MIT Press and McGraw-Hill. pp. 651–664. ISBN 0-262-03293-7.
- Ford, L. R.; Fulkerson, D. R. (1956). "Maximal flow through a network". Canadian Journal of Mathematics. 8: 399–404.