Hungarian algorithm: Difference between revisions

Content deleted Content added
revision
Line 1:
The '''Hungarian''' or '''Munkres''' algorithm is a method for solving the linear [[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.
 
==Theory==
==The Algorithm In General==
 
The algorithm developped by Kuhn was largerly based on the earlier works of two other [[Hungarian]] mathematicians: Konig and Egervary. The great advantage of Kuhn’s method is that it is strongly polynomial. The main idea of the algorithm is that it combines two separate parts in Egervery’s proof (computing a deficient set and revising the current <math>\pi</math> ) into one.
 
==Modelisation==
 
The algorithm requires to modelise an assignment problem in the form of a NxM matrix, known as the cost matrix, in which each elemenent represents the cost of assigning one of the N workers to one of M jobs. By default the algorithm performs minimization on the elements of the matrix, hence in the case of a price-minimization problem, it is sufficient to begin [[Gaussian elimination]] to make zeros appear (at least one zero per line and per column). However in the case of a profit-maximization problem, the cost matrix needs to be modified in a way that the minimization of its elements will result to a maximization of the original cost values. In an infinite-cost problem, the initial cost matrix can be re-modelised by subtracting every element of each line from the maximum value of the element that line (or column respectively). In a finite-cost problem, the all elements are subtracted from the maximum value of the whole matrix.
 
==Algorithm [http://www.public.iastate.edu/~ddoty/HungarianAlgorithm.html]==
 
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.
 
:'''Step 0''': Create an nxm matrix called the cost matrix in which each element represents the cost of assigning one of n workers to one of m jobs. Rotate the matrix so that there are at least as many rows as columns and let k=min(n,m).
==Solving an assignment problem==
 
:'''Step 1''': For each row of the matrix, find the smallest element and subtract it from every element in its row. Go to Step 2.
 
:'''Step 2''': Find a zero (Z) in the resulting matrix. If there is no starred zero in its row or column, star Z. Repeat for each element in the matrix. Go to Step 3.
 
:'''Step 3''': Cover each column containing a starred zero. If K columns are covered, the starred zeros describe a complete set of unique assignments. In this case, Go to DONE, otherwise, Go to Step 4.
 
:'''Step 4''': Find a noncovered zero and prime it. If there is no starred zero in the row containing this primed zero, Go to Step 5. Otherwise, cover this row and uncover the column containing the starred zero. Continue in this manner until there are no uncovered zeros left. Save the smallest uncovered value and Go to Step 6.
 
:'''Step 5''': Construct a series of alternating primed and starred zeros as follows. Let Z0 represent the uncovered primed zero found in Step 4. Let Z1 denote the starred zero in the column of Z0 (if any). Let Z2 denote the primed zero in the row of Z1 (there will always be one). Continue until the series terminates at a primed zero that has no starred zero in its column. Unstar each starred zero of the series, star each primed zero of the series, erase all primes and uncover every line in the matrix. Return to Step 3.
 
:'''Step 6''': Add the value found in Step 4 to every element of each covered row, and subtract it from every element of each uncovered column. Return to Step 4 without altering any stars, primes, or covered lines.
 
:'''DONE''': Assignment pairs are indicated by the positions of the starred zeros in the cost matrix. If C(i,j) is a starred zero, then the element associated with row i is assigned to the element associated with column j.
 
==Example of a minimization problem==
 
First the problem is written in the form of a matrix as given below