# Ford–Fulkerson algorithm

The Ford–Fulkerson method or Ford–Fulkerson algorithm (FFA) is an algorithm which computes the maximum flow in a flow network. It was published in 1956 by L. R. Ford, Jr. and D. R. Fulkerson.[1] 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 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 these paths. Then we find another path, and so on. A path with available capacity is called an augmenting path.

## Algorithm

Let ${\displaystyle G(V,E)}$ be a graph, and for each edge from ${\displaystyle u}$ to ${\displaystyle v}$, let ${\displaystyle c(u,v)}$ be the capacity and ${\displaystyle f(u,v)}$ be the flow. We want to find the maximum flow from the source ${\displaystyle s}$ to the sink ${\displaystyle t}$. After every step in the algorithm the following is maintained:

 Capacity constraints: ${\displaystyle \forall (u,v)\in E\ f(u,v)\leq c(u,v)}$ The flow along an edge can not exceed its capacity. Skew symmetry: ${\displaystyle \forall (u,v)\in E\ f(u,v)=-f(v,u)}$ The net flow from ${\displaystyle u}$ to ${\displaystyle v}$ must be the opposite of the net flow from ${\displaystyle v}$ to ${\displaystyle u}$ (see example). Flow conservation: ${\displaystyle \forall u\in V:u\neq s{\text{ and }}u\neq t\Rightarrow \sum _{w\in V}f(u,w)=0}$ That is, unless ${\displaystyle u}$ is ${\displaystyle s}$ or ${\displaystyle t}$. The net flow to a node is zero, except for the source, which "produces" flow, and the sink, which "consumes" flow. Value(f): ${\displaystyle \sum _{(s,u)\in E}f(s,u)=\sum _{(v,t)\in E}f(v,t)}$ That is, the flow leaving from ${\displaystyle s}$ must be equal to the flow arriving at ${\displaystyle t}$.

This means that the flow through the network is a legal flow after each round in the algorithm. We define the residual network ${\displaystyle G_{f}(V,E_{f})}$ to be the network with capacity ${\displaystyle c_{f}(u,v)=c(u,v)-f(u,v)}$ and no flow. Notice that it can happen that a flow from ${\displaystyle v}$ to ${\displaystyle u}$ is allowed in the residual network, though disallowed in the original network: if ${\displaystyle f(u,v)>0}$ and ${\displaystyle c(v,u)=0}$ then ${\displaystyle c_{f}(v,u)=c(v,u)-f(v,u)=f(u,v)>0}$.

Algorithm Ford–Fulkerson

Inputs Given a Network ${\displaystyle G=(V,E)}$ with flow capacity ${\displaystyle c}$, a source node ${\displaystyle s}$, and a sink node ${\displaystyle t}$
Output Compute a flow ${\displaystyle f}$ from ${\displaystyle s}$ to ${\displaystyle t}$ of maximum value

The path in step 2 can be found with for example a breadth-first search or a depth-first search in ${\displaystyle G_{f}(V,E_{f})}$. If you use the former, the algorithm is called Edmonds–Karp.

When no more paths in step 2 can be found, ${\displaystyle s}$ will not be able to reach ${\displaystyle t}$ in the residual network. If ${\displaystyle S}$ is the set of nodes reachable by ${\displaystyle s}$ in the residual network, then the total capacity in the original network of edges from ${\displaystyle S}$ to the remainder of ${\displaystyle V}$ is on the one hand equal to the total flow we found from ${\displaystyle s}$ to ${\displaystyle 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 ${\displaystyle G(V,E)}$ has multiple sources and sinks, we act as follows: Suppose that ${\displaystyle T=\{t|t{\text{ is a sink}}\}}$ and ${\displaystyle S=\{s|s{\text{ is a source}}\}}$. Add a new source ${\displaystyle s^{*}}$ with an edge ${\displaystyle (s^{*},s)}$ from ${\displaystyle s^{*}}$ to every node ${\displaystyle s\in S}$, with capacity ${\displaystyle c(s^{*},s)=d_{s}\;(d_{s}=\sum _{(s,u)\in E}c(s,u))}$. And add a new sink ${\displaystyle t^{*}}$ with an edge ${\displaystyle (t^{*},t)}$ from ${\displaystyle t^{*}}$ to every node ${\displaystyle t\in T}$, with capacity ${\displaystyle c(t^{*},t)=d_{t}\;(d_{t}=\sum _{(v,t)\in E}c(v,t))}$. Then apply the Ford–Fulkerson algorithm.

Also, if a node ${\displaystyle u}$ has capacity constraint ${\displaystyle d_{u}}$, we replace this node with two nodes ${\displaystyle u_{in},u_{out}}$, and an edge ${\displaystyle (u_{in},u_{out})}$, with capacity ${\displaystyle c(u_{in},u_{out})=d_{u}}$. 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 ${\displaystyle O(Ef)}$ (see big O notation), where ${\displaystyle E}$ is the number of edges in the graph and ${\displaystyle f}$ is the maximum flow in the graph. This is because each augmenting path can be found in ${\displaystyle O(E)}$ time and increases the flow by an integer amount which is at least ${\displaystyle 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 ${\displaystyle O(VE^{2})}$ time.

## Integral example

The following example shows the first steps of Ford–Fulkerson in a flow network with 4 nodes, source ${\displaystyle A}$ and sink ${\displaystyle D}$. This example shows the worst-case behaviour of the algorithm. In each step, only a flow of ${\displaystyle 1}$ 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 ${\displaystyle C}$ to ${\displaystyle B}$ when finding the path ${\displaystyle A,C,B,D}$.

## Non-terminating example

Consider the flow network shown on the right, with source ${\displaystyle s}$, sink ${\displaystyle t}$, capacities of edges ${\displaystyle e_{1}}$, ${\displaystyle e_{2}}$ and ${\displaystyle e_{3}}$ respectively ${\displaystyle 1}$, ${\displaystyle r=({\sqrt {5}}-1)/2}$ and ${\displaystyle 1}$ and the capacity of all other edges some integer ${\displaystyle M\geq 2}$. The constant ${\displaystyle r}$ was chosen so, that ${\displaystyle r^{2}=1-r}$. We use augmenting paths according to the following table, where ${\displaystyle p_{1}=\{s,v_{4},v_{3},v_{2},v_{1},t\}}$, ${\displaystyle p_{2}=\{s,v_{2},v_{3},v_{4},t\}}$ and ${\displaystyle p_{3}=\{s,v_{1},v_{2},v_{3},t\}}$.

Note that after step 1 as well as after step 5, the residual capacities of edges ${\displaystyle e_{1}}$, ${\displaystyle e_{2}}$ and ${\displaystyle e_{3}}$ are in the form ${\displaystyle r^{n}}$, ${\displaystyle r^{n+1}}$ and ${\displaystyle 0}$, respectively, for some ${\displaystyle n\in \mathbb {N} }$. This means that we can use augmenting paths ${\displaystyle p_{1}}$, ${\displaystyle p_{2}}$, ${\displaystyle p_{1}}$ and ${\displaystyle p_{3}}$ 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 ${\displaystyle 1+2(r^{1}+r^{2})}$. If we continue to use augmenting paths as above, the total flow converges to ${\displaystyle \textstyle 1+2\sum _{i=1}^{\infty }r^{i}=3+2r}$, while the maximum flow is ${\displaystyle 2M+1}$. In this case, the algorithm never terminates and the flow doesn't even converge to the maximum flow.[2]

## Python implementation

class Edge(object):
def __init__(self, u, v, w):
self.source = u
self.sink = v
self.capacity = w
def __repr__(self):
return "%s->%s:%s" % (self.source, self.sink, self.capacity)

class FlowNetwork(object):
def __init__(self):
self.flow = {}

def get_edges(self, v):

if u == v:
raise ValueError("u == v")
edge = Edge(u,v,w)
redge = Edge(v,u,0)
edge.redge = redge
redge.redge = edge
self.flow[edge] = 0
self.flow[redge] = 0

def find_path(self, source, sink, path):
if source == sink:
return path
for edge in self.get_edges(source):
residual = edge.capacity - self.flow[edge]
if residual > 0 and edge not in path:
result = self.find_path( edge.sink, sink, path + [edge])
if result != None:
return result

def max_flow(self, source, sink):
path = self.find_path(source, sink, [])
while path != None:
residuals = [edge.capacity - self.flow[edge] for edge in path]
flow = min(residuals)
for edge in path:
self.flow[edge] += flow
self.flow[edge.redge] -= flow
path = self.find_path(source, sink, [])
return sum(self.flow[edge] for edge in self.get_edges(source))


### Usage example

For the example flow network in maximum flow problem we do the following:

>>> g = FlowNetwork()
>>> [g.add_vertex(v) for v in "sopqrt"]
[None, None, None, None, None, None]
>>> print (g.max_flow('s','t'))
5


## Notes

1. Template:Cite doi
2. {{#invoke:Citation/CS1|citation |CitationClass=journal }}

## References

• {{#invoke:citation/CS1|citation

|CitationClass=book }}

• {{#invoke:citation/CS1|citation

|CitationClass=book }}

• {{#invoke:citation/CS1|citation

|CitationClass=book }}