|
| 1 | +/** |
| 2 | + * @author dbatchunag |
| 3 | + */ |
| 4 | + |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +public class EdmondsKarp { |
| 8 | + |
| 9 | + private int maxFlowEdmondsKarp(final int [][]edges, final int[][] capacity, final int source, final int target) { |
| 10 | + final int n = capacity.length; |
| 11 | + int maxFlow = 0; |
| 12 | + final int[][] resGraph = new int [n][n]; |
| 13 | + while (true) { |
| 14 | + //backtrack for augmenting path |
| 15 | + final int[] parent = new int[n]; |
| 16 | + Arrays.fill(parent, -1); |
| 17 | + parent[source] = -2; |
| 18 | + |
| 19 | + //parent is updated during the bfs |
| 20 | + final int pathFlow = getAugmentingPath(edges, capacity, source, target, resGraph, parent); |
| 21 | + if (pathFlow == 0) { |
| 22 | + //No augmenting path is found |
| 23 | + break; |
| 24 | + } |
| 25 | + maxFlow += pathFlow; |
| 26 | + int v = target; |
| 27 | + while (v != source) { |
| 28 | + //traverse along the backtrack |
| 29 | + final int u = parent[v]; |
| 30 | + resGraph[u][v] = resGraph[u][v] + pathFlow; |
| 31 | + resGraph[v][u] = resGraph[v][u] - pathFlow; |
| 32 | + v = u; |
| 33 | + } |
| 34 | + } |
| 35 | + return maxFlow; |
| 36 | + } |
| 37 | + |
| 38 | + //Returns |
| 39 | + private int getAugmentingPath(final int[][] edges, |
| 40 | + final int[][] capacity, |
| 41 | + final int source, |
| 42 | + final int target, |
| 43 | + final int[][] resGraph, |
| 44 | + final int[] parent) { |
| 45 | + final int n = capacity.length; |
| 46 | + final int[] maxFlow = new int[n]; |
| 47 | + maxFlow[source] = Integer.MAX_VALUE; |
| 48 | + |
| 49 | + final Deque<Integer> bfsQueue = new ArrayDeque<Integer>(); |
| 50 | + bfsQueue.add(source); |
| 51 | + |
| 52 | + while (bfsQueue.size()> 0) { |
| 53 | + final int u = bfsQueue.pop(); |
| 54 | + for (final int v : edges[u]) { |
| 55 | + if (capacity[u][v] - resGraph[u][v] > 0 && parent[v] == -1) { |
| 56 | + //we may use edge u->v (on source ~> .. ~> target) |
| 57 | + parent[v] = u; |
| 58 | + //Maximum possible flow size from source to v is computed below. |
| 59 | + maxFlow[v] = Math.min(maxFlow[u], capacity[u][v] - resGraph[u][v]); |
| 60 | + if (v != target) { |
| 61 | + bfsQueue.add(v); |
| 62 | + } else { |
| 63 | + //Path found |
| 64 | + return maxFlow[target]; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + return 0; |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + private void run() { |
| 74 | + // Adjacency Matrix |
| 75 | + int [][] edges = new int[][]{{1, 2}, {2, 3}, {3}, {}}; |
| 76 | + // Capacity Matrix |
| 77 | + int [][] capacity = new int[][]{ |
| 78 | + {0, 1000000, 1000000, 0}, |
| 79 | + {0, 0, 1, 1000000}, |
| 80 | + {0, 0, 0, 1000000}, |
| 81 | + {0, 0, 0, 0} |
| 82 | + }; |
| 83 | + final int source = 0; |
| 84 | + final int target = 3; |
| 85 | + |
| 86 | + System.out.println(String.format("The maximum possible flow is %d", maxFlowEdmondsKarp(edges, capacity, source, target))); |
| 87 | + } |
| 88 | + |
| 89 | + public static void main(String[] args) { |
| 90 | + new EdmondsKarp().run(); |
| 91 | + } |
| 92 | + |
| 93 | +} |
| 94 | + |
0 commit comments