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:
22 | 58 | 6 | 85 | 39 |
30 | 85 | 82 | 4 | 19 |
6 | 22 | 49 | 5 | 55 |
87 | 21 | 58 | 7 | 97 |
51 | 5 | 78 | 93 | 45 |
Subtract row minima
We subtract the row minimum from each row:
16 | 52 | 0 | 79 | 33 | (-6) |
26 | 81 | 78 | 0 | 15 | (-4) |
1 | 17 | 44 | 0 | 50 | (-5) |
80 | 14 | 51 | 0 | 90 | (-7) |
46 | 0 | 73 | 88 | 40 | (-5) |
Subtract column minima
We subtract the column minimum from each column:
15 | 52 | 0 | 79 | 18 |
25 | 81 | 78 | 0 | 0 |
0 | 17 | 44 | 0 | 35 |
79 | 14 | 51 | 0 | 75 |
45 | 0 | 73 | 88 | 25 |
(-1) | (-15) |
Cover all zeros with a minimum number of lines
There are 5 lines required to cover all zeros:
15 | 52 | 0 | 79 | 18 | x |
25 | 81 | 78 | 0 | 0 | x |
0 | 17 | 44 | 0 | 35 | x |
79 | 14 | 51 | 0 | 75 | x |
45 | 0 | 73 | 88 | 25 | x |
The optimal assignment
Because there are 5 lines required, the zeros cover an optimal assignment:
15 | 52 | 0 | 79 | 18 |
25 | 81 | 78 | 0 | 0 |
0 | 17 | 44 | 0 | 35 |
79 | 14 | 51 | 0 | 75 |
45 | 0 | 73 | 88 | 25 |
This corresponds to the following optimal assignment in the original cost matrix:
22 | 58 | 6 | 85 | 39 |
30 | 85 | 82 | 4 | 19 |
6 | 22 | 49 | 5 | 55 |
87 | 21 | 58 | 7 | 97 |
51 | 5 | 78 | 93 | 45 |
The optimal value equals 43.
HungarianAlgorithm.com © 2013-2025