Resources For Teachers For Tutors For Students & Parents AI New Books Pricing
Year 12 Maths - General (Unit 3 & Unit 4) Flow and matching problems

The Hungarian algorithm

20 practice questions 0 video lessons Theory + worked examples

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.

Create a free accountTrack your progress and save your work as you go.
Create free account

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.

Key idea. Reduce rows, reduce columns, cover the zeros with the fewest lines, and adjust until the number of lines equals the number of rows. Then allocate on the zeros and total the original costs.
Covering the zeros with the fewest linesA 4 by 4 matrix after row and column reduction. Three red lines through rows B and D and column Y cover every zero. W X Y Z A B C D 23 6 0 1 0 12 0 0 9 10 0 3 0 0 12 11
After Steps 1 and 2, all the zeros are covered by \(3\) lines (red). That is fewer than \(4\), so Step 4 is needed. The smallest uncovered element is \(1\).
Allocating on the zerosThe matrix after one Step 4 adjustment. The allocated zeros are circled: A–Z, B–W, C–Y, D–X. W X Y Z A B C D 22 5 0 0 0 12 1 0 8 9 0 2 0 0 13 11
After one Step 4 adjustment, \(4\) lines are needed, so the zeros give the allocation (circled): A–Z, B–W, C–Y, D–X, total \(12 + 7 + 8 + 15 = 42\) from the original table.

Step 4 for a smallest uncovered element \(m\):

\[\text{uncovered: } x - m \qquad \text{covered twice: } x + m \qquad \text{covered once: } x\]
uncovered:x-m,covered twice:x+m

Converting a maximising problem, where \(L\) is the largest element:

\[\text{new element} = L - \text{old element}\]
new element=L-old element

When to stop covering and allocate (\(n \times n\) matrix):

\[\text{fewest lines} = n \;\Rightarrow\; \text{allocate} \qquad \text{fewest lines} < n \;\Rightarrow\; \text{Step 4}\]
lines=n⇒allocate

The Hungarian algorithm (minimum)

  1. Step 1, rows. Subtract the smallest element of each row from every element in that row.
  2. 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.)
  3. 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.
  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

  1. Find a row or column with only one zero and allocate that pairing. Cross out its row and column.
  2. Repeat until every worker has a task.
  3. Add the matching entries from the original table to get the total.

For a maximum

  1. Subtract every element from the largest element in the table, then use the steps above.
  2. Total the allocation using the original (profit) table.
Example 1 — No adjustment needed
Three cleaners, \(A\), \(B\) and \(C\), are to clean three Warrnambool offices, \(X\), \(Y\) and \(Z\). The table shows the time each takes in minutes. Use the Hungarian algorithm to find the allocation with the least total time.
XYZ
A17917
B61113
C171713
Solution

Work through Steps 1 to 3, then allocate.

Step 1 (rows). Subtract each row’s smallest element (\(9\), \(6\), \(13\)):

\[\begin{bmatrix}8 & 0 & 8\\ 0 & 5 & 7\\ 4 & 4 & 0\end{bmatrix}\]

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.

Covering lines 13 lines through rows A, B and C. X Y Z A B C 8 0 8 0 5 7 4 4 0

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
Allocation on the zerosFinal matrix with the allocated zeros circled: A–Y, B–X, C–Z. X Y Z A B C 8 0 8 0 5 7 4 4 0

Use the original table for the total:

\(\text{total}\)\(=\)\(9 + 6 + 13\)
\(\)\(=\)\(28\)
total=28minutes

The best allocation is A–Y, B–X, C–Z, taking 28 minutes in total.

Example 2 — Column reduction and one adjustment
Three electricians, \(P\), \(Q\) and \(R\), are sent to three Ballarat building sites, \(X\), \(Y\) and \(Z\). The table shows the cost of each job in hundreds of dollars. Find the allocation with the least total cost.
XYZ
P13144
Q1186
R8713
Solution

Follow the four steps in order.

Step 1 (rows). Subtract each row’s smallest element (\(4\), \(6\), \(7\)):

\[\begin{bmatrix}9 & 10 & 0\\ 5 & 2 & 0\\ 1 & 0 & 6\end{bmatrix}\]

Step 2 (columns). Subtract each column’s smallest element (\(1\), \(0\), \(0\)):

\[\begin{bmatrix}8 & 10 & 0\\ 4 & 2 & 0\\ 0 & 0 & 6\end{bmatrix}\]

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.

Covering lines 12 lines through row R and column Z. X Y Z P Q R 8 10 0 4 2 0 0 0 6

Step 4. The smallest uncovered element is \(2\). Subtract \(2\) from every uncovered element, add \(2\) where two lines cross, and leave the rest:

\[\begin{bmatrix}6 & 8 & 0\\ 2 & 0 & 0\\ 0 & 0 & 8\end{bmatrix}\]

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.

Covering lines 23 lines through rows Q and R and column Z. X Y Z P Q R 6 8 0 2 0 0 0 0 8

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
Allocation on the zerosFinal matrix with the allocated zeros circled: P–Z, Q–Y, R–X. X Y Z P Q R 6 8 0 2 0 0 0 0 8

Use the original table for the total:

\(\text{total}\)\(=\)\(4 + 8 + 8\)
\(\)\(=\)\(20\)
total=20

The best allocation is P–Z, Q–Y, R–X, costing \(20\) hundred dollars, which is $2000.

Example 3 — A 4 × 4 problem
A Frankston removalist has four trucks, \(A\) to \(D\), for four jobs, \(W\) to \(Z\). The table gives the time in minutes for each truck to reach each job. Find the allocation with the least total time.
WXYZ
A14231914
B26182627
C40293035
D29282930
Solution

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\)):

\[\begin{bmatrix}0 & 9 & 5 & 0\\ 8 & 0 & 8 & 9\\ 11 & 0 & 1 & 6\\ 1 & 0 & 1 & 2\end{bmatrix}\]

Step 2 (columns). Subtract each column’s smallest element (\(0\), \(0\), \(1\), \(0\)):

\[\begin{bmatrix}0 & 9 & 4 & 0\\ 8 & 0 & 7 & 9\\ 11 & 0 & 0 & 6\\ 1 & 0 & 0 & 2\end{bmatrix}\]

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.

Covering lines 13 lines through row A and columns X and Y. W X Y Z A B C D 0 9 4 0 8 0 7 9 11 0 0 6 1 0 0 2

Step 4. The smallest uncovered element is \(1\). Subtract \(1\) from every uncovered element, add \(1\) where two lines cross, and leave the rest:

\[\begin{bmatrix}0 & 10 & 5 & 0\\ 7 & 0 & 7 & 8\\ 10 & 0 & 0 & 5\\ 0 & 0 & 0 & 1\end{bmatrix}\]

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.

Covering lines 24 lines through rows A, C and D and column X. W X Y Z A B C D 0 10 5 0 7 0 7 8 10 0 0 5 0 0 0 1

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
Allocation on the zerosFinal matrix with the allocated zeros circled: A–Z, B–X, C–Y, D–W. W X Y Z A B C D 0 10 5 0 7 0 7 8 10 0 0 5 0 0 0 1

Use the original table for the total:

\(\text{total}\)\(=\)\(14 + 18 + 30 + 29\)
\(\)\(=\)\(91\)
total=91minutes

The best allocation is A–Z, B–X, C–Y, D–W, with a total time of 91 minutes.

Example 4 — Maximising sales
Four sales staff, \(E\) to \(H\), are each given one of four regions of Victoria, \(W\) to \(Z\). The table shows expected monthly sales in thousands of dollars. Find the allocation with the greatest total sales.
WXYZ
E57224153
F55523151
G43253338
H40605939
Solution

The algorithm finds a minimum, so convert the table first.

Convert to minimising. The largest element is \(60\). Subtract every element from \(60\):

\[\begin{bmatrix}3 & 38 & 19 & 7\\ 5 & 8 & 29 & 9\\ 17 & 35 & 27 & 22\\ 20 & 0 & 1 & 21\end{bmatrix}\]

Step 1 (rows). Subtract each row’s smallest element (\(3\), \(5\), \(17\), \(0\)):

\[\begin{bmatrix}0 & 35 & 16 & 4\\ 0 & 3 & 24 & 4\\ 0 & 18 & 10 & 5\\ 20 & 0 & 1 & 21\end{bmatrix}\]

Step 2 (columns). Subtract each column’s smallest element (\(0\), \(0\), \(1\), \(4\)):

\[\begin{bmatrix}0 & 35 & 15 & 0\\ 0 & 3 & 23 & 0\\ 0 & 18 & 9 & 1\\ 20 & 0 & 0 & 17\end{bmatrix}\]

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.

Covering lines 13 lines through row H and columns W and Z. W X Y Z E F G H 0 35 15 0 0 3 23 0 0 18 9 1 20 0 0 17

Step 4. The smallest uncovered element is \(3\). Subtract \(3\) from every uncovered element, add \(3\) where two lines cross, and leave the rest:

\[\begin{bmatrix}0 & 32 & 12 & 0\\ 0 & 0 & 20 & 0\\ 0 & 15 & 6 & 1\\ 23 & 0 & 0 & 20\end{bmatrix}\]

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.

Covering lines 24 lines through rows E, F and H and column W. W X Y Z E F G H 0 32 12 0 0 0 20 0 0 15 6 1 23 0 0 20

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
Allocation on the zerosFinal matrix with the allocated zeros circled: E–Z, F–X, G–W, H–Y. W X Y Z E F G H 0 32 12 0 0 0 20 0 0 15 6 1 23 0 0 20

Use the original table for the total:

\(\text{total}\)\(=\)\(53 + 52 + 43 + 59\)
\(\)\(=\)\(207\)
maximum=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

Too many lines. The test only works with the fewest lines that cover every zero. Watch out for drawing a line through every row, which always gives \(n\) lines and can stop the algorithm too early.
Step 4 errors. Subtract only from the uncovered elements and add only where two lines cross. Watch out for changing elements that are covered by just one line.
Totalling the wrong table. The zeros show which pairings to use, not what they cost. Watch out for adding the reduced numbers; always total the original table (or the original profits for a maximum).

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.