Hungarian Algorithm Assignment Visualizer

Who does which job at the lowest total cost.

  • Step by step
  • Count of optima
  • Greedy comparison
A small rounded clay table holding a grid of rounded pegs joined by rounded bars, two rows of rounded beads facing each other and a few loose beads.

The guide

Subtract a constant from a row and the best assignment cannot change: the whole trick of the Hungarian method

Reducing rows and columns, covering zeros and adjusting potentials look like bookkeeping, yet each move preserves the answer and ends in a proof that nothing cheaper exists.

  • 15 minimum total of the three by three example
  • 16 total of the greedy row-by-row choice on the same matrix
  • 10 sum of the row minima, a floor and not an answer
  • 4 optimal assignments in the five by five matrix with ties
  • 40,320 assignments to try at 8 by 8, which is 8 factorial
Read the full guide Fold the guide 7 min · 18 sources

Three workers must take three jobs, one each, and every worker quotes a different cost for every job. The costs in a table are 8, 4, 7 for the first worker, 5, 2, 3 for the second and 9, 4, 8 for the third. Which pairing is cheapest? There are only six, so a person can try them all and find 15.

Now make it twelve workers and twelve jobs. Dantzig’s original example of a linear program was this very problem, with 70 people and 70 jobs, where testing all the permutations is out of reach. The pairings number 479,001,600, and trying each one is hopeless. The Hungarian method finds the cheapest pairing in time that grows with the cube of the size (time complexity counts elementary operations, so it is the growth that matters and not the clock), and it does so with a few moves that each preserve the answer. This essay is about why those moves are safe and why the end of the method is a proof.

The assignment problem

The assignment problem asks for the cheapest way to give each agent one task and each task one agent. It is a combinatorial optimization problem: the answer is the best of a finite set of candidates, and the set is too large to search. In graph terms it is a minimum-cost perfect matching in a bipartite graph, where workers sit on one side, jobs on the other and every worker is joined to every job by an edge that carries a cost.

A bipartite graph is one whose vertices split into two sets with every edge running between them. A matching is a set of edges with no shared endpoint, and a perfect matching covers every vertex. Hall’s marriage theorem says exactly when one exists without costs: every group of workers must be able to reach at least as many jobs as there are workers in the group. When the two sides differ in size the problem is called unbalanced. The usual remedy is to invent dummy tasks of cost zero, such as a task named sitting still, until the sides match. The page does exactly that when a matrix is not square, and it says so on screen.

Maximising instead of minimising is a change of sign: negate every entry, solve, and negate the total back. Forget the negation and the method happily returns the cheapest pairing instead of the dearest, which for a three-row, four-column example is 16 instead of 31.

Why reducing a row is safe

Every pairing takes exactly one entry from each row. If 5 is subtracted from every entry of a row, then every pairing costs exactly 5 less, whichever entry it uses from that row. The totals all move together, so the ranking of pairings is untouched, and so is the identity of the best one. The same argument holds for columns.

The method begins by subtracting each row’s smallest entry and then each column’s. In the example the rows lose 4, 2 and 4 and the columns of the result lose 3, 0 and 1, and the matrix becomes 1, 0, 2 over 0, 0, 0 over 2, 0, 3. Together the reductions have removed 10 plus 4, which is 14 of the answer, and a zero in every row and column marks where the remaining cost could hide.

If a full set of zeros with no two in the same row or column exists, the problem is solved: those cells cost nothing in the reduced matrix, so the original costs add up to everything that was subtracted. Here the zeros in the first and third rows both sit in the second column, so no full set exists. The method must do more.

Covering zeros and Konig’s theorem

The next question is whether the zeros contain a full set of independent ones, meaning no two share a line. That is a matching problem, and Konig’s theorem answers it from the other side. In a bipartite graph the largest matching has exactly as many edges as the smallest set of vertices that touches every edge. Finding the largest matching in a bipartite graph can also be treated as a network flow problem. Read for the matrix, the largest set of independent zeros equals the fewest rows and columns that cover all the zeros.

So the method draws the fewest lines it can through the zeros. In the example two lines suffice, which is fewer than the three needed, so no assignment of zeros exists yet. If the number of lines equalled the size of the matrix, the matching would be full and the work would be finished.

When the lines are too few, the method takes the smallest value that no line touches. Subtract it from every uncovered entry and add it to every entry covered twice. Entries covered once stay put. For the example that value is 1, and after the adjustment three lines are needed and the zeros give the answer: first worker to the first job, second to the third, third to the second, for 15.

Why is the adjustment safe? Subtracting from the uncovered rows and adding to the covered columns is a pair of row and column shifts, and the argument of the previous section applies to each. The method is also guaranteed to progress, because the amount taken off grows every time, which is where the polynomial running time comes from.

What the potentials prove

The same method has a second reading in terms of potentials, a number y for every vertex with the rule that the potentials at the two ends of any edge add up to at most its cost. In the example the workers can be given 8, 4 and 8 and the jobs 0, -4 and -1, and every edge respects the rule. The potentials add up to 15.

Here is the certificate. Any perfect matching uses each vertex once, and each of its edges costs at least the sum of its end potentials, so the matching costs at least the total of all potentials. That is weak duality in miniature, and the page explains it as a lower bound that the dual problem places under the primal. An edge whose cost equals the sum of its end potentials is called tight.

The method stops when a perfect matching of tight edges exists. Then the matching costs exactly the potential total, and no matching can be cheaper. The page prints the reduced cost of every cell, which is its cost minus the two potentials, and shows that all of them are zero or more and that the chosen ones are zero. The proof can be read off the grid.

This is also why potentials explain what reducing rows and columns were doing all along. A row reduction is a rise of a worker potential, and a column reduction a rise of a job potential. The matrix method and the potentials method are two descriptions of one algorithm, and the O(n cubed) version adds one job at a time and keeps the potentials valid at every step.

Where greedy goes wrong, and what the method cannot do

Greedy is the tempting alternative: let each row take its cheapest remaining column. The assignment problem page gives a two by two case where it loses, and the three by three example shows it too. The greedy choice costs 16 against 15: the first worker grabs the second job, which the others also price low, and the last worker is left with the first job at 9.

A softer mistake is to add up the smallest entry in each row and call it the answer. For the example that sum is 10, which is only a floor. It ignores the rule that two rows may not use one column. The sum can equal the true minimum, as in the five by five matrix, but nothing in it says so.

Ties deserve a warning. The matrix 4 4 5 6 7, 4 4 6 7 5, 7 6 3 3 4, 8 7 3 3 4, 6 5 4 4 2 costs 16, and there are 4 pairings that reach it. The method returns one of them, and it cannot tell by itself that there are others. Counting them takes an exhaustive search over every pairing, which is feasible at 8 by 8, where there are 40,320, and out of reach at 12 by 12.

Dummy rows and columns count as well. In the three by four example the best total is 16, and exactly 2 pairings reach it, whether the missing row is handled by a zero-cost dummy row or by choosing which column goes unused.

The method has limits. It solves the assignment problem and nothing close to it: not stable matching, where preferences and not costs decide, not flow with capacities, and not the travelling salesman problem. Entries here are exact decimals, so a tie is a real tie and not a rounding accident. The totals were checked against SciPy’s linear_sum_assignment and against a search over every permutation, and the totals were sorted with an explicit comparison, because the default sort of JavaScript converts elements to strings first.

Its history is worth a line. Harold Kuhn published the method in 1955 and named it for two Hungarian mathematicians, Denes Konig and Jeno Egervary, whose earlier work it rested on. James Munkres showed in 1957 that it is strongly polynomial, which is why it is also called the Kuhn-Munkres algorithm. The original running time grew with the fourth power of the size, and Edmonds and Karp, and independently Tomizawa, brought it to the third.

Later it was found that Carl Gustav Jacobi had solved the problem in the nineteenth century. Today the same idea tracks objects across video frames, matching each new detection to an existing track.

Sources

Every source consulted for this guide. Links checked November 21, 2026.

  1. Hungarian algorithm Wikipedia
  2. Assignment problem Wikipedia
  3. Linear programming Wikipedia
  4. Bipartite graph Wikipedia
  5. Harold W. Kuhn Wikipedia
  6. Matching (graph theory) Wikipedia
  7. Konig's theorem (graph theory) Wikipedia
  8. Time complexity Wikipedia
  9. Maximum flow problem Wikipedia
  10. Combinatorial optimization Wikipedia
  11. James Munkres Wikipedia
  12. Jeno Egervary Wikipedia
  13. Perfect matching Wikipedia
  14. Hall's marriage theorem Wikipedia
  15. Duality (optimization) Wikipedia
  16. scipy.optimize.linear_sum_assignment SciPy documentation
  17. itertools - Functions creating iterators for efficient looping Python Software Foundation
  18. Array.prototype.sort() MDN Web Docs