|
| 1 | +package com.thealgorithms.graph; |
| 2 | + |
| 3 | +import java.util.ArrayDeque; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.Queue; |
| 6 | + |
| 7 | +/** |
| 8 | + * Gomory–Hu tree construction for undirected graphs via n−1 max-flow computations. |
| 9 | + * |
| 10 | + * <p>API: {@code buildTree(int[][])} returns {@code {parent, weight}} arrays for the tree. |
| 11 | + * |
| 12 | + * @see <a href="https://en.wikipedia.org/wiki/Gomory%E2%80%93Hu_tree">Wikipedia: Gomory–Hu tree</a> |
| 13 | + */ |
| 14 | + |
| 15 | +public final class GomoryHuTree { |
| 16 | + private GomoryHuTree() { |
| 17 | + } |
| 18 | + |
| 19 | + public static int[][] buildTree(int[][] cap) { |
| 20 | + validateCapacityMatrix(cap); |
| 21 | + final int n = cap.length; |
| 22 | + if (n == 1) { |
| 23 | + return new int[][] {new int[] {-1}, new int[] {0}}; |
| 24 | + } |
| 25 | + |
| 26 | + int[] parent = new int[n]; |
| 27 | + int[] weight = new int[n]; |
| 28 | + Arrays.fill(parent, 0); |
| 29 | + parent[0] = -1; |
| 30 | + weight[0] = 0; |
| 31 | + |
| 32 | + for (int s = 1; s < n; s++) { |
| 33 | + int t = parent[s]; |
| 34 | + MaxFlowResult res = edmondsKarpWithMinCut(cap, s, t); |
| 35 | + int f = res.flow; |
| 36 | + weight[s] = f; |
| 37 | + |
| 38 | + for (int v = 0; v < n; v++) { |
| 39 | + if (v != s && parent[v] == t && res.reachable[v]) { |
| 40 | + parent[v] = s; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + if (t != 0 && res.reachable[parent[t]]) { |
| 45 | + parent[s] = parent[t]; |
| 46 | + parent[t] = s; |
| 47 | + weight[s] = weight[t]; |
| 48 | + weight[t] = f; |
| 49 | + } |
| 50 | + } |
| 51 | + return new int[][] {parent, weight}; |
| 52 | + } |
| 53 | + |
| 54 | + private static void validateCapacityMatrix(int[][] cap) { |
| 55 | + if (cap == null || cap.length == 0) { |
| 56 | + throw new IllegalArgumentException("Capacity matrix must not be null or empty"); |
| 57 | + } |
| 58 | + final int n = cap.length; |
| 59 | + for (int i = 0; i < n; i++) { |
| 60 | + if (cap[i] == null || cap[i].length != n) { |
| 61 | + throw new IllegalArgumentException("Capacity matrix must be square"); |
| 62 | + } |
| 63 | + for (int j = 0; j < n; j++) { |
| 64 | + if (cap[i][j] < 0) { |
| 65 | + throw new IllegalArgumentException("Capacities must be non-negative"); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private static final class MaxFlowResult { |
| 72 | + final int flow; |
| 73 | + final boolean[] reachable; |
| 74 | + MaxFlowResult(int flow, boolean[] reachable) { |
| 75 | + this.flow = flow; |
| 76 | + this.reachable = reachable; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private static MaxFlowResult edmondsKarpWithMinCut(int[][] capacity, int source, int sink) { |
| 81 | + final int n = capacity.length; |
| 82 | + int[][] residual = new int[n][n]; |
| 83 | + for (int i = 0; i < n; i++) { |
| 84 | + residual[i] = Arrays.copyOf(capacity[i], n); |
| 85 | + } |
| 86 | + |
| 87 | + int[] parent = new int[n]; |
| 88 | + int maxFlow = 0; |
| 89 | + |
| 90 | + while (bfs(residual, source, sink, parent)) { |
| 91 | + int pathFlow = Integer.MAX_VALUE; |
| 92 | + for (int v = sink; v != source; v = parent[v]) { |
| 93 | + int u = parent[v]; |
| 94 | + pathFlow = Math.min(pathFlow, residual[u][v]); |
| 95 | + } |
| 96 | + for (int v = sink; v != source; v = parent[v]) { |
| 97 | + int u = parent[v]; |
| 98 | + residual[u][v] -= pathFlow; |
| 99 | + residual[v][u] += pathFlow; |
| 100 | + } |
| 101 | + maxFlow += pathFlow; |
| 102 | + } |
| 103 | + |
| 104 | + boolean[] reachable = new boolean[n]; |
| 105 | + markReachable(residual, source, reachable); |
| 106 | + return new MaxFlowResult(maxFlow, reachable); |
| 107 | + } |
| 108 | + |
| 109 | + private static boolean bfs(int[][] residual, int source, int sink, int[] parent) { |
| 110 | + Arrays.fill(parent, -1); |
| 111 | + parent[source] = source; |
| 112 | + Queue<Integer> q = new ArrayDeque<>(); |
| 113 | + q.add(source); |
| 114 | + while (!q.isEmpty()) { |
| 115 | + int u = q.poll(); |
| 116 | + for (int v = 0; v < residual.length; v++) { |
| 117 | + if (residual[u][v] > 0 && parent[v] == -1) { |
| 118 | + parent[v] = u; |
| 119 | + if (v == sink) { |
| 120 | + return true; |
| 121 | + } |
| 122 | + q.add(v); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + return false; |
| 127 | + } |
| 128 | + |
| 129 | + private static void markReachable(int[][] residual, int source, boolean[] vis) { |
| 130 | + Arrays.fill(vis, false); |
| 131 | + Queue<Integer> q = new ArrayDeque<>(); |
| 132 | + vis[source] = true; |
| 133 | + q.add(source); |
| 134 | + while (!q.isEmpty()) { |
| 135 | + int u = q.poll(); |
| 136 | + for (int v = 0; v < residual.length; v++) { |
| 137 | + if (!vis[v] && residual[u][v] > 0) { |
| 138 | + vis[v] = true; |
| 139 | + q.add(v); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments