|
| 1 | +package com.thealgorithms.graph; |
| 2 | + |
| 3 | +/** |
| 4 | + * An implementation of the Stoer-Wagner algorithm to find the global minimum cut of an undirected, weighted graph. |
| 5 | + * A minimum cut is a partition of the graph's vertices into two disjoint sets with the minimum possible edge weight |
| 6 | + * sum connecting the two sets. |
| 7 | + * |
| 8 | + * Wikipedia: https://en.wikipedia.org/wiki/Stoer%E2%80%93Wagner_algorithm |
| 9 | + * Time Complexity: O(V^3) where V is the number of vertices. |
| 10 | + */ |
| 11 | +public class StoerWagner { |
| 12 | + |
| 13 | + /** |
| 14 | + * Finds the minimum cut in the given undirected, weighted graph. |
| 15 | + * |
| 16 | + * @param graph An adjacency matrix representing the graph. graph[i][j] is the weight of the edge between i and j. |
| 17 | + * @return The weight of the minimum cut. |
| 18 | + */ |
| 19 | + public int findMinCut(int[][] graph) { |
| 20 | + int n = graph.length; |
| 21 | + if (n < 2) { |
| 22 | + return 0; |
| 23 | + } |
| 24 | + |
| 25 | + // Make a working copy of the adjacency matrix so we can merge vertices |
| 26 | + int[][] g = new int[n][n]; |
| 27 | + for (int i = 0; i < n; i++) { |
| 28 | + System.arraycopy(graph[i], 0, g[i], 0, n); |
| 29 | + } |
| 30 | + |
| 31 | + // vertices contains the list of active vertex indices (initially 0..n-1) |
| 32 | + int[] vertices = new int[n]; |
| 33 | + for (int i = 0; i < n; i++) { |
| 34 | + vertices[i] = i; |
| 35 | + } |
| 36 | + |
| 37 | + int bestCut = Integer.MAX_VALUE; |
| 38 | + |
| 39 | + // Repeat n-1 phases; in each phase we reduce number of active vertices by 1 |
| 40 | + for (int m = n; m > 1; m--) { |
| 41 | + int[] weights = new int[n]; // accumulated weights for selection |
| 42 | + boolean[] added = new boolean[n]; // which original vertices have been added in this phase |
| 43 | + int prev = -1; // previously added vertex id in the growing set |
| 44 | + |
| 45 | + for (int i = 0; i < m; i++) { |
| 46 | + // Select the not-yet-added vertex (among the first m active vertices) with maximum weight |
| 47 | + int sel = -1; |
| 48 | + for (int j = 0; j < m; j++) { |
| 49 | + int v = vertices[j]; |
| 50 | + if (!added[v] && (sel == -1 || weights[v] > weights[sel])) { |
| 51 | + sel = v; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // If sel is -1 it means the graph is disconnected in the remaining part; |
| 56 | + // the minimum cut value is 0 in that case. |
| 57 | + if (sel == -1) { |
| 58 | + return 0; |
| 59 | + } |
| 60 | + |
| 61 | + added[sel] = true; |
| 62 | + |
| 63 | + // If this is the last vertex added in this phase, weights[sel] is the cut weight between sel and the rest. |
| 64 | + if (i == m - 1) { |
| 65 | + // Update best cut |
| 66 | + if (weights[sel] < bestCut) { |
| 67 | + bestCut = weights[sel]; |
| 68 | + } |
| 69 | + |
| 70 | + // Merge 'sel' into 'prev' (combine their edges) to reduce vertex count |
| 71 | + if (prev != -1) { |
| 72 | + for (int k = 0; k < n; k++) { |
| 73 | + // accumulate edges from sel into prev |
| 74 | + g[prev][k] += g[sel][k]; |
| 75 | + g[k][prev] = g[prev][k]; |
| 76 | + } |
| 77 | + |
| 78 | + // Remove 'sel' from vertices[] by replacing it with last active vertex |
| 79 | + int selIndex = -1; |
| 80 | + for (int j = 0; j < m; j++) { |
| 81 | + if (vertices[j] == sel) { |
| 82 | + selIndex = j; |
| 83 | + break; |
| 84 | + } |
| 85 | + } |
| 86 | + // replace position selIndex with last active vertex (m-1) |
| 87 | + vertices[selIndex] = vertices[m - 1]; |
| 88 | + } |
| 89 | + // Phase done |
| 90 | + } else { |
| 91 | + // not last: update weights of remaining active vertices |
| 92 | + for (int j = 0; j < m; j++) { |
| 93 | + int v = vertices[j]; |
| 94 | + if (!added[v]) { |
| 95 | + weights[v] += g[sel][v]; |
| 96 | + } |
| 97 | + } |
| 98 | + prev = sel; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return bestCut == Integer.MAX_VALUE ? 0 : bestCut; |
| 104 | + } |
| 105 | +} |
| 106 | + |
0 commit comments