Hungarian algorithm explained

The 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.[1] [2] 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.[3]

James Munkres reviewed the algorithm in 1957 and observed that it is (strongly) polynomial.[4] 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.[5] [6] Ford and Fulkerson extended the method to general maximum flow problems in form of the Ford–Fulkerson algorithm.

The problem

See main article: Assignment 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:

Clean
bathroom
Sweep
floors
Wash
windows
Alice$8$4$7
Bob$5$2$3
Carol$9$4$8

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:

AliceBobCarol
Alice - $17$16
Bob$18 - $18
Carol$15$16 -
(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,

minP\operatorname{Tr}(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

G=(S,T;E)

with worker vertices and job vertices, and the edges 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\cupT)\toR

a potential if

y(i)+y(j)\leqc(i,j)

for each

i\inS,j\inT

. The value of potential is the sum of the potential over all vertices:

\sumv\iny(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

Gy

. The cost of a perfect matching in

Gy

(if there is one) equals the value of .

During the algorithm we maintain a potential and an orientation of

Gy

(denoted by

\overrightarrow{Gy}

) 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

RS\subseteqS

and

RT\subseteqT

be the vertices not covered by (so

RS

consists of the vertices in with no incoming edge and

RT

consists of the vertices in with no outgoing edge). Let be the set of vertices reachable in

\overrightarrow{Gy}

from

RS

by a directed path. This can be computed by breadth-first search.

If

RT\capZ

is nonempty, then reverse the orientation of all edges along a directed path in

\overrightarrow{Gy}

from

RS

to

RT

. Thus the size of the corresponding matching increases by 1.

If

RT\capZ

is empty, then let

\Delta:=min\{c(i,j)-y(i)-y(j):i\inZ\capS,j\inT\setminusZ\}.

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\capS

and

T\setminusZ

. Increase by on the vertices of

Z\capS

and decrease by on the vertices of

Z\capT

. The resulting is still a potential, and although the graph

Gy

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

RS

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(n4)

: 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(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:

Gy

contains an augmenting path.

RS

to a vertex in

T\setminusZ

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\capS

, guaranteeing that is well defined.

If is of maximum possible size, we are of course finished. Otherwise, by Berge's lemma, there must exist an augmenting path with respect to in the underlying graph . However, this path may not exist in

Gy

: Although every even-numbered edge in is tight by the definition of, odd-numbered edges may be loose and thus absent from

Gy

. One endpoint of is in

RS

, the other in

RT

; w.l.o.g., suppose it begins in

RS

. If every edge on is tight, then it remains an augmenting path in

Gy

and we are done. Otherwise, let

uv

be the first loose edge on . If

v\notinZ

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

RS

. Let

Pv

be the subpath of beginning at and continuing to the end, and let

P'

be the path formed by traveling along until a vertex on

Pv

is reached, and then continuing to the end of

Pv

. 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

Gy

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\inZ

but

v\notinZ

. itself cannot be in

RS

because it is the endpoint of a matched edge, so there must be some directed path of tight edges from a vertex in

RS

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'\inT

.

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\inZ\capT

, in which case

y(v)

is decreased by, leaving the total potential of the edge unchanged, or

v\inT\setminusZ

, in which case the definition of guarantees that

y(u)+y(v)+\Delta\leqc(u,v)

. Thus remains a potential.

The algorithm in O(n3) time

Suppose there are

J

jobs and

W

workers (

J\leW

). We describe how to compute for each prefix of jobs the minimum total cost to assign each of these jobs to distinct workers. Specifically, we add the

j

th job and update the total cost in time

O(jW)

, yielding an overall time complexity of
JjW\right)=O(J
O\left(\sum
j=1

2W)

. Note that this is better than

O(W3)

when the number of jobs is small relative to the number of workers.

Adding the j-th job in O(jW) time

We use the same notation as the previous section, though we modify their definitions as necessary. Let

Sj

denote the set of the first

j

jobs and

T

denote the set of all workers.

Before the

j

th step of the algorithm, we assume that we have a matching on

Sj-1\cupT

that matches all jobs in

Sj-1

and potentials

y

satisfying the following condition: the matching is tight with respect to the potentials, and the potentials of all unmatched workers are zero, and the potentials of all matched workers are non-positive. Note that such potentials certify the optimality of the matching.

During the

j

th step, we add the

j

th job to

Sj-1

to form

Sj

and initialize

Z=\{j\}

. At all times, every vertex in

Z

will be reachable from the

j

th job in

Gy

. While

Z

does not contain a worker that has not been assigned a job, let

\Delta:=min\{c(j,w)-y(j)-y(w):j\inZ\capSj,w\inT\setminusZ\}

and

wnext

denote any

w

at which the minimum is attained. After adjusting the potentials in the way described in the previous section, there is now a tight edge from

Z

to

wnext

.

wnext

is unmatched, then we have an augmenting path in the subgraph of tight edges from

j

to

wnext

. After toggling the matching along this path, we have now matched the first

j

jobs, and this procedure terminates.

wnext

and the job matched with it to

Z

.

Adjusting potentials takes

O(W)

time. Recomputing

\Delta

and

wnext

after changing the potentials and

Z

also can be done in

O(W)

time. Case 1 can occur at most

j-1

times before case 2 occurs and the procedure terminates, yielding the overall time complexity of

O(jW)

.

Implementation in C++

For convenience of implementation, the code below adds an additional worker

wW

such that

y(wW)

stores the negation of the sum of all

\Delta

computed so far. After the

j

th job is added and the matching updated, the cost of the current matching equals the sum of all

\Delta

computed so far, or

-y(wW)

.

This code is adapted from e-maxx :: algo.[7]

/** * Solution to https://open.kattis.com/problems/cordonbleu using Hungarian * algorithm. */

  1. include
  2. include
  3. include
  4. include

using namespace std;

/** * Sets a = min(a, b) * @return true if b < a */template bool ckmin(T &a, const T &b)

/** * Given J jobs and W workers (J <= W), computes the minimum cost to assign each * prefix of jobs to distinct workers. * * @tparam T a type large enough to represent integers on the order of J * * max(|C|) * @param C a matrix of dimensions JxW such that C[j][w] = cost to assign j-th * job to w-th worker (possibly negative) * * @return a vector of length J, with the j-th entry equaling the minimum cost * to assign the first (j+1) jobs to distinct workers */template vector hungarian(const vector> &C)

/** * Sanity check: https://en.wikipedia.org/wiki/Hungarian_algorithm#Example * First job (5): * clean bathroom: Bob -> 5 * First + second jobs (9): * clean bathroom: Bob -> 5 * sweep floors: Alice -> 4 * First + second + third jobs (15): * clean bathroom: Alice -> 8 * sweep floors: Carol -> 4 * wash windows: Bob -> 3 */void sanity_check_hungarian

// solves https://open.kattis.com/problems/cordonbleuvoid cordon_bleu

int main

Connection to successive shortest paths

The Hungarian algorithm can be seen to be equivalent to the successive shortest path algorithm for minimum cost flow,[8] [9] where the reweighting technique from Johnson's algorithm is used to find the shortest paths.The implementation from the previous section is rewritten below in such a way as to emphasize this connection; it can be checked that the potentials

h

for workers

0...W-1

are equal to the potentials

y

from the previous solution up to a constant offset. When the graph is sparse (there are only

M

allowed job, worker pairs), it is possibleto optimize this algorithm to run in

O(JM+J2logW)

time by using a Fibonacci heap to determine

wnext

instead of iterating over all

W

workers to find the one with minimum distance (alluded to here).

template vector hungarian(const vector> &C)

Matrix interpretation

This variant of the algorithm follows the formulation given by Flood,[10] and later described more explicitly by Munkres, who proved it runs in

l{O}(n4)

time.[4] Instead of keeping track of the potentials of the vertices, the algorithm operates only on a matrix:

aij:=c(i,j)-y(i)-y(j)

where

c(i,j)

is the original cost matrix and

y(i),y(j)

are the potentials from the graph interpretation. Changing the potentials corresponds to adding or subtracting from rows or columns of this matrix. The algorithm starts with

aij=c(i,j)

. As such, it can be viewed as taking the original cost matrix and modifying it.

Given workers and tasks, the problem is written in the form of an × cost matrix

where a, b, c and d are workers who have to perform tasks 1, 2, 3 and 4. a1, a2, a3, and a4 denote the penalties incurred when worker "a" does task 1, 2, 3, and 4 respectively.

The problem is equivalent to assigning each worker a unique task such that the total penalty is minimized. Note that each task can only be worked on by one worker.

Step 1

For each row, its minimum element is subtracted from every element in that row. This causes all elements to have nonnegative values. Therefore, an assignment with a total penalty of 0 is by definition a minimum assignment.

This also leads to at least one zero in each row. As such, a naive greedy algorithm can attempt to assign all workers a task with a penalty of zero. This is illustrated below.

The zeros above would be the assigned tasks.

Worst-case there are ! combinations to try, since multiple zeroes can appear in a row if multiple elements are the minimum. So at some point this naive algorithm should be short circuited.

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.

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 with penalty 0 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. Steps 3 and 4 form one way to accomplish this.

For each row, try to assign an arbitrary zero. Assigned tasks are represented by starring a zero. Note that assignments can't be in the same row or column.

We could end with another assignment if we choose another ordering of the rows and columns.

Step 4

Cover all columns containing a (starred) zero.

× ×
0*a20 a4
b10*b30
0 c2c3c4
0 d2d3d4

Find a non-covered zero and prime it (mark it with a prime symbol). If no such zero can be found, meaning all zeroes are covered, skip to step 5.

×
0*a20'a4×
b10*b30
0 c2c3c4
0 d2d3d4
0*a20'a4×
b10*b30'×
0 c2c3c4
0 d2d3d4

The zero on Row 3 is uncovered. We add to the path the first zero of Row 1, then the second zero of Row 1, then we are done.

0* a20' a4×
b10*b30'×
0' c2c3c4
0 d2d3d4
0 a20*a4
b10*b30
0*c2c3c4
0 d2d3d4
× ×
0 a20*a4
b10*b30'×
0*c2c3c4
0 d2d3d4
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 5

If the number of starred zeros is (or in the general case

min(n,m)

, where is the number of people and is the number of jobs), the algorithm terminates. See the Result subsection below on how to interpret the results.

Otherwise, find the lowest uncovered value. Subtract this from every unmarked element and add it to every element covered by two lines. Go back to step 4.

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.

Result

If following this specific version of the algorithm, the starred zeros form the minimum assignment.

From Kőnig's theorem,[11] the minimum number of lines (minimum vertex cover[12]) will be (the size of maximum matching[13]). Thus, when lines are required, minimum cost assignment can be found by looking at only zeroes in the matrix.

Bibliography

External links

Implementations

Note that not all of these satisfy the

O(n3)

time complexity, even if they claim so. Some may contain errors, implement the slower

O(n4)

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.

Notes and References

  1. Harold W. Kuhn, "The Hungarian Method for the assignment problem", Naval Research Logistics Quarterly, 2: 83–97, 1955. Kuhn's original publication.
  2. Harold W. Kuhn, "Variants of the Hungarian method for assignment problems", Naval Research Logistics Quarterly, 3: 253–258, 1956.
  3. Web site: Presentation. https://web.archive.org/web/20151016182619/http://www.lix.polytechnique.fr/~ollivier/JACOBI/presentationlEngl.htm. 16 October 2015.
  4. J. Munkres, "Algorithms for the Assignment and Transportation Problems", Journal of the Society for Industrial and Applied Mathematics, 5(1):32–38, 1957 March.
  5. Edmonds. Jack. Karp. Richard M.. 1972-04-01. Theoretical Improvements in Algorithmic Efficiency for Network Flow Problems. Journal of the ACM. 19. 2. 248–264. EN. 10.1145/321694.321699. 6375478. free.
  6. Tomizawa. N.. 1971. On some techniques useful for solution of transportation network problems. Networks. en. 1. 2. 173–194. 10.1002/net.3230010206. 1097-0037.
  7. Web site: Hungarian Algorithm for Solving the Assignment Problem . . August 23, 2012 . e-maxx :: algo . May 13, 2023.
  8. Web site: Minimum-cost flow - Successive shortest path algorithm . Jacob Kogler . December 20, 2022 . Algorithms for Competitive Programming . May 14, 2023.
  9. Web site: Solving assignment problem using min-cost-flow . . July 17, 2022 . Algorithms for Competitive Programming . May 14, 2023.
  10. Flood . Merrill M. . The Traveling-Salesman Problem . Operations Research . 4 . 1 . 1956 . 0030-364X . 10.1287/opre.4.1.61 . 61–75.
  11. [K%C5%91nig%27s theorem (graph theory)]
  12. [Vertex cover]
  13. [Matching (graph theory)]