All revision notes topics

The Hungarian algorithmEdexcel A-Level Further Maths: Revision notes

Section 1

Allocation problems and the cost matrix

In an allocation (assignment) problem, nn workers must be matched one-to-one with nn tasks so that the total cost is a minimum. The costs form an n×nn\times n cost matrix: the entry in row ii, column jj is the cost of giving worker ii task jj. Checking every one of the n!n! possible allocations is hopeless for large nn, so we use the Hungarian algorithm, which finds the optimum in a few steps. The algorithm works because subtracting the same number from every entry in a row, or in a column, changes the total cost of every complete allocation by the same amount, so the best allocation is unchanged.

Key termsallocation problemcost matrixHungarian algorithm

Section 2

Reducing the matrix: rows first

Step 1: reduce the rows. Subtract the smallest entry in each row from every entry in that row, so each row has at least one zero. Step 2: reduce the columns. Subtract the smallest entry in each column from that column (a column that already contains a zero is unchanged). Always do rows first. Example: the matrix (8128107111239)\begin{pmatrix} 8 & 12 & 8 \\ 10 & 7 & 11 \\ 12 & 3 & 9 \end{pmatrix} has row minima 8,7,38, 7, 3, giving (040304906)\begin{pmatrix} 0 & 4 & 0 \\ 3 & 0 & 4 \\ 9 & 0 & 6 \end{pmatrix}. Every column already has a zero, so column reduction changes nothing.

Key termsrow reductioncolumn reduction
Common mistake

Reducing columns before rows. The method asks for rows first.

Section 3

Covering the zeros and augmenting

Step 3: test. Draw the minimum number of straight lines (horizontal or vertical) that cover every zero. If the number of lines equals nn, a complete allocation can be made using only zero entries: go to step 5. Step 4: augment. If fewer than nn lines are needed, find the smallest entry not covered by any line. Subtract it from every uncovered entry, and add it to every entry covered by two lines (where a horizontal and a vertical line cross). Entries covered by one line are unchanged. Return to step 3. Step 5: allocate. Choose a complete allocation using only zeros (start with a row or column that has a single zero). Add up the costs in the original matrix. In the example, two lines (row 1 and column 2) cover the zeros; the smallest uncovered entry is 33, giving (070001603)\begin{pmatrix} 0 & 7 & 0 \\ 0 & 0 & 1 \\ 6 & 0 & 3 \end{pmatrix}. Now Cleo takes column 2, Ben column 1 and Ana column 3: cost 8+10+3=218+10+3=21.

Key termsaugmentingcovering lines
Common mistake

Adding up the reduced entries. The cost of the allocation is always found from the original matrix.

Exam tip

Cover the zeros with the fewest lines you can. If you use more lines than necessary you may wrongly conclude that the allocation is ready.

Section 4

Dummy locations: unequal numbers

The algorithm needs a square matrix. If there are more workers than tasks (or the reverse), add a dummy task (or worker) so the matrix is square. Fill the dummy row or column with the same value, normally 00, so that it does not favour anyone. In the final allocation, whoever is matched with the dummy does not take part, and the dummy adds nothing to the cost. Example: four drivers and three deliveries. Add a dummy delivery with cost 00 for every driver. Row reduction then changes nothing, because every row already has a zero.

Key termsdummysquare matrix

Section 5

Incomplete data

Sometimes a worker cannot do a task (for example, no qualification). Put a very large number MM in that cell. It is so big that it can never appear in the least-cost allocation, but you can still apply the algorithm. After reducing, entries such as M−40M-40 are still huge and stay out of the allocation. If a different matrix is needed later (for example, the worker is trained and can now do the task at cost cc), replace MM by cc and run the algorithm again.

Key termsincomplete dataM
Exam tip

Treat MM symbolically and subtract from it: write M−23M-23, not a number. Remember the cost of an allocation is read from the original matrix.

Section 6

Maximising profit

The algorithm finds a minimum. To maximise profit, convert the matrix: subtract every entry from the largest entry of the original matrix, then apply the algorithm to the new matrix. The allocation that minimises the converted total maximises the profit. Read the profit from the original matrix. Example: if the largest profit is 1414, a profit of 55 becomes 99 and a profit of 1414 becomes 00. For n=3n=3 the converted total is 3×14−3\times14-(profit), so a converted minimum of 99 means a maximum profit of 42−9=3342-9=33.

Key termsmaximum profitconversion
Common mistake

Applying the algorithm directly to profits. That would give the allocation with the least profit.

That's the notes covered.

Carry on to the next subtopic.

Exam questions on The Hungarian algorithm

  1. A firm has three engineers, Ana, Ben and Cleo, and three tasks: wiring (W), testing (T) and installing (I). Each engineer must do exactly one task and each task is done by exactly one engineer. The cost, in £, of each engineer doing each task is given by the matrix (8128107111239)\begin{pmatrix} 8 & 12 & 8 \\ 10 & 7 & 11 \\ 12 & 3 & 9 \end{pmatrix}, where the rows are Ana, Ben, Cleo and the columns are W, T, I. The firm wants the total cost to be as small as possible.
    Complete the Hungarian algorithm to find the allocation with the least total cost, and state that cost.2 marks
  2. A company has three sales representatives, Dev, Eli and Fay, to send to three regions: North, South and East. Each representative goes to a different region. The expected monthly profit, in £ thousand, of each representative in each region is given by the matrix (513111011145125)\begin{pmatrix} 5 & 13 & 11 \\ 10 & 11 & 14 \\ 5 & 12 & 5 \end{pmatrix}, where the rows are Dev, Eli, Fay and the columns are North, South, East. The company wants the total profit to be as large as possible and uses the Hungarian algorithm.
    A trainee applies the Hungarian algorithm directly to the original profit matrix, with no conversion. Explain why the allocation found would not be the one the company wants.2 marks
  3. A courier firm has four drivers, P, Q, R and S, and three deliveries, X, Y and Z. Each delivery is made by exactly one driver and each driver makes at most one delivery. The cost, in £, of each driver making each delivery is given by the matrix (212712331426322025293814)\begin{pmatrix} 21 & 27 & 12 \\ 33 & 14 & 26 \\ 32 & 20 & 25 \\ 29 & 38 & 14 \end{pmatrix}, where the rows are P, Q, R, S and the columns are X, Y, Z. The firm wants the total cost to be as small as possible.
    A dummy delivery is added so that the Hungarian algorithm can be used. Explain why this is needed, and state what should be entered in the dummy column.3 marks
See the full worksheet

Written by the Exaim team, led by Shaun Daswani (Head of Upper Secondary, Improve ME Institute; MSc Financial Mathematics, Imperial College London; BSc, UCL) and Jason Daswani (operational lead, Improve ME Institute; LSE).