In computer science and graph theory, the Edmonds-Karp algorithm is an implementation of the Ford-Fulkerson method for computing the maximum flow in a flow network. The distinguishing feature is that the shortest augmenting path is used at each step, which guarantees that the computation will terminate. In most implementations, the shortest augmenting path is found using a breadth-first search, which gives a running time of . It is asymptotically slower than the relabel-to-front algorithm, which runs in , but it is often faster in practice for sparse graphs. The algorithm was first published by a Russian scientist, Dinic, in 1970, and later, independently, by Jack Edmonds and Richard Karp who published it in 1972. Dinic' algorithm includes additional techniques that reduce the running time to .
Algorithm
The algorithm is identical to the Ford-Fulkerson algorithm, except that the search order when finding the augmenting path is defined. The path found must be the shortest path which has available capacity. This can be found by a breadth-first search, as we let edges have unit length.
Sample implementation
Python implementation:
def edmonds_karp(C, source, sink): n = len(C) # C is the capacity matrix F = [[0] * n for _ in xrange(n)] # residual capacity from u to v is C[u][v] - F[u][v] while True: path = bfs(C, F, source, sink) if not path: break flow = Inf # traverse path to find smallest capacity for i in xrange(len(path) - 1): u,v = path[i], path[i+1] flow = min(flow, C[u][v] - F[u][v]) # traverse path to update flow for i in range(len(path) - 1): u,v = path[i], path[i+1] F[u][v] += flow F[v][u] -= flow return sum([F[source][i] for i in xrange(n)]) def bfs(C, F, source, sink): P = [-1] * len(C) # parent in search tree P[source] = source queue = [source] while queue: u = queue.pop(0) for v in xrange(len(C)): if C[u][v] - F[u][v] > 0 and P[v] == -1: P[v] = u queue.append(v) if v == sink: path = [] while True: path.insert(0, v) if v == source: break v = P[v] return path return None
Example
Given a network of seven nodes, and capacities as shown below:
In the pairs written on the edges, is the current flow, and is the capacity. The residual capacity from to is , the total capacity, minus the flow you have already used. If the net flow from to is negative, it contributes to the residual capacity.
Path | Capacity | Resulting network |
---|---|---|
|
||
|
||
|
||
|
Notice how the length of the augmenting path found by the algorithm never decreases. The paths found are the shortest possible. The flow found is equal to the capacity across the smallest cut in the graph separating the source and the sink. There is only one minimal cut in this graph, partitioning the nodes into the sets and , with the capacity .
References
- E. A. Dinic, Algorithm for solution of a problem of maximum flow in a network with power estimation, Soviet Math. Doklady, Vol 11 (1970) pp1277-1280.
- J. Edmonds and R. M. Karp, Theoretical improvements in algorithmic efficiency for network flow problems, Journal of the ACM, Vol 19, No. 2 (1972) pp248-264. PDF (needs subscription)