License YOLO
Back to Ultralytics Glossary

Hungarian Algorithm

Hungarian algorithm finds the minimum-cost one-to-one assignment across two sets, powering object tracking and detection training in computer vision.

The Hungarian algorithm is an exact optimization method that finds the lowest-cost one-to-one matching that pairs every item in the smaller of two sets. It optimizes the total cost across all selected pairs. In computer vision, it connects detections with tracked objects or predictions with training targets.

Harold W. Kuhn introduced it in 1955 in The Hungarian Method for the Assignment Problem. Hungarian method and Kuhn–Munkres algorithm are alternative names for the same algorithm family. Its name honors Hungarian mathematicians Dénes Kőnig and Jenő Egerváry, whose work underpins the method.

How the Hungarian Algorithm Works#

The input is a cost matrix: each row represents an item from one set, each column represents an item from the other, and each entry measures the cost of pairing them. For a square matrix, the solution selects exactly one entry per row and column. A greedy strategy commits to attractive individual pairs; Hungarian matching finds the globally minimum total.

In the classic matrix reduction procedure, subtract each row's minimum from that row, then each column's minimum from that column. These reductions preserve the best complete assignment because every assignment uses every row and column once. Cover all zeros with the fewest horizontal and vertical lines. If n lines are needed for an n x n matrix, select n zeros with no shared row or column. Otherwise, subtract the smallest uncovered value from uncovered entries, add it at intersections of covering lines, and repeat the covering test. Calculate the assignment's cost using the original matrix.

Efficient Hungarian implementations run in O(n³) time for square matrices, compared with n! possible complete assignments.

Applications in Computer Vision#

Multi-object tracking. The original SORT design combines Hungarian assignment with a Kalman filter, which predicts each track's position. Intersection over union measures overlap between predicted and detected bounding boxes. SORT's reference implementation solves assignment using LAPJV or SciPy, rejects low-overlap associations, and creates identities for unmatched detections.

Object detection training. DETR uses Hungarian matching to pair unordered predictions with labeled objects. Its transformer produces prediction slots, and matching combines classification and box-location costs. With enough slots, each target receives one prediction, making the loss function independent of annotation order; unmatched slots learn the "no object" class. Ultralytics applies this assignment formulation in RT-DETR: its HungarianMatcher combines classification, L1 coordinate distance, and generalized IoU costs. Its linear assignment backend uses a modified Jonker–Volgenant solver to select training pairs.

Hungarian Matching in Python#

The munkres Python package implements the Hungarian method. Install it with pip install munkres, then solve its sample cost matrix:

from munkres import Munkres

costs = [
    [5, 9, 1],
    [10, 3, 2],
    [8, 7, 4],
]
solver = Munkres()
assignments = solver.compute(costs)
# Read costs from the original matrix.
minimum_cost = sum(costs[row][col] for row, col in assignments)
print(f"Minimum total cost: {minimum_cost}")

The output is Minimum total cost: 12, the smallest sum attainable while selecting one entry per row and column.

The assignment problem is the task; the Hungarian algorithm is one solver. Non-maximum suppression removes competing detections within an image. Object re-identification supplies appearance information for association, often through embeddings compared using cosine similarity. Such scores can become matching costs.

Unequal group sizes produce rectangular matrices. Tracking also needs rejection rules or dummy entries with defined unmatched costs to accommodate missing detections and newly appearing objects. A wrong association causes an identity switch: a track ID transfers to another object.

SciPy's linear assignment solver solves the same optimization problem using a modified Jonker–Volgenant algorithm. For an r x c rectangular matrix, it returns exactly min(r, c) pairs when a feasible full assignment exists.

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Turn drone and aerial imagery into real-time insights for agriculture, aquaculture, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Apply computer vision in aerospace with Ultralytics YOLO for aircraft inspection, runway FOD detection, airport ground operations, and composite defect detection.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning