VOOZH about

URL: https://www.geeksforgeeks.org/dsa/hungarian-algorithm-assignment-problem-set-1-introduction/

⇱ Hungarian Algorithm for Assignment Problem (Introduction and Implementation) - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Hungarian Algorithm for Assignment Problem (Introduction and Implementation)

Last Updated : 26 Apr, 2025

You are the head of a company with n agents and n tasks. Every agent incurs a different cost to complete each task, as given by the cost[][] matrix, where cost[i][j] represents the cost for the ith agent to perform the jth task. Your objective is to assign exactly one agent to each task—and one task to each agent—in such a way that the total cost of all assignments is minimized.

Example:

Input: n = 3
cost[][] = [ [2500, 4000, 3500],
[4000, 6000, 3500],
[2000, 4000, 2500] ]
Output: 9500
Explanation: The optimal assignment is to assign job 2 to the 1st worker, job 3 to the 2nd worker and job 1 to the 3rd worker.
Hence, the optimal cost is 4000 + 3500 + 2000 = 9500.

This example can also be understood in the following way:
You work as a manager for a chip manufacturer, and you currently have 3 people on the road meeting clients. Your salespeople are in Jaipur, Pune and Bangalore, and you want them to fly to three other cities: Delhi, Mumbai and Kerala. The table below shows the cost of airline tickets in INR between the cities: 👁 hungarian1
The question: where would you send each of your salespeople in order to minimize fair? Possible assignment: Cost = 11000 INR 👁 hungerain2
Other Possible assignment: Cost = 9500 INR and this is the best of the 3! possible assignments. 👁 hungarian4

Approach:

The Hungarian algorithm (also known as the Munkres assignment algorithm) is designed to find an optimal assignment between n agents and n tasks with a worst-case time complexity of O(n³). Its key insight is that if you add or subtract a constant from all elements in any row or column of the cost matrix, the set of optimal assignments does not change. By leveraging this property, the algorithm reduces the original cost matrix to one containing zeros, thereby simplifying the assignment process to one where each agent can be assigned a task at zero penalty.

Follow the below given step-by-step approach:

  1. Row Reduction: For each row in the matrix, identify the smallest element and subtract it from every element in that row.
  2. Column Reduction: For each column in the matrix, identify the smallest element and subtract it from every element in that column.
  3. Zero Coverage: Cover all zeros in the resulting matrix using the minimum number of horizontal and vertical lines.
  4. Optimality Check:
    • If the number of covering lines equals n, then an optimal assignment is possible, and the algorithm terminates.
    • If the number of covering lines is less than n, proceed to the next step.
  5. Adjust the Matrix:
    • Determine the smallest entry that is not covered by any line.
    • Subtract this entry from every uncovered row.
    • Add the same entry to every column that is covered by a line.
    • Return to step 3 with the modified matrix.
  6. This process is repeated until an optimal assignment is identified.

Consider few examples to understand the approach:

Let the 2D array be:
cost[][] = [ [2500, 4000, 3500],
[4000, 6000, 3500],
[2000, 4000, 2500] ]

Step 1: Subtract minimum of every row. Thus, 2500, 3500 and 2000 are subtracted from rows 1, 2 and 3 respectively.

cost[][] = [ [0, 1500, 1000],
[500, 2500, 0],
[0, 2000, 500] ]

Step 2: Subtract minimum of every column. Thus 0, 1500 and 0 are subtracted from columns 1, 2 and 3 respectively.

cost[][] = [ [0, 0, 1000],
[500, 1000, 0],
[0, 500, 500] ]

Step 3: Cover all zeroes with minimum number of horizontal and vertical lines.

👁 Image

Step 4: Since we need 3 lines to cover all zeroes, the optimal assignment is found. 

cost[][] = [ [2500, 4000, 3500],
[4000, 6000, 3500],
[2000, 4000, 2500] ]

So the optimal cost is 4000 + 3500 + 2000 = 9500

In the above example, the first check for optimality did give us solution, but what if we the number covering lines is less than n. The below given example consider this case:

Let the 2D array be:
cost[][] = [ [1500, 4000, 4500],
[2000, 6000, 3500],
[2000, 4000, 2500] ]

Step 1: Subtract minimum of every row. Thus, 1500, 2000 and 2000 are subtracted from rows 1, 2 and 3 respectively.

cost[][] = [ [0, 2500, 3000],
[0, 4000, 1500],
[0, 2000, 500] ]

Step 2: Subtract minimum of every column. Thus 0, 2000 and 500 are subtracted from columns 1, 2 and 3 respectively.

cost[][] = [ [0, 500, 2500],
[0, 2000, 1000],
[0, 0, 0] ]

Step 3: Cover all zeroes with minimum number of horizontal and vertical lines.

Step 4: Since we need only 2 lines to cover all zeroes, the optimal assignment is not found. 

Step 5: We subtract the smallest uncovered entry from all uncovered rows. Smallest entry is 500.

cost[][] = [ [-500, 0, 2000],
[-500, 1500, 500],
[0, 0, 0] ]

Step 6: Then we add the smallest entry to all covered columns, we get

cost[][] = [ [0, 0, 2000],
[0, 1500, 500],
[500, 0, 0] ]

Now we return to Step 3: Here we cover again using lines. and go to Step 4. Since we need 3 lines to cover, we found the optimal solution.

cost[][] = [ [1500, 4000, 4500],
[2000, 6000, 3500],
[2000, 4000, 2500] ]

So the optimal cost is 4000 + 2000 + 2500 = 8500

Below is given the implementation:


Output
9500

Time complexity : O(n^3), where n is the number of workers and jobs. This is because the algorithm implements the Hungarian algorithm, which is known to have a time complexity of O(n^3).
Space complexity :  O(n^2), where n is the number of workers and jobs. This is because the algorithm uses a 2D cost matrix of size n x n to store the costs of assigning each worker to a job, and additional arrays of size n to store the labels, matches, and auxiliary information needed for the algorithm.

Comment
Article Tags: