Hungarian algorithm
The Hungarian algorithm is usually studied in algorithms and data structure courses. Nevertheless, it also has applications in matching systems, and, in particular, is frequently used to solve the tracklet matching problem mentioned above.
We are going to study the workflow of the Hungarian algorithm in simpler settings. Once we have understood how it is used, we will be able to apply it to MOT problems as well easily.
The algorithm is called Hungarian because it is “was largely based on the earlier works of two Hungarian mathematicians, Dénes Kőnig and Jenő Egerváry” — Hungarian Algorithm | Wikipedia
Formulation
There exist many examples to demonstrate the Hungarian algorithm. I like the one with workers and tasks. Here is the formulation:
There are n workers available, and n tasks need to be completed by them. There is information about the salary every worker receives for every task. As for the company director, the problem consists of optimally assigning tasks to workers given the following conditions:
- every worker gets assigned only one task;
- all tasks get completed;
- the money spent on salaries should be minimized.
We are going to solve this problem by using the following 4 x 4 cost matrix as an example:

Geometrically, given the matrix above, the objective consists of choosing n matrix elements in a manner that there are no repeating elements in any row or column, and the total sum of chosen elements is minimal.
Idea
The Hungarian method involves transforming the initial cost matrix into a new form that facilitates the solution search. For this, we will use several matrix transformations. Even though the matrix will be modified, the problem will always remain equivalent, meaning that the solution will still be the same.
To keep things simple, we are not going to prove here mathematically why this or that matrix transformation maintains the problem invariant. Instead, we will provide some logical thoughts to explain why the solution remains the same.
Example
1. Row & column reduction
The first step consists of identifying a minimum element in every row of the matrix and subtracting it from each row. The idea here is to get at least one zero in every row. In practice, having more zeros simplifies the problem.
Suppose that some number m is subtracted from a given row. While the objective value (the total minimized salary) changes during the transformation (it decreases by m), the relative cost between the assignments for the same worker remains unchanged. Therefore, the ranking of solutions remained unchanged.
The analogous procedure is then performed on columns: a minimum element in every column is subtracted from that column.

After the first two transformations, we obtain a matrix with some zeros representing potential assignments.
The next step consists of drawing the minimal number of horizontal and vertical lines in a way that they pass through all zeros in the matrix. In the image below, we can draw k = 3 lines in total to cover all zeros.

If the number of drawn lines equals the dimension n of the matrix, then we have found a solution. The only step left in such a case is to choose n zeros such that no zero is on the same horizontal or vertical line as another zero.
Since, in our example, n ≠ k, (the matrix dimension n = 4; the number of drawn lines k = 3), it means we must perform an adjustment step.
2. Adjustment step
So far, we have drawn several lines, and we can classify the matrix elements into three categories:
- Uncovered elements;
- Covered elements (only once);
- Corner elements (elements that are covered twice — horizontally and vertically).
The idea of the adjustment step consists of identifying the minimal element among uncovered elements and subtracting it from all uncovered elements. At the same time, this value gets added to all corner elements.
In our example, the minimal uncovered element is 2. As a result, we subtract 2 from all uncovered elements (in red) and 2 from all corner elements (in green).

Although it might not be obvious why the problem invariant remains maintained after the adjustment step, it can be mathematically proven that subtracting a number from all uncovered costs is equally compensated by its addition to all covered costs twice, which maintains the optimal solution the same.
This transformation was a single iteration of the adjustment step, which led us to another equivalent matrix form. As before, we perform the check to find out if we can cover all zeros using only n lines.

As we can see, this time we indeed have to draw k = n = 4 lines to cover all zeros. It means that we can finally retrieve our solution!
Otherwise, if we could have drawn fewer than k < 4 lines, we should have repeated the adjustment step until the number of lines became k = 4.
3. Solution retrieval
The only step left is to find n zeros on different vertical and horizontal lines. If doing it manually, it is better to start with lines that have fewer zeroes.
After finding the positions of zeros in the matrix, we can switch back to the original matrix and choose the initial elements with the same positions as the found zeros. And that will be the final assignment!

The complexity of the Hungarian algorithm is O(n³), where n is the matrix dimension.
The assignment problem we have just seen can also be solved by linear programming methods.
Applications
One of the most obvious applications of the Hungarian algorithm consists of using it for assignment problems, where, given n tasks, the goal is to optimally associate them with other people or objects (e.g., machines) that will complete them.
There is also a particular application in computer vision. Many video tracking algorithms (MOT) are based on the combination of standard image detection algorithms (e.g., YOLO) and logic of merging detection results from several independent frames into a video flow.
Let us take a simplified example of two consecutive image frames of a video:

We run a MOT algorithm for object tracking based on YOLO. The predictions of YOLO are shown below in gray boxes.

The objective is to associate bounding boxes between both frames to keep track of objects. One possible way to do this involves analyzing the change in distance between bounding boxes between the two frames. It is logical to assume that a pair of bounding boxes belongs to the same object if their position do not change a lot between the two frames.
Here is where the Hungarian algorithm comes into play. We can construct a matrix representing pairwise distances (which will be the cost functions) between the coordinates of different bounding boxes in both frames. We can find such a mapping that minimizes the total distance between bounding boxes.

By running the Hungarian algorithm, we get the following mappings:
(A₁, C₂), (B₁, A₂), (C₁, B₂).
We can verify that they result in a total cost of 8 + 1 + 5 = 14 which is the minimal possible function cost for this matrix. Hence, the found mappings are optimal.

Certainly, modern MOT algorithms consider additional factors when matching bounding boxes, including trajectory analysis, object speed and direction, and physical similarity, among others. For simplicity, we only considered a single factor: the distance between the bounding boxes. However, in reality, more factors are taken into account.
Conclusion
In this article, we have looked at the Hungarian algorithm used to solve task assignment problems. By performing simple operations on the initial data matrix, the Hungarian algorithm transforms it to other formats while maintaining the problem invariant.
Despite its cubic complexity, the Hungarian algorithm has a wide range of applications in matching and computer vision problems where the number of objects is not too large.
Resources
All images unless otherwise noted are by the author.