Kuhn's "Hungarian" Assignment Algorithm, Sans Dual Variables Eric Bach Dec 11, 2009 Let a_ij be an n x n matrix. Our goal is to determine a permutation sigma that minimizes cost := sum_i a_{i, sigma(i)}. In graph theory terms, we want to choose, among the n! matchings of the weighted complete bipartite graph K_{n,n}, one of smallest total weight. Without loss of generality all the costs are non-negative. In matrix terms, we want to select positions for n non-attacking rooks in a way that minimizes the sum of the numbers at those positions. The first idea behind the algorithm is that adding or subtracting a constant to any row or column does not change the identity of the optimal matching. So, if we start with a matrix like 3 1 2 1 5 9 2 6 5 we can subtract the minimum element from each row to get 2 0 1 0 4 8 0 4 3 Note that the first two columns have zeroes, but the last doesn't. So we can subtract the minimum element from the last column to obtain 2 0 0 0 4 7 0 4 2 If we can make a perfect matching out of the 0's, we are done. Otherwise, we must use another idea. This is Koenig's theorem: in any bipartite graph, the number of edges in a maximum matching equals the minimum number of vertices needed to cover all the edges of the graph. On the matrix, the vertex cover is indicated by choosing rows and columns (called "lines"). For the above array, the largest zero matching we can make has size two, so two lines will suffice to cover. For our matrix, the lines are as indicated: | -- 2-0-0 -- | 0 4 7 | 0 4 2 | We must now process more than one line at a time. Let delta be the minimum of the uncovered entries, in this case delta = 2. What we do is Add delta/2 to covered lines (rows or columns) Subtract delta/2 from uncovered lines. This is called a "pivot step". The effect of this is to subtract delta from uncovered entries, and add delta to doubly covered entries. After pivoting the new matrix is 4 0 0 0 2 5 0 2 0 There is now a matching of 0's, indicated by *'s in the matrix: 4 0* 0 0* 2 5 0 2 0* In terms of the original costs, this is 3 1* 4 1* 5 9 2 6 5* so the optimal assignment has cost 7. In general, we go back and forth between increasing the matching and pivoting, until we have a perfect matching. It is clear that if this procedure works, we get an optimal assignment. The next task is to see why it terminates. If the costs are integers, we can reason as follows. Let there be k covered rows, l covered columns, with k+l < n. Then we have (n-k)(n-l) entries reduced by delta, and kl entries increased by delta. The net reduction (in units of delta) is (n-k)(n-l) - kl = n^2 - n(k+l) > 0 So the sum of the entries decreases with each pivot step. However, all entries are non-negative. So the algorithm terminates. This gives a proof of termination, but the running time bound is not polynomial in input length. To get such a bound we must restrict the way in which the algorithm chooses a cover. As is well known, bipartite matching can be reduced to maximum flow in a network with unit capacities. Computation of a maximum flow proceeds by a series of searches, in the flow's residual network. Let us call a node *wet* if the search reaches it, and *dry* otherwise. Since left (resp. right) nodes in a bipartite graph are associated with rows (resp. columns) we extend this terminology and refer to wet and dry lines. The Koenig cover (K-cover for short) consists of the dry rows and the wet columns. (Intuitively, we associate moisture with the left half of the network, so these are rows and columns that are on the "wrong" side.) This is a minimal cover. We can now prove the following theorem: If the algorithm always uses the K-cover, its running time is polynomial. This will follow from the following observation: After each pivot step, either the maximum matching has increased in size, or the number of wet nodes increases. --------------- (dry) --------------- | | | | | | 0 <------- new zero produced by pivot | | | (wet) Proof: Number rows and columns so that we have the above picture. Let the new zero entry be in position (i,j). If neither row i nor column j has a zero, then the matching can be enlarged. Otherwise, the residual network has a new edge i -----> j (wet) (dry) and the search will reach the dry column vertex j, provided that pivoting hasn't changed the residual network. So we must also verify this last point. Note that any singly covered zero entry will survive pivoting, since singly covered entries don't change. A doubly covered zero, however, cannot be in the matching. (If it were, say at position (i,j), the residual network would have an edge i <------ j (dry) (wet) which is impossible.) Notes. 1. I first saw the "decreasing" version of the Hungarian algorithm, as done here, in a lecture by R.M. Karp. The polynomial running time bound follows ideas in A. Frank, On Kuhn's Hungarian method -- a tribute from Hungary, TR 2004-14, Egervary Research Group, Budapest, 2004. 2. The number of steps in the computation depends only on n, the dimension of the problem. Since it does not involve the matrix entries, this is a so-called strongly polynomial bound. 3. The Hungarian algorithm is often presented using dual variables. For this, see L. Ford and D. Fulkerson, Flows in Networks, pp. 111-112. If one compares the two algorithms, one sees that the numbers in the algorithm above are "slack" values which indicate how far each dual constraint is from being tight.