The Hungarian algorithm
Master the Hungarian algorithm in Year 12 VCE General Mathematics. When a Mildura grower has four harvest crews and four blocks of vines, there are \(24\) ways to pair them up, and with five there are \(120\): far too many to check by hand. This neat matrix method finds the cheapest or quickest pairing in a handful of steps.
You will reduce the rows and then the columns, cover every zero with the fewest possible lines, adjust the matrix when more lines are needed, and read the optimal allocation from the zeros. You will total it from the original table and convert profit tables so the same steps find a maximum, before moving on to scheduling projects.
Theory
Larger allocation problems need a method, not a list. This Year 12 General Mathematics guide for students in Victoria explains the Hungarian algorithm step by step: reducing rows and columns, covering the zeros with the fewest lines, adjusting the matrix, and making the optimal allocation, including how to handle problems where the total should be as large as possible.
The Hungarian algorithm finds the best allocation of \(n\) workers to \(n\) tasks without listing every allocation. That matters because the number of allocations grows fast: \(24\) for a \(4 \times 4\) table and \(120\) for \(5 \times 5\). The algorithm works on a square cost matrix with one row per worker and one column per task.
Subtracting the same amount from every element of a row (or of a column) changes every allocation’s total by that same amount, so the best allocation stays the best. The algorithm keeps doing this until there are enough zeros to choose one in each row and each column. Those zeros give the optimal allocation, and its total is read from the original table.
To test whether there are enough zeros, cover them all with the fewest horizontal and vertical lines. If \(n\) lines are needed, an allocation can be made. For a maximum (profit, sales, score), first subtract every element from the largest element, then find the minimum as usual.
Step 4 for a smallest uncovered element \(m\):
Converting a maximising problem, where \(L\) is the largest element:
When to stop covering and allocate (\(n \times n\) matrix):
The Hungarian algorithm (minimum)
- Step 1, rows. Subtract the smallest element of each row from every element in that row.
- Step 2, columns. Subtract the smallest element of each column from every element in that column. (A column that already has a zero does not change.)
- Step 3, cover. Cover all the zeros using the fewest horizontal and vertical lines. If the number of lines equals the number of rows, go to the allocation. If not, go to Step 4.
- Step 4, adjust. Find the smallest element not covered by a line. Subtract it from every uncovered element and add it to every element where two lines cross. Elements covered by one line stay the same. Return to Step 3.
Making the allocation
- Find a row or column with only one zero and allocate that pairing. Cross out its row and column.
- Repeat until every worker has a task.
- Add the matching entries from the original table to get the total.
For a maximum
- Subtract every element from the largest element in the table, then use the steps above.
- Total the allocation using the original (profit) table.
| X | Y | Z | |
|---|---|---|---|
| A | 17 | 9 | 17 |
| B | 6 | 11 | 13 |
| C | 17 | 17 | 13 |
Work through Steps 1 to 3, then allocate.
Step 1 (rows). Subtract each row’s smallest element (\(9\), \(6\), \(13\)):
Step 2 (columns). Every column already has a zero, so nothing changes.
Step 3. The fewest lines that cover every zero: \(3\) (rows A, B and C). This equals \(3\), so an optimal allocation can be made.
Allocate on the zeros, starting with a row or column that has only one zero:
- row A has only one zero left (column Y): A–Y
- row B has only one zero left (column X): B–X
- row C has only one zero left (column Z): C–Z
Use the original table for the total:
| \(\text{total}\) | \(=\) | \(9 + 6 + 13\) |
| \(\) | \(=\) | \(28\) |
The best allocation is A–Y, B–X, C–Z, taking 28 minutes in total.
| X | Y | Z | |
|---|---|---|---|
| P | 13 | 14 | 4 |
| Q | 11 | 8 | 6 |
| R | 8 | 7 | 13 |
Follow the four steps in order.
Step 1 (rows). Subtract each row’s smallest element (\(4\), \(6\), \(7\)):
Step 2 (columns). Subtract each column’s smallest element (\(1\), \(0\), \(0\)):
Step 3. The fewest lines that cover every zero: \(2\) (row R and column Z). This is fewer than \(3\), so go to Step 4.
Step 4. The smallest uncovered element is \(2\). Subtract \(2\) from every uncovered element, add \(2\) where two lines cross, and leave the rest:
Step 3. The fewest lines that cover every zero: \(3\) (rows Q and R and column Z). This equals \(3\), so an optimal allocation can be made.
Allocate on the zeros, starting with a row or column that has only one zero:
- row P has only one zero left (column Z): P–Z
- row Q has only one zero left (column Y): Q–Y
- row R has only one zero left (column X): R–X
Use the original table for the total:
| \(\text{total}\) | \(=\) | \(4 + 8 + 8\) |
| \(\) | \(=\) | \(20\) |
The best allocation is P–Z, Q–Y, R–X, costing \(20\) hundred dollars, which is $2000.
| W | X | Y | Z | |
|---|---|---|---|---|
| A | 14 | 23 | 19 | 14 |
| B | 26 | 18 | 26 | 27 |
| C | 40 | 29 | 30 | 35 |
| D | 29 | 28 | 29 | 30 |
There are \(24\) allocations, so use the algorithm rather than a list.
Step 1 (rows). Subtract each row’s smallest element (\(14\), \(18\), \(29\), \(28\)):
Step 2 (columns). Subtract each column’s smallest element (\(0\), \(0\), \(1\), \(0\)):
Step 3. The fewest lines that cover every zero: \(3\) (row A and columns X and Y). This is fewer than \(4\), so go to Step 4.
Step 4. The smallest uncovered element is \(1\). Subtract \(1\) from every uncovered element, add \(1\) where two lines cross, and leave the rest:
Step 3. The fewest lines that cover every zero: \(4\) (rows A, C and D and column X). This equals \(4\), so an optimal allocation can be made.
Allocate on the zeros, starting with a row or column that has only one zero:
- row B has only one zero left (column X): B–X
- row C has only one zero left (column Y): C–Y
- row D has only one zero left (column W): D–W
- row A has only one zero left (column Z): A–Z
Use the original table for the total:
| \(\text{total}\) | \(=\) | \(14 + 18 + 30 + 29\) |
| \(\) | \(=\) | \(91\) |
The best allocation is A–Z, B–X, C–Y, D–W, with a total time of 91 minutes.
| W | X | Y | Z | |
|---|---|---|---|---|
| E | 57 | 22 | 41 | 53 |
| F | 55 | 52 | 31 | 51 |
| G | 43 | 25 | 33 | 38 |
| H | 40 | 60 | 59 | 39 |
The algorithm finds a minimum, so convert the table first.
Convert to minimising. The largest element is \(60\). Subtract every element from \(60\):
Step 1 (rows). Subtract each row’s smallest element (\(3\), \(5\), \(17\), \(0\)):
Step 2 (columns). Subtract each column’s smallest element (\(0\), \(0\), \(1\), \(4\)):
Step 3. The fewest lines that cover every zero: \(3\) (row H and columns W and Z). This is fewer than \(4\), so go to Step 4.
Step 4. The smallest uncovered element is \(3\). Subtract \(3\) from every uncovered element, add \(3\) where two lines cross, and leave the rest:
Step 3. The fewest lines that cover every zero: \(4\) (rows E, F and H and column W). This equals \(4\), so an optimal allocation can be made.
Allocate on the zeros, starting with a row or column that has only one zero:
- row G has only one zero left (column W): G–W
- row E has only one zero left (column Z): E–Z
- row F has only one zero left (column X): F–X
- row H has only one zero left (column Y): H–Y
Use the original table for the total:
| \(\text{total}\) | \(=\) | \(53 + 52 + 43 + 59\) |
| \(\) | \(=\) | \(207\) |
The best allocation is E–Z, F–X, G–W, H–Y, with expected sales of \(207\) thousand dollars, which is $207000 a month.
Common pitfalls
Frequently asked questions
What is the Hungarian algorithm used for?
It finds the best way to give n workers one task each from n tasks, so that the total cost or time is as small as possible (or the profit as large as possible), without checking every allocation.
What are the steps of the Hungarian algorithm?
Step 1: subtract each row's smallest element from that row. Step 2: subtract each column's smallest element from that column. Step 3: cover all zeros with the fewest lines. Step 4: if there are fewer lines than rows, subtract the smallest uncovered element from the uncovered elements, add it where lines cross, and go back to Step 3.
How do you know when to stop the Hungarian algorithm?
Stop when the fewest lines needed to cover all the zeros equals the number of rows. Then an allocation using only zeros is possible.
How do you use the Hungarian algorithm to find a maximum?
Subtract every element in the table from the largest element, then apply the usual steps to find a minimum. Work out the total from the original table.
Why is the total taken from the original table?
The reduced matrix only shows which pairings are best. Its numbers have been changed by the subtractions, so the real cost, time or profit must come from the original table.
Does the Hungarian algorithm work for any size table?
It needs a square table, with the same number of workers as tasks. The same steps work for a 3 by 3, 4 by 4 or larger table; only the amount of arithmetic grows.