|
2 | 2 |
|
3 | 3 | import java.util.LinkedList;
|
4 | 4 | import java.util.Queue;
|
5 |
| -import java.util.Vector; |
6 | 5 |
|
7 | 6 | public final class FordFulkerson {
|
8 |
| - private FordFulkerson() { |
9 |
| - } |
10 |
| - |
11 |
| - static final int INF = 987654321; |
12 |
| - // edges |
13 |
| - static int vertexCount; |
14 |
| - static int[][] capacity; |
15 |
| - static int[][] flow; |
| 7 | + private static final int INF = Integer.MAX_VALUE; |
16 | 8 |
|
17 |
| - public static void main(String[] args) { |
18 |
| - System.out.println("Vertex Count : 6"); |
19 |
| - vertexCount = 6; |
20 |
| - capacity = new int[vertexCount][vertexCount]; |
21 |
| - |
22 |
| - capacity[0][1] = 12; |
23 |
| - capacity[0][3] = 13; |
24 |
| - capacity[1][2] = 10; |
25 |
| - capacity[2][3] = 13; |
26 |
| - capacity[2][4] = 3; |
27 |
| - capacity[2][5] = 15; |
28 |
| - capacity[3][2] = 7; |
29 |
| - capacity[3][4] = 15; |
30 |
| - capacity[4][5] = 17; |
31 |
| - |
32 |
| - System.out.println("Max capacity in networkFlow : " + networkFlow(0, 5)); |
| 9 | + private FordFulkerson() { |
33 | 10 | }
|
34 | 11 |
|
35 |
| - private static int networkFlow(int source, int sink) { |
36 |
| - flow = new int[vertexCount][vertexCount]; |
| 12 | + public static int networkFlow(int vertexCount, int[][] capacity, int[][] flow, int source, int sink) { |
37 | 13 | int totalFlow = 0;
|
| 14 | + |
38 | 15 | while (true) {
|
39 |
| - Vector<Integer> parent = new Vector<>(vertexCount); |
40 |
| - for (int i = 0; i < vertexCount; i++) { |
41 |
| - parent.add(-1); |
42 |
| - } |
43 |
| - Queue<Integer> q = new LinkedList<>(); |
44 |
| - parent.set(source, source); |
45 |
| - q.add(source); |
46 |
| - while (!q.isEmpty() && parent.get(sink) == -1) { |
47 |
| - int here = q.peek(); |
48 |
| - q.poll(); |
49 |
| - for (int there = 0; there < vertexCount; ++there) { |
50 |
| - if (capacity[here][there] - flow[here][there] > 0 && parent.get(there) == -1) { |
51 |
| - q.add(there); |
52 |
| - parent.set(there, here); |
| 16 | + int[] parent = new int[vertexCount]; |
| 17 | + boolean[] visited = new boolean[vertexCount]; |
| 18 | + Queue<Integer> queue = new LinkedList<>(); |
| 19 | + |
| 20 | + queue.add(source); |
| 21 | + visited[source] = true; |
| 22 | + parent[source] = -1; |
| 23 | + |
| 24 | + while (!queue.isEmpty() && !visited[sink]) { |
| 25 | + int current = queue.poll(); |
| 26 | + |
| 27 | + for (int next = 0; next < vertexCount; next++) { |
| 28 | + if (!visited[next] && capacity[current][next] - flow[current][next] > 0) { |
| 29 | + queue.add(next); |
| 30 | + visited[next] = true; |
| 31 | + parent[next] = current; |
53 | 32 | }
|
54 | 33 | }
|
55 | 34 | }
|
56 |
| - if (parent.get(sink) == -1) { |
57 |
| - break; |
| 35 | + |
| 36 | + if (!visited[sink]) { |
| 37 | + break; // No more augmenting paths |
58 | 38 | }
|
59 | 39 |
|
60 |
| - int amount = INF; |
61 |
| - String printer = "path : "; |
62 |
| - StringBuilder sb = new StringBuilder(); |
63 |
| - for (int p = sink; p != source; p = parent.get(p)) { |
64 |
| - amount = Math.min(capacity[parent.get(p)][p] - flow[parent.get(p)][p], amount); |
65 |
| - sb.append(p + "-"); |
| 40 | + int pathFlow = INF; |
| 41 | + for (int v = sink; v != source; v = parent[v]) { |
| 42 | + int u = parent[v]; |
| 43 | + pathFlow = Math.min(pathFlow, capacity[u][v] - flow[u][v]); |
66 | 44 | }
|
67 |
| - sb.append(source); |
68 |
| - for (int p = sink; p != source; p = parent.get(p)) { |
69 |
| - flow[parent.get(p)][p] += amount; |
70 |
| - flow[p][parent.get(p)] -= amount; |
| 45 | + |
| 46 | + for (int v = sink; v != source; v = parent[v]) { |
| 47 | + int u = parent[v]; |
| 48 | + flow[u][v] += pathFlow; |
| 49 | + flow[v][u] -= pathFlow; |
71 | 50 | }
|
72 |
| - totalFlow += amount; |
73 |
| - printer += sb.reverse() + " / max flow : " + totalFlow; |
74 |
| - System.out.println(printer); |
| 51 | + |
| 52 | + totalFlow += pathFlow; |
75 | 53 | }
|
76 | 54 |
|
77 | 55 | return totalFlow;
|
|
0 commit comments