Edmonds-Karp algorithm
From Free net encyclopedia
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 <math>O(VE^2)</math>. It is asymptotically slower than the relabel-to-front algorithm, which runs in <math>O(V^3)</math>, 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 <math>O(V^2E)</math>.
Contents |
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 <math>O(VE^2)</math>. This can be seen from the following argument:
Image:Ek-flow comp1.png 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 <math>s \dots w u v x \dots t</math> (green), such that there opened a path <math>s \dots y v u z \dots t</math> (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 <math>|s \dots w u v| <= |s \dots y v|</math>, which means that <math>|s \dots w u| <= |s \dots y v| - 1</math>, as the length of <math>uv</math> is 1. Likewise we know that <math>|u v x \dots t| <= |u z \dots t|</math>, which means that <math>|v x \dots t| <= |u z \dots t| - 1</math>. From this we conclude that <math>|s \dots w u v x \dots t| <= |s \dots y v u z \dots t| - 2</math>, 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 <math>O(V)</math>. We know that if <math>uv</math> is saturated when sending flow along a path, flow must be sent in the opposite direction, on <math>vu</math> on a second path, before flow can be sent on <math>uv</math> 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 <math>O(V)</math>. Hence the number of saturating sends on an edge is <math>O(V)</math>.
Each time a path is found, at least one of the <math>E</math> edges is saturated. Since each edge is saturated <math>O(V)</math> times, the maximum flow is found in <math>O(VE)</math> rounds. As the cost of a breadth-first-search is <math>O(V+E)</math>, the total running time is <math>O(VE^2)</math> (if <math>E<V</math> 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 <math>f/c</math> written on the edges, <math>f</math> is the current flow, and <math>c</math> is the capacity. The residual capacity from <math>u</math> to <math>v</math> is <math>c_f(u,v)=c(u,v)-f(u,v)</math>, the total capacity, minus the flow you have already used. If the net flow from <math>u</math> to <math>v</math> is negative, it contributes to the residual capacity.
Path | Capacity | Resulting network |
---|---|---|
<math>A,D,E,G</math> |
<math>\min(c_f(A,D),c_f(D,E),c_f(E,G)) = </math></br> <math>\min(3-0,2-0,1-0) = </math></br> <math>\min(3,2,1) = 1</math></br> |
Image:Ek-flow 1.png |
<math>A,D,F,G</math> |
<math>\min(c_f(A,D),c_f(D,F),c_f(F,G)) = </math></br> <math>\min(3-1,6-0,9-0) = </math></br> <math>\min(2,6,9) = 2</math></br> |
Image:Ek-flow 2.png |
<math>A,B,C,D,F,G</math> |
<math>\min(c_f(A,B),c_f(B,C),c_f(C,D),c_f(D,F),c_f(F,G)) = </math></br> <math>\min(3-0,4-0,1-0,6-2,9-2) = </math></br> <math>\min(3,4,1,4,7) = 1</math></br> |
Image:Ek-flow 3.png |
<math>A,B,C,E,D,F,G</math> |
<math>\min(c_f(A,B),c_f(B,C),c_f(C,E),c_f(E,D),c_f(D,F),c_f(F,G)) = </math></br> <math>\min(3-1,4-1,2-0,0--1,6-3,9-3) = </math></br> <math>\min(2,3,2,1,3,6) = 1</math></br> |
Image:Ek-flow 4.png |
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 <math>\{A,B,C,E\}</math> and <math>\{D,F,G\}</math>, with the capacity <math>c(A,D)+c(C,D)+c(E,G)=3+1+1=5</math>.
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)pt:Algoritmo de Edmonds-Karp