topic badge

7.05 Assignment allocation

Lesson

What do these networks have in common?

These all have a special property that is more obvious when we colour the vertices a little differently.

For each network, we have split the vertices into two groups, blue and orange. Notice that the orange vertices are only connected to blue vertices, and the blue vertices are only connected to orange vertices - there are no edges between blue vertices, and no edges between orange ones either.

Whenever we can divide the vertices up like this, we call the network a bipartite graph.

We are going to study these kinds of networks, with one group of vertices representing tasks, and the other representing agents - people, companies, countries, and so on. These are called allocation problems (sometimes assignment problems). 

 

Exploration

Paula, Rohan and Erin are working together on a group project over the weekend. There are a number of tasks that need to get done, but each student is better at some things and worse at others. This table shows how many hours it would take for them to do each part of the project:

  Making the poster Writing the report Writing the speech
Paula $10$10 $5$5 $8$8
Rohan $6$6 $7$7 $12$12
Erin $9$9 $8$8 $5$5

Once every task is completed, they get to go home. Who should do what part to make sure they can go home the earliest?

There are two ways we can represent this same information - one is by using a matrix:

The matrix is just a more condensed version of the table. A more interesting way is to represent the information using a bipartite graph:

This is a bipartite network because every person is connected to one or more tasks, but a person isn’t connected to a person, or a task to a task.

We want to pick out three edges so that each person and task has exactly one edge coming out of it, forming an allocation. Here are two ways we could do it:

In the first allocation, the group has to stay for $12$12 hours, since Rohan takes $12$12 hours to do the speech while Paula and Erin wait around. The second allocation is a little better since the group stays for $8$8 hours and, in this allocation, it’s Rohan who does the waiting.

With some further investigation, we would find that this is the best allocation possible:

This allocation has the group only taking $6$6 hours to complete the project, half the time of the first allocation we tried! It is the optimum allocation.

We may have spotted this allocation straight away just by looking at the table. It allocates everyone the task that they’re best at. But this isn’t always the case, so be sure to check thoroughly!

 

Practice questions

QUESTION 1

Three people, Elise, Poppy and Richard, are each to be given $1$1 task ($T1$T1, $T2$T2 or $T3$T3) to complete on their own.

Each person is able to complete the $3$3 tasks that are available in a set time. These times, in minutes, are shown in the network diagram below.

  1. Represent this data in a matrix.

          $T1$T1 $T2$T2 $T3$T3    
    $E$E     $\editable{}$ $\editable{}$ $\editable{}$    
    $P$P     $\editable{}$ $\editable{}$ $\editable{}$    
    $R$R     $\editable{}$ $\editable{}$ $\editable{}$    

QUESTION 2

Consider the following bipartite graph.

  1. Which of the following allocations agree with the bipartite graph?

    A 1
    B 4
    C 2
    D 3
    E 5
    A
    A 3
    B 1
    C 5
    D 2
    E 4
    B
    A 3
    B 5
    C 2
    D 1
    E 4
    C
    A 1
    B 3
    C 5
    D 2
    E 4
    D

 

Hungarian algorithm (graph version)

As we just saw, we can represent allocation problems using graphs, tables and matrices. Now we need to look at how to solve them.

We will use the Hungarian algorithm, which was invented by Harold Kuhn in 1955. He relied heavily on earlier work done by two Hungarian mathematicians, Jenő Egerváry and Dénes Kőnig, so he named it after them.

We will first apply the Hungarian algorithm with a graphical procedure to a simple problem.  For more complex examples it is easier to use a matrix based procedure that is described later in this lesson.

 

Worked example

Example 1

Harvey, Kiara, and Dao are on the medical support team for a company that runs events. There are events at three major cities this weekend, and the cost for each of them to travel to each city is given in the table below:

  Sydney Melbourne Perth
Harvey $\$200$$200 $\$250$$250 $\$320$$320
Kiara $\$280$$280 $\$300$$300 $\$250$$250
Dao $\$300$$300 $\$470$$470 $\$510$$510

There are six possible allocations, but which one costs the least money overall? We start by drawing out the network:

 

The first step of the algorithm is to subtract the lowest weight of the edges coming out of their vertex from the other edges:

 

Harvey’s cheapest trip is $\$200$$200...

 

...so we subtract $\$200$$200 from all his trips. 

 

Kiara’s cheapest trip is $\$250$$250...

 

...so we subtract $\$250$$250 from all her trips.

Dao’s cheapest trip is $\$300$$300...

...so we subtract $\$300$$300 from all her trips.

Each person now has a $0$0-edge, but these edges do not all point to different destinations - both Harvey and Dao have a $0$0-edge with Sydney, and nobody has a $0$0-edge with Melbourne.

We go back to the network and do essentially the same thing, but this time looking at the destinations rather than the people. The lowest weight coming out of the Sydney vertex is $0$0, and since subtracting $0$0 doesn’t change the network we do nothing - and the same for Perth. But for the Melbourne vertex, there is something to do:

The cheapest trip to Melbourne is $\$50$$50...

...so we subtract $\$50$$50 from all Melbourne trips.

Now there are $0$0-edges both to and from each person and task. We are ready to do the allocation. There’s only one $0$0-edge from Perth, and only one from Dao, so we highlight those:

This leaves Harvey with a $0$0-edge to Melbourne:

This is the optimum allocation. If we recover the original edge weights, we can calculate the cost:

The total, cheapest cost is $\$250+\$300+\$250=\$800$$250+$300+$250=$800. Interestingly, this involves paying the most to send someone to Sydney! This procedure finds what’s best for the group, not always the individual.

 

Hungarian algorithm (from a graph)

These steps can be used to solve simple assignment problems using the graphical procedure:

  1. Subtract the lowest weight of edges coming out of each vertex on the left from the others.
  2. Subtract the lowest weight of edges coming out of each vertex on the right from the others.
  3. Make a valid assignment using the edges of weight zero.
  4. Relate the zeros back to the data in the original matrix to answer the question in context.

If after step $2$2 you cannot make a valid assignment then using the matrix version of this algorithm would be recommended. It is generally easier to keep track of the changes required at this stage.  

Practice questions

Question 3

A courier company needs to make multiple pick-ups across several stores.

The lengths of the roads (in km) linking these stores are shown in the diagram below.

  1. Adam, Ben and Caitlin are couriers who are working in the area and they have each just arrived at stores $A$A, $B$B, and $C$C, respectively. They still need to make the pick-ups from stores $X$X, $Y$Y and $Z$Z. Each courier can only visit one more store.

    Complete the table below, showing the shortest distance that each courier could travel to each remaining store.

    $X$X $Y$Y $Z$Z

    Adam

    $\editable{}$ $\editable{}$ $\editable{}$

    Ben

    $\editable{}$ $\editable{}$ $\editable{}$

    Caitlin

    $\editable{}$ $\editable{}$ $\editable{}$

 

Hungarian algorithm (matrix version)

In the previous example, we learned how to find the optimum allocation for a network represented as a graph.
 
Now we will learn a different way to find the optimum allocation from within the table itself, without ever drawing a bipartite network. This can be very useful when there would be a lot of vertices and edges since the network can get very crowded.
 
In these examples, we will also look at some small variations to the algorithm to deal with situations where the number of tasks and resources is not the same, and where we want to find the maximum values for our optimum allocation.
 

 

Worked examples

Example 2

A chemical company has four factories, and each factory  can produce one of four chemicals. The cost per day (in thousands of dollars) of shipping the materials required to produce each chemical to each factory is given in the table below.

  Polyvinyl chloride Titanium dioxide Diammonium phosphate Ethylene glycol
Sydney $12$12 $10$10 $92$92 $28$28
Auckland $14$14 $79$79 $9$9 $84$84
Jakarta $74$74 $73$73 $61$61 $79$79
Mumbai $68$68 $75$75 $34$34 $81$81

We start by getting rid of the column and row headings (we will put them back later):

We then subtract the lowest number in each row from every number in that row.  So from Row 1 we subtract $10$10 from each number, Row 2 we subtract $9$9 from each number; Row 3 we subtract $61$61 from each number; and Row 4 we subtract $34$34 from each number:

This is the same as when we subtracted the lowest weight edge for each person in the graphical example.

We then do the same with the columns.  So from Column 1, we subtract $2$2; Column 2 we subtract $0$0; Column 3 we subtract $0$0; and Column 4 we subtract $18$18.

Just like subtracting the edge weights for each task.

Now we ask: what is the least number of vertical and horizontal lines we need to cover all the $0$0’s we have made? In this example it’s three:

We need to make more zeros until we need four lines, because there are four tasks. We find the smallest number in the table not covered by any lines, which is $3$3. We subtract $3$3 from all uncovered numbers, and add $3$3 to all $0$0’s covered twice by the lines, i.e. the $82$82 and the $0$0 in the top right: 

Now we need four lines to cover every $0$0, so we are ready to read off our optimum allocation! We start by looking at each row, picking the $0$0 in the row if it’s the only one:

We choose the zero in the bottom row as it is the only zero in that row.  This means that we have allocated diammonium phosphate to Mumbai.

Then do the same with the columns:

We choose the zero in row 1, column 2 because it is the only $0$0 in that column. This allocates titanium dioxide to Sydney. Similarly, we choose the zero in row 3, column 4 because it is the only $0$0 in that column, and this allocates ethylene glycol to Jakarta. This leaves only one allocation - the zero in row 2, column 1 - which allocates polyvinyl chloride to Auckland.

If we highlight the corresponding entries in the original table, we can not only read off the optimal allocation but also calculate the cost:

  Polyvinyl chloride Titanium dioxide Diammonium phosphate Ethylene glycol
Sydney $12$12 $10$10 $92$92 $28$28
Auckland $14$14 $79$79 $9$9 $84$84
Jakarta $74$74 $73$73 $61$61 $79$79
Mumbai $68$68 $75$75 $34$34 $81$81

Sydney makes titanium dioxide, Auckland makes polyvinyl chloride, Jakarta makes ethylene glycol, and Mumbai makes diammonium phosphate, for a total (minimum!) cost of $10+14+79+34=137$10+14+79+34=137 thousand dollars per day.

 

Hungarian algorithm minimisation (from a matrix)
  1. Subtract the lowest number in each row from every number in that row.
  2. Subtract the lowest number in each column from every number in that column.
  3. Cover the $0$0’s with the minimum number of lines. If the number of lines required is less than the total number of rows (or columns), subtract the smallest uncovered value in the matrix from all other uncovered values, and add this value to any number covered by two lines. Repeat this step as many times as required.
  4. Make a valid assignment using the zeros in the matrix.
  5. Relate the zeros back to the data in the original matrix to answer the question in context.

The matrix version saves space, is easier to enter into a computer, and makes the tricky cases a lot easier to spot and deal with. However, the graph version is easiest for smaller allocations.

 

Practice questions

Question 4

In a sports tournament, a team of 3 athletes need to complete 3 different types of heats in the shortest time possible. The following table represents the average time (in minutes) of each athlete in each type of heat.

Athlete/Competition $D$D $E$E $F$F
$A$A 4 8 7
$B$B 1 4 3
$C$C 9 2 6
  1. If every athlete can only compete in one heat each, use the Hungarian algorithm to find which of the following bipartite graphs is the optimum allocation for the team of athletes?

    A

    B

    C

    D
  2. What is the minimum total time of the competitions?

Question 5

Consider the following cost table.

  $A$A $B$B $C$C
$D$D $10$10 $16$16 $11$11
$E$E $11$11 $15$15 $18$18
$F$F $15$15 $10$10 $8$8
  1. Fill in the table below after the first row and column reduction of the Hungarian Algorithm.

      $A$A $B$B $C$C
    $D$D $\editable{}$ $\editable{}$ $\editable{}$
    $E$E $\editable{}$ $\editable{}$ $\editable{}$
    $F$F $\editable{}$ $\editable{}$ $\editable{}$
  2. Give the final reduced table.

      $A$A $B$B $C$C
    $D$D $\editable{}$ $\editable{}$ $\editable{}$
    $E$E $\editable{}$ $\editable{}$ $\editable{}$
    $F$F $\editable{}$ $\editable{}$ $\editable{}$

QUESTION 6

A limousine company has to dispatch 3 cars to 3 different locations. The following table displays the traveling cost (in dollars) of each limousine to each area.

Locations\Cars $A_1$A1 $A_2$A2 $A_3$A3
$L_1$L1 $29$29 $37$37 $22$22
$L_2$L2 $32$32 $27$27 $26$26
$L_3$L3 $33$33 $34$34 $25$25
  1. Use the Hungarian algorithm to find the allocation which uses the minimum cost.

    $L_1$L1 $\rightarrow$ $A$A$\editable{}$
    $L_2$L2 $\rightarrow$ $A$A$\editable{}$
    $L_3$L3 $\rightarrow$ $A$A$\editable{}$
  2. What is the minimum cost of travel?

QUESTION 7

Consider the following cost table.

  $A$A $B$B $C$C
$D$D $10$10 $16$16 $11$11
$E$E $11$11 $15$15 $18$18
$F$F $15$15 $10$10 $8$8
  1. Fill in the table below after the first row and column reduction of the Hungarian Algorithm.

      $A$A $B$B $C$C
    $D$D $\editable{}$ $\editable{}$ $\editable{}$
    $E$E $\editable{}$ $\editable{}$ $\editable{}$
    $F$F $\editable{}$ $\editable{}$ $\editable{}$
  2. Give the final reduced table.

      $A$A $B$B $C$C
    $D$D $\editable{}$ $\editable{}$ $\editable{}$
    $E$E $\editable{}$ $\editable{}$ $\editable{}$
    $F$F $\editable{}$ $\editable{}$ $\editable{}$

 

Worked examples

Example 3

In this example, we will look at a scenario where the number of resources is more, or less, than the number of tasks.

In either case, the variation is as simple as adding a row or column of zeros to the matrix before beginning the optimisation.

If we revisit the scenario of Example 2, but now consider the case that the company has decided that it will no longer produce ethylene glycol. 

Since the company has four factories, and each factory can produce one of four chemicals. The cost per day (in thousands of dollars of shipping the materials required to produce each chemical to each warehouse is given in the table below.

  Polyvinyl chloride Titanium dioxide Diammonium phosphate
Sydney $12$12 $10$10 $92$92
Auckland $14$14 $79$79 $9$9
Jakarta $74$74 $73$73 $61$61
Mumbai $68$68 $75$75 $34$34

The matrix representation is now a $4$4 by $3$3 matrix: 

 
There are fewer tasks than resources so we have to make this into a square matrix by adding a column of zeros (or a row of zeros if there are fewer resources than tasks):

 

We then proceed in the same way as before.  After subtracting the lowest value in each column the matrix looks like this:

 
The zeros can be covered with just three lines:

 

We need to make more zeros until we need four lines because there are four tasks. We find the smallest number in the table not covered by any lines, which is $25$25. We subtract $25$25 from all uncovered numbers, and add $25$25 to all s covered twice by the lines (the zeros in the top right): 

 

This still only needs three lines to cover the zeros:

 

So we will repeat the last step, this time, subtracting $2$2 from uncovered values, and add $2$2 to the twice-covered values.

 

Now we need four lines to cover all of the zeros, and we can make an optimum allocation. Start by looking at each row/column, picking the $0$0 in the row/column if it’s the only one:

 
Then allocating the remaining tasks:

 

If we highlight the corresponding entries in the original table, we can not only read off the optimal allocation but also calculate the cost:

  Polyvinyl chloride Titanium dioxide Diammonium phosphate
Sydney $12$12 $10$10 $92$92
Auckland $14$14 $79$79 $9$9
Jakarta $74$74 $73$73 $61$61
Mumbai $68$68 $75$75 $34$34

Sydney makes titanium dioxide, Auckland makes polyvinyl chloride, and Mumbai makes diammonium phosphate.  The Jakarta factory is no longer required.

 

Example 4

In this final example, we will use the Hungarian algorithm to find an optimum allocation in a scenario that we want to maximise some quantity. 

The optimum allocation might require finding the maximum value in scenarios such as a business wanting to maximise revenue or profits, a farmer wanting the highest possible grain yield from the available paddocks, a factory aiming to produce the greatest number of parts with available machinery and many others.

We will consider a sales manager for a toy manufacturer, and you currently have three salespeople on the road meeting buyers.

The salespeople are Alan, Beverley and Cat.  During a sales campaign, they can each visit one of three stores in Denver, Edmonton and Fargo to launch the new range of Gizmo toys. 

Based on the results of previous campaigns, the expected sales (units) for each of the salespersons in each of the stores is shown in the table below:

  Denver Edmonton Fargo
Alan $250$250 $400$400 $350$350
Beverley $400$400 $600$600 $350$350
Cat $200$200 $350$350 $250$250

How should the sales manager direct the salespeople in order to achieve maximum sales from the campaign?

We will represent this table as a matrix:

To adapt the Hungarian algorithm to solve a maximisation problem, an initial conversion is performed:

  1. Find the maximum value in the matrix;
  2. Subtract each element in the matrix from this maximum value.

The converted matrix produced represents a cost, in this case, a loss of sales that we can minimise using the standard algorithm.

If necessary, for a problem where the number of tasks is not equal to the number of resources, the conversion step should be done before the matrix is extended with a row or column of zeros.

For the toy sales manager, the maximum value is $600$600.  Subtracting each value from $600$600 produces this new matrix:

Reducing the rows and columns produces this reduced matrix, from which the optimum solution can be extracted:

The original table can be restored with the optimum allocation highlighted.

  Denver Edmonton Fargo
Alan $250$250 $400$400 $350$350
Beverley $400$400 $600$600 $350$350
Cat $200$200 $350$350 $250$250

With Alan selling to the Fargo store, Beverley selling to Edmonton and Cat selling to Denver, we can see that the optimum allocation produces the expected sales of $600+200+350=1150$600+200+350=1150 units.

 

Hungarian algorithm maximisation (from a matrix)
  • Find the maximum value in the matrix and subtract each element in the matrix from this maximum value.

  • Now complete the algorithm with the minimisation procedure:
    1. Subtract the lowest number in each row from every number in that row.
    2. Subtract the lowest number in each column from every number in that column.
    3. Cover the $0$0’s with the minimum number of lines. If the number of lines required is less than the total number of rows (or columns), subtract the smallest uncovered value in the matrix from all other uncovered values, and add this value to any number covered by two lines. Repeat this step as many times as required.
    4. Make a valid assignment using the zeros in the matrix.
    5. Relate the zeros back to the data in the original matrix to answer the question in context.

 

Practice question

Question 8

The matrix below represents a cost network.

We are going to use the Hungarian algorithm to determine the allocation that maximises the cost, by performing the following steps.

    $A$A   $B$B   $C$C   $D$D   $E$E    
$V$V   $25$25   $83$83   $29$29   $11$11   $59$59    
$W$W   $32$32   $12$12   $46$46   $33$33   $29$29    
$X$X   $61$61   $17$17   $12$12   $13$13   $11$11    
$Y$Y   $15$15   $73$73   $37$37   $67$67   $43$43    
  1. First invert the elements of the matrix to convert to a minimisation problem (so that we can use the Hungarian algorithm).

     

        $A$A   $B$B   $C$C   $D$D   $E$E    
    $V$V   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$    
    $W$W   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$    
    $X$X   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$    
    $Y$Y   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$    
  2. In order to use the Hungarian algorithm, now create a square matrix from the inverted matrix.

     

        $A$A   $B$B   $C$C   $D$D   $E$E    
    $V$V   $58$58   $0$0   $54$54   $72$72   $24$24    
    $W$W   $51$51   $71$71   $37$37   $50$50   $54$54    
    $X$X   $22$22   $66$66   $71$71   $70$70   $72$72    
    $Y$Y   $68$68   $10$10   $46$46   $16$16   $40$40    
    $Z$Z   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$    
  3. Now complete the initial row and column reduction.

     

        $A$A   $B$B   $C$C   $D$D   $E$E    
    $V$V   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$    
    $W$W   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$    
    $X$X   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$    
    $Y$Y   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$    
    $Z$Z   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$    
  4. Complete the next stage using this strike-out pattern:

     

        $A$A   $B$B   $C$C   $D$D   $E$E    
    $V$V   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$    
    $W$W   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$    
    $X$X   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$    
    $Y$Y   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$    
    $Z$Z   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$   $\editable{}$    
  5. Determine the optimum allocation for the cost network.

     

    $V$V$\to$$\editable{}$

    $W$W$\to$$\editable{}$

    $X$X$\to$$\editable{}$

    $Y$Y$\to$$\editable{}$

    $Z$Z$\to$$\editable{}$

  6. Calculate the maximum cost for the network.

Outcomes

4.3.10

use a bipartite graph and/or its tabular or matrix form to represent an assignment/ allocation problem

4.3.11

determine the optimum assignment(s), by inspection for small-scale problems, or by use of the Hungarian algorithm for larger problems

What is Mathspace

About Mathspace