Hungarian algorithm: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
Line 1:
The '''Hungarian''' or '''Munkres''' algorithm is a method for solving the asignmentlinear problem[[Assignment_problem]]: Given <math>n</math> workers and tasks, and an <math>N x N</math> matrix containing the cost of assigning each worker to a task, find the cost minimizing assigment. The algorithm works in polynomial time.
 
===The Algorithm= In General==
The algorithm works by increasing the number of zeros in the matrix and searching for a set of starred zeros, one in every row and column. Zeros are primed, starred, or neither during the algorithm. If there are insufficent zeros a quick [[Gaussian elimination]] adds more. If there are not enough starred zeros, the primed zeros are starred and the starred zeros primed. Primed zeros are zeros in a column without any more zeros, which, because they are in the same row as another zero were not starred.
 
==Solving an assignment problem==
 
First the problem is written in the form of a matrix as given below
 
:<math>\begin{bmatrix}
a1 & a2 & a3 & a4\\
b1 & b2 & b3 & b4\\
c1 & c2 & c3 & c4\\
d1 & d2 & d3 & d4\end{bmatrix}</math>
 
Where a,b,c and d are the agents who have to perform tasks 1,2,3 and 4. a1,a2,a3,a4 denote the penalties incurred when a does task 1,2,3,4 respectively. Same hold true for the other symbols as well. Note that the matrix is a square matrix[this has to be so since each agent can perform only one task].
 
Then we perform row operations on the matrix. To do this, the lowest of all ai [i belonging to 1-4] is taken and is subtracted from the other elements in that row. This will lead to at least one zero in that row [We get multiple zeros when there are two equal elements which also happen to be the lowest in that row]. This procedure is repeated for all rows. We now have a matrix with at least one zero per row. Now we try to assign tasks to agents such that each agent is doing only one task and the penalty incurred in each case is zero. This is illustrated below.
 
'''Note-Matrix notation hasnt been used since formatting is not possible with that'''
 
 
0 &nbsp;&nbsp;&nbsp; a2' &nbsp; 0'&nbsp;&nbsp; a4' <br/>
b1' &nbsp; b2' &nbsp; b3' &nbsp; 0' <br/>
0' &nbsp;&nbsp; c2' &nbsp; c3' &nbsp; c4' <br/>
d1' &nbsp; 0' &nbsp;&nbsp; d3'&nbsp; d4' <br/>
 
The zeros that are indicated as 0' are the assigned tasks.
 
In some cases it may turn out that the above matrix cannot be used for assigning.
 
0 &nbsp;&nbsp;&nbsp; a2' &nbsp; a3'&nbsp; a4' <br/>
b1' &nbsp; b2' &nbsp; b3' &nbsp; 0 <br/>
0 &nbsp;&nbsp;&nbsp; c2' &nbsp; c3' &nbsp; c4' <br/>
d1' &nbsp; 0 &nbsp;&nbsp;&nbsp; d3'&nbsp; d4' <br/>
 
In the above case no assignment can be made. Note that task 1 is done efficiently by both agent a and c. Both cant be assigned the same task. Also note that no one does task 3 efficiently.
To overcome this, we repeat the above procedure for all columns and then check if an assignment is possible. In most situations this will give the result, but if it is still not possible to assign then the procedure described below must be followed.
 
Initially assign as many tasks as possible then do the following[assign tasks in rows 2,3 and 4]-
 
 
0 &nbsp;&nbsp;&nbsp; a2' &nbsp; a3'&nbsp; a4' <br/>
b1' &nbsp; b2' &nbsp; b3' &nbsp; 0' <br/>
0' &nbsp;&nbsp; c2' &nbsp; c3' &nbsp; c4' <br/>
d1' &nbsp; 0' &nbsp;&nbsp; d3'&nbsp; d4' <br/>
 
Mark all rows having no assignments [row 1]. Then mark all columns having zeros in that row [column 1]. Then mark all rows having assignments in the given column [row 3]. Then mark all columns having assignments in the given rows. Repeat this till a closed loop is obtained.
 
&times;
 
0 &nbsp;&nbsp;&nbsp; a2' &nbsp; a3' &nbsp; a4' &nbsp;&nbsp; &times; <br/>
b1' &nbsp; b2' &nbsp; b3' &nbsp; 0' &nbsp;&nbsp; <br/>
0' &nbsp;&nbsp; c2' &nbsp; c3' &nbsp; c4' &nbsp;&nbsp; &times; <br/>
d1' &nbsp; 0' &nbsp;&nbsp; d3' &nbsp; d4' &nbsp; <br/>
 
Now draw lines through all marked columns and unmarked rows.
 
<s>0</s> &nbsp;&nbsp;&nbsp; a2' &nbsp; a3' &nbsp; a4' <br/>
<s>b1' &nbsp; b2' &nbsp; b3' &nbsp; 0'&nbsp;&nbsp;</s> <br/>
<s>0'</s> &nbsp;&nbsp; c2' &nbsp; c3' &nbsp; c4' <br/>
<s>d1' &nbsp; 0' &nbsp;&nbsp; d3' &nbsp; d4'</s> <br/>
 
'''Note-Since a vertical line cannot be drawn a horizontal line is used to cut the 1st column.'''
 
From the elements that are left, find the lowest value. Subtract this from all elements that are not struck. Add this to elements that are present at the intersection of two lines. Leave other elements unchanged. Now assign the tasks using above rules. Repeat the procedure till an assignment is possible.
 
===External links===
[[http://www.arcavia.com/kyle/BLOG/arc20060101.htm#BlogID25]] A Java implementation
[[http://www.netlib.org/utk/lsi/pcwLSI/text/node220.html Munkres Algorithm]] A description of serial and parallel implementations.