# Assignment Problem Hungarian Method C Code Tutorial

Q1.

Machines

A B C D E

Jobs 1[ 4 3 6 2 7

2[ 10 12 11 14 16

3[ 4 3 2 1 5

4[ 8 7 6 9 6]**Soln:****STEP I:**

The given matrix is unbalanced matrix.Hence add a row of "0"

[ 4 3 6 2 7

10 12 11 14 16

4 3 2 1 5

8 7 6 9 6

0 0 0 0 0]**STEP II:**

Process row wise.

[ ~~2~~ 1 4 ~~0~~ 5

~~ 0 ~~ 2 1 ~~4~~ 6

~~3~~ 2 1 ~~0~~ 4~~ 2 1 0 3 1~~~~ 0 0 0 0 0~~]

Number of straight lines N=4

Order of matrix n=5

N<n

Therefore the smallest value in the previous matrix is "1" subtract from the remaining values and add in the intersecting points.

[~~ 2 0 3 0 4~~

~~0~~ 1 ~~0~~ ~~4~~ 5

~~ 3~~ 1 ~~ 0~~ ~~0~~ 3

~~3~~ 1 ~~ 0~~ ~~4~~ 1~~ 1 0 0 1 0~~]

Number of Straight Lines N =5

Order of Matrix n=5

N=n

Assign the jobs to machines.

A B C D E

1[ 2 0 3 ~~ 0 ~~ 4

2[ 0 1 ~~ 0 ~~ 4 5

3 [ 3 1 ~~ 0 ~~ 0 3

4 [3 1 0 4 1

5[ 1 ~~0~~ ~~ 0 ~~ 1 0]

1----->B ; 2-->A ; 3-->D; 4---> C ; 5-->E

Cost= 3+10+1+6+0

=Rs.20

An assignment problem can be easily solved by applying Hungarian method which consists of two phases. In the first phase, row reductions and column reductions are carried out. In the second phase, the solution is optimized on iterative basis.

*Phase 1*

*Step 0: *Consider the given matrix.*Step 1: *In a given problem, if the number of rows is not equal to the number of columns and vice versa, then add a dummy row or a dummy column. The assignment costs for dummy cells are always assigned as zero.*Step 2: *Reduce the matrix by selecting the smallest element in each row and subtract with other elements in that row.

*Phase 2:*

*Step 3*: Reduce the new matrix column-wise using the same method as given in step 2.*Step 4*: Draw minimum number of lines to cover all zeros.*Step 5*: If Number of lines drawn = order of matrix, then optimally is reached, so proceed to step 7. If optimally is not reached, then go to step 6.*Step 6: *Select the smallest element of the whole matrix, which is **NOT COVERED **by lines. Subtract this smallest element with all other remaining elements that are **NOT COVERED **by lines and add the element at the intersection of lines. Leave the elements covered by single line as it is. Now go to step 4.*Step 7: *Take any row or column which has a single zero and assign by squaring it. Strike off the remaining zeros, if any, in that row and column (X). Repeat the process until all the assignments have been made.*Step 8: *Write down the assignment results and find the minimum cost/time.

**Note: **While assigning, if there is no single zero exists in the row or column, choose any one zero and assign it. Strike off the remaining zeros in that column or row, and repeat the same for other assignments also. If there is no single zero allocation, it means multiple numbers of solutions exist. But the cost will remain the same for different sets of allocations.

** Example : **Assign the four tasks to four operators. The assigning costs are given in Table.

*Assignment Problem*

*Solution:*

*Step 1: *The given matrix is a square matrix and it is not necessary to add a dummy row/column*Step 2: *Reduce the matrix by selecting the smallest value in each row and subtracting from other values in that corresponding row. In row A, the smallest value is 13, row B is 15, row C is 17 and row D is 12. The row wise reduced matrix is shown in table below.

*Row-wise Reduction*

*Step 3: *Reduce the new matrix given in the following table by selecting the smallest value in

each column and subtract from other values in that corresponding column. In column 1, the smallest value is 0, column 2 is 4, column 3 is 3 and column 4 is 0. The column-wise reduction matrix is shown in the following table.

*Column-wise Reduction Matrix*

*Step 4: *Draw minimum number of lines possible to cover all the zeros in the matrix given in Table

*Matrix with all Zeros Covered*

The first line is drawn crossing row C covering three zeros, second line is drawn crossing column 4 covering two zeros and third line is drawn crossing column 1 (or row B) covering a single zero.*Step 5: *Check whether number of lines drawn is equal to the order of the matrix, i.e., 3 ≠ 4. Therefore optimally is not reached. Go to step 6.*Step 6: *Take the smallest element of the matrix that is not covered by single line, which is 3. Subtract 3 from all other values that are not covered and add 3 at the intersection of lines. Leave the values which are covered by single line. The following table shows the details.

*Subtracted or Added to Uncovered Values and Intersection Lines Respectively*

*Step 7: *Now, draw minimum number of lines to cover all the zeros and check for optimality. Here in table minimum number of lines drawn is 4 which are equal to the order of matrix. Hence optimality is reached.

*Optimality Matrix*

*Step 8: *Assign the tasks to the operators. Select a row that has a single zero and assign by squaring it. Strike off remaining zeros if any in that row or column. Repeat the assignment for other tasks. The final assignment is shown in table below.

*Final Assignment*

Therefore, optimal assignment is:

** Example : **Solve the following assignment problem shown in Table using Hungarian method. The matrix entries are processing time of each man in hours.

*Assignment Problem*

**Solution: **The row-wise reductions are shown in Table

*Row-wise Reduction Matrix*

The column wise reductions are shown in Table.

*Column-wise Reduction Matrix*

Matrix with minimum number of lines drawn to cover all zeros is shown in Table.

*Matrix will all Zeros Covered*

The number of lines drawn is 5, which is equal to the order of matrix. Hence optimality is reached. The optimal assignments are shown in Table.

*Optimal Assignment*

Therefore, the optimal solution is:

## 0 thoughts on “Assignment Problem Hungarian Method C Code Tutorial”

-->