Edmonds–Karp algorithm: Difference between revisions

Content deleted Content added
m Stray "=" symbol
ce
 
(17 intermediate revisions by 9 users not shown)
Line 1:
{{Short description|Algorithm to compute the maximum flow in a flow network}}
{{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}}
{{Wikibooks|Algorithm implementation|Graphs/Maximum flow/Edmonds-Karp|Edmonds-Karp}}
 
==Algorithm==
{{Merge to|Ford–Fulkerson algorithm|date=April 2024}}
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 <math>{{mvar|E</math>}} 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 There is an accessible proof in name='clrs'[[Introduction to Algorithms]]''.<ref>{{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'/>
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}}
 
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'/>
==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 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 <math>E</math> 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. There is an accessible proof in ''[[Introduction to Algorithms]]''.<ref>{{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>
 
==Pseudocode==
Line 55 ⟶ 57:
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]]
 
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.
Line 71 ⟶ 73:
= & \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>
Line 79 ⟶ 81:
= & \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>
Line 87 ⟶ 89:
= & \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>
Line 95 ⟶ 97:
= & \min(2,3,2,1,3,6) = 1
\end{align}</math>
| [[Image:Edmonds-Karp flow example 4.svg|300px|class=skin-invert-image]]</td>
|}