Ford-Fulkerson Algorithm
The Ford–Fulkerson algorithm is a very straightforward method to find the Maximum Flow in a graph.
Extra Terminology:
- A residual capacity of an directed edge is the capacity minus the flow, i.e.
- The residual graph/network is the graph with strictly positive residual capacities
- An augmenting path is any path from to in the residual graph
The Ford-Fulkerson method works as follows.:
- For all , set
- While an augmenting path in exists, for all , augment by
- we increase by and decrease by for every edge
- Repeat until an augmenting path doesn’t exist anymore
When the algorithm cannot increase the flow anymore, the maximum flow has been found.
Important
You can shrink the flow. You can represent the shrinkage of the flow by a reverse edge in the residual network.
If there is a flow , then you make (but you need to remember that this is a reverse flow, because in the actual flow network you can’t make this flow go in the direction). See code for implementation.
See this example:
Edmonds-Karp algorithm
Edmonds-Karp algorithm is an implementation of the Ford-Fulkerson method that uses BFS for finding augmenting paths in time.
The intuition is, that every time we find an augmenting path one of the edges becomes saturated, and the distance from the edge to s will be longer if it appears later again in an augmenting path. The length of the simple paths is bounded by V.
Implementation
The matrix capacity
stores the capacity for every pair of vertices. adj
is the adjacency list of the undirected graph, since we have also to use the reversed of directed edges when we are looking for augmenting paths.
The function maxflow
will return the value of the maximal flow. During the algorithm, the matrix capacity
will actually store the residual capacity of the network. The value of the flow in each edge will actually not be stored, but it is easy to extend the implementation - by using an additional matrix - to also store the flow and return it.
Extra Constraints
Maximum flow with vertex capacities