Edmonds–Karp algorithm: Difference between revisions

Content deleted Content added
ce
 
(253 intermediate revisions by more than 100 users not shown)
Line 1:
{{Short description|Algorithm to compute the maximum flow in a flow network}}
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>.
{{Use American English|date = April 2019}}
In [[computer science]], 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]] in [[big O notation|<math>O(|V||E|^2)</math>]] time. The algorithm was first published by [[Yefim Dinitz]] in 1970,<ref>{{cite journal |first=E. A. |last=Dinic |author-link=Yefim Dinitz |title=Algorithm for solution of a problem of maximum flow in a network with power estimation |journal=Soviet Mathematics - Doklady |volume=11 |pages=1277–1280 |publisher=Doklady |year=1970 }}</ref><ref name="ipv">{{cite book | author = Yefim Dinitz | editor = [[Oded Goldreich]] |editor2=Arnold L. Rosenberg |editor3=Alan L. Selman |editor3-link = Alan Selman| title = Theoretical Computer Science: Essays in Memory of [[Shimon Even]] | chapter = Dinitz' Algorithm: The Original Version and Even's Version | year = 2006 | publisher = Springer | isbn = 978-3-540-32880-3 | pages = 218–240 | chapter-url = https://rangevoting.org/Dinitz_alg.pdf}}</ref> and independently published by [[Jack Edmonds]] and [[Richard Karp]] in 1972.<ref>{{cite journal |last1=Edmonds |first1=Jack |author1-link=Jack Edmonds |last2=Karp |first2=Richard M. |author2-link=Richard Karp |title=Theoretical improvements in algorithmic efficiency for network flow problems |journal=Journal of the ACM |volume=19 |issue=2 |pages=248–264 |year=1972 |url=http://www.eecs.umich.edu/%7Epettie/matching/Edmonds-Karp-network-flow.pdf |doi=10.1145/321694.321699 |s2cid=6375478 }}</ref> [[Dinic's algorithm|Dinitz's algorithm]] includes additional techniques that reduce the running time to <math>O(|V|^2|E|)</math>.<ref name="ipv" />
{{Wikibooks|Algorithm implementation|Graphs/Maximum flow/Edmonds-Karp|Edmonds-Karp}}
 
==Algorithm==
The algorithm is identical to the [[Ford–Fulkerson algorithm]], except that the search order when finding the [[Flow network#Augmenting paths|augmenting path]] is defined. The path found must be a [[Shortest path problem|shortest path]] that has available capacity. This can be found by a [[breadth-first search]], where we apply a weight of 1 to each edge. The running time of <math>O(|V||E|^2)</math> is found by showing that each augmenting path can be found in <math>O(|E|)</math> time, that every time at least one of the {{mvar|E}} edges becomes saturated (an edge which has the maximum possible flow), that the distance from the saturated edge to the source along the augmenting path must be longer than last time it was saturated, and that the length is at most <math>|V|</math>. Another property of this algorithm is that the length of the shortest augmenting path increases monotonically.<ref name='clrs'>{{cite book |author=[[Thomas H. Cormen]], [[Charles E. Leiserson]], [[Ronald L. Rivest]] and [[Clifford Stein]] |title=Introduction to Algorithms |publisher=MIT Press | year = 2009 |isbn=978-0-262-03384-8 |edition=third |chapter=26.2 |pages=727–730 |title-link=Introduction to Algorithms }}</ref> A proof outline using these properties is as follows:
 
The proof first establishes that distance of the shortest path from the source node {{mvar|s}} to any non-sink node {{mvar|v}} in a residual flow network increases monotonically after each augmenting iteration (Lemma 1, proven below). Then, it shows that each of the <math>|E|</math> edges can be critical at most <math>\frac{|V|}{2}</math> times for the duration of the algorithm, giving an upper-bound of <math>O\left( \frac{|V||E|}{2} \right) \in O(|V||E|)</math> augmenting iterations. Since each iteration takes <math>O(|E|)</math> time (bounded by the time for finding the shortest path using Breadth-First-Search), the total running time of Edmonds-Karp is <math>O(|V||E|^2)</math> as required. <ref name='clrs'/>
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.
 
To prove Lemma 1, one can use [[proof by contradiction]] by assuming that there is an augmenting iteration that causes the shortest path distance from {{mvar|s}} to {{mvar|v}} to ''decrease''. Let {{mvar|f}} be the flow before such an augmentation and <math>f'</math> be the flow after. Denote the minimum distance in a residual flow network {{tmath|G_f}} from nodes <math>u, v</math> as <math>\delta_f (u, v)</math>. One can derive a contradiction by showing that <math>\delta_f (s, v) \leq \delta _{f'} (s, v)</math>, meaning that the shortest path distance between source node {{mvar|s}} and non-sink node {{mvar|v}} did not in fact decrease. <ref name='clrs'/>
==Complexity==
 
==Pseudocode==
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:
'''algorithm''' EdmondsKarp '''is'''
 
'''input''':
[[Image:ek-flow_comp1.png|right]]
graph ''(graph[v] should be the list of edges coming out of vertex v in the''
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.
'' original graph '''and''' their corresponding constructed reverse edges''
 
'' which are used for push-back flow.''
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 edge should have a capacity 'cap', flow, source 's' and sink 't' ''
 
'' as parameters, as well as a pointer to the reverse edge 'rev'.)''
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).
s ''(Source vertex)''
 
t ''(Sink vertex)''
==Sample implementation==
'''output''':
 
flow ''(Value of maximum flow)''
[[Python programming language|Python]] implementation:
 
flow := 0 ''(Initialize flow to zero)''
'''def''' edmonds_karp(C, source, sink):
'''repeat'''
n = len(C) # ''C is the capacity matrix''
''(Run a breadth-first search (bfs) to find the shortest s-t path.''
F = [[0] * n for _ in xrange(n)]
# ''residual capacityWe fromuse u'pred' to vstore isthe C[u][v]edge taken to get to -each F[u][v]vertex,''
'' so we can recover the path afterwards)''
q := '''queue'''()
q.push(s)
pred := '''array'''(graph.length)
'''while''' '''not''' empty(q) '''and''' pred[t] = null
cur := q.pop()
'''for''' Edge e '''in''' graph[cur] '''do'''
'''if''' pred[e.t] = '''null''' '''and''' e.t ≠ s '''and''' e.cap > e.flow '''then'''
pred[e.t] := e
q.push(e.t)
'''whileif''' True:'''not''' (pred[t] = null) '''then'''
path = bfs ''(C,We F,found source,an sink)augmenting path.''
'''if not'See how much flow we can send)'' path:
df := '''break'''
flow = Inf # ''traverse'for''' path(e to:= findpred[t]; smalleste capacity≠ null; e := pred[e.s]) '''do'''
'''for''' i df := '''inmin''' xrange(len(path)df, e.cap - 1e.flow):
u,v''(And =update path[i],edges path[i+1]by that amount)''
flow'''for''' = min(flow,e C:= pred[u][vt]; -e F≠ null; e := pred[u][ve.s]) '''do'''
# ''traverse path to update e.flow'' := e.flow + df
e.rev.flow := e.rev.flow - df
'''for''' i '''in''' range(len(path) - 1):
u,vflow := path[i],flow path[i+1] df
F[u][v] += flow
F[v][u] -= flow
'''return''' sum([F[source][i] for i in xrange(n)])
'''until''' pred[t] = null ''(i.e., until no augmenting path was found)''
'''def''' bfs(C, F, source, sink):
'''return''' flow
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, source A, sink G, and capacities as shown below:
 
[[Image:Edmonds-Karp flow example 0.svg|300px|class=skin-invert-image]]
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 that is already used. If the net flow from <math>u</math> to <math>v</math> is negative, it ''contributes'' to the residual capacity.
[[Image:ek-flow_0.png]]
 
{| class="wikitable"
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
|-
| align="center" | <math>A,D,E,G</math>
| <math>\begin{align}
& \min(c_f(A,D),c_f(D,E),c_f(E,G)) \\
= & \min(3-0,2-0,1-0) \\
= & \min(3,2,1) = 1
\end{align}</math>
| [[Image:Edmonds-Karp flow example 1.svg|300px|class=skin-invert-image]]</td>
|-
| align="center" | <math>A,D,F,G</math>
| <math>\begin{align}
& \min(c_f(A,D),c_f(D,F),c_f(F,G)) \\
= & \min(3-1,6-0,9-0) \\
= & \min(2,6,9) = 2
\end{align}</math>
| [[Image:Edmonds-Karp flow example 2.svg|300px|class=skin-invert-image]]</td>
|-
| align="center" | <math>A,B,C,D,F,G</math>
| <math>\begin{align}
& \min(c_f(A,B),c_f(B,C),c_f(C,D),c_f(D,F),c_f(F,G)) \\
= & \min(3-0,4-0,1-0,6-2,9-2) \\
= & \min(3,4,1,4,7) = 1
\end{align}</math>
| [[Image:Edmonds-Karp flow example 3.svg|300px|class=skin-invert-image]]</td>
|-
| align="center" | <math>A,B,C,E,D,F,G</math>
| <math>\begin{align}
& \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)) \\
= & \min(3-1,4-1,2-0,0-(-1),6-3,9-3) \\
= & \min(2,3,2,1,3,6) = 1
\end{align}</math>
| [[Image:Edmonds-Karp flow example 4.svg|300px|class=skin-invert-image]]</td>
|}
 
Notice how the length of the [[augmenting path]] found by the algorithm (in red) never decreases. The paths found are the shortest possible. The flow found is equal to the capacity across the [[max flow min cut theorem|minimum 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
<table width="100%">
:<math>c(A,D)+c(C,D)+c(E,G)=3+1+1=5.\ </math>
<tr>
<th>Path</th>
<th>Capacity</th>
<th>Resulting network</th>
</tr>
 
==Notes==
<tr>
{{reflist|30em}}
<td><math>A,D,E,G</math></td>
<td>
<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>
</td>
<td>[[Image:ek-flow_1.png]]</td>
</tr>
 
<tr>
<td><math>A,D,F,G</math></td>
<td>
<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>
</td>
<td>[[Image:ek-flow_2.png]]</td>
</tr>
 
<tr>
<td><math>A,B,C,D,F,G</math></td>
<td>
<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>
</td>
<td>[[Image:ek-flow_3.png]]</td>
</tr>
 
<tr>
<td><math>A,B,C,E,D,F,G</math></td>
<td>
<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>
</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==
# Algorithms and Complexity (see pages 63–69). https://web.archive.org/web/20061005083406/http://www.cis.upenn.edu/~wilf/AlgComp3.html
 
{{Optimization algorithms|combinatorial}}
* 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.
{{DEFAULTSORT:Edmonds-Karp Algorithm}}
* 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)]
[[Category:Network flow problem]]
 
[[Category:Graph algorithms]]