| 3 | import java.util.LinkedList; |
| 4 | |
| 5 | class FordFulkerson { |
| 6 | static final int V = 6; |
| 7 | |
| 8 | // Using BFS as a searching algorithm |
| 9 | boolean bfs(int Graph[][], int s, int t, int p[]) { |
| 10 | boolean visited[] = new boolean[V]; |
| 11 | for (int i = 0; i < V; ++i) |
| 12 | visited[i] = false; |
| 13 | |
| 14 | LinkedList<Integer> queue = new LinkedList<Integer>(); |
| 15 | queue.add(s); |
| 16 | visited[s] = true; |
| 17 | p[s] = -1; |
| 18 | |
| 19 | while (queue.size() != 0) { |
| 20 | int u = queue.poll(); |
| 21 | |
| 22 | for (int v = 0; v < V; v++) { |
| 23 | if (visited[v] == false && Graph[u][v] > 0) { |
| 24 | queue.add(v); |
| 25 | p[v] = u; |
| 26 | visited[v] = true; |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | return (visited[t] == true); |
| 32 | } |
| 33 | |
| 34 | // Applying fordfulkerson algorithm |
| 35 | int fordFulkerson(int graph[][], int s, int t) { |
| 36 | int u, v; |
| 37 | int Graph[][] = new int[V][V]; |
| 38 | |
| 39 | for (u = 0; u < V; u++) |
| 40 | for (v = 0; v < V; v++) |
| 41 | Graph[u][v] = graph[u][v]; |
| 42 | |
| 43 | int p[] = new int[V]; |
| 44 | |
| 45 | int max_flow = 0; |
| 46 | |
| 47 | // Updating the residual calues of edges |
| 48 | while (bfs(Graph, s, t, p)) { |
| 49 | int path_flow = Integer.MAX_VALUE; |
| 50 | for (v = t; v != s; v = p[v]) { |
| 51 | u = p[v]; |
| 52 | path_flow = Math.min(path_flow, Graph[u][v]); |
| 53 | } |
| 54 | |
| 55 | for (v = t; v != s; v = p[v]) { |
| 56 | u = p[v]; |
| 57 | Graph[u][v] -= path_flow; |
| 58 | Graph[v][u] += path_flow; |
| 59 | } |
| 60 | |
| 61 | // Adding the path flows |
| 62 | max_flow += path_flow; |
nothing calls this directly
no outgoing calls
no test coverage detected