In this tutorial, we’ll explore the Edmonds-Karp algorithm for finding the maximum flow in network graphs. It’s a variant of the Ford-Fulkerson method, where augmenting paths are identified using breadth-first search (BFS).
In this section, we define the network flow-related notions required throughout our discussion. We assume we’re given a directed graph , where is the set of vertices, and is the set of edges of the graph. We also assume that the size of is and the size of is .
2.1. Network Graph
In the maximum flow problem, we are given a network graph, and we need to find the maximum flow that can go from a source vertex, , to a sink vertex, . A network graph is a directed graph where each edge is assigned a non-negative capacity. That is, there’s a non-negative capacity function defined on the edges of the graph: . satisfies the following conditions:
We also define a non-negative function of flow defined on the edges of : . also conforms to several conditions:
The last condition simply means that the edge capacities constrain the flow and can’t exceed them.
Finally, we assume doesn’t contain reverse edges. That is, if , then . The reverse edges are not allowed because they constructively appear in the residual graph to find the maximum flow. Actually, we can get an equivalent graph without reverse edges from the original graph with reverse edges by introducing new vertices:
We perform the transformation above for all the reverse edges in the graph.
2.2. Residual Graph
The residual graph, , of is defined in the following way: , where if in . Shortly, consists of the same vertices as , and any edge is present in if has the potential to conduct more flow. If for edge , , that means the edge’s capacity is fully used, and it can’t accept any more flow. Such an edge is absent in .
Additionally, we include reverse edges in . Reverse edges are absent in and may appear in while running the Ford-Fulkerson method. If edge currently conducts the flow of value , then we add edge to with residual capacity .
Finally, we define a positive function of residual capacities on the edges of : . is defined as:
2.3. Augmenting Paths
An augmenting path is any path from to in . They’re used by the algorithm to increase the flow from to gradually.
3. Edmonds-Karp Algorithm
3.1. Algorithm Demonstration
Assume we have a network graph shown below. The edge capacities are depicted as well:
Let’s show how the Edmonds-Karp algorithm works by finding the maximum flow on the graph step by step. Initially, we build , which fully matches as there’s no flow in yet. Below, we can see on the left and on the right. For the edges of , we depict the current flow on them followed by their capacity:
Next, we find the first shortest path from to using BFS. The path consists of three edges and is shown on below. The flow increases by 8 along the augmenting path. We parallelly change the flow across the edges of as the algorithm progresses:
As the flow changes, new reverse edges may appear in . Below, we can see new reverse edges, along with the second shortest path from to consisting of four edges. The flow increases by 5 this time:
Next, we find another augmenting path consisting of four edges. The path increases the flow by 2:
We’re in a situation now where there’re no augmenting paths left in . It’s the indication that the algorithm has finished and we’ve found the maximum flow from to :
The maximum flow value may be fetched by either adding the flows on the outgoing edges of or by adding the flows on the incoming edge of . In either case, the maximum flow value is 15 for our example.
3.2. Algorithm Pseudocode
The pseudocode of the Edmonds-Karp algorithm is almost the same as the one for the Ford-Fulkerson method. The difference is that the Ford-Fulkerson method doesn’t specify how augmenting paths are found, while the Edmonds-Karp algorithm states they’re found using BFS. We can find the pseudocode of the Edmonds-Karp algorithm below:
3.3. Algorithm Complexity
The drawback of the Ford-Fulkerson method is that it may run in exponential time. Its time complexity is , where is the value of the maximum flow. So, the method’s time complexity depends on the maximum flow value. It effectively means that we may have various performances by changing the capacity function on the same graph. Additionally, the Ford-Fulkerson method may never end if the capacities of the edges are irrational.
The Edmonds-Karp algorithm fixes the drawbacks as it runs in polynomial time: . The complexity follows from the observation that there can’t be more than flow augmentations, and each flow augmentation runs in .
In this tutorial, we’ve discussed the Edmonds-Karp algorithm for finding the maximum flow in network graphs. It’s a customization of the Ford-Fulkerson method where augmenting paths are found using BFS. The algorithm is polynomial.