Hungarian Algorithm

on July 09, 2021 · 6 mins read

Introduction to the Hungarian Algorithm

The Hungarian Algorithm is an algorithm that is used to solve the Assignment Problem. It is a combinatorial optimization algorithm that is used to find the optimal solution to a given problem. It is also known as the Kuhn-Munkres algorithm, after two of its inventors.

The Assignment Problem is a problem where there are a number of agents and tasks. Each agent is assigned to one task, and each task is assigned to one agent. The goal is to assign each agent to a task such that the total cost of the assignment is minimized.

The Hungarian Algorithm is an efficient and effective way to solve the Assignment Problem. In this article, we will discuss the basics of the algorithm, how it works, and how it can be used to solve the Assignment Problem.

What Is the Assignment Problem?

The Assignment Problem is a problem where there are a number of agents and tasks. Each agent is assigned to one task, and each task is assigned to one agent. The goal is to assign each agent to a task such that the total cost of the assignment is minimized.

For example, consider a company that needs to assign employees to tasks. Each employee has a different skill set and each task requires a different skill set. The goal is to assign each employee to a task such that the total cost of the assignment is minimized.

How Does the Hungarian Algorithm Work?

The Hungarian Algorithm is an efficient and effective way to solve the Assignment Problem. It works by finding the optimal assignment of agents to tasks such that the total cost of the assignment is minimized.

The algorithm works by constructing a matrix with the cost of assigning each agent to each task. The algorithm then finds the optimal assignment by finding the lowest cost path through the matrix.

The algorithm works by iteratively reducing the cost of the assignment by finding a set of zeros in the matrix. A zero is a cell in the matrix that has a cost of zero. The algorithm then finds a set of zeros such that each row and column contains at least one zero. This set of zeros is called a matching.

Once a matching is found, the algorithm reduces the cost of the assignment by subtracting the minimum cost from each row and column that contains a zero. This process is repeated until there are no more zeros in the matrix. At this point, the algorithm has found the optimal assignment.

Pseudocode

The following pseudocode describes the Hungarian Algorithm:

// Input: Cost matrix C
// Output: Assignment matrix A

// Step 1: Initialize A to all zeros

// Step 2: Find the minimum cost in each row and subtract it from each element in the row

for i = 1 to n
    min = min(C[i, 1], C[i, 2], ..., C[i, n])
    for j = 1 to n
        C[i, j] = C[i, j] - min

// Step 3: Find the minimum cost in each column and subtract it from each element in the column

for j = 1 to n
    min = min(C[1, j], C[2, j], ..., C[n, j])
    for i = 1 to n
        C[i, j] = C[i, j] - min

// Step 4: Find a set of zeros such that each row and column contains at least one zero

while (there are zeros in the matrix)
    find a set of zeros such that each row and column contains at least one zero

// Step 5: Reduce the cost of the assignment by subtracting the minimum cost from each row and column that contains a zero

for each row and column that contains a zero
    min = min(C[i, 1], C[i, 2], ..., C[i, n])
    for j = 1 to n
        C[i, j] = C[i, j] - min

// Step 6: Repeat steps 4 and 5 until there are no more zeros in the matrix

// Step 7: The optimal assignment is found

for i = 1 to n
    for j = 1 to n
        if C[i, j] == 0
            A[i, j] = 1
        else
            A[i, j] = 0

Conclusion

The Hungarian Algorithm is an efficient and effective way to solve the Assignment Problem. It works by constructing a matrix with the cost of assigning each agent to each task, and then finding the optimal assignment by finding the lowest cost path through the matrix.

The algorithm works by iteratively reducing the cost of the assignment by finding a set of zeros in the matrix. Once a matching is found, the algorithm reduces the cost of the assignment by subtracting the minimum cost from each row and column that contains a zero. This process is repeated until there are no more zeros in the matrix. At this point, the algorithm has found the optimal assignment.

The Hungarian Algorithm is a powerful tool for solving the Assignment Problem, and it is an important algorithm for developers to know.