Hungarian algorithm
From Wikipedia, the free encyclopedia
The Hungarian algorithm is a combinatorial optimization algorithm which solves assignment problems in polynomial time (O(n3)). The first version, known as the Hungarian method, was invented and published by Harold Kuhn in 1955. This was revised by James Munkres in 1957, and has been known since as the Hungarian algorithm, the Munkres assignment algorithm, or the Kuhn-Munkres algorithm.
The algorithm developed by Kuhn was largely based on the earlier works of two other Hungarian mathematicians: Dénes König and Jenő Egerváry. The great advantage of Kuhn’s method is that it is strongly polynomial (see Computational complexity theory for details). The main innovation of the algorithm was to combine two separate parts in Egerváry’s proof into one.
Contents |
[edit] Modeling
The algorithm models an assignment problem as a n×m cost matrix, where each element represents the cost of assigning the ith worker to the jth job. 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 so that minimization of its elements results in maximizing the original cost values. In an infinite-cost problem, the initial cost matrix can be re-modelled by subtracting every element of each line from the maximum value of the element of that line (or column respectively). In a finite-cost problem, all the elements are subtracted from the maximum value of the whole matrix.
[edit] Algorithm
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 insufficient 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.
The procedure of obtaining the prime zeros can be implemented in a flow network, and automated by means of the Ford-Fulkerson algorithm. The n×m matrix is transformed in a G = (U,V) bipartite graph, with I/O capacity equal to 1. Each arc joining the nodes of the flow network represents a zero in the cost matrix. After the maximum flow is obtained, the affected arcs represent the prime zeros. The minimum cut obtained by Ford-Fulkerson automates the process of marking the independent zeros. Each node that gets "cut" on the max-flow graph represents a marked column or line on the cost matrix. In the Hungarian method, assignment can be easily found on the cost matrix, yet the expansion into a max-flow sub-problem is a useful method in more complex scenarios.
[edit] A minimization problem
Given n workers and tasks, and an n×n matrix containing the cost of assigning each worker to a task, find the cost minimizing assignment.
First the problem is written in the form of a matrix as given below
where a, b, c and d are the workers 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. The matrix is square: 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 has not been used since formatting is not possible with that
0 a2' 0' a4'
b1' b2' b3' 0'
0' c2' c3' c4'
d1' 0' d3' d4'
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 a2' a3' a4'
b1' b2' b3' 0
0 c2' c3' c4'
d1' 0 d3' d4'
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 a2' a3' a4'
b1' b2' b3' 0'
0' c2' c3' c4'
d1' 0' d3' d4'
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.
×
0 a2' a3' a4' ×
b1' b2' b3' 0'
0' c2' c3' c4' ×
d1' 0' d3' d4'
Now draw lines through all marked columns and unmarked rows.
0 a2' a3' a4'
b1' b2' b3' 0'
0' c2' c3' c4'
d1' 0' d3' d4'
Note-Since a vertical line cannot be drawn horizontal lines are 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.
Basically you find the second minimum cost among the two rows. The procedure is repeated until you are able to distinguish among the workers in terms of least cost.
[edit] Bibliography
- Harold W. Kuhn, "The Hungarian Method for the assignment problem", Naval Research Logistic Quarterly, 2:83-97, 1955. Kuhn's original publication.
- Harold W. Kuhn, "Variants of the Hungarian method for assignment problems", Naval Research Logistic Quarterly, 3: 253-258, 1956.
- J. Munkres, "Algorithms for the Assignment and Transportation Problems", Journal of the Society of Industrial and Applied Mathematics, 5(1):32-38, 1957 March.
- M. Fischetti, "Lezioni di Ricerca Operativa", Edizioni Libreria Progetto Padova, Italia, 1995.
- R. Ahuja, T. Magnanti, J. Orlin, "Network Flows", Prentice Hall, 1993.
[edit] External links
- R. A. Pilgrim, Munkres' Assignment Algorithm. Modified for Rectangular Matrices, Course notes, Murray State University.
- Mike Dawes, The Optimal Assignment Problem, Course notes, University of Western Ontario.
- On Kuhn's Hungarian Method - A tribute from Hungary, Andras Frank, Egrervary Research Group, Pazmany P. setany 1/C, H1117, Budapest, Hungary.
[edit] Implementations
- Online interactive implementation
- Serial and parallel implementations.
- Petr Tichavský, maxmatching, Matlab implementation.
- Perl implementation
- Lisp implementation
- C++ implementation