The Hungarian algorithm or Hungarian method is a combinatorial optimization algorithm that solves the assignment problem in polynomial time and which anticipated later primal–dual methods. It was developed and published in 1955 by Harold Kuhn, who gave it the name "Hungarian method" because the algorithm was largely based on the earlier works of two Hungarian mathematicians, Dénes Kőnig and Jenő Egerváry. However, in 2006 it was discovered that Carl Gustav Jacobi had solved the assignment problem in the 19th century, and the solution had been published posthumously in 1890 in Latin. James Munkres reviewed the algorithm in 1957 and observed that it is (strongly) polynomial. Since then the algorithm has been known also as the Kuhn–Munkres algorithm or Munkres assignment algorithm. The time complexity of the original algorithm was O(n4), however Edmonds and Karp, and independently Tomizawa, noticed that it can be modified to achieve an O(n3) running time. Ford and Fulkerson extended the method to general maximum flow problems in the form of the Ford–Fulkerson algorithm.
The problem
Example In this simple example, there are three workers: Alice, Bob and Carol. 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 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 $15, achieved by having Alice clean the bathroom, Carol sweep the floors, and Bob wash the windows. This can be confirmed using brute force:
(the unassigned person washes the windows)
Matrix formulation In the matrix formulation, we are given an n×n matrix, 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 of a cost matrix C to minimize the trace of a matrix,
minPTr(PC), where P is a permutation matrix. (Equivalently, the columns can be permuted using CP.) 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 G=(S,T;E) with n worker vertices (S) and n job vertices (T), and the edges (E) each have a cost c(i,j). We want to find a perfect matching with a minimum total cost.
The algorithm in terms of bipartite graphs Let us call a function y:(S∪T)→ℝ a potential if y(i)+y(j)≤c(i,j) for each i∈S,j∈T. The value of potential y is the sum of the potential over all vertices:
∑v∈S∪Ty(v). The cost of each perfect matching is at least the value of each potential. This can be seen by first noticing that the total cost of the matching is the sum of costs of all edges it contains. 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 y if y(i)+y(j)=c(i,j). Let us denote the subgraph of tight edges by Gy. The cost of a perfect matching in Gy (if there is one) equals the value of y. During the algorithm we maintain a potential y and an orientation of Gy (denoted by Gy→) which has the property that the edges oriented from T to S form a matching M. Initially, y is 0 everywhere, and all edges are oriented from S to T (so M is empty). In each step, either we modify y 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 M are tight. We are done if M is a perfect matching. In a general step, let RS⊆S and RT⊆T be the vertices not covered by M (so RS consists of the vertices in S with no incoming edge and RT consists of the vertices in T with no outgoing edge). Let Z be the set of vertices reachable in Gy→ from RS by a directed path. This can be computed by breadth-first search. If RT∩Z is nonempty, then reverse the orientation of all edges along a directed path in Gy→ from RS to RT. Thus the size of the corresponding matching increases by 1. If RT∩Z is empty, then let
Δ:=min{c(i,j)−y(i)−y(j):i∈Z∩S,j∈T∖Z}. Δ 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∩S and T∖Z. Increase y by Δ on the vertices of Z∩S and decrease y by Δ on the vertices of Z∩T. The resulting y is still a potential, and although the graph Gy changes, it still contains M (see the next subsections). We orient the new edges from S to T. By the definition of Δ the set Z of vertices reachable from RS increases (note that the number of tight edges does not necessarily increase). If the vertex added to Z∩T is unmatched (that is, it is also in RT), then at the next iteration the graph will have an augmenting path. We repeat these steps until M is a perfect matching, in which case it gives a minimum cost assignment. The running time of this version of the method is O(n4): M is augmented n times, and in a phase where M is unchanged, there are at most n potential changes (since Z increases every time). The time sufficient for a potential change is O(n2).
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:
… excerpt ends here. Continue reading the full article.