Fill in the cost matrix of an assignment problem and click on 'Solve'. The optimal assignment will be determined and a step by step explanation of the hungarian algorithm will be given.
Fill in the cost matrix (random cost matrix):
Size: 3x3 4x4 5x5 6x6 7x7 8x8 9x9 10x10
This is the original cost matrix:
93 | 85 | 67 | 85 |
57 | 85 | 44 | 75 |
48 | 69 | 81 | 77 |
5 | 21 | 57 | 26 |
Subtract row minima
We subtract the row minimum from each row:
26 | 18 | 0 | 18 | (-67) |
13 | 41 | 0 | 31 | (-44) |
0 | 21 | 33 | 29 | (-48) |
0 | 16 | 52 | 21 | (-5) |
Subtract column minima
We subtract the column minimum from each column:
26 | 2 | 0 | 0 |
13 | 25 | 0 | 13 |
0 | 5 | 33 | 11 |
0 | 0 | 52 | 3 |
(-16) | (-18) |
Cover all zeros with a minimum number of lines
There are 4 lines required to cover all zeros:
26 | 2 | 0 | 0 | x |
13 | 25 | 0 | 13 | x |
0 | 5 | 33 | 11 | x |
0 | 0 | 52 | 3 | x |
The optimal assignment
Because there are 4 lines required, the zeros cover an optimal assignment:
26 | 2 | 0 | 0 |
13 | 25 | 0 | 13 |
0 | 5 | 33 | 11 |
0 | 0 | 52 | 3 |
This corresponds to the following optimal assignment in the original cost matrix:
93 | 85 | 67 | 85 |
57 | 85 | 44 | 75 |
48 | 69 | 81 | 77 |
5 | 21 | 57 | 26 |
The optimal value equals 198.
HungarianAlgorithm.com © 2013-2025