# The Hungarian algorithm for non-square arrays

## Presentation on theme: "The Hungarian algorithm for non-square arrays"— Presentation transcript:

The Hungarian algorithm for non-square arrays
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 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.

The Hungarian Algorithm – Non-square arrays
Example A 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. Area 1 Area 2 Area 3 Area 4 Ian 18 11 16 20 Jane 14 19 26 Kate 21 23 35 29 Lucy 32 27 17 Mike 15 28 25

The Hungarian Algorithm – Non-square arrays
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. Because the problem is a minimisation problem, we have used the largest element so that it does not occur in the solution Area 1 Area 2 Area 3 Area 4 Dummy Ian 18 11 16 20 35 Jane 14 19 26 Kate 21 23 29 Lucy 32 27 17 Mike 15 28 25 Area 1 Area 2 Area 3 Area 4 Ian 18 11 16 20 Jane 14 19 26 Kate 21 23 35 29 Lucy 32 27 17 Mike 15 28 25 Area 1 Area 2 Area 3 Area 4 Dummy Ian 18 11 16 20 Jane 14 19 26 Kate 21 23 35 29 Lucy 32 27 17 Mike 15 28 25

The Hungarian Algorithm – Non-square arrays
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. Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 5 9 24 Jane 14 19 26 18 35 Kate 21 23 29 Lucy 32 27 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 Kate 21 23 29 Lucy 32 27 17 Mike 15 28 25 Area 1 Area 2 Area 3 Area 4 Dummy Ian 18 11 16 20 35 Jane 14 19 26 Kate 21 23 29 Lucy 32 27 17 Mike 15 28 25

The Hungarian Algorithm – Non-square arrays
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. Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 5 9 24 Jane 12 4 21 Kate 23 35 29 Lucy 32 27 17 Mike 16 15 28 25 Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 5 9 24 Jane 14 19 26 18 35 Kate 21 23 29 Lucy 32 27 17 Mike 16 15 28 25 Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 5 9 24 Jane 14 19 26 18 35 Kate 21 23 29 Lucy 32 27 17 Mike 16 15 28 25

The Hungarian Algorithm – Non-square arrays
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. Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 5 9 24 Jane 12 4 21 Kate 2 14 8 Lucy 32 27 17 35 Mike 16 15 28 25 Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 5 9 24 Jane 12 4 21 Kate 23 35 29 Lucy 32 27 17 Mike 16 15 28 25 Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 5 9 24 Jane 12 4 21 Kate 23 35 29 Lucy 32 27 17 Mike 16 15 28 25

The Hungarian Algorithm – Non-square arrays
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. Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 5 9 24 Jane 12 4 21 Kate 2 14 8 Lucy 15 10 18 Mike 16 28 25 35 Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 5 9 24 Jane 12 4 21 Kate 2 14 8 Lucy 32 27 17 35 Mike 16 15 28 25 Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 5 9 24 Jane 12 4 21 Kate 2 14 8 Lucy 32 27 17 35 Mike 16 15 28 25

The Hungarian Algorithm – Non-square arrays
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. Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 5 9 24 Jane 12 4 21 Kate 2 14 8 Lucy 15 10 18 Mike 1 13 20 Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 5 9 24 Jane 12 4 21 Kate 2 14 8 Lucy 15 10 18 Mike 16 28 25 35 Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 5 9 24 Jane 12 4 21 Kate 2 14 8 Lucy 15 10 18 Mike 16 28 25 35

Allocation problems - The Hungarian Algorithm
Step 1: Reduce the array by both row and column subtractions The minimum number of lines needed to cover all the zeros is 3, so the solution is not optimal. Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 5 9 24 Jane 12 4 21 Kate 2 14 8 Lucy 15 10 18 Mike 1 13 20 Now reduce the array by subtracting the minimum entry in each column from all the entries in the column.

The Hungarian Algorithm – Non-square arrays
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. Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 5 9 24 Jane 12 4 21 Kate 2 14 8 Lucy 15 10 18 Mike 1 13 20 Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 5 9 24 Jane 12 4 21 Kate 2 14 8 Lucy 15 10 18 Mike 1 13 20

The Hungarian Algorithm – Non-square arrays
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. Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 1 9 24 Jane 5 8 4 21 Kate 2 10 14 Lucy 15 18 Mike 20 Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 5 9 24 Jane 12 4 21 Kate 2 14 8 Lucy 15 10 18 Mike 1 13 20 Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 5 9 24 Jane 12 4 21 Kate 2 14 8 Lucy 15 10 18 Mike 1 13 20

The Hungarian Algorithm – Non-square arrays
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. Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 1 9 10 Jane 5 8 4 Kate 2 Lucy 15 Mike 6 Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 1 9 24 Jane 5 8 4 21 Kate 2 10 14 Lucy 15 18 Mike 20 Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 1 9 24 Jane 5 8 4 21 Kate 2 10 14 Lucy 15 18 Mike 20

The Hungarian Algorithm – Non-square arrays
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. Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 1 9 10 Jane 5 8 4 Kate 2 Lucy 15 Mike 6 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
Step 3: Augment the elements The smallest uncovered element is 1. Subtract 1 from all uncovered elements and add 1 to all elements covered by two lines Area 1 Area 2 Area 3 Area 4 Dummy Ian 6 8 9 Jane 4 7 Kate 3 10 Lucy 15 11 Mike 5 Area 1 Area 2 Area 3 Area 4 Dummy Ian 6 8 9 Jane 5 4 7 Kate 2 10 Lucy 15 Mike Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 1 9 10 Jane 5 8 4 Kate 2 Lucy 15 Mike 6

The Hungarian Algorithm – Non-square arrays
Step 3: Augment the elements The smallest uncovered element is 1. Subtract 1 from all uncovered elements and add 1 to all elements covered by two lines Area 1 Area 2 Area 3 Area 4 Dummy Ian 6 8 9 Jane 4 7 Kate 3 10 Lucy 15 11 Mike 5 Area 1 Area 2 Area 3 Area 4 Dummy Ian 6 8 9 Jane 5 4 7 Kate 2 10 Lucy 15 Mike Area 1 Area 2 Area 3 Area 4 Dummy Ian 7 1 9 10 Jane 5 8 4 Kate 2 Lucy 15 Mike 6 Now return to Step 2.

The Hungarian Algorithm – Non-square arrays
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. There are several different ways of covering them with five lines. Area 1 Area 2 Area 3 Area 4 Dummy Ian 6 8 9 Jane 4 7 Kate 3 10 Lucy 15 11 Mike 5 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.

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. Area 1 Area 2 Area 3 Area 4 Dummy Ian 6 8 9 Jane 4 7 Kate 3 10 Lucy 15 11 Mike 5 Area 1 Area 2 Area 3 Area 4 Dummy Ian 6 8 9 Jane 4 7 Kate 3 10 Lucy 15 11 Mike 5 Area 1 Area 2 Area 3 Area 4 Dummy Ian 6 8 9 Jane 4 7 Kate 3 10 Lucy 15 11 Mike 5 Area 1 Area 2 Area 3 Area 4 Dummy Ian 6 8 9 Jane 4 7 Kate 3 10 Lucy 15 11 Mike 5

The Hungarian Algorithm – Non-square arrays
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. Kate is not required. Going back to the original array gives the distances for each person. Area 1 Area 2 Area 3 Area 4 Dummy Ian 18 11 16 20 35 Jane 14 19 26 Kate 21 23 29 Lucy 32 27 17 Mike 15 28 25 Area 1 Area 2 Area 3 Area 4 Dummy Ian 6 8 9 Jane 4 7 Kate 3 10 Lucy 15 11 Mike 5 Total distance to be travelled = = 62.

Download ppt "The Hungarian algorithm for non-square arrays"

Similar presentations