the hungarian algorithm – non-square arrays the hungarian algorithm for non-square arrays if an...

18
The Hungarian Algorithm – Non-square arrays The Hungarian algorithm for non- square arrays If an array is not square, start by adding a dummy row or column to make the array square. Fill this with the largest number in the existing array. Then apply the standard 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

Upload: phoebe-jefferson

Post on 19-Dec-2015

232 views

Category:

Documents


0 download

TRANSCRIPT

The Hungarian Algorithm – Non-square arrays

The Hungarian algorithm for non-square arrays

If an array is not square, start by adding a dummy row or column to make the array square. Fill this with the largest number in the existing array.

Then apply the standard Hungarian algorithm:

Step 1 Reduce the array by both row and column subtractionsStep 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.

ExampleA company has five representatives availiable on a particular day. Four representatives are needed to attend meetings in different areas. The distance each representative would need to drive from home to attend each of the meetings is shown in the table below. Use the Hungarian algorithm to find out which representative should visit each area in order to keep the total distance travelled as low as possible.

The Hungarian Algorithm – Non-square arrays

Area 1 Area 2 Area 3 Area 4

Ian 18 11 16 20

Jane 14 19 26 18

Kate 21 23 35 29

Lucy 32 27 21 17

Mike 16 15 28 25

This array has five rows and four columns, so first you need to add a dummy column to give a square array.

The largest element in the array is 35, so each element in the dummy column is set to be 35.

The Hungarian Algorithm – Non-square arrays

Area 1 Area 2 Area 3 Area 4

Ian 18 11 16 20

Jane 14 19 26 18

Kate 21 23 35 29

Lucy 32 27 21 17

Mike 16 15 28 25

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 18 11 16 20

Jane 14 19 26 18

Kate 21 23 35 29

Lucy 32 27 21 17

Mike 16 15 28 25

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 18 11 16 20 35

Jane 14 19 26 18 35

Kate 21 23 35 29 35

Lucy 32 27 21 17 35

Mike 16 15 28 25 35

Because the problem is a minimisation problem, we have used the largest element so that it does not occur in the solution

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 Ian’s row is 11, so subtract 11 from each element in the row.

The Hungarian Algorithm – Non-square arrays

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 18 11 16 20 35

Jane 14 19 26 18 35

Kate 21 23 35 29 35

Lucy 32 27 21 17 35

Mike 16 15 28 25 35

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 18 11 16 20 35

Jane 14 19 26 18 35

Kate 21 23 35 29 35

Lucy 32 27 21 17 35

Mike 16 15 28 25 35

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 5 9 24

Jane 14 19 26 18 35

Kate 21 23 35 29 35

Lucy 32 27 21 17 35

Mike 16 15 28 25 35

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 Jane’s row is 14, so subtract 14 from each element in the row.

The Hungarian Algorithm – Non-square arrays

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 5 9 24

Jane 14 19 26 18 35

Kate 21 23 35 29 35

Lucy 32 27 21 17 35

Mike 16 15 28 25 35

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 5 9 24

Jane 14 19 26 18 35

Kate 21 23 35 29 35

Lucy 32 27 21 17 35

Mike 16 15 28 25 35

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 5 9 24

Jane 0 5 12 4 21

Kate 21 23 35 29 35

Lucy 32 27 21 17 35

Mike 16 15 28 25 35

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 Kate’s row is 21, so subtract 21 from each element in this row.

The Hungarian Algorithm – Non-square arrays

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 5 9 24

Jane 0 5 12 4 21

Kate 21 23 35 29 35

Lucy 32 27 21 17 35

Mike 16 15 28 25 35

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 5 9 24

Jane 0 5 12 4 21

Kate 21 23 35 29 35

Lucy 32 27 21 17 35

Mike 16 15 28 25 35

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 5 9 24

Jane 0 5 12 4 21

Kate 0 2 14 8 14

Lucy 32 27 21 17 35

Mike 16 15 28 25 35

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 Lucy’s row is 17, so subtract 17 from each element in this row.

The Hungarian Algorithm – Non-square arrays

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 5 9 24

Jane 0 5 12 4 21

Kate 0 2 14 8 14

Lucy 32 27 21 17 35

Mike 16 15 28 25 35

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 5 9 24

Jane 0 5 12 4 21

Kate 0 2 14 8 14

Lucy 32 27 21 17 35

Mike 16 15 28 25 35

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 5 9 24

Jane 0 5 12 4 21

Kate 0 2 14 8 14

Lucy 15 10 4 0 18

Mike 16 15 28 25 35

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 Mike’s row is 15, so subtract 15 from each element in this row.

The Hungarian Algorithm – Non-square arrays

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 5 9 24

Jane 0 5 12 4 21

Kate 0 2 14 8 14

Lucy 15 10 4 0 18

Mike 16 15 28 25 35

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 5 9 24

Jane 0 5 12 4 21

Kate 0 2 14 8 14

Lucy 15 10 4 0 18

Mike 16 15 28 25 35

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 5 9 24

Jane 0 5 12 4 21

Kate 0 2 14 8 14

Lucy 15 10 4 0 18

Mike 1 0 13 10 20

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 5 9 24

Jane 0 5 12 4 21

Kate 0 2 14 8 14

Lucy 15 10 4 0 18

Mike 1 0 13 10 20

Allocation problems - The Hungarian Algorithm

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

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.

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

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.

Columns 1, 2 and 4 already contain zeros, so cannot be reduced.

The Hungarian Algorithm – Non-square arrays

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 5 9 24

Jane 0 5 12 4 21

Kate 0 2 14 8 14

Lucy 15 10 4 0 18

Mike 1 0 13 10 20

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 5 9 24

Jane 0 5 12 4 21

Kate 0 2 14 8 14

Lucy 15 10 4 0 18

Mike 1 0 13 10 20

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 number in column 3 is 4, so subtract 4 from each element in this column.

The Hungarian Algorithm – Non-square arrays

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 5 9 24

Jane 0 5 12 4 21

Kate 0 2 14 8 14

Lucy 15 10 4 0 18

Mike 1 0 13 10 20

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 5 9 24

Jane 0 5 12 4 21

Kate 0 2 14 8 14

Lucy 15 10 4 0 18

Mike 1 0 13 10 20

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 1 9 24

Jane 0 5 8 4 21

Kate 0 2 10 8 14

Lucy 15 10 0 0 18

Mike 1 0 9 10 20

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 number in column 5 is 14, so subtract 14 from each element in this column.

The Hungarian Algorithm – Non-square arrays

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 1 9 24

Jane 0 5 8 4 21

Kate 0 2 10 8 14

Lucy 15 10 0 0 18

Mike 1 0 9 10 20

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 1 9 24

Jane 0 5 8 4 21

Kate 0 2 10 8 14

Lucy 15 10 0 0 18

Mike 1 0 9 10 20

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 1 9 10

Jane 0 5 8 4 7

Kate 0 2 10 8 0

Lucy 15 10 0 0 4

Mike 1 0 9 10 6

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 1 9 10

Jane 0 5 8 4 7

Kate 0 2 10 8 0

Lucy 15 10 0 0 4

Mike 1 0 9 10 6

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, Kate’s row, Lucy’s row and column 2 all contain two zeros, so cover these with lines.

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

Four lines have been used for this 5 by 5 array, so this array does not give the optimal allocation.

The Hungarian Algorithm – Non-square arrays

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 1 9 10

Jane 0 5 8 4 7

Kate 0 2 10 8 0

Lucy 15 10 0 0 4

Mike 1 0 9 10 6

The Hungarian Algorithm – Non-square arrays

Step 3: Augment the elements

The smallest uncovered element is 1.

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

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 6 0 0 8 9

Jane 0 5 8 4 7

Kate 0 2 10 8 0

Lucy 15 10 0 0 4

Mike 0 0 8 9 5

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 6 0 0 8 9

Jane 0 6 8 4 7

Kate 0 3 10 8 0

Lucy 15 11 0 0 4

Mike 0 0 8 9 5

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 7 0 1 9 10

Jane 0 5 8 4 7

Kate 0 2 10 8 0

Lucy 15 10 0 0 4

Mike 1 0 9 10 6

The Hungarian Algorithm – Non-square arrays

Step 3: Augment the elements

The smallest uncovered element is 1.

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

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 6 0 0 8 9

Jane 0 5 8 4 7

Kate 0 2 10 8 0

Lucy 15 10 0 0 4

Mike 0 0 8 9 5

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 6 0 0 8 9

Jane 0 6 8 4 7

Kate 0 3 10 8 0

Lucy 15 11 0 0 4

Mike 0 0 8 9 5

Now return to Step 2.

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 6 0 0 8 9

Jane 0 6 8 4 7

Kate 0 3 10 8 0

Lucy 15 11 0 0 4

Mike 0 0 8 9 5

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 five lines.

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

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

The Hungarian Algorithm – Non-square arrays

Step 4: Identify the maximal matching using zero elements.

Look for rows and columns containing just one zero.

There is just one zero in Jane’s row,and there is just one zero in column 4 and in the dummy column.This means that Mike must be matched with Area 2, and Ian with Area 3.

The Hungarian Algorithm – Non-square arrays

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 6 0 0 8 9

Jane 0 6 8 4 7

Kate 0 3 10 8 0

Lucy 15 11 0 0 4

Mike 0 0 8 9 5

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 6 0 0 8 9

Jane 0 6 8 4 7

Kate 0 3 10 8 0

Lucy 15 11 0 0 4

Mike 0 0 8 9 5

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 6 0 0 8 9

Jane 0 6 8 4 7

Kate 0 3 10 8 0

Lucy 15 11 0 0 4

Mike 0 0 8 9 5

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 6 0 0 8 9

Jane 0 6 8 4 7

Kate 0 3 10 8 0

Lucy 15 11 0 0 4

Mike 0 0 8 9 5

Step 4: Identify the maximal matching using zero elements.

From the array, Ian is allocated Area 3, Jane is allocated Area 1, Lucy is allocated Area 4 and Mike is allocated Area 2.

Going back to the original array gives the distances for each person.

Total distance to be travelled = 16 + 14 + 17 + 15 = 62.

The Hungarian Algorithm – Non-square arrays

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 6 0 0 8 9

Jane 0 6 8 4 7

Kate 0 3 10 8 0

Lucy 15 11 0 0 4

Mike 0 0 8 9 5

Kate is not required.

Area 1 Area 2 Area 3 Area 4 Dummy

Ian 18 11 16 20 35

Jane 14 19 26 18 35

Kate 21 23 35 29 35

Lucy 32 27 21 17 35

Mike 16 15 28 25 35