19
Allocation problems - The Hungarian Algorithm The Hungarian algorithm Step 1 Reduce the array by both row and column subtractions Step 2 Cover the zero elements with the minimum number of lines. If the number of lines is the same as the size of the array, then go to Step 4. Step 3 Augment the elements. To do this, identify the minimum uncovered element. Subtract this element from all uncovered elements, and add this element to all elements covered by two lines. Then return to step 2. Step 4 Identify the maximal matching which uses only zero elements, and apply this matching to the original array to find the minimum cost.

Allocation problems - The Hungarian Algorithm The Hungarian algorithm Step 1Reduce the array by both row and column subtractions Step 2Cover the zero elements

Embed Size (px)

Citation preview

Allocation problems - The Hungarian Algorithm

The Hungarian algorithm

Step 1 Reduce the array by both row and column subtractions

Step 2 Cover the zero elements with the minimum number of lines. If the number of lines is the same as the size of the array, then go to Step 4.

Step 3 Augment the elements. To do this, identify the minimum uncovered element. Subtract this element from all uncovered elements, and add this element to all elements covered by two lines. Then return to step 2.

Step 4 Identify the maximal matching which uses only zero elements, and apply this matching to the original array to find the

minimum cost.

Allocation problems - The Hungarian Algorithm

Example

Four workers are to be assigned to four tasks. No worker is to carry out more than one task.The expected times taken for each worker to carry out each of the tasks is shown below.Allocate the workers to the tasks so that the total time taken is as short as possible, giving the minimum time required.

Task 1 Task 2 Task 3 Task 4

Eva 35 24 17 42

Fred 33 21 23 35

George 27 20 16 33

Holly 29 31 19 38

Task 1 Task 2 Task 3 Task 4

Eva 35 24 17 42

Fred 33 21 23 35

George 27 20 16 33

Holly 29 31 19 38

Allocation problems - The Hungarian Algorithm

Step 1: Reduce the array by both row and column subtractions

Task 1 Task 2 Task 3 Task 4

Eva 35 24 17 42

Fred 33 21 23 35

George 27 20 16 33

Holly 29 31 19 38

To reduce by row subtractions, subtract the smallest element in each row from each element in the row.

The smallest element in Eva’s row is 17, so subtract 17 from each element in the row.

Task 1 Task 2 Task 3 Task 4

Eva 18 24 17 42

Fred 33 21 23 35

George 27 20 16 33

Holly 29 31 19 38

Task 1 Task 2 Task 3 Task 4

Eva 18 7 17 42

Fred 33 21 23 35

George 27 20 16 33

Holly 29 31 19 38

Task 1 Task 2 Task 3 Task 4

Eva 18 7 0 42

Fred 33 21 23 35

George 27 20 16 33

Holly 29 31 19 38

Task 1 Task 2 Task 3 Task 4

Eva 18 7 0 25

Fred 33 21 23 35

George 27 20 16 33

Holly 29 31 19 38

Allocation problems - The Hungarian Algorithm

Step 1: Reduce the array by both row and column subtractions

To reduce by row subtractions, subtract the smallest element in each row from each element in the row.

The smallest element in Fred’s row is 21, so subtract 21 from each element in the row.

Task 1 Task 2 Task 3 Task 4

Eva 18 7 0 25

Fred 33 21 23 35

George 27 20 16 33

Holly 29 31 19 38

Task 1 Task 2 Task 3 Task 4

Eva 18 7 0 25

Fred 12 21 23 35

George 27 20 16 33

Holly 29 31 19 38

Task 1 Task 2 Task 3 Task 4

Eva 18 7 0 25

Fred 12 0 23 35

George 27 20 16 33

Holly 29 31 19 38

Task 1 Task 2 Task 3 Task 4

Eva 18 7 0 25

Fred 12 0 2 35

George 27 20 16 33

Holly 29 31 19 38

Task 1 Task 2 Task 3 Task 4

Eva 18 7 0 25

Fred 12 0 2 14

George 27 20 16 33

Holly 29 31 19 38

Allocation problems - The Hungarian Algorithm

Step 1: Reduce the array by both row and column subtractions

To reduce by row subtractions, subtract the smallest element in each row from each element in the row.

The smallest element in George’s row is 16, so subtract 16 from each element in the row.

Task 1 Task 2 Task 3 Task 4

Eva 18 7 0 25

Fred 12 0 2 14

George 27 20 16 33

Holly 29 31 19 38

Task 1 Task 2 Task 3 Task 4

Eva 18 7 0 25

Fred 12 0 2 14

George 11 20 16 33

Holly 29 31 19 38

Task 1 Task 2 Task 3 Task 4

Eva 18 7 0 25

Fred 12 0 2 14

George 11 4 16 33

Holly 29 31 19 38

Task 1 Task 2 Task 3 Task 4

Eva 18 7 0 25

Fred 12 0 2 14

George 11 4 0 33

Holly 29 31 19 38

Task 1 Task 2 Task 3 Task 4

Eva 18 7 0 25

Fred 12 0 2 14

George 11 4 0 17

Holly 29 31 19 38

Allocation problems - The Hungarian Algorithm

Step 1: Reduce the array by both row and column subtractions

To reduce by row subtractions, subtract the smallest element in each row from each element in the row.

The smallest element in Holly’s row is 19, so subtract 19 from each element in the row.

Task 1 Task 2 Task 3 Task 4

Eva 18 7 0 25

Fred 12 0 2 14

George 11 4 0 17

Holly 29 31 19 38

Task 1 Task 2 Task 3 Task 4

Eva 18 7 0 25

Fred 12 0 2 14

George 11 4 0 17

Holly 10 31 19 38

Task 1 Task 2 Task 3 Task 4

Eva 18 7 0 25

Fred 12 0 2 14

George 11 4 0 17

Holly 10 12 19 38

Task 1 Task 2 Task 3 Task 4

Eva 18 7 0 25

Fred 12 0 2 14

George 11 4 0 17

Holly 10 12 0 38

Task 1 Task 2 Task 3 Task 4

Eva 18 7 0 25

Fred 12 0 2 14

George 11 4 0 17

Holly 10 12 0 19

Allocation problems - The Hungarian Algorithm

The minimum number of lines needed to cover all the zeros is 2,

Now reduce the array by subtracting the minimum entry in each column from all the entries in the column.

so the solution is not optimal.

Task 1 Task 2 Task 3 Task 4

Eva 18 7 0 25

Fred 12 0 2 14

George 11 4 0 17

Holly 10 12 0 19

Step 1: Reduce the array by both row and column subtractions

Task 1 Task 2 Task 3 Task 4

Eva 18 7 0 25

Fred 12 0 2 14

George 11 4 0 17

Holly 10 12 0 19

Task 1 Task 2 Task 3 Task 4

Eva 18 7 0 25

Fred 12 0 2 14

George 11 4 0 17

Holly 10 12 0 19

Allocation problems - The Hungarian Algorithm

Step 1: Reduce the array by both row and column subtractions

To reduce by column subtractions, subtract the smallest element in each column from each element in the column.

The smallest element in the Task 1 column is 10, so subtract 10 from each element in the column.

Task 1 Task 2 Task 3 Task 4

Eva 8 7 0 25

Fred 12 0 2 14

George 11 4 0 17

Holly 10 12 0 19

Task 1 Task 2 Task 3 Task 4

Eva 8 7 0 25

Fred 2 0 2 14

George 11 4 0 17

Holly 10 12 0 19

Task 1 Task 2 Task 3 Task 4

Eva 8 7 0 25

Fred 2 0 2 14

George 1 4 0 17

Holly 10 12 0 19

Task 1 Task 2 Task 3 Task 4

Eva 8 7 0 25

Fred 2 0 2 14

George 1 4 0 17

Holly 0 12 0 19

Allocation problems - The Hungarian Algorithm

Step 1: Reduce the array by both row and column subtractions

To reduce by column subtractions, subtract the smallest element in each column from each element in the column.

The Task 2 and Task 3 columns both contain zeros, so no column subtraction can be done in these two columns.

Task 1 Task 2 Task 3 Task 4

Eva 8 7 0 25

Fred 2 0 2 14

George 1 4 0 17

Holly 0 12 0 19

Allocation problems - The Hungarian Algorithm

Step 1: Reduce the array by both row and column subtractions

To reduce by column subtractions, subtract the smallest element in each column from each element in the column.

The smallest element in the Task 4 column is 14, so subtract 14 from each element in column 4.

Task 1 Task 2 Task 3 Task 4

Eva 8 7 0 25

Fred 2 0 2 14

George 1 4 0 17

Holly 0 12 0 19

Task 1 Task 2 Task 3 Task 4

Eva 8 7 0 11

Fred 2 0 2 14

George 1 4 0 17

Holly 0 12 0 19

Task 1 Task 2 Task 3 Task 4

Eva 8 7 0 11

Fred 2 0 2 0

George 1 4 0 17

Holly 0 12 0 19

Task 1 Task 2 Task 3 Task 4

Eva 8 7 0 11

Fred 2 0 2 0

George 1 4 0 3

Holly 0 12 0 19

Task 1 Task 2 Task 3 Task 4

Eva 8 7 0 11

Fred 2 0 2 0

George 1 4 0 3

Holly 0 12 0 5

Allocation problems - The Hungarian Algorithm

Step 2: Cover the zero elements with the minimum number of lines

Look for rows and columns containing more than one zero. In this case, the Task 3 column has three zeros and Fred’s row has two zeros, so cover these with lines.

Task 1 Task 2 Task 3 Task 4

Eva 8 7 0 11

Fred 2 0 2 0

George 1 4 0 3

Holly 0 12 0 5

The remaining zero, in square H1, can be covered by either a vertical or horizontal line. The choice of line will not affect the final result.

Three lines have been used, so this array does not give the optimal allocation.

Allocation problems - The Hungarian Algorithm

Step 3: Augment the elements

The smallest uncovered element is 1 (in square G1).

Task 1 Task 2 Task 3 Task 4

Eva 8 7 0 11

Fred 2 0 2 0

George 1 4 0 3

Holly 0 12 0 5

Subtract 1 from all uncovered elementsand add 1 to all elements covered by two lines

Task 1 Task 2 Task 3 Task 4

Eva 7 7 0 11

Fred 2 0 2 0

George 1 4 0 3

Holly 0 12 0 5

Task 1 Task 2 Task 3 Task 4

Eva 7 6 0 11

Fred 2 0 2 0

George 1 4 0 3

Holly 0 12 0 5

Task 1 Task 2 Task 3 Task 4

Eva 7 6 0 10

Fred 2 0 2 0

George 1 4 0 3

Holly 0 12 0 5

Task 1 Task 2 Task 3 Task 4

Eva 7 6 0 10

Fred 2 0 2 0

George 0 4 0 3

Holly 0 12 0 5

Task 1 Task 2 Task 3 Task 4

Eva 7 6 0 10

Fred 2 0 2 0

George 0 3 0 3

Holly 0 12 0 5

Task 1 Task 2 Task 3 Task 4

Eva 7 6 0 10

Fred 2 0 2 0

George 0 3 0 2

Holly 0 12 0 5

Task 1 Task 2 Task 3 Task 4

Eva 7 6 0 10

Fred 2 0 3 0

George 0 3 0 2

Holly 0 12 0 5

Task 1 Task 2 Task 3 Task 4

Eva 7 6 0 10

Fred 2 0 3 0

George 0 3 0 2

Holly 0 12 1 5

Allocation problems - The Hungarian Algorithm

Step 3: Augment the elements

The smallest uncovered element is 1 (in square G1).

Subtract 1 from all uncovered elementsand add 1 to all elements covered by two lines

Task 1 Task 2 Task 3 Task 4

Eva 7 6 0 10

Fred 2 0 3 0

George 0 3 0 2

Holly 0 12 1 5

Now return to Step 2.

Allocation problems - The Hungarian Algorithm

Step 2: Cover the zero elements with the minimum number of lines.

Again, look for rows and columns containing more than one zero. Here, the Task 1 and Task 3 columns and Fred’s row each have two zeros. Covering these with lines covers all the zeros.

Task 1 Task 2 Task 3 Task 4

Eva 7 6 0 10

Fred 2 0 3 0

George 0 3 0 2

Holly 0 12 1 5

Three lines have been used, so this array still does not give the optimal allocation.

Allocation problems - The Hungarian Algorithm

Step 3: Augment the elements.

The smallest uncovered element is 2 (in square G4).

Task 1 Task 2 Task 3 Task 4

Eva 7 6 0 10

Fred 2 0 3 0

George 0 3 0 2

Holly 0 12 1 5

Subtract 2 from all uncovered elementsand add 2 to all elements covered by two lines.

Task 1 Task 2 Task 3 Task 4

Eva 7 4 0 10

Fred 2 0 3 0

George 0 3 0 2

Holly 0 12 1 5

Task 1 Task 2 Task 3 Task 4

Eva 7 4 0 8

Fred 2 0 3 0

George 0 3 0 2

Holly 0 12 1 5

Task 1 Task 2 Task 3 Task 4

Eva 7 4 0 8

Fred 2 0 3 0

George 0 1 0 2

Holly 0 12 1 5

Task 1 Task 2 Task 3 Task 4

Eva 7 4 0 8

Fred 2 0 3 0

George 0 1 0 0

Holly 0 12 1 5

Task 1 Task 2 Task 3 Task 4

Eva 7 4 0 8

Fred 2 0 3 0

George 0 1 0 0

Holly 0 10 1 5

Task 1 Task 2 Task 3 Task 4

Eva 7 4 0 8

Fred 2 0 3 0

George 0 1 0 0

Holly 0 10 1 3

Task 1 Task 2 Task 3 Task 4

Eva 7 4 0 8

Fred 4 0 3 0

George 0 1 0 0

Holly 0 10 1 3

Task 1 Task 2 Task 3 Task 4

Eva 7 4 0 8

Fred 4 0 5 0

George 0 1 0 0

Holly 0 10 1 3

Allocation problems - The Hungarian Algorithm

Step 3: Augment the elements.

The smallest uncovered element is 2 (in square G4).

Subtract 2 from all uncovered elementsand add 2 to all elements covered by two lines.

Task 1 Task 2 Task 3 Task 4

Eva 7 4 0 8

Fred 4 0 5 0

George 0 1 0 0

Holly 0 10 1 3

Now return to Step 2.

Allocation problems - The Hungarian Algorithm

Step 2: Cover the zero elements with the minimum number of lines.

In this case, it is not possible to cover the zero elements with less than four lines.

Since four lines are required for this 4 by 4 array, the optimal allocation can now be found from this array. Now go to Step 4.

Task 1 Task 2 Task 3 Task 4

Eva 7 4 0 8

Fred 4 0 5 0

George 0 1 0 0

Holly 0 10 1 3

There are several different ways of covering them with four lines.

Allocation problems - The Hungarian Algorithm

Step 4: Identify the maximal matching using zero elements.

Look for rows and columns containing just one zero.

Task 1 Task 2 Task 3 Task 4

Eva 7 4 0 8

Fred 4 0 5 0

George 0 1 0 0

Holly 0 10 1 3

There is just one zero in Eva’s row and Holly’s row,and there is just one zero in the Task 2 column.

Task 1 Task 2 Task 3 Task 4

Eva 7 4 0 8

Fred 4 0 5 0

George 0 1 0 0

Holly 0 10 1 3

Task 1 Task 2 Task 3 Task 4

Eva 7 4 0 8

Fred 4 0 5 0

George 0 1 0 0

Holly 0 10 1 3

Task 1 Task 2 Task 3 Task 4

Eva 7 4 0 8

Fred 4 0 5 0

George 0 1 0 0

Holly 0 10 1 3

This means that square G4 must be the fourth zero selected.

Allocation problems - The Hungarian Algorithm

Step 4: Identify the maximal matching using zero elements.

Task 1 Task 2 Task 3 Task 4

Eva 7 4 0 8

Fred 4 0 5 0

George 0 1 0 0

Holly 0 10 1 3

From the array, Eva is allocated Task 3, Fred is allocated Task 2, George is allocated Task 4, and Holly is allocated Task 1.

Going back to the original array gives the times for each task.

Task 1 Task 2 Task 3 Task 4

Eva 35 24 17 42

Fred 33 21 23 35

George 27 20 16 33

Holly 29 31 19 38

Total time required = 17 + 21 + 33 + 29 = 100.