Short answer: The Hungarian algorithm, or Kuhn-Munkres, solves the assignment problem: pair n agents with n jobs so that the sum of costs is minimum. It maintains dual variables on agents and jobs so that a reduced cost c(i, j) − u(i) − v(j) is never negative, and it grows an alternating tree in the equality subgraph where the reduced cost is zero. A phase ends by augmenting the matching or by raising duals. The time is O(n³) for a dense n by n cost matrix.
What the problem actually asks
Minimum-cost bipartite matching with equal sides is the assignment problem. Cardinality algorithms ignore the costs. Min-cost flow solves the problem and hides the combinatorics inside a flow code. The Hungarian algorithm keeps the duals explicit. Complementary slackness says a perfect matching of tight edges, those with reduced cost zero, is optimal. The algorithm grows such a matching and changes the duals when the tight graph has no augmenting path.
A phase starts at a free agent and builds an alternating tree of tight edges. If the tree reaches a free job, the matching augments. If it stalls, every job in the tree is matched and a dual update creates a new tight edge out of the tree, or makes an existing one tight, without destroying tightness inside.
What the algorithm guarantees
The algorithm returns a perfect matching of minimum total cost, or reports that none exists if some costs are forbidden. For a full matrix it always exists. Time is O(n³) with the classical dense implementation that scans the matrix to find the dual step. Extra memory is the dual arrays, the mate array, and the tree. The same optimum is what successive shortest paths compute on the assignment flow network.
Maximizing a weight matrix is minimizing the negated weights, after a shift that keeps the implementation’s nonnegativity if it assumes nonnegative reduced costs. The dual update’s step size is the minimum reduced cost on an edge from the tree’s agents to a job outside the tree. A wrong step goes negative and the invariant dies, or stays zero and the phase loops.
The working steps
- Subtract the minimum of each row from that row, then the minimum of each column from that column, or initialize duals u and v so that every reduced cost is nonnegative. Start with an empty matching.
- While some agent is free, grow an alternating tree of tight edges from that agent.
- If the tree reaches a free job, augment the matching along the tree path and start the next free agent.
- If the tree stalls, let δ be the smallest reduced cost from an agent in the tree to a job outside it. Add δ to u on tree agents and subtract δ from v on tree jobs. New tight edges appear. Continue the tree.
- When every agent is matched, the matching is optimal and its cost equals the sum of all duals.
The cubic bound is for a dense n by n assignment. A sparse min-cost matching with supplies and demands is min-cost flow. Cardinality without costs is Hopcroft-Karp or Kuhn. The dual step has to be the minimum reduced cost. A fixed step of 1 is correct only for integer costs and can be slow, and it is wrong for general reals if it overshoots.
Approaches that look simpler and fail
| Approach | Why it fails when the dual step overshoots the next tight edge |
|---|---|
| Raise duals by 1 on integer data with large costs | The matching stays correct if the step never skips a tight edge, and the number of raises can become proportional to the cost values. The minimum reduced cost finishes a phase in polynomial matrix scans. |
| Augment along a non-tight edge | Complementary slackness fails. The matching can be perfect and still more expensive than the duals claim. |
| Use Kuhn’s algorithm and then sort by cost | Cardinality matching does not look at costs. A cheap edge it ignored can be part of the unique optimum. |
| Forget the column dual when reading the cost | The reduced cost is c(i, j) − u(i) − v(j). Dropping v makes a tight edge look positive and stalls a phase that should augment. |
Unweighted bipartite matching belongs to Hopcroft-Karp. A general min-cost flow, with capacities above one, belongs to successive shortest paths or a cost-scaling push-relabel. The Hungarian algorithm is the assignment special case with the duals in the open.
A working example
This Python fragment grows an alternating tree on tight edges and raises duals by the smallest reduced cost when the tree stalls. The assignment cost equals the brute-force minimum, 5.
import itertools
cost = [[4, 1, 3], [2, 0, 5], [3, 2, 2]]
def hungarian(a):
n = len(a)
u = [0] * (n + 1)
v = [0] * (n + 1)
p = [0] * (n + 1)
way = [0] * (n + 1)
for i in range(1, n + 1):
p[0] = i
j0 = 0
minv = [10 ** 9] * (n + 1)
used = [False] * (n + 1)
while True:
used[j0] = True
i0 = p[j0]
delta, j1 = 10 ** 9, 0
for j in range(1, n + 1):
if used[j]:
continue
cur = a[i0 - 1][j - 1] - u[i0] - v[j]
if cur < minv[j]:
minv[j] = cur
way[j] = j0
if minv[j] < delta:
delta, j1 = minv[j], j
for j in range(n + 1):
if used[j]:
u[p[j]] += delta
v[j] -= delta
else:
minv[j] -= delta
j0 = j1
if p[j0] == 0:
break
while True:
j1 = way[j0]
p[j0] = p[j1]
j0 = j1
if j0 == 0:
break
assign = [p.index(i) - 1 for i in range(1, n + 1)]
return sum(a[i][assign[i]] for i in range(n))
brute = min(sum(cost[i][perm[i]] for i in range(3)) for perm in itertools.permutations(range(3)))
assert hungarian(cost) == brute == 5
print(hungarian(cost))
Where it is used
The Hungarian algorithm, Kuhn-Munkres, solves an assignment with costs. SciPy linear_sum_assignment is that solver, and multi-object tracking uses it to match detections to tracks. An unweighted matching uses Hopcroft-Karp and ignores cost.
When you can drop this algorithm
Drop a hand-rolled Hungarian implementation if a tested min-cost flow solver is already the tool and the matrix is only one client. Keep the algorithm when the problem is an n by n assignment and the O(n³) dual growth is the intended solution.
Why this stays published
We publish this so a search for the Hungarian algorithm returns the tight-edge tree and the dual step equal to the minimum reduced cost leaving the tree. The Hungarian algorithm solves the n by n assignment problem in O(n³) time by growing a matching of tight edges and raising duals when that growth stalls.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
