# Edmonds–Karp algorithm

In computer science, the Edmonds–Karp algorithm is an implementation of the Ford–Fulkerson method for computing the maximum flow in a flow network in O(V E2) time. The algorithm was first published by Yefim (Chaim) Dinic in 1970[1] and independently published by Jack Edmonds and Richard Karp in 1972.[2] Dinic's algorithm includes additional techniques that reduce the running time to O(V2E).

## 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 a shortest path that has available capacity. This can be found by a breadth-first search, as we let edges have unit length. The running time of O(V E2) is found by showing that each augmenting path can be found in O(E) time, that every time at least one of the 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 V. 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.[3]

## Pseudocode

For a more high level description, see Ford–Fulkerson algorithm.
```algorithm EdmondsKarp
input:
C[1..n, 1..n] (Capacity matrix)
E[1..n, 1..?] (Neighbour lists)
s             (Source)
t             (Sink)
output:
f             (Value of maximum flow)
F             (A matrix giving a legal flow with the maximum value)
f := 0 (Initial flow is zero)
F := array(1..n, 1..n) (Residual capacity from u to v is C[u,v] - F[u,v])
forever
m, P := BreadthFirstSearch(C, E, s, t, F)
if m = 0
break
f := f + m
(Backtrack search, and write flow)
v := t
while v ≠ s
u := P[v]
F[u,v] := F[u,v] + m
F[v,u] := F[v,u] - m
v := u
return (f, F)
```
```algorithm BreadthFirstSearch
input:
C, E, s, t, F
output:
M[t]          (Capacity of path found)
P             (Parent table)
P := array(1..n)
for u in 1..n
P[u] := -1
P[s] := -2 (make sure source is not rediscovered)
M := array(1..n) (Capacity of found path to node)
M[s] := ∞
Q := queue()
Q.offer(s)
while Q.size() > 0
u := Q.poll()
for v in E[u]
(If there is available capacity, and v is not seen before in search)
if C[u,v] - F[u,v] > 0 and P[v] = -1
P[v] := u
M[v] := min(M[u], C[u,v] - F[u,v])
if v ≠ t
Q.offer(v)
else
return M[t], P
return 0, P
```

EdmondsKarp pseudo code using Adjacency nodes.
```algorithm EdmondsKarp
input:
graph (Graph with list of Adjacency nodes with capacities,flow,reverse and destinations)
s             (Source)
t             (Sink)
output:
flow             (Value of maximum flow)
flow := 0 (Initial flow to zero)
q := array(1..n) (Initialize q to graph length)
while true
qt := 0		(Variable to iterate over all the corresponding edges for a source)
q[qt+1] := s    (initialize source array)
pred := array(q.length)    (Initialize predecessor List with the graph length)
for qh=0;qh < qt && pred[t] := null
cur := q[qh]
for (graph[cur]) (Iterate over list of Edges)
Edge[] e :=  graph[cur]  (Each edge should be associated with Capacity)
if pred[e.t] == null && e.cap > e.f
pred[e.t] := e
q[qt++] : = e.t
if pred[t] = null
break
int df := MAX VALUE (Initialize to max integer value)
for u = t; u != s; u = pred[u].s
df := min(df, pred[u].cap - pred[u].f)
for u = t; u != s; u = pred[u].s
pred[u].f  := pred[u].f + df
pEdge := array(PredEdge)
pEdge := graph[pred[u].t]
pEdge[pred[u].rev].f := pEdge[pred[u].rev].f - df;
flow := flow + df
return flow
```

## Example

Given a network of seven nodes, source A, sink G, and capacities as shown below:

In the pairs ${\displaystyle f/c}$ written on the edges, ${\displaystyle f}$ is the current flow, and ${\displaystyle c}$ is the capacity. The residual capacity from ${\displaystyle u}$ to ${\displaystyle v}$ is ${\displaystyle c_{f}(u,v)=c(u,v)-f(u,v)}$, the total capacity, minus the flow that is already used. If the net flow from ${\displaystyle u}$ to ${\displaystyle v}$ is negative, it contributes to the residual capacity.

Capacity Path
Resulting network
${\displaystyle \min(c_{f}(A,D),c_{f}(D,E),c_{f}(E,G))=}$

${\displaystyle \min(3-0,2-0,1-0)=}$
${\displaystyle \min(3,2,1)=1}$

${\displaystyle A,D,E,G}$
${\displaystyle \min(c_{f}(A,D),c_{f}(D,F),c_{f}(F,G))=}$

${\displaystyle \min(3-1,6-0,9-0)=}$
${\displaystyle \min(2,6,9)=2}$

${\displaystyle A,D,F,G}$
${\displaystyle \min(c_{f}(A,B),c_{f}(B,C),c_{f}(C,D),c_{f}(D,F),c_{f}(F,G))=}$

${\displaystyle \min(3-0,4-0,1-0,6-2,9-2)=}$
${\displaystyle \min(3,4,1,4,7)=1}$

${\displaystyle A,B,C,D,F,G}$
${\displaystyle \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))=}$

${\displaystyle \min(3-1,4-1,2-0,0-(-1),6-3,9-3)=}$
${\displaystyle \min(2,3,2,1,3,6)=1}$

${\displaystyle A,B,C,E,D,F,G}$

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 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 ${\displaystyle \{A,B,C,E\}}$ and ${\displaystyle \{D,F,G\}}$, with the capacity

${\displaystyle c(A,D)+c(C,D)+c(E,G)=3+1+1=5.\ }$

## Notes

1. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
2. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
3. {{#invoke:citation/CS1|citation |CitationClass=book }}

## References

1. Algorithms and Complexity (see pages 63–69). http://www.cis.upenn.edu/~wilf/AlgComp3.html