|
| 1 | +package com.thealgorithms.graph; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +/** |
| 6 | + * Hungarian algorithm (a.k.a. Kuhn–Munkres) for the Assignment Problem. |
| 7 | + * |
| 8 | + * <p>Given an n x m cost matrix (n tasks, m workers), finds a minimum-cost |
| 9 | + * one-to-one assignment. If the matrix is rectangular, the algorithm pads to a |
| 10 | + * square internally. Costs must be finite non-negative integers. |
| 11 | + * |
| 12 | + * <p>Time complexity: O(n^3) with n = max(rows, cols). |
| 13 | + * |
| 14 | + * <p>API returns the assignment as an array where {@code assignment[i]} is the |
| 15 | + * column chosen for row i (or -1 if unassigned when rows != cols), and a total |
| 16 | + * minimal cost. |
| 17 | + * |
| 18 | + * @see <a href="https://en.wikipedia.org/wiki/Hungarian_algorithm">Wikipedia: Hungarian algorithm</a> |
| 19 | + */ |
| 20 | +public final class HungarianAlgorithm { |
| 21 | + |
| 22 | + private HungarianAlgorithm() { |
| 23 | + } |
| 24 | + |
| 25 | + /** Result holder for the Hungarian algorithm. */ |
| 26 | + public static final class Result { |
| 27 | + public final int[] assignment; // assignment[row] = col or -1 |
| 28 | + public final int minCost; |
| 29 | + |
| 30 | + public Result(int[] assignment, int minCost) { |
| 31 | + this.assignment = assignment; |
| 32 | + this.minCost = minCost; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Solves the assignment problem for a non-negative cost matrix. |
| 38 | + * |
| 39 | + * @param cost an r x c matrix of non-negative costs |
| 40 | + * @return Result with row-to-column assignment and minimal total cost |
| 41 | + * @throws IllegalArgumentException for null/empty or negative costs |
| 42 | + */ |
| 43 | + public static Result solve(int[][] cost) { |
| 44 | + validate(cost); |
| 45 | + int rows = cost.length; |
| 46 | + int cols = cost[0].length; |
| 47 | + int n = Math.max(rows, cols); |
| 48 | + |
| 49 | + // Build square matrix with padding 0 for missing cells |
| 50 | + int[][] a = new int[n][n]; |
| 51 | + for (int i = 0; i < n; i++) { |
| 52 | + if (i < rows) { |
| 53 | + for (int j = 0; j < n; j++) { |
| 54 | + a[i][j] = (j < cols) ? cost[i][j] : 0; |
| 55 | + } |
| 56 | + } else { |
| 57 | + Arrays.fill(a[i], 0); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Potentials and matching arrays |
| 62 | + int[] u = new int[n + 1]; |
| 63 | + int[] v = new int[n + 1]; |
| 64 | + int[] p = new int[n + 1]; |
| 65 | + int[] way = new int[n + 1]; |
| 66 | + |
| 67 | + for (int i = 1; i <= n; i++) { |
| 68 | + p[0] = i; |
| 69 | + int j0 = 0; |
| 70 | + int[] minv = new int[n + 1]; |
| 71 | + boolean[] used = new boolean[n + 1]; |
| 72 | + Arrays.fill(minv, Integer.MAX_VALUE); |
| 73 | + Arrays.fill(used, false); |
| 74 | + do { |
| 75 | + used[j0] = true; |
| 76 | + int i0 = p[j0]; |
| 77 | + int delta = Integer.MAX_VALUE; |
| 78 | + int j1 = 0; |
| 79 | + for (int j = 1; j <= n; j++) { |
| 80 | + if (!used[j]) { |
| 81 | + int cur = a[i0 - 1][j - 1] - u[i0] - v[j]; |
| 82 | + if (cur < minv[j]) { |
| 83 | + minv[j] = cur; |
| 84 | + way[j] = j0; |
| 85 | + } |
| 86 | + if (minv[j] < delta) { |
| 87 | + delta = minv[j]; |
| 88 | + j1 = j; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + for (int j = 0; j <= n; j++) { |
| 93 | + if (used[j]) { |
| 94 | + u[p[j]] += delta; |
| 95 | + v[j] -= delta; |
| 96 | + } else { |
| 97 | + minv[j] -= delta; |
| 98 | + } |
| 99 | + } |
| 100 | + j0 = j1; |
| 101 | + } while (p[j0] != 0); |
| 102 | + do { |
| 103 | + int j1 = way[j0]; |
| 104 | + p[j0] = p[j1]; |
| 105 | + j0 = j1; |
| 106 | + } while (j0 != 0); |
| 107 | + } |
| 108 | + |
| 109 | + int[] matchColForRow = new int[n]; |
| 110 | + Arrays.fill(matchColForRow, -1); |
| 111 | + for (int j = 1; j <= n; j++) { |
| 112 | + if (p[j] != 0) { |
| 113 | + matchColForRow[p[j] - 1] = j - 1; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // Build assignment for original rows only, ignore padded rows |
| 118 | + int[] assignment = new int[rows]; |
| 119 | + Arrays.fill(assignment, -1); |
| 120 | + int total = 0; |
| 121 | + for (int i = 0; i < rows; i++) { |
| 122 | + int j = matchColForRow[i]; |
| 123 | + if (j >= 0 && j < cols) { |
| 124 | + assignment[i] = j; |
| 125 | + total += cost[i][j]; |
| 126 | + } |
| 127 | + } |
| 128 | + return new Result(assignment, total); |
| 129 | + } |
| 130 | + |
| 131 | + private static void validate(int[][] cost) { |
| 132 | + if (cost == null || cost.length == 0) { |
| 133 | + throw new IllegalArgumentException("Cost matrix must not be null or empty"); |
| 134 | + } |
| 135 | + int c = cost[0].length; |
| 136 | + if (c == 0) { |
| 137 | + throw new IllegalArgumentException("Cost matrix must have at least 1 column"); |
| 138 | + } |
| 139 | + for (int i = 0; i < cost.length; i++) { |
| 140 | + if (cost[i] == null || cost[i].length != c) { |
| 141 | + throw new IllegalArgumentException("Cost matrix must be rectangular with equal row lengths"); |
| 142 | + } |
| 143 | + for (int j = 0; j < c; j++) { |
| 144 | + if (cost[i][j] < 0) { |
| 145 | + throw new IllegalArgumentException("Costs must be non-negative"); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments