http://en.wikipedia.org/wiki/Edmonds%E2%80%93Karp_algorithm
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 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.
Java code: http://en.wikibooks.org/wiki/Algorithm_Implementation/Graphs/Maximum_flow/Edmonds-Karp
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 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.
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.push(s) while Q.size() > 0 u := Q.pop() 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.push(v) else return M[t], P return 0, P
Java code: http://en.wikibooks.org/wiki/Algorithm_Implementation/Graphs/Maximum_flow/Edmonds-Karp
/** * Finds the maximum flow in a flow network. * @param E neighbour lists * @param C capacity matrix (must be n by n) * @param s source * @param t sink * @return maximum flow */ public class EdmondsKarp { public static int edmondsKarp(int[][] E, int[][] C, int s, int t) { int n = C.length; // Residual capacity from u to v is C[u][v] - F[u][v] int[][] F = new int[n][n]; while (true) { int[] P = new int[n]; // Parent table Arrays.fill(P, -1); P[s] = s; int[] M = new int[n]; // Capacity of path to node M[s] = Integer.MAX_VALUE; // BFS queue Queue<Integer> Q = new LinkedList<Integer>(); Q.offer(s); LOOP: while (!Q.isEmpty()) { int u = Q.poll(); for (int v : E[u]) { // There is available capacity, // and v is not seen before in search if (C[u][v] - F[u][v] > 0 && P[v] == -1) { P[v] = u; M[v] = Math.min(M[u], C[u][v] - F[u][v]); if (v != t) Q.offer(v); else { // Backtrack search, and write flow while (P[v] != v) { u = P[v]; F[u][v] += M[t]; F[v][u] -= M[t]; v = u; } break LOOP; } } } } if (P[t] == -1) { // We did not find a path to t int sum = 0; for (int x : F[s]) sum += x; return sum; } } } }