Content deleted Content added
Nils Grimsmo (talk | contribs) Added sample implementation. |
Nils Grimsmo (talk | contribs) No edit summary |
||
Line 1:
In [[computer science]] and [[graph theory]], the '''Edmonds-Karp algorithm''' is an implementation of the [[Ford-Fulkerson algorithm|Ford-Fulkerson method]] for computing the [[maximum flow problem|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 practise for [[sparse graph]]s. 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 <math>O(V^2E)</math>.
==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 shoortest 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|right]]
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 egdes. 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 saturise it have strictly increasing length. Since paths do not have cycles, their length is <math>O(V)</math>. Hence the number of saturising 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 saturises <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==
Line 9 ⟶ 21:
[[Python programming language|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'''
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:
[[Image:ek-flow_0.png]]
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>R_{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.
<table width="100%">
<tr>
<th>Path</th>
<th>Capacity</th>
<th>Resulting network</th>
</tr>
<tr>
<td><math>A-D-E-G</math></td>
<td>
<math>\min(R_{A,D},R_{D,E},R_{E,G}) = </math></br>
<math>\min(3-0,2-0,1-0) = </math></br>
<math>\min(3,2,1) = 1</math></br>
</td>
<td>[[Image:ek-flow_1.png]]</td>
</tr>
<tr>
<td><math>A-D-F-G</math></td>
<td>
<math>\min(R_{A,D},R_{D,F},R_{F,G}) = </math></br>
<math>\min(3-1,6-0,9-0) = </math></br>
<math>\min(2,6,9) = 2</math></br>
</td>
<td>[[Image:ek-flow_2.png]]</td>
</tr>
<tr>
<td><math>A-B-C-D-F-G</math></td>
<td>
<math>\min(R_{A,B},R_{B,C},R_{C,D},R_{D,F},R_{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>
</td>
<td>[[Image:ek-flow_3.png]]</td>
</tr>
<tr>
<td><math>A-B-C-E-D-F-G</math></td>
<td>
<math>\min(R_{A,B},R_{B,C},R_{C,E},R_{E,D},R_{D,F},R_{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>
</td>
<td>[[Image:ek-flow_4.png]]</td>
</tr>
</table>
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 [[max_flow_min_cut_theorem|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 [[Association for Computing Machinery|ACM]]'', Vol 19, No. 2 (1972) pp248-264. [http://delivery.acm.org/10.1145/330000/321699/p248-edmonds.pdf PDF (needs subscription)]
|