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:
53 | 27 | 13 | 59 | 10 | 85 | 81 |
3 | 49 | 4 | 63 | 54 | 99 | 59 |
68 | 66 | 60 | 39 | 7 | 79 | 94 |
76 | 50 | 40 | 94 | 26 | 14 | 53 |
52 | 77 | 76 | 18 | 28 | 74 | 90 |
32 | 37 | 66 | 13 | 40 | 85 | 41 |
51 | 11 | 63 | 47 | 1 | 28 | 43 |
Subtract row minima
We subtract the row minimum from each row:
43 | 17 | 3 | 49 | 0 | 75 | 71 | (-10) |
0 | 46 | 1 | 60 | 51 | 96 | 56 | (-3) |
61 | 59 | 53 | 32 | 0 | 72 | 87 | (-7) |
62 | 36 | 26 | 80 | 12 | 0 | 39 | (-14) |
34 | 59 | 58 | 0 | 10 | 56 | 72 | (-18) |
19 | 24 | 53 | 0 | 27 | 72 | 28 | (-13) |
50 | 10 | 62 | 46 | 0 | 27 | 42 | (-1) |
Subtract column minima
We subtract the column minimum from each column:
43 | 7 | 2 | 49 | 0 | 75 | 43 |
0 | 36 | 0 | 60 | 51 | 96 | 28 |
61 | 49 | 52 | 32 | 0 | 72 | 59 |
62 | 26 | 25 | 80 | 12 | 0 | 11 |
34 | 49 | 57 | 0 | 10 | 56 | 44 |
19 | 14 | 52 | 0 | 27 | 72 | 0 |
50 | 0 | 61 | 46 | 0 | 27 | 14 |
(-10) | (-1) | (-28) |
Cover all zeros with a minimum number of lines
There are 6 lines required to cover all zeros:
43 | 7 | 2 | 49 | 0 | 75 | 43 | |
0 | 36 | 0 | 60 | 51 | 96 | 28 | x |
61 | 49 | 52 | 32 | 0 | 72 | 59 | |
62 | 26 | 25 | 80 | 12 | 0 | 11 | x |
34 | 49 | 57 | 0 | 10 | 56 | 44 | x |
19 | 14 | 52 | 0 | 27 | 72 | 0 | x |
50 | 0 | 61 | 46 | 0 | 27 | 14 | x |
x |
Create additional zeros
The number of lines is smaller than 7. The smallest uncovered number is 2. We subtract this number from all uncovered elements and add it to all elements that are covered twice:
41 | 5 | 0 | 47 | 0 | 73 | 41 |
0 | 36 | 0 | 60 | 53 | 96 | 28 |
59 | 47 | 50 | 30 | 0 | 70 | 57 |
62 | 26 | 25 | 80 | 14 | 0 | 11 |
34 | 49 | 57 | 0 | 12 | 56 | 44 |
19 | 14 | 52 | 0 | 29 | 72 | 0 |
50 | 0 | 61 | 46 | 2 | 27 | 14 |
Cover all zeros with a minimum number of lines
There are 7 lines required to cover all zeros:
41 | 5 | 0 | 47 | 0 | 73 | 41 | x |
0 | 36 | 0 | 60 | 53 | 96 | 28 | x |
59 | 47 | 50 | 30 | 0 | 70 | 57 | x |
62 | 26 | 25 | 80 | 14 | 0 | 11 | x |
34 | 49 | 57 | 0 | 12 | 56 | 44 | x |
19 | 14 | 52 | 0 | 29 | 72 | 0 | x |
50 | 0 | 61 | 46 | 2 | 27 | 14 | x |
The optimal assignment
Because there are 7 lines required, the zeros cover an optimal assignment:
41 | 5 | 0 | 47 | 0 | 73 | 41 |
0 | 36 | 0 | 60 | 53 | 96 | 28 |
59 | 47 | 50 | 30 | 0 | 70 | 57 |
62 | 26 | 25 | 80 | 14 | 0 | 11 |
34 | 49 | 57 | 0 | 12 | 56 | 44 |
19 | 14 | 52 | 0 | 29 | 72 | 0 |
50 | 0 | 61 | 46 | 2 | 27 | 14 |
This corresponds to the following optimal assignment in the original cost matrix:
53 | 27 | 13 | 59 | 10 | 85 | 81 |
3 | 49 | 4 | 63 | 54 | 99 | 59 |
68 | 66 | 60 | 39 | 7 | 79 | 94 |
76 | 50 | 40 | 94 | 26 | 14 | 53 |
52 | 77 | 76 | 18 | 28 | 74 | 90 |
32 | 37 | 66 | 13 | 40 | 85 | 41 |
51 | 11 | 63 | 47 | 1 | 28 | 43 |
The optimal value equals 107.
HungarianAlgorithm.com © 2013-2024