|
17 | 17 | * |
18 | 18 | * <p>This implementation mirrors the API and validation style of |
19 | 19 | * {@link EdmondsKarp#maxFlow(int[][], int, int)} for consistency.</p> |
| 20 | + * |
| 21 | + * @see <a href="https://en.wikipedia.org/wiki/Dinic%27s_algorithm">Wikipedia: Dinic's algorithm</a> |
20 | 22 | */ |
21 | 23 | public final class Dinic { |
| 24 | + private Dinic() {} |
22 | 25 |
|
23 | | - private Dinic() {} |
24 | | - |
25 | | - /** |
26 | | - * Computes the maximum flow from source to sink using Dinic's algorithm. |
27 | | - * |
28 | | - * @param capacity square capacity matrix (n x n); entries must be >= 0 |
29 | | - * @param source source vertex index in [0, n) |
30 | | - * @param sink sink vertex index in [0, n) |
31 | | - * @return the maximum flow value |
32 | | - * @throws IllegalArgumentException if the input matrix is null/non-square/has negatives or indices invalid |
33 | | - */ |
34 | | - public static int maxFlow(int[][] capacity, int source, int sink) { |
35 | | - if (capacity == null || capacity.length == 0) { |
36 | | - throw new IllegalArgumentException("Capacity matrix must not be null or empty"); |
37 | | - } |
38 | | - final int n = capacity.length; |
39 | | - for (int i = 0; i < n; i++) { |
40 | | - if (capacity[i] == null || capacity[i].length != n) { |
41 | | - throw new IllegalArgumentException("Capacity matrix must be square"); |
42 | | - } |
43 | | - for (int j = 0; j < n; j++) { |
44 | | - if (capacity[i][j] < 0) { |
45 | | - throw new IllegalArgumentException("Capacities must be non-negative"); |
46 | | - } |
47 | | - } |
48 | | - } |
49 | | - if (source < 0 || sink < 0 || source >= n || sink >= n) { |
50 | | - throw new IllegalArgumentException("Source and sink must be valid vertex indices"); |
51 | | - } |
52 | | - if (source == sink) { |
53 | | - return 0; |
| 26 | + /** |
| 27 | + * Computes the maximum flow from source to sink using Dinic's algorithm. |
| 28 | + * |
| 29 | + * @param capacity square capacity matrix (n x n); entries must be >= 0 |
| 30 | + * @param source source vertex index in [0, n) |
| 31 | + * @param sink sink vertex index in [0, n) |
| 32 | + * @return the maximum flow value |
| 33 | + * @throws IllegalArgumentException if the input matrix is null/non-square/has negatives or |
| 34 | + * indices invalid |
| 35 | + */ |
| 36 | + public static int maxFlow(int[][] capacity, int source, int sink) { |
| 37 | + if (capacity == null || capacity.length == 0) { |
| 38 | + throw new IllegalArgumentException("Capacity matrix must not be null or empty"); |
| 39 | + } |
| 40 | + final int n = capacity.length; |
| 41 | + for (int i = 0; i < n; i++) { |
| 42 | + if (capacity[i] == null || capacity[i].length != n) { |
| 43 | + throw new IllegalArgumentException("Capacity matrix must be square"); |
| 44 | + } |
| 45 | + for (int j = 0; j < n; j++) { |
| 46 | + if (capacity[i][j] < 0) { |
| 47 | + throw new IllegalArgumentException("Capacities must be non-negative"); |
54 | 48 | } |
| 49 | + } |
| 50 | + } |
| 51 | + if (source < 0 || sink < 0 || source >= n || sink >= n) { |
| 52 | + throw new IllegalArgumentException("Source and sink must be valid vertex indices"); |
| 53 | + } |
| 54 | + if (source == sink) { |
| 55 | + return 0; |
| 56 | + } |
55 | 57 |
|
56 | | - // residual capacities |
57 | | - int[][] residual = new int[n][n]; |
58 | | - for (int i = 0; i < n; i++) { |
59 | | - residual[i] = Arrays.copyOf(capacity[i], n); |
60 | | - } |
| 58 | + // residual capacities |
| 59 | + int[][] residual = new int[n][n]; |
| 60 | + for (int i = 0; i < n; i++) { |
| 61 | + residual[i] = Arrays.copyOf(capacity[i], n); |
| 62 | + } |
61 | 63 |
|
62 | | - int[] level = new int[n]; |
63 | | - int flow = 0; |
64 | | - while (bfsBuildLevelGraph(residual, source, sink, level)) { |
65 | | - int[] next = new int[n]; // current-edge optimization |
66 | | - int pushed; |
67 | | - do { |
68 | | - pushed = dfsBlocking(residual, level, next, source, sink, Integer.MAX_VALUE); |
69 | | - flow += pushed; |
70 | | - } while (pushed > 0); |
71 | | - } |
72 | | - return flow; |
| 64 | + int[] level = new int[n]; |
| 65 | + int flow = 0; |
| 66 | + while (bfsBuildLevelGraph(residual, source, sink, level)) { |
| 67 | + int[] next = new int[n]; // current-edge optimization |
| 68 | + int pushed; |
| 69 | + do { |
| 70 | + pushed = dfsBlocking(residual, level, next, source, sink, Integer.MAX_VALUE); |
| 71 | + flow += pushed; |
| 72 | + } while (pushed > 0); |
73 | 73 | } |
| 74 | + return flow; |
| 75 | + } |
74 | 76 |
|
75 | | - private static boolean bfsBuildLevelGraph(int[][] residual, int source, int sink, int[] level) { |
76 | | - Arrays.fill(level, -1); |
77 | | - level[source] = 0; |
78 | | - Queue<Integer> q = new ArrayDeque<>(); |
79 | | - q.add(source); |
80 | | - while (!q.isEmpty()) { |
81 | | - int u = q.poll(); |
82 | | - for (int v = 0; v < residual.length; v++) { |
83 | | - if (residual[u][v] > 0 && level[v] == -1) { |
84 | | - level[v] = level[u] + 1; |
85 | | - if (v == sink) { |
86 | | - return true; |
87 | | - } |
88 | | - q.add(v); |
89 | | - } |
90 | | - } |
| 77 | + private static boolean bfsBuildLevelGraph(int[][] residual, int source, int sink, int[] level) { |
| 78 | + Arrays.fill(level, -1); |
| 79 | + level[source] = 0; |
| 80 | + Queue<Integer> q = new ArrayDeque<>(); |
| 81 | + q.add(source); |
| 82 | + while (!q.isEmpty()) { |
| 83 | + int u = q.poll(); |
| 84 | + for (int v = 0; v < residual.length; v++) { |
| 85 | + if (residual[u][v] > 0 && level[v] == -1) { |
| 86 | + level[v] = level[u] + 1; |
| 87 | + if (v == sink) { |
| 88 | + return true; |
| 89 | + } |
| 90 | + q.add(v); |
91 | 91 | } |
92 | | - return level[sink] != -1; |
| 92 | + } |
93 | 93 | } |
| 94 | + return level[sink] != -1; |
| 95 | + } |
94 | 96 |
|
95 | | - private static int dfsBlocking(int[][] residual, int[] level, int[] next, int u, int sink, int f) { |
96 | | - if (u == sink) { |
97 | | - return f; |
98 | | - } |
99 | | - final int n = residual.length; |
100 | | - for (int v = next[u]; v < n; v++, next[u] = v) { |
101 | | - if (residual[u][v] <= 0) continue; |
102 | | - if (level[v] != level[u] + 1) continue; |
103 | | - int pushed = dfsBlocking(residual, level, next, v, sink, Math.min(f, residual[u][v])); |
104 | | - if (pushed > 0) { |
105 | | - residual[u][v] -= pushed; |
106 | | - residual[v][u] += pushed; |
107 | | - return pushed; |
108 | | - } |
109 | | - } |
110 | | - return 0; |
| 97 | + private static int dfsBlocking( |
| 98 | + int[][] residual, int[] level, int[] next, int u, int sink, int f) { |
| 99 | + if (u == sink) { |
| 100 | + return f; |
| 101 | + } |
| 102 | + final int n = residual.length; |
| 103 | + for (int v = next[u]; v < n; v++, next[u] = v) { |
| 104 | + if (residual[u][v] <= 0) |
| 105 | + continue; |
| 106 | + if (level[v] != level[u] + 1) |
| 107 | + continue; |
| 108 | + int pushed = dfsBlocking(residual, level, next, v, sink, Math.min(f, residual[u][v])); |
| 109 | + if (pushed > 0) { |
| 110 | + residual[u][v] -= pushed; |
| 111 | + residual[v][u] += pushed; |
| 112 | + return pushed; |
| 113 | + } |
111 | 114 | } |
| 115 | + return 0; |
| 116 | + } |
112 | 117 | } |
0 commit comments