Edmonds–Karp algorithm

This is an old revision of this page, as edited by Nils Grimsmo (talk | contribs) at 05:51, 3 August 2006 (Sample implementation: More readable). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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 i 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 = float("infinity") 
        # traverse path to find smallest capacity
        for (u,v) in path:
            flow = min(flow, C[u][v] - F[u][v])
        # traverse path to update flow
        for u,v in path:
            F[u][v] += flow
            F[v][u] -= flow
    return sum([F[source][i] for i in xrange(n)])

def bfs(C, F, source, sink):
    queue = [source]                 
    paths = {source: []}
    while queue:
        u = queue.pop(0)
        for v in xrange(len(C)):
            if C[u][v] - F[u][v] > 0 and v not in paths:
                paths[v] = paths[u] + [(u,v)]
                if v == sink:
                    return paths[v]
                queue.append(v)
    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)