forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHungarianAlgorithmTest.java
More file actions
44 lines (38 loc) · 1.51 KB
/
HungarianAlgorithmTest.java
File metadata and controls
44 lines (38 loc) · 1.51 KB
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
package com.thealgorithms.graph;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
class HungarianAlgorithmTest {
@Test
@DisplayName("Classic 3x3 example: minimal cost 5 with assignment [1,0,2]")
void classicSquareExample() {
int[][] cost = {{4, 1, 3}, {2, 0, 5}, {3, 2, 2}};
HungarianAlgorithm.Result res = HungarianAlgorithm.solve(cost);
assertEquals(5, res.minCost);
assertArrayEquals(new int[] {1, 0, 2}, res.assignment);
}
@Test
@DisplayName("Rectangular (more rows than cols): pads to square and returns -1 for unassigned rows")
void rectangularMoreRows() {
int[][] cost = {{7, 3}, {2, 8}, {5, 1}};
// Optimal selects any 2 rows: choose row1->col0 (2) and row2->col1 (1) => total 3
HungarianAlgorithm.Result res = HungarianAlgorithm.solve(cost);
assertEquals(3, res.minCost);
// Two rows assigned to 2 columns; one row remains -1.
int assigned = 0;
for (int a : res.assignment) {
if (a >= 0) {
assigned++;
}
}
assertEquals(2, assigned);
}
@Test
@DisplayName("Zero diagonal yields zero total cost")
void zeroDiagonal() {
int[][] cost = {{0, 5, 9}, {4, 0, 7}, {3, 6, 0}};
HungarianAlgorithm.Result res = HungarianAlgorithm.solve(cost);
assertEquals(0, res.minCost);
}
}