ArticleslgStudy

computer science

Hungarian algorithm

Hungarian algorithm is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Hungarian algorithm rather than just read about it. In short: 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.

Key takeaways

  • Hungarian algorithm belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Hungarian algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Hungarian algorithm from memory before moving on to harder problems.

Reference excerpt

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.

Worked examples

Example 1 — a first encounter with Hungarian algorithm

Start with the simplest possible case. Write down what Hungarian algorithm claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Hungarian algorithm before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Hungarian algorithm ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Hungarian algorithm

In research
Hungarian algorithm appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Hungarian algorithm in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Hungarian algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Combinatorial optimization, Matching (graph theory), so understanding it makes those chapters shorter.
In everyday life
Look for Hungarian algorithm outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Hungarian algorithm” →

How to study Hungarian algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Hungarian algorithm means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Hungarian algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Hungarian algorithm in simple terms?

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 alg…

Why does Hungarian algorithm matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Hungarian algorithm?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Hungarian algorithm.

Tags

  • Combinatorial optimization
  • Matching (graph theory)

Keep exploring