HOME

TheInfoList



OR:

The Hungarian method is a
combinatorial optimization Combinatorial optimization is a subfield of mathematical optimization that consists of finding an optimal object from a finite set of objects, where the set of feasible solutions is discrete or can be reduced to a discrete set. Typical combi ...
algorithm In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algorithms are used as specificat ...
that solves the
assignment problem The assignment problem is a fundamental combinatorial optimization problem. 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 ta ...
in polynomial time and which anticipated later primal–dual methods. It was developed and published in 1955 by Harold Kuhn, who gave the name "Hungarian method" because the algorithm was largely based on the earlier works of two Hungarian mathematicians:
Dénes Kőnig Dénes Kőnig (September 21, 1884 – October 19, 1944) was a Hungarian mathematician of Jewish heritage who worked in and wrote the first textbook on the field of graph theory. Biography Kőnig was born in Budapest, the son of mathematician Gyu ...
and
Jenő Egerváry Jenő Elek Egerváry (April 16, 1891 – November 30, 1958) was a Hungarian mathematician. Biography Egerváry was born in Debrecen in 1891. In 1914, he received his doctorate at the Pázmány Péter University in Budapest, where he studied und ...
.Harold W. Kuhn, "The Hungarian Method for the assignment problem", ''
Naval Research Logistics Quarterly ''Naval Research Logistics'' is a peer-reviewed scientific journal that publishes papers in the field of logistics, especially those in the areas of operations research, applied statistics, and quantitative modeling. It was established in 19 ...
'', 2: 83–97, 1955. Kuhn's original publication.
Harold W. Kuhn, "Variants of the Hungarian method for assignment problems", ''Naval Research Logistics Quarterly'', 3: 253–258, 1956.
James Munkres James Raymond Munkres (born August 18, 1930) is a Professor Emeritus of mathematics at MIT and the author of several texts in the area of topology, including ''Topology'' (an undergraduate-level text), ''Analysis on Manifolds'', ''Elements of Alge ...
reviewed the algorithm in 1957 and observed that it is (strongly) polynomial.J. Munkres, "Algorithms for the Assignment and Transportation Problems", ''
Journal of the Society for Industrial and Applied Mathematics The ''SIAM Journal on Applied Mathematics'' is a peer-reviewed academic journal in applied mathematics published by the Society for Industrial and Applied Mathematics (SIAM), with Paul A. Martin (Colorado School of Mines) as its editor-in-chief. ...
'', 5(1):32–38, 1957 March.
Since then the algorithm has been known also as the Kuhn–Munkres algorithm or Munkres assignment algorithm. The
time complexity In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by t ...
of the original algorithm was O(n^4), however
Edmonds Edmonds may refer to: * Edmonds (surname), a surname (including a list of people with the surname) * Edmonds, Washington, a city in Washington, US ** Edmonds station (Washington), a passenger train station in Washington, US * Edmonds station (SkyTr ...
and
Karp Karp may refer to: Places * Karp, Podlaskie Voivodeship, in north-east Poland * Karp, Lublin Voivodeship, in east Poland People * Karp (surname) * Karp Khachvankyan (1923–1998), Armenian actor and director Other uses * KARP-FM, a radio s ...
, and independently Tomizawa noticed that it can be modified to achieve an O(n^3) running time. One of the most popular O(n^3) variants is the Jonker–Volgenant algorithm.
Ford Ford commonly refers to: * Ford Motor Company, an automobile manufacturer founded by Henry Ford * Ford (crossing), a shallow crossing on a river Ford may also refer to: Ford Motor Company * Henry Ford, founder of the Ford Motor Company * Ford F ...
and Fulkerson extended the method to general maximum flow problems in form of the Ford–Fulkerson algorithm. In 2006, it was discovered that
Carl Gustav Jacobi Carl Gustav Jacob Jacobi (; ; 10 December 1804 – 18 February 1851) was a German mathematician who made fundamental contributions to elliptic functions, dynamics, differential equations, determinants, and number theory. His name is occas ...
had solved the assignment problem in the 19th century, and the solution had been published posthumously in 1890 in Latin.


The problem


Example

In this simple example there are three workers: Paul, Dave, and Chris. One of them has to clean the bathroom, another sweep the floors and the third washes the windows, but they each demand different pay for the various tasks. The problem is to find the lowest-cost way to assign the jobs. The problem can be represented in a
matrix Matrix most commonly refers to: * ''The Matrix'' (franchise), an American media franchise ** ''The Matrix'', a 1999 science-fiction action film ** "The Matrix", a fictional setting, a virtual reality environment, within ''The Matrix'' (franchis ...
of the costs of the workers doing the jobs. For example: The Hungarian method, when applied to the above table, would give the minimum cost: this is $6, achieved by having Paul clean the bathroom, Dave sweep the floors, and Chris wash the windows.


Matrix formulation

In the matrix formulation, we are given a nonnegative ''n''×''n''
matrix Matrix most commonly refers to: * ''The Matrix'' (franchise), an American media franchise ** ''The Matrix'', a 1999 science-fiction action film ** "The Matrix", a fictional setting, a virtual reality environment, within ''The Matrix'' (franchis ...
, where the element in the ''i''-th row and ''j''-th column represents the cost of assigning the ''j''-th job to the ''i''-th worker. We have to find an assignment of the jobs to the workers, such that each job is assigned to one worker and each worker is assigned one job, such that the total cost of assignment is minimum. This can be expressed as permuting the rows and columns of a cost matrix ''C'' to minimize the
trace Trace may refer to: Arts and entertainment Music * ''Trace'' (Son Volt album), 1995 * ''Trace'' (Died Pretty album), 1993 * Trace (band), a Dutch progressive rock band * ''The Trace'' (album) Other uses in arts and entertainment * ''Trace'' ...
of a matrix: : \min_ (L C R) where ''L'' and ''R'' are
permutation matrices In mathematics, particularly in matrix theory, a permutation matrix is a square binary matrix that has exactly one entry of 1 in each row and each column and 0s elsewhere. Each such matrix, say , represents a permutation of elements and, when ...
. If the goal is to find the assignment that yields the ''maximum'' cost, the problem can be solved by negating the cost matrix ''C''.


Bipartite graph formulation

The algorithm can equivalently be described by formulating the problem using a bipartite graph. We have a
complete bipartite graph In the mathematical field of graph theory, a complete bipartite graph or biclique is a special kind of bipartite graph where every vertex of the first set is connected to every vertex of the second set..Electronic edition page 17. Graph theory i ...
G=(S, T; E) with worker vertices () and job vertices (), and each edge has a nonnegative cost c(i,j). We want to find a
perfect matching In graph theory, a perfect matching in a graph is a matching that covers every vertex of the graph. More formally, given a graph , a perfect matching in is a subset of edge set , such that every vertex in the vertex set is adjacent to exactl ...
with a minimum total cost.


The algorithm in terms of bipartite graphs

Let us call a function y: (S\cup T) \to \mathbb a potential if y(i)+y(j) \leq c(i, j) for each i \in S, j \in T. The ''value'' of potential is the sum of the potential over all vertices: \sum_ y(v). The cost of each perfect matching is at least the value of each potential: the total cost of the matching is the sum of costs of all edges; the cost of each edge is at least the sum of potentials of its endpoints; since the matching is perfect, each vertex is an endpoint of exactly one edge; hence the total cost is at least the total potential. The Hungarian method finds a perfect matching and a potential such that the matching cost equals the potential value. This proves that both of them are optimal. In fact, the Hungarian method finds a perfect matching of tight edges: an edge ij is called tight for a potential if y(i)+y(j) = c(i, j). Let us denote the subgraph of tight edges by G_y. The cost of a perfect matching in G_y (if there is one) equals the value of . During the algorithm we maintain a potential and an
orientation Orientation may refer to: Positioning in physical space * Map orientation, the relationship between directions on a map and compass directions * Orientation (housing), the position of a building with respect to the sun, a concept in building de ...
of G_y (denoted by \overrightarrow) which has the property that the edges oriented from to form a matching . Initially, is 0 everywhere, and all edges are oriented from to (so is empty). In each step, either we modify so that its value increases, or modify the orientation to obtain a matching with more edges. We maintain the invariant that all the edges of are tight. We are done if is a perfect matching. In a general step, let R_S \subseteq S and R_T \subseteq T be the vertices not covered by (so R_S consists of the vertices in with no incoming edge and R_T consists of the vertices in with no outgoing edge). Let be the set of vertices reachable in \overrightarrow from R_S by a directed path only following edges that are tight. This can be computed by
breadth-first search Breadth-first search (BFS) is an algorithm for searching a tree data structure for a node that satisfies a given property. It starts at the tree root and explores all nodes at the present depth prior to moving on to the nodes at the next de ...
. If R_T \cap Z is nonempty, then reverse the orientation of a directed path in \overrightarrow from R_S to R_T. Thus the size of the corresponding matching increases by 1. If R_T \cap Z is empty, then let :\Delta := \min \. is well defined because at least one such edge ij must exist whenever the matching is not yet of maximum possible size (see the following section); it is positive because there are no tight edges between Z \cap S and T \setminus Z. Increase by on the vertices of Z \cap S and decrease by on the vertices of Z \cap T. The resulting is still a potential, and although the graph G_y changes, it still contains (see the next subsections). We orient the new edges from to . By the definition of the set of vertices reachable from R_S increases (note that the number of tight edges does not necessarily increase). We repeat these steps until is a perfect matching, in which case it gives a minimum cost assignment. The running time of this version of the method is O(n^4): is augmented times, and in a phase where is unchanged, there are at most potential changes (since increases every time). The time sufficient for a potential change is O(n^2).


Proof that the algorithm makes progress

We must show that as long as the matching is not of maximum possible size, the algorithm is always able to make progress — that is, to either increase the number of matched edges, or tighten at least one edge. It suffices to show that at least one of the following holds at every step: * is of maximum possible size. * G_y contains an augmenting path. * contains a loose-tailed path: a path from some vertex in R_S to a vertex in T \setminus Z that consists of any number (possibly zero) of tight edges followed by a single loose edge. The trailing loose edge of a loose-tailed path is thus from Z \cap S, guaranteeing that is well defined. If is of maximum possible size, we are of course finished. Otherwise, by
Berge's lemma In graph theory, Berge's theorem states that a matching ''M'' in a graph ''G'' is maximum (contains the largest possible number of edges) if and only if there is no augmenting path (a path that starts and ends on free (unmatched) vertices, and alt ...
, there must exist an augmenting path with respect to in the underlying graph . However, this path may not exist in G_y: Although every even-numbered edge in is tight by the definition of , odd-numbered edges may be loose and thus absent from G_y. One endpoint of is in R_S, the other in R_T; w.l.o.g., suppose it begins in R_S. If every edge on is tight, then it remains an augmenting path in G_y and we are done. Otherwise, let uv be the first loose edge on . If v \notin Z then we have found a loose-tailed path and we are done. Otherwise, is reachable from some other path of tight edges from a vertex in R_S. Let P_v be the subpath of beginning at and continuing to the end, and let P' be the path formed by travelling along until a vertex on P_v is reached, and then continuing to the end of P_v. Observe that P' is an augmenting path in with at least one fewer loose edge than . can be replaced with P' and this reasoning process iterated (formally, using induction on the number of loose edges) until either an augmenting path in G_y or a loose-tailed path in is found.


Proof that adjusting the potential ''y'' leaves ''M'' unchanged

To show that every edge in remains after adjusting , it suffices to show that for an arbitrary edge in , either both of its endpoints, or neither of them, are in . To this end let vu be an edge in from to . It is easy to see that if is in then must be too, since every edge in is tight. Now suppose, toward contradiction, that u \in Z but v \notin Z. itself cannot be in R_S because it is the endpoint of a matched edge, so there must be some directed path of tight edges from a vertex in R_S to . This path must avoid , since that is by assumption not in , so the vertex immediately preceding in this path is some other vertex v' \in T. v'u is a tight edge from to and is thus in . But then contains two edges that share the vertex , contradicting the fact that is a matching. Thus every edge in has either both endpoints or neither endpoint in .


Proof that remains a potential

To show that remains a potential after being adjusted, it suffices to show that no edge has its total potential increased beyond its cost. This is already established for edges in by the preceding paragraph, so consider an arbitrary edge from to . If y(u) is increased by , then either v \in Z \cap T, in which case y(v) is decreased by , leaving the total potential of the edge unchanged, or v \in T \setminus Z, in which case the definition of guarantees that y(u)+y(v)+\Delta \leq c(u,v). Thus remains a potential.


Matrix interpretation

Given workers and tasks, and an × 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 worker "a" does task 1, 2, 3, 4 respectively. The same holds true for the other symbols as well. The matrix is square, so each worker can perform only one task.


Step 1

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 each element 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.'' As there are workers and tasks, adding or subtracting a fixed number to each item in a row or a column will only change the cost of the assignment by that amount; but the minimum cost assignment under old weights will remain a minimum cost assignment under new weights. 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. As all weights are non-negative, the assignment will be of minimum cost. This is illustrated below. : The zeros that are indicated as 0 are the assigned tasks.


Step 2

Sometimes it may turn out that the matrix at this stage cannot be used for assigning, as is the case for the matrix below. : In the above case, no assignment can be made. Note that task 1 is done efficiently by both agents c and d. Both can't 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 (i.e. the minimum element in each column is subtracted from all the elements in that column) and then check if an assignment is possible. In most situations this will give the result, but if it is still not possible then we need to keep going.


Step 3

All zeros in the matrix must be covered by marking as few rows and/or columns as possible. The following procedure is ''one way'' to accomplish this: *First, assign as many tasks as possible. The assigned tasks are represented by starring a zero. ** We assign the first zero of Row 1. The second zero of Row 1 can't be assigned, because it is on the same row as the first zero. ** We assign the first zero of Row 2. The second zero of Row 2 can't be assigned. ** Zeros on Row 3 and Row 4 can't be assigned, because they are on the same column as the zero assigned on Row 1. We could end with another assignment if we choose another ordering of the rows and columns. : * Cover all columns having an assignment (columns 1 and 2). : * Find a non-covered zero and prime it. If the zero is on the same row as a starred zero, cover the corresponding row, and uncover the column of the starred zero. **Here, the second zero of Row 1 is uncovered. Because there is another zero starred on Row 1, we cover Row 1 and uncover Column 1. **Then, the second zero of Row 2 is uncovered. We cover Row 2 and uncover Column 2. : : * If a non-covered zero has no assigned zero on its row, perform the following steps : ** Step 1: Find a starred zero on the corresponding column. If there is one, go to Step 2, else, stop. ** Step 2: Find a primed zero on the corresponding row (there should always be one). Go to Step 1. The zero on Row 3 is uncovered. We find on the path the first zero of Row 1, then the second zero of Row 1, then we are done. : * For all zeros encountered during the path, star primed zeros and unstar starred zeros, remove all covered lines and primed zeros. **As the path begins and ends by a primed zero when swapping starred zeros, we have assigned one more zero. : * Repeat the previous steps ** we cover columns 1, 2 and 3. The second zero on Row 2 is uncovered, so we cover Row 2 and uncover Column 2 : All zeros are now covered with a minimal number of rows and columns. The aforementioned detailed description is ''just one way'' to draw the minimum number of lines to cover all the 0s. Other methods work as well.


Step 4

Find the lowest uncovered value. Subtract this from every unmarked element and add it to every element covered by two lines. This is equivalent to subtracting a number from all rows which are not covered and adding the same number to all columns which are covered. These operations do not change optimal assignments. Repeat steps 3–4 until an assignment is possible; this is when the minimum number of lines used to cover all the 0s is equal to min(number of people, number of assignments), assuming dummy variables (usually the max cost) are used to fill in when the number of people is greater than the number of assignments. From Kőnig's theorem, the minimum number of lines (minimum Vertex cover) will be (the size of maximum matching
Matching (graph theory) In the mathematical discipline of graph theory, a matching or independent edge set in an undirected graph is a set of edges without common vertices. Finding a matching in a bipartite graph can be treated as a network flow problem. Definiti ...
matching
). Thus, when lines are required, minimum cost assignment can be found by looking at only zeroes in the matrix.


Bibliography

* R.E. Burkard, M. Dell'Amico, S. Martello: ''Assignment Problems'' (Revised reprint). SIAM, Philadelphia (PA.) 2012. * M. Fischetti, "Lezioni di Ricerca Operativa", Edizioni Libreria Progetto Padova, Italia, 1995. * R. Ahuja, T. Magnanti, J. Orlin, "Network Flows", Prentice Hall, 1993. * S. Martello, "Jeno Egerváry: from the origins of the Hungarian algorithm to satellite communication". Central European Journal of Operational Research 18, 47–58, 2010


References


External links

* Bruff, Derek
The Assignment Problem and the Hungarian Method
(matrix formalism). * Mordecai J. Golin
Bipartite Matching and the Hungarian Method
(bigraph formalism), Course Notes,
Hong Kong University of Science and Technology The Hong Kong University of Science and Technology (HKUST) is a public research university in Clear Water Bay Peninsula, New Territories, Hong Kong. Founded in 1991 by the British Hong Kong Government, it was the territory's third institut ...
.
Hungarian maximum matching algorithm
(both formalisms), in Brilliant website. * R. A. Pilgrim,
Munkres' Assignment Algorithm. Modified for Rectangular Matrices
', Course notes, Murray State University. *
Mike Dawes Mike Dawes is an English fingerstyle guitar player known for composing, arranging and performing multiple parts simultaneously on the guitar. Biography Dawes experimented with keyboards before picking up his first electric guitar at age twelve ...
,
The Optimal Assignment Problem
', Course notes,
University of Western Ontario The University of Western Ontario (UWO), also known as Western University or Western, is a public research university in London, Ontario, Canada. The main campus is located on of land, surrounded by residential neighbourhoods and the Thames R ...
.
On Kuhn's Hungarian Method – A tribute from Hungary
András Frank András Frank (born 3 June 1949) is a Hungarian mathematician, working in combinatorics, especially in graph theory, and combinatorial optimisation. He is director of the Institute of Mathematics of the Faculty of Sciences of the Eötvös Lo ...
, Egervary Research Group, Pazmany P. setany 1/C, H1117, Budapest, Hungary. * Lecture
Fundamentals of Operations Research - Assignment Problem - Hungarian Algorithm
Prof. G. Srinivasan, Department of Management Studies, IIT Madras. * Extension

Liu, Shell.
Solve any Assignment Problem online
provides a step by step explanation of the Hungarian Algorithm.


Implementations

Note that not all of these satisfy the O(n^3) time complexity, even if they claim so. Some may contain errors, implement the slower O(n^4) algorithm, or have other inefficiencies. In the worst case, a code example linked from Wikipedia could later be modified to include exploit code. Verification and benchmarking is necessary when using such code examples from unknown authors.
Julia implementation

C implementation claiming O(n^3) time complexity

Java implementation claiming O(n^3) time complexity

Python implementation

Ruby implementation with unit tests

C# implementation claiming O(n^3) time complexity

D implementation with unit tests (port of a Java version claiming O(n^3))


!--Please note that this implements a variant of the algorithm as described above. -->


Matlab and C

Perl implementation

C++ implementation


(BSD style open source licensed)
MATLAB implementation

C implementation

JavaScript implementation with unit tests (port of a Java version claiming O(n^3) time complexity)

Clue R package proposes an implementation, solve_LSAP

Node.js implementation on GitHub


{{DEFAULTSORT:Hungarian Algorithm Matching (graph theory) Combinatorial optimization