Edmonds–Karp algorithm

This is an old revision of this page, as edited by 199.207.253.101 (talk) at 22:49, 20 September 2006 (References). 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 in . 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[1], and independently by Jack Edmonds and Richard Karp in 1972[2] (discovered earlier). Dinic's 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. The running time of   is found by showing that the length of the augmenting path found never decreases, that for every time one of the   edge becomes saturated the augmenting path must be longer than last time it was saturated, that a path is at most   long, and can be found in   time. There is an accessible proof in [3].

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

  1. ^ E. A. Dinic (1970). "Algorithm for solution of a problem of maximum flow in a network with power estimation". Soviet Math. Doklady. Vol 11. Doklady: 1277–1280. {{cite journal}}: |volume= has extra text (help)
  2. ^ Jack Edmonds and Richard M. Karp (1972). "Theoretical improvements in algorithmic efficiency for network flow problems" (PDF). Journal of the ACM. 19 (2): 248–264.
  3. ^ Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein (2001). "26.2". Introduction to Algorithms (second edition ed.). MIT Press and McGraw-Hill. pp. 660–663. ISBN 0-262-53196-8. {{cite book}}: |edition= has extra text (help)CS1 maint: multiple names: authors list (link)
  1. Algorithms and Complexity (see pages 63 - 69). http://www.cis.upenn.edu/~wilf/AlgComp3.html