# Hungarian Algorithm Assignment Problem In Operations

## The Hungarian algorithm: An example

We consider an example where four jobs (J1, J2, J3, and J4) need to be executed by four workers (W1, W2, W3, and W4), one job per worker. The matrix below shows the cost of assigning a certain worker to a certain job. The objective is to minimize the total cost of the assignment.

J1 | J2 | J3 | J4 | |

W1 | 82 | 83 | 69 | 92 |

W2 | 77 | 37 | 49 | 92 |

W3 | 11 | 69 | 5 | 86 |

W4 | 8 | 9 | 98 | 23 |

Below we will explain the Hungarian algorithm using this example. Note that a general description of the algorithm can be found here.

**Step 1: Subtract row minima**

We start with subtracting the row minimum from each row. The smallest element in the first row is, for example, 69. Therefore, we substract 69 from each element in the first row. The resulting matrix is:

J1 | J2 | J3 | J4 | ||

W1 | 13 | 14 | 0 | 23 | (-69) |

W2 | 40 | 0 | 12 | 55 | (-37) |

W3 | 6 | 64 | 0 | 81 | (-5) |

W4 | 0 | 1 | 90 | 15 | (-8) |

**Step 2: Subtract column minima**

Similarly, we subtract the column minimum from each column, giving the following matrix:

J1 | J2 | J3 | J4 | |

W1 | 13 | 14 | 0 | 8 |

W2 | 40 | 0 | 12 | 40 |

W3 | 6 | 64 | 0 | 66 |

W4 | 0 | 1 | 90 | 0 |

(-15) |

**Step 3: Cover all zeros with a minimum number of lines**

We will now determine the minimum number of lines (horizontal or vertical) that are required to cover all zeros in the matrix. All zeros can be covered using 3 lines:

J1 | J2 | J3 | J4 | ||

W1 | 13 | 14 | 0 | 8 | |

W2 | 40 | 0 | 12 | 40 | x |

W3 | 6 | 64 | 0 | 66 | |

W4 | 0 | 1 | 90 | 0 | x |

x |

Because the number of lines required (3) is lower than the size of the matrix (*n*=4), we continue with Step 4.

**Step 4: Create additional zeros**

First, we find that the smallest uncovered number is 6. We subtract this number from all uncovered elements and add it to all elements that are covered twice. This results in the following matrix:

J1 | J2 | J3 | J4 | |

W1 | 7 | 8 | 0 | 2 |

W2 | 40 | 0 | 18 | 40 |

W3 | 0 | 58 | 0 | 60 |

W4 | 0 | 1 | 96 | 0 |

Now we return to Step 3.

**Step 3: Cover all zeros with a minimum number of lines**

Again, We determine the minimum number of lines required to cover all zeros in the matrix. Now there are 4 lines required:

J1 | J2 | J3 | J4 | ||

W1 | 7 | 8 | 0 | 2 | x |

W2 | 40 | 0 | 18 | 40 | x |

W3 | 0 | 58 | 0 | 60 | x |

W4 | 0 | 1 | 96 | 0 | x |

Because the number of lines required (4) equals the size of the matrix (*n*=4), an optimal assignment exists among the zeros in the matrix. Therefore, the algorithm stops.

**The optimal assignment**

The following zeros cover an optimal assignment:

J1 | J2 | J3 | J4 | |

W1 | 7 | 8 | 0 | 2 |

W2 | 40 | 0 | 18 | 40 |

W3 | 0 | 58 | 0 | 60 |

W4 | 0 | 1 | 96 | 0 |

This corresponds to the following optimal assignment in the original cost matrix:

J1 | J2 | J3 | J4 | |

W1 | 82 | 83 | 69 | 92 |

W2 | 77 | 37 | 49 | 92 |

W3 | 11 | 69 | 5 | 86 |

W4 | 8 | 9 | 98 | 23 |

Thus, worker 1 should perform job 3, worker 2 job 2, worker 3 job 1, and worker 4 should perform job 4. The total cost of this optimal assignment is to 69 + 37 + 11 + 23 = 140.

Solve your own problem online

The **assignment problem** is one of the fundamental combinatorial optimization problems in the branch of optimization or operations research in mathematics. It consists of finding a maximum weight matching (or minimum weight perfect matching) in a weightedbipartite graph.

In its most general form, the problem is as follows:

- The problem instance has a number of
*agents*and a number of*tasks*. Any agent can be assigned to perform any task, incurring some*cost*that may vary depending on the agent-task assignment. It is required to perform all tasks by assigning exactly one agent to each task and exactly one task to each agent in such a way that the*total cost*of the assignment is minimized.

If the numbers of agents and tasks are equal and the total cost of the assignment for all tasks is equal to the sum of the costs for each agent (or the sum of the costs for each task, which is the same thing in this case), then the problem is called the *linear assignment problem*. Commonly, when speaking of the *assignment problem* without any additional qualification, then the *linear assignment problem* is meant.

## Algorithms and generalizations[edit]

The Hungarian algorithm is one of many algorithms that have been devised that solve the linear assignment problem within time bounded by a polynomial expression of the number of agents. Other algorithms include adaptations of the primal simplex algorithm, and the auction algorithm.

The assignment problem is a special case of the transportation problem, which is a special case of the minimum cost flow problem, which in turn is a special case of a linear program. While it is possible to solve any of these problems using the simplex algorithm, each specialization has more efficient algorithms designed to take advantage of its special structure.

When a number of agents and tasks is very large, a parallel algorithm with randomization can be applied. The problem of finding minimum weight maximum matching can be converted to finding a minimum weight perfect matching. A bipartite graph can be extended to a complete bipartite graph by adding artificial edges with large weights. These weights should exceed the weights of all existing matchings to prevent appearance of artificial edges in the possible solution. As shown by Mulmuley, Vazirani & Vazirani (1987), the problem of minimum weight perfect matching is converted to finding minors in the adjacency matrix of a graph. Using the isolation lemma, a minimum weight perfect matching in a graph can be found with probability at least ½. For a graph with n vertices, it requires time.

## Example[edit]

Suppose that a taxi firm has three taxis (the agents) available, and three customers (the tasks) wishing to be picked up as soon as possible. The firm prides itself on speedy pickups, so for each taxi the "cost" of picking up a particular customer will depend on the time taken for the taxi to reach the pickup point. The solution to the assignment problem will be whichever combination of taxis and customers results in the least total cost.

However, the assignment problem can be made rather more flexible than it first appears. In the above example, suppose that there are four taxis available, but still only three customers. Then a fourth dummy task can be invented, perhaps called "sitting still doing nothing", with a cost of 0 for the taxi assigned to it. The assignment problem can then be solved in the usual way and still give the best solution to the problem.

Similar adjustments can be done in order to allow more tasks than agents, tasks to which multiple agents must be assigned (for instance, a group of more customers than will fit in one taxi), or maximizing profit rather than minimizing cost.

## Formal mathematical definition[edit]

The formal definition of the **assignment problem** (or **linear assignment problem**) is

- Given two sets,
*A*and*T*, of equal size, together with a weight function*C*:*A*×*T*→**R**. Find a bijection*f*:*A*→*T*such that the cost function:

is minimized.

Usually the weight function is viewed as a square real-valued matrix*C*, so that the cost function is written down as:

The problem is "linear" because the cost function to be optimized as well as all the constraints contain only linear terms.

The problem can be expressed as a standard linear program with the objective function

subject to the constraints

The variable represents the assignment of agent to task , taking value 1 if the assignment is done and 0 otherwise. This formulation allows also fractional variable values, but there is always an optimal solution where the variables take integer values. This is because the constraint matrix is totally unimodular. The first constraint requires that every agent is assigned to exactly one task, and the second constraint requires that every task is assigned exactly one agent.