The Hungarian algorithmEdexcel A-Level Further Maths: Revision notes
Section 1
Allocation problems and the cost matrix
In an allocation (assignment) problem, workers must be matched one-to-one with tasks so that the total cost is a minimum. The costs form an cost matrix: the entry in row , column is the cost of giving worker task . Checking every one of the possible allocations is hopeless for large , 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.
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 has row minima , giving . Every column already has a zero, so column reduction changes nothing.
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 , a complete allocation can be made using only zero entries: go to step 5. Step 4: augment. If fewer than 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 , giving . Now Cleo takes column 2, Ben column 1 and Ana column 3: cost .
Adding up the reduced entries. The cost of the allocation is always found from the original matrix.
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 , 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 for every driver. Row reduction then changes nothing, because every row already has a zero.
Section 5
Incomplete data
Sometimes a worker cannot do a task (for example, no qualification). Put a very large number 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 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 ), replace by and run the algorithm again.
Treat symbolically and subtract from it: write , 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 , a profit of becomes and a profit of becomes . For the converted total is (profit), so a converted minimum of means a maximum profit of .
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
- 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 , 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
- 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 , 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
- 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 , 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
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).