hungarian_java

先附上开源代码,等赶完任务回来仔细看看 🤣🤣🤣

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300

package hungarian_algorithm;

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;

/**
* @author ShaoYJ
* @date 2023/3/3 周五
* @desc implement of hungarian algorithm
* ref:https://github.com/aalmi/HungarianAlgorithm/blob/master/HungarianAlgorithm.java
*/
public class HungarianAlgorithm {

// initial matrix (cost matrix)
int[][] matrix;

// markers in the matrix
int[] squareInRow, squareInCol, rowIsCovered, colIsCovered, staredZeroesInRow;

public HungarianAlgorithm(int[][] matrix) {
if (matrix.length != matrix[0].length) {
try {
throw new IllegalAccessException("The matrix is not square!");
} catch (IllegalAccessException ex) {
System.err.println(ex);
System.exit(1);
}
}

this.matrix = matrix;
squareInRow = new int[matrix.length]; // squareInRow & squareInCol indicate the position
squareInCol = new int[matrix[0].length]; // of the marked zeroes

rowIsCovered = new int[matrix.length]; // indicates whether a row is covered
colIsCovered = new int[matrix[0].length]; // indicates whether a column is covered
staredZeroesInRow = new int[matrix.length]; // storage for the 0*
Arrays.fill(staredZeroesInRow, -1);
Arrays.fill(squareInRow, -1);
Arrays.fill(squareInCol, -1);
}

/**
* find an optimal assignment
*
* @return optimal assignment
*/
public int[][] findOptimalAssignment() {
step1(); // reduce matrix
step2(); // mark independent zeroes
step3(); // cover columns which contain a marked zero

while (!allColumnsAreCovered()) {
int[] mainZero = step4();
while (mainZero == null) { // while no zero found in step4
step5();
mainZero = step4();
}
if (squareInRow[mainZero[0]] == -1) {
// there is no square mark in the mainZero line
step6(mainZero);
step3(); // cover columns which contain a marked zero
} else {
// there is square mark in the mainZero line
// step 5
rowIsCovered[mainZero[0]] = 1; // cover row of mainZero
colIsCovered[squareInRow[mainZero[0]]] = 0; // uncover column of mainZero
step5();
}
}

int[][] optimalAssignment = new int[matrix.length][];
for (int i = 0; i < squareInCol.length; i++) {
optimalAssignment[i] = new int[]{i, squareInCol[i]};
}
return optimalAssignment;
}

/**
* Check if all columns are covered. If that's the case then the
* optimal solution is found
*
* @return true or false
*/
private boolean allColumnsAreCovered() {
for (int i : colIsCovered) {
if (i == 0) {
return false;
}
}
return true;
}

/**
* Step 1:
* Reduce the matrix so that in each row and column at least one zero exists:
* 1. subtract each row minima from each element of the row
* 2. subtract each column minima from each element of the column
*/
private void step1() {
// rows
for (int i = 0; i < matrix.length; i++) {
// find the min value of the current row
int currentRowMin = Integer.MAX_VALUE;
for (int j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] < currentRowMin) {
currentRowMin = matrix[i][j];
}
}
// subtract min value from each element of the current row
for (int k = 0; k < matrix[i].length; k++) {
matrix[i][k] -= currentRowMin;
}
}

// cols
for (int i = 0; i < matrix[0].length; i++) {
// find the min value of the current column
int currentColMin = Integer.MAX_VALUE;
for (int j = 0; j < matrix.length; j++) {
if (matrix[j][i] < currentColMin) {
currentColMin = matrix[j][i];
}
}
// subtract min value from each element of the current column
for (int k = 0; k < matrix.length; k++) {
matrix[k][i] -= currentColMin;
}
}
}

/**
* Step 2:
* mark each 0 with a "square", if there are no other marked zeroes in the same row or column
*/
private void step2() {
int[] rowHasSquare = new int[matrix.length];
int[] colHasSquare = new int[matrix[0].length];

for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
// mark if current value == 0 & there are no other marked zeroes in the same row or column
if (matrix[i][j] == 0 && rowHasSquare[i] == 0 && colHasSquare[j] == 0) {
rowHasSquare[i] = 1;
colHasSquare[j] = 1;
squareInRow[i] = j; // save the row-position of the zero
squareInCol[j] = i; // save the column-position of the zero
continue; // jump to next row
}
}
}
}

/**
* Step 3:
* Cover all columns which are marked with a "square"
*/
private void step3() {
for (int i = 0; i < squareInCol.length; i++) {
colIsCovered[i] = squareInCol[i] != -1 ? 1 : 0;
}
}

/**
* Step 4:
* Find zero value Z_0 and mark it as "0*".
*
* @return position of Z_0 in the matrix
*/
private int[] step4() {
for (int i = 0; i < matrix.length; i++) {
if (rowIsCovered[i] == 0) {
for (int j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] == 0 && colIsCovered[j] == 0) {
staredZeroesInRow[i] = j; // mark as 0*
return new int[]{i, j};
}
}
}
}
return null;
}

/**
* Step 5:
* 1. Find the smallest uncovered value in the matrix.
* 2. Subtract it from all uncovered values
* 3. Add it to all twice-covered values
*/
private void step5() {
// Find the smallest uncovered value in the matrix
int minUncoveredValue = Integer.MAX_VALUE;
for (int i = 0; i < matrix.length; i++) {
if (rowIsCovered[i] == 1) {
continue;
}
for (int j = 0; j < matrix[0].length; j++) {
if (colIsCovered[j] == 0 && matrix[i][j] < minUncoveredValue) {
minUncoveredValue = matrix[i][j];
}
}
}

if (minUncoveredValue > 0) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (rowIsCovered[i] == 1 && colIsCovered[j] == 1) {
// Add min to all twice-covered values
matrix[i][j] += minUncoveredValue;
} else if (rowIsCovered[i] == 0 && colIsCovered[j] == 0) {
// Subtract min from all uncovered values
matrix[i][j] -= minUncoveredValue;
}
}
}
}
}

/**
* Step 6:
* Create a chain K of alternating "squares" and "0*"
*
* @param mainZero => Z_0 of Step 4
*/
private void step6(int[] mainZero) {
int i = mainZero[0];
int j = mainZero[1];

Set<int[]> K = new LinkedHashSet<>();
//(a)
// add Z_0 to K
K.add(mainZero);
boolean found = false;
do {
// (b)
// add Z_1 to K if
// there is a zero Z_1 which is marked with a "square " in the column of Z_0
if (squareInCol[j] != -1) {
K.add(new int[]{squareInCol[j], j});
found = true;
} else {
found = false;
}

// if no zero element Z_1 marked with "square" exists in the column of Z_0, then cancel the loop
if (!found) {
break;
}

// (c)
// replace Z_0 with the 0* in the row of Z_1
i = squareInCol[j];
j = staredZeroesInRow[i];
// add the new Z_0 to K
if (j != -1) {
K.add(new int[]{i, j});
found = true;
} else {
found = false;
}

} while (found); // (d) as long as no new "square" marks are found

// (e)
for (int[] zero : K) {
// remove all "square" marks in K
if (squareInCol[zero[1]] == zero[0]) {
squareInCol[zero[1]] = -1;
squareInRow[zero[0]] = -1;
}
// replace the 0* marks in K with "square" marks
if (staredZeroesInRow[zero[0]] == zero[1]) {
squareInRow[zero[0]] = zero[1];
squareInCol[zero[1]] = zero[0];
}
}

// (f)
// remove all marks
Arrays.fill(staredZeroesInRow, -1);
Arrays.fill(rowIsCovered, 0);
Arrays.fill(colIsCovered, 0);
}

public static void main(String[] args) {
int[][] dataMatrix = {
{70, 40, 20, 55},
{65, 60, 45, 90},
{30, 45, 50, 75},
{25, 30, 55, 40}
};

HungarianAlgorithm ha = new HungarianAlgorithm(dataMatrix);
int[][] assignment = ha.findOptimalAssignment();
System.out.println(Arrays.deepToString(assignment));
}
}



hungarian_java
http://oowatermelon.github.io/OoWaterMelonS/2023/03/04/hungarian-java/
作者
OoWaterMelonS Shao
发布于
2023年3月4日
许可协议