Hungarian Method Calculator

Solve an assignment problem with the Hungarian method, step by step. Every row and column reduction, the minimum number of lines that cover the zeros and each adjustment is shown and explained, in exact fractions. Maximization, unbalanced matrices and forbidden assignments are supported.

  • Every matrix explained
  • Maximization and unbalanced problems
  • Forbidden assignments (M)

Cost matrix

Workers ↓Jobs →

Rows are workers and columns are jobs. Click a name to rename it.

Type integers, decimals or fractions (12, 7.5, 3/4). Type M (or -) where an assignment is not allowed. Empty boxes count as 0.

Result

Optimal assignment found
Minimum total cost31
Assignment
WorkerJobCost
A→ 314
B→ 17
C→ 25
D→ 45
Total31

This optimal assignment is unique.

Matrix size
4 × 4
Adjustment rounds
2
The assignment as a bipartite graphWorkers on the left, jobs on the right. Bold edges form the optimal assignment; faint edges are the other allowed pairs.
14755AABBCCDD11223344

Step-by-step solution

0zero0boxed (assigned) zerocovering line✓ticked row or columnsmallest uncovered value− k (uncovered)+ k (covered twice)
Step 1 of 8

Step 1 of 8Row reduction

Subtract the smallest value of each row from every value of that row. Every row now has a zero, and the optimal assignment does not change.

Row reduction
1234Row min
A06717
B0101347
C130555
D212905

Tip: use the ← and → keys to move between steps.

How the Hungarian method works

What is the assignment problem?

The assignment problem asks how to give n jobs to n workers, one job each, so that the total cost is as small as possible (or the total profit as large as possible). The data is a cost matrix: row i, column j holds the cost of giving job j to worker i. Typical examples are assigning machines to tasks, drivers to routes, nurses to shifts and salespeople to regions.

In graph theory terms it is a minimum-weight perfect matching in a complete bipartite graph: workers on one side, jobs on the other, each edge weighted by its cost. Trying every assignment means n! possibilities (40,320 for n = 8); the Hungarian method finds the optimum in polynomial time.

The Hungarian method step by step

1. Subtract the smallest value of each row from that row (row reduction), then the smallest value of each column from that column (column reduction). Subtracting a constant from a whole row or column changes the total of every assignment by the same amount, so the optimal assignment does not change.

2. Cover all the zeros with the minimum number of horizontal and vertical lines. If the number of lines equals n, an assignment that uses only zeros exists, and it is optimal because no value is negative.

3. Otherwise, find the smallest uncovered value k. Subtract k from every uncovered value and add k to every value at the intersection of two lines, then go back to step 2. Every round creates a new zero where it is needed, so the method finishes after a few rounds.

How to draw the minimum number of lines

Covering the zeros by eye can use too many lines. The reliable way is the ticking procedure used in this calculator: box a largest set of independent zeros, tick the rows without a boxed zero, tick the columns that have a zero in a ticked row, tick the rows whose boxed zero is in a ticked column, and repeat. Lines through the unticked rows and the ticked columns then cover every zero.

By König’s theorem, the minimum number of lines that cover all the zeros equals the largest number of independent zeros. That is why the method can stop exactly when the number of lines reaches n.

Maximization, unbalanced problems and forbidden assignments

Maximization: subtract every profit from the largest profit and minimize these opportunity costs. Unbalanced problem: if there are more jobs than workers (or the reverse), add dummy rows (or columns) with cost 0; whoever is matched with a dummy is left out. Forbidden assignment: give the cell a very large cost M, so the method never chooses it. If the forbidden cells leave some workers with too few allowed jobs, no complete assignment exists at all (Hall’s marriage theorem).

History

Harold Kuhn published the method in 1955 and called it the Hungarian method in honour of the Hungarian mathematicians Dénes Kőnig and Jenő Egerváry, whose earlier results it builds on. James Munkres showed in 1957 that it always runs in polynomial time, which is why it is also called the Kuhn–Munkres algorithm.

Frequently asked questions

How do you solve an unbalanced assignment problem?

Add dummy rows or columns with cost 0 until the matrix is square, then apply the Hungarian method as usual. A worker matched with a dummy column (or a job matched with a dummy row) is left unassigned. The calculator adds the dummies for you and explains it in the steps.

How does the Hungarian method handle maximization?

Subtract every value from the largest value of the matrix and minimize the result. The assignment that minimizes these opportunity costs maximizes the original profit. Choose “Maximize profit” and the calculator does this conversion as its first step.

What if a worker cannot do a job?

Type M (or a dash) in that cell. It is treated as a very large cost, so the method never assigns it. If the forbidden cells make a complete assignment impossible, the calculator says so and shows which workers are short of allowed jobs.

Can there be more than one optimal assignment?

Yes. When the final matrix contains more than one complete set of independent zeros, several assignments have the same optimal total. The calculator checks this and shows another optimal assignment when one exists.

How large can the matrix be?

Up to 8 rows and 8 columns, which covers textbook and exam problems while keeping every matrix readable. The numbers of rows and columns do not need to be equal.

Related on LearnGraphTheory