The Hungarian Algorithm
The Hungarian Algorithm
The Hungarian algorithm is an algorithm for solving a matching problem or more generally an assignment linear
programming problem. The name was given by H.W.Kuhn in recognition of the work of the two mathematicians
J.Egerváry and D.König. The Hungarian Algorithm is actually a special case of the Primal-Dual Algorithm. It takes a
bipartite graph and produces a maximal matching.
Recall an assignment problem has the standard form
LP:
Minimize ∑ i, j cij xij
Subject to the constraints
∑ j xij = 1 (1 ≤ i ≤ m)
∑ i xij = 1 (1 ≤ i ≤ n)
0 ≤ xij ≤ 1.
Here we should take cij ≥ 0. If m = n, the problem is a typical transportation problem and it always has an optimal
solution. Furthermore, the simplex algorithm only produces integer basic solutions and so xij = 0 or xij = 1. If
xij = 1, then we can say that the ith person is assigned to the jth task. The problem is set up so that every person is
assigned 1 task and so that every task gets done. The cost coefficients measure the suitability of a person to a task
while the objective value measures quantitatively the effectiveness of doing all the tasks. The problem can also be
assignments of machines to different jobs and the cost coefficient can be the cost of a certain machine to do a certain
job.
We can transform various related assignment problems into the standard form of LP.
1) If m ≠ n the problem is said to be unbalanced. We can create a number of false variables to force m = n and
assign costs equal to some large number for the false variables.
WARNING. The Hungarian algorithm can yield a false answer if the problem is unbalanced. We assume that the
assignment problem is balanced and has an m × m matrix.
The Assignment Algorithm takes the assignment problem LP and replaces it with an new assignment problem
LP′ with the same number of variables but with a new cost matrix A′ = (c′ij) such that the optimal solutions x* (but
not the optimal objectives) of both LP and LP′ are the same. The cost matrix A′ will have “enough” zero elements so
that the i,j with x*ij = 1 satisfying Σi x*ij = 1 and Σj x*ij = 1 can be chosen using only those i,j with c′ij = 0. This
gives the objective value 0 for x* in LP′ and means that x* is optimal for LP′ and consequently for LP.
The algorithm that chooses the i,j with c′ij = 0 is called the Hungarian Algorithm. It is an algorithm that
constructs a maximal matching for a bipartite graph. We discuss this later in this Chapter. Right now we give the
assignment algorithm for LP.
Assignment Algorithm
Ia. Minimum entries are found for each separate row of A and subtracted from each entry in the respective rows.
The original matrix A is replaced by the new matrix.
b. Minimum entries are found for each separate column of A and these are subtracted from each entry in the
respective columns. The original matrix (from 1a) is replaced by the new matrix.
II. Cross out the minimal number of rows and columns in the matrix A to cover all 0 entries of A.
a. (Termination) If m lines are used in II to cover, STOP.
b. If less than m lines are used in II to cover, go to III.
III. (Iteration) Find the minimal entry not crossed out in II. Add this entry to all elements that are doubly crossed
out (by both a horizontal and vertical line) in II and subtract from all entries of A that are not crossed out.
Return to step II with the new matrix.
We defer the verification that the Assignment Algorithm does not change the optimal solution x* until we present
some ideas from graph theory to clarify. We first implement the Assignment Algorithm on an example.
Example
so the last two basis elements are x12 = 1 and x24 = 1. So the basic solution (and assignment) is
x 33 = 1, x31 = 1, x12 = 1, x24 = 1.
Graphs
DEFINITION. A graph is an ordered pair G = (V, E) consisting of a finite set and a subset E of elements of the form
(x, y) where x and y are in V. The set V are called the vertices of the graph and the set E are called the edges
DEFINITION. A graph G is said to be a bipartite (or bicolored) graph if the vertices can be partitioned into two
mutually disjoint sets X and Y so that there is no edge of the form (x, x′) with x and x′ in X or of the form (y, y′)
with y and y′ in Y. A bipartite graph will be denoted by G = ({X, Y}, E).
Bipartite graphs G = ({X, Y}, E) are represented by matrices. The X vertices are for example used for row indices
and the Y vertices are used as column indices. Generally, the existence of an edge (x, y) is indicated by a 1 in the x,y
cell of the |X| × |Y| matrix; no edge is indicated by 0. For the assignment problem, we are representing an edge by 0
and no edge by a nonzero number.
DEFINITION. A matching for a bipartite graph G = ({X, Y}, E) is a subset M of E such that no two elements of M
have a common vertex.
DEFINITION. A set of vertices V′ is said to be a cover of a set of edges E′ if every edge in E′ is incident on one or
more of the vertices of V′. A set of vertices S will be called a cover of the bipartite graph G = ({X, Y}, E) if every
edge of G is incident on one or more of the vertices of S.
The assignment problem corresponds to a bipartite graph. The vertices V may be partitioned into two sets: (1)
the assigned tasks and (2) the assignees. The edges consist of (unordered) pairs connected the assigned tasks and the
assignees. Since we allow the theoretic possibility of assigning any task to any assignee, the graph for the assignment
problem consists of vertices V = (X, Y) where X = {x1, … , xm} and Y = {y1, … , ym} and edges consisting of all
combinations E = {(xi, yj) 1 ≤ i ≤ m, 1 ≤ j ≤ m}. We can denote the incidence matrix of this graph as an m × m
matrix consisting entirely of 1’s and we can use the matrix as a substitute for the graph. This has some disadvantage
in that an index, say 1, can denote x1 or y1 and so the row and column indices should be kept separate.
In the next section we consider subgraphs of the assignment problem. For example, let (aij) be the m × m cost
matrix of the assignment problem and let G′ be the subgraph whose edges are all (i, j) with aij = 0 and all vertices
incident on any of the edges. If we are considering i as row indices and j as column indices, the vertices will consist of
all rows of A which have a 0 entry and all columns of A which have a zero entry.
We verify the assignment algorithm terminates at some stage with a cover of size m. The proof is obtained by
noting that sum of the current cost matrices is strictly decreasing at each stage provided there is a minimal cover of size
less than m. We use the viewpoint of the previous section.
THEOREM. Let be A = (aij) be an m × m matrix of positive entries. Suppose that there is a cover S of the set E′ of
edges (i, j) with aij = 0 of size less than or equal to m - 1. Let D = { (i, j) ∈ E′ both i and j are in S}. Let a0 be
the minimum of {aij i ∉ S, j ∉ S}. Suppose that
REMARK. We can paraphrase the theorem as follows. Suppose that 0’s of A are crossed by crossing out the rows
and columns containing a 0. Suppose that the minimal number of crossed out rows and columns necessary to cross
out all the 0’s of A is less than m - 1. Suppose that the minimal entry in the entries not crossed out by the minimal
number of crossed out rows and columns is added to all doubly crossed out entries of A and subtracted from all non
crossed out entries to give a matrix B = (bij). Then B is a positive matrix and ∑ aij > ∑bij.
PROOF. Let s and t be any integers with 0 ≤ s + t ≤ m - 1 and suppose that s rows and t columns are crossed out,
i.e., the cover C consists of s vetices of X and t vertices of Y. Then there are st doubly crossed out elements,
m(s + t) – st crossed out elements, and m(s + t) singly crossed out elements. So there are
m 2 - m(s + t) + st
entries that are not crossed out. Now suppose that the minimal (nonzero) entry in the uncrossed out part of A is r.
Since all the zero entries are crossed out r, we get that r > 0. So we get
∑ a ij - ∑bij = - rst + r(m2 - m(s + t) + st ) = rm(m - (s + t)) >0
since m > s + t. Also note that all bij ≥ 0 since the minimal entry is subtracted. Q.E.D.
The process of adding to double crossed out entries and subtracting from non crossed out entries is a combination
of operations that does not change the optimal solution of the assignment problem.
THEOREM. Suppose that step I or III in the assignment algorithm is implemented. Then the optimal solution of the
assignment problem with the new cost matrix does not change.
PROOF. For step I note that the optimal solution x* does not change if a constant is subtracted from any row or
column. To see this suppose that c is subtracted from every element in the first row of the cost coefficient matrix.
Then the problem becomes
Let G = ((X, Y}, E) be a bipartite graph. Let M be a matching for G. The Hungarian Algorithm either shows
that M is a maximal matching for G or finds a matching M′ for G with |M′| = |M| + 1.
I. Label all vertices in X with (*) when the vertices do not meet an edge of M and call all vertices untested.
II. If in the previous step no new labels have been given to a vertex of X, then STOP. Otherwise, go to III.
III. While there is a labeled but untested vertex xi of X, label with xi all vertices yj of Y that have not yet been
labeled and that can be connected to xi with an edge NOT IN M. The vertex xi is now tested (even if no edge is added)
and the vertices yj are now labeled.
IV. If no new label has been given in III, then STOP. Otherwise, find an untested but labeled vertex yj of Y and
label with yj any unlabeled vertex xk of X which is joined to yj by an edge IN M. The vertex yj is now tested (even if
no edge is added) and vertex xk is now labeled. If yj cannot be connected to an unlabeled vertex in X, then STOP and
an Augmenting Tree has been found.
V. Return to II.
The algorithm stops after a finite number of iterations since each vertex is receiving at most one label and each
vertex is tested at most once. There are two possibilities:
I. (Augmenting Tree) There is a labeled vertex of Y that does not meet an edge of M. This has the following
diagram.
x1 y1 x2 y2 x3 y3
where the dashed lines represent edges in M and the solid lines represent edges not in M. In this case the size of the
matching can be increased by switching edges to look like
x1 y1 x2 y2 x3 y3
THEOREM. Suppose that G = ((X, Y}, E) be a bipartite graph and that M is a matching for G. Suppose that a
Augmenting Tree {x1, y1 , … , xn, yn} has been found using the Hungarian algorithm. Let S be the set of edges
given by
S = {(y1, x2), (y2, x3) , … , {yn - 1, xn)}
and let M1 be the set of edges
M 1 = {(x1, y2), (x2, y2) , … , (xn, yn)}.
Then the set of edges M′ given by
M′ = S ∪(M - M1)
is a matching for G with |M′| = |M| + 1.
PROOF. First note that yn cannot be attached to a labeled vertex x by an edge e = (yn, x) in M; otherwise, the vertex
yn would have to be a labeled and an unlabeled vertex at the stage when {x1, y1 , … , xn} is formed. To see that yn is
labeled at this stage, we see that the vertex x cannot be a * vertex (since * vertices are incident on no edges of M), and
therefore, x must have been labeled by some y using an edge (y, x) in M. This would mean that y = yn since both
(yn, x) and (y, x) are in the matching M and have a vertex in common. So x must have been labeled by yn and this
implies that yn must have been labeled. To see yn is unlabeled, we see that the vertex yn is labeled by xn in the
algorithm only if yn is unlabeled.
Now we show that M′ is a matching by showing each vertex z is incident on at most one edge in M′. If z is in
{y1, x2, … , yn - 1, xn}, then z is incident on an edge (yi, xi + 1) in M1 and no other edge in M. So z is incident on
one edge in M′. If z= x1, then z is *-vertex and z is incident on no edge in M and incident only on the edge (x1, y1) in
M′. If z = yn, then z is incident on one edge (xn, yn) since yn is incident on no edge of M; otherwise, (yn, x) would
be in M but x cannot be labeled by the preceding paragraph or unlabeled by the fact that the Hungarian Algorithm has
stopped on yn. So we get that the Augmenting Tree produces a matching M′.
We note that the matching M′ has one more edge than M. Q.E.D.
We see that the Hungarian algorithm terminates after a finite number of iterations. On each level the points are
tested one after the other and after all the points are tested or perhaps before the algorithm ends. As a whole the
algorithm either terminates when the only X elements left are Hungarian acorns.
THEOREM. Suppose M is a matching of the bipartite graph G = ({X, Y}, E) and suppose the Hungarian algorithm
produces only Hungarian trees for all unmatched X. Let Xun be all unlabeled X vertices and Ylab be all labeled Y
vertices. Then
1) S = Xun ∪ Ylab is a minimal cover of G, and
2) |M| = |S| and M is a maximal matching of G.
PROOF. First we show S is a cover. We argue by contradiction. Suppose (x, y) ∈ E and assume x ∈ Xlab and
y ∈ Yun. We show that such an edge does not exist. First assume (x, y) ∈ M. Since x is labelled and (x, y) is in
M, the vertex cannot be the first vertex in a path labelled with a (*) due to I of the algorithm. So x must be part of
chain of the form
x1------- y1 - - - - x2 ------- y2 - - - - ... - - - - xk ------- yk - - - - xk + 1 = x.
COROLLARY. Let G = ({X, Y}, E) be a bipartite graph with |X| = |Y|. Suppose that G has a minimal cover S with
|S| = |X|. Then there is a matching M such that M is incident on all vertices of G.
PROOF. Since S is a minimal cover, there is a matching M with ρ(M) = |S| = |X|. Since no vertex of G is incident
on more than one edge of M, we must have that M is incident on 2|X| vertices which must mean the M is incident on
all vertices of G. Q.E.D.
Now back to the algorithm for the assignment problem running on a cost matrix of dimension m × m. The first
part of the algorithm runs until a minimal cover of size M is for the zeroes in the cost matrix. The first part of the
algorithm goes to termination since the sum of all the costs decreases at every stage when the minimal cover has less that
m vertices. When the minimal cover is reached with m vertices, we run the Hungarian algorithm on the bipartite graph
defined by the zeroes in the final cost matrix. A matching of size m is obtained to give the minimal cost assignment.