|
| 1 | +package com.thealgorithms.graph; |
| 2 | + |
| 3 | +import java.util.ArrayDeque; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.Queue; |
| 6 | + |
| 7 | +/** |
| 8 | + * Push–Relabel (Relabel-to-Front variant simplified to array scanning) for maximum flow. |
| 9 | + * |
| 10 | + * <p>Input graph is a capacity matrix where {@code capacity[u][v]} is the capacity of the edge |
| 11 | + * {@code u -> v}. Capacities must be non-negative. Vertices are indexed in {@code [0, n)}. |
| 12 | + * |
| 13 | + * <p>Time complexity: O(V^3) in the worst case for the array-based variant; typically fast in |
| 14 | + * practice. This implementation uses a residual network over an adjacency-matrix representation. |
| 15 | + * |
| 16 | + * <p>The API mirrors {@link EdmondsKarp#maxFlow(int[][], int, int)} and {@link Dinic#maxFlow(int[][], int, int)}. |
| 17 | + * |
| 18 | + * @see <a href="https://en.wikipedia.org/wiki/Push%E2%80%93relabel_maximum_flow_algorithm">Wikipedia: Push–Relabel maximum flow algorithm</a> |
| 19 | + */ |
| 20 | +public final class PushRelabel { |
| 21 | + |
| 22 | + private PushRelabel() { |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Computes the maximum flow from {@code source} to {@code sink} using Push–Relabel. |
| 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 inputs are invalid |
| 33 | + */ |
| 34 | + public static int maxFlow(int[][] capacity, int source, int sink) { |
| 35 | + validate(capacity, source, sink); |
| 36 | + final int n = capacity.length; |
| 37 | + if (source == sink) { |
| 38 | + return 0; |
| 39 | + } |
| 40 | + |
| 41 | + int[][] residual = new int[n][n]; |
| 42 | + for (int i = 0; i < n; i++) { |
| 43 | + residual[i] = Arrays.copyOf(capacity[i], n); |
| 44 | + } |
| 45 | + |
| 46 | + int[] height = new int[n]; |
| 47 | + int[] excess = new int[n]; |
| 48 | + int[] nextNeighbor = new int[n]; |
| 49 | + |
| 50 | + // Preflow initialization |
| 51 | + height[source] = n; |
| 52 | + for (int v = 0; v < n; v++) { |
| 53 | + int cap = residual[source][v]; |
| 54 | + if (cap > 0) { |
| 55 | + residual[source][v] -= cap; |
| 56 | + residual[v][source] += cap; |
| 57 | + excess[v] += cap; |
| 58 | + excess[source] -= cap; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Active queue contains vertices (except source/sink) with positive excess |
| 63 | + Queue<Integer> active = new ArrayDeque<>(); |
| 64 | + for (int v = 0; v < n; v++) { |
| 65 | + if (v != source && v != sink && excess[v] > 0) { |
| 66 | + active.add(v); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + State state = new State(residual, height, excess, nextNeighbor, source, sink, active); |
| 71 | + |
| 72 | + while (!active.isEmpty()) { |
| 73 | + int u = active.poll(); |
| 74 | + discharge(u, state); |
| 75 | + if (excess[u] > 0) { |
| 76 | + // still active after discharge; push to back |
| 77 | + active.add(u); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Total flow equals excess at sink |
| 82 | + return excess[sink]; |
| 83 | + } |
| 84 | + |
| 85 | + private static void discharge(int u, State s) { |
| 86 | + final int n = s.residual.length; |
| 87 | + while (s.excess[u] > 0) { |
| 88 | + if (s.nextNeighbor[u] >= n) { |
| 89 | + relabel(u, s.residual, s.height); |
| 90 | + s.nextNeighbor[u] = 0; |
| 91 | + continue; |
| 92 | + } |
| 93 | + int v = s.nextNeighbor[u]; |
| 94 | + if (s.residual[u][v] > 0 && s.height[u] == s.height[v] + 1) { |
| 95 | + int delta = Math.min(s.excess[u], s.residual[u][v]); |
| 96 | + s.residual[u][v] -= delta; |
| 97 | + s.residual[v][u] += delta; |
| 98 | + s.excess[u] -= delta; |
| 99 | + int prevExcessV = s.excess[v]; |
| 100 | + s.excess[v] += delta; |
| 101 | + if (v != s.source && v != s.sink && prevExcessV == 0) { |
| 102 | + s.active.add(v); |
| 103 | + } |
| 104 | + } else { |
| 105 | + s.nextNeighbor[u]++; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private static final class State { |
| 111 | + final int[][] residual; |
| 112 | + final int[] height; |
| 113 | + final int[] excess; |
| 114 | + final int[] nextNeighbor; |
| 115 | + final int source; |
| 116 | + final int sink; |
| 117 | + final Queue<Integer> active; |
| 118 | + |
| 119 | + State(int[][] residual, int[] height, int[] excess, int[] nextNeighbor, int source, int sink, Queue<Integer> active) { |
| 120 | + this.residual = residual; |
| 121 | + this.height = height; |
| 122 | + this.excess = excess; |
| 123 | + this.nextNeighbor = nextNeighbor; |
| 124 | + this.source = source; |
| 125 | + this.sink = sink; |
| 126 | + this.active = active; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private static void relabel(int u, int[][] residual, int[] height) { |
| 131 | + final int n = residual.length; |
| 132 | + int minHeight = Integer.MAX_VALUE; |
| 133 | + for (int v = 0; v < n; v++) { |
| 134 | + if (residual[u][v] > 0) { |
| 135 | + minHeight = Math.min(minHeight, height[v]); |
| 136 | + } |
| 137 | + } |
| 138 | + if (minHeight < Integer.MAX_VALUE) { |
| 139 | + height[u] = minHeight + 1; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private static void validate(int[][] capacity, int source, int sink) { |
| 144 | + if (capacity == null || capacity.length == 0) { |
| 145 | + throw new IllegalArgumentException("Capacity matrix must not be null or empty"); |
| 146 | + } |
| 147 | + int n = capacity.length; |
| 148 | + for (int i = 0; i < n; i++) { |
| 149 | + if (capacity[i] == null || capacity[i].length != n) { |
| 150 | + throw new IllegalArgumentException("Capacity matrix must be square"); |
| 151 | + } |
| 152 | + for (int j = 0; j < n; j++) { |
| 153 | + if (capacity[i][j] < 0) { |
| 154 | + throw new IllegalArgumentException("Capacities must be non-negative"); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + if (source < 0 || sink < 0 || source >= n || sink >= n) { |
| 159 | + throw new IllegalArgumentException("Source and sink must be valid vertex indices"); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments