Edmonds–Karp algorithm

This is an old revision of this page, as edited by Nils Grimsmo (talk | contribs) at 06:25, 5 January 2006 (Spelling.). 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 practise for sparse graphs. The algorithm was first published by a Russian scientist, Dinic, in 1970, and later, independently, by Edmonds and 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.

Complexity

Given that the augmenting path is found with a breadth-first search, the running time of the Edmonds-Karp algorithm is  . This can be seen from the following argument:

 

The length of the augmenting paths found never decreases. For a new path to open, flow must have been sent in the opposite direction along at least one of its edges. Assume that flow was sent along the path   (green), such that there opened a path   (blue) which was shorter, and that only one edge on this path was closed previously. Since we always choose the shortest path, we know that  , which means that  , as the length of   is 1. Likewise we know that  , which means that  . From this we conclude that  , which contradicts the assumption that the second path was shorter. The argument can be extended to cases where multiple edges in the second path are opened when flow is sent on the first.

The number of times each edge is saturated is  . We know that if   is saturated when sending flow along a path, flow must be sent in the opposite direction, on   on a second path, before flow can be sent on   again, on a third path. The first path must be shorter than the second, which again must be shorter than the third. For each edge, the series of augmenting paths which saturated it have strictly increasing length. Since paths do not have cycles, their length is  . Hence the number of saturating sends on an edge is  .

Each time a path is found, at least one of the   edges is saturated. Since each edge is saturated   times, the maximum flow is found in   rounds. As the cost of a breadth-first-search is  , the total running time is   (if   we can remove the unused nodes in O(V) first).


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)