|
| 1 | +/** |
| 2 | + * @author dbatchunag |
| 3 | + */ |
| 4 | + |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +public class FordFulkerson { |
| 8 | + private final int V = 6; |
| 9 | + |
| 10 | + //Check if there is a path from source to target in residue graph. |
| 11 | + private boolean bfs(final int[][] resGraph, final int source, final int target, final int[] parent) |
| 12 | + { |
| 13 | + // Create a visited array and mark all vertices as not visited |
| 14 | + final boolean[] visited = new boolean[V]; |
| 15 | + |
| 16 | + // Create a queue, enqueue source vertex |
| 17 | + // and mark source vertex as visited |
| 18 | + final Deque<Integer> queue= new ArrayDeque<>(); |
| 19 | + //We start from s, marking as visited. |
| 20 | + queue.push(source); |
| 21 | + visited[source] = true; |
| 22 | + parent[source] = -1; |
| 23 | + |
| 24 | + // Standard BFS Loop |
| 25 | + while (!queue.isEmpty()) { |
| 26 | + final int u = queue.pollFirst(); |
| 27 | + |
| 28 | + for (int v=0; v<V; v++) { |
| 29 | + if (!visited[v] && resGraph[u][v] > 0) { |
| 30 | + //Put the neighbors to the queue |
| 31 | + queue.push(v); |
| 32 | + //keep track for augmenting path |
| 33 | + parent[v] = u; |
| 34 | + visited[v] = true; |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + //augmenting path is found |
| 39 | + return visited[target]; |
| 40 | + } |
| 41 | + |
| 42 | + private int fordFulkerson(final int[][] graph, final int source, final int target) |
| 43 | + { |
| 44 | + final int[][] residualGraph = graph.clone(); |
| 45 | + final int[] parent= new int[V]; |
| 46 | + |
| 47 | + int max_flow = 0; |
| 48 | + |
| 49 | + // Augment the flow while there is path from source to sink |
| 50 | + while (bfs(residualGraph, source, target, parent)) |
| 51 | + { |
| 52 | + //augmenting path can be constructed through parent |
| 53 | + int path_flow = Integer.MAX_VALUE; |
| 54 | + for (int v=target; v!=source; v=parent[v]) { |
| 55 | + final int u = parent[v]; |
| 56 | + //get the maximum possible size augmenting flow |
| 57 | + path_flow = Math.min(path_flow, residualGraph[u][v]); |
| 58 | + } |
| 59 | + |
| 60 | + //update the graph |
| 61 | + for (int v=target; v != source; v=parent[v]) { |
| 62 | + final int u = parent[v]; |
| 63 | + residualGraph[u][v] -= path_flow; |
| 64 | + residualGraph[v][u] += path_flow; |
| 65 | + } |
| 66 | + |
| 67 | + max_flow += path_flow; |
| 68 | + } |
| 69 | + |
| 70 | + return max_flow; |
| 71 | + } |
| 72 | + |
| 73 | + private void run() { |
| 74 | + // Let us create a graph shown in the above example |
| 75 | + final int[][] graph = new int[][] { |
| 76 | + {0, 16, 13, 0, 0, 0}, |
| 77 | + {0, 0, 10, 12, 0, 0}, |
| 78 | + {0, 4, 0, 0, 14, 0}, |
| 79 | + {0, 0, 9, 0, 0, 20}, |
| 80 | + {0, 0, 0, 7, 0, 4}, |
| 81 | + {0, 0, 0, 0, 0, 0} |
| 82 | + }; |
| 83 | + |
| 84 | + System.out.println(String.format("The maximum possible flow is %d", fordFulkerson(graph, 0, 5))); |
| 85 | + } |
| 86 | + |
| 87 | + public static void main(String[] args) { |
| 88 | + new FordFulkerson().run(); |
| 89 | + } |
| 90 | + |
| 91 | +} |
| 92 | + |
0 commit comments