The Wayback Machine - https://web.archive.org/web/20240805014917/https://www.geeksforgeeks.org/travelling-salesman-problem-using-hungarian-method/
Open In App

Travelling Salesman Problem using Hungarian method

Last Updated : 19 Dec, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

A travelling salesman plans to visit N cities in such a way that he visits each city exactly once and return to the city from where he started. The distance between City i and City j is Cij. Find the shortest tour he can take.

Note: Travelling from City i to City i is not possible and Cij may not be equal to Cji.

Examples:

Input: See the routes in the image.

Output: 80
Explanation: The shortest route can be achieved following the path C1 -> C2 -> C4 -> C3 -> C1. 
So the route length is 10 + 25 + 30 + 15 = 80.

Input: See the routes in the image.

Output: 15
Explanation: The shortest route is C1 -> C2 -> C3 -> C4 -> C5 -> C1. So the path length is 2 + 3 + 4 + 5 + 1 = 15.

 

Intuition: Suppose there are 3 cities namely city 1 ,city 2 , and city 3.  All the possible combinations to visit the cities are

1->2->3->1       (1->2, 2->3, 3->1)
1->3->2->1       (1->3, 3->2, 2->1)
2->1->3->2       (2->1, 1->3, 3->2)
2->3->1->2       (2->3, 3->1, 1->2)
3->1->2->3       (3->1, 1->2, 2->3)
3->2->1->3       (3->2, 2->1,1->3)

So, the cities can be visited in 6 different ways but if observed carefully it can be seen that tour 1st, 4th and 5th are equivalent and tour 2nd, 3rd and 6th are equivalent. Therefore, different combinations possible are actually 2 and those are

1->2->3->1
1->3->2->1

Hence, If there are N cities to visit then there can be (N-1)! ways to travel to each city exactly once and return to the starting city. This type of problem can be solved by the Hungarian method, branch and bound method, penalty method, and nearest neighbor method. We will see how to solve this type of problem using Hungarian method.

Approach: Mentioned below are the steps to follow to solve the problem using Hungarian method.
Consider the example shown in the image: 

Follow the illustrations of solution of the above example for better understanding.

Step 1: Locate the smallest cost elements in each row of the cost matrix. Subtract this element from every other element of the corresponding row. As a result, there is at least one zero in each row of the reduced cost matrix

Illustration:

  1. Subtracting 1st row with the minimum value 1.
  2. Subtracting 2nd row with the minimum value 2.
  3. Subtracting 3rd row with the minimum value 4.
  4. Subtracting 4th row with the minimum value 4.
  5. Subtracting 5th row with the minimum value 1.

Step 2: Similarly, locate the smallest element of each column In the reduced cost matrix obtained and subtract that from each element of the corresponding column. As a result, there should be at least one zero in each row and columns of the second reduced cost matrix.

Illustration:

  1. Subtracting 1st column with the minimum value 0.
  2. Subtracting 2nd column with the minimum value 0.
  3. Subtracting 3rd column with the minimum value 1.
  4. Subtracting 4th column with the minimum value 0.
  5. Subtracting 5th column with the minimum value 0.

Step 3: Make assignment in the matrix as follows.

  1. Move row by row until a row with single zero is found .
  2. Assign the cell containing the zero and cross off all other zeros in its column. 
  3. Continue this process until all the rows with single zero is assigned. 
  4. Repeat the procedure for each column.
  5. If a row and ( or ) a column has two or more zeros and one cannot be chosen by inspection then assign arbitrary any one of these zeros and cross off all other zeros of that row / column.

Illustration:

Row wise cell (1,5) is assigned. So, column wise cell (2,5) is crossed off.
Row wise cell (2,3) is assigned. So, column wise cell (5,3) is crossed off.
Row wise cell (3,4) is assigned.
Row wise cell (4,2) is assigned.
Row wise cell (5,1) is assigned.

Here, the number of assignment is equal to the number of cities and the tour is 1->5->1 and 2->3->4->2 which means salesman will travel from city 1 to city 5 and return to city 1 and then again start from city , travel to city 3, city 4 and return to city 2. Therefore, we are getting two cycle and hence it is not the solution.

Step 4: The next best solution can be obtained by bringing the minimum non-zero element and repeat the step 2 and step 3

Illustration: In this case, minimum non-zero element is 1.

Row wise cell (3,4) is assigned.      
Row wise cell (1,2) is assigned. So, column wise cell (4,2) is crossed off and row wise cell (1,5) is crossed off.
Row wise cell (2,5) is assigned. So, row wise cell (2,3) is crossed off and column wise cell (4,5) is crossed off.
Row wise cell (4,3) is assigned. So, column wise cell (5,3) is crossed off.
Row wise cell (5,1) is assigned.

Now, the sequence is 1->2->5->1 and 3->4->3. Again, we are getting two cycle therefore this is also not the solution.

Step 5:  Make different possible assignment

Illustration:

Row wise cell (3,4) is assigned.
Row wise cell (1,2) is assigned. So, column wise cell (4,2) is crossed off and row wise cell (1,5) is crossed off.
Row wise cell (2,3) is assigned. So, column wise cell (4,3) and (5,3) is crossed off and row wise cell (2,5) is crossed off.
Row wise cell (4,5) is assigned.
Row wise cell (5,1) is assigned.

The sequence is 1->2->3->4->5->1. So, this solution is optimal and total distance of this tour is 

Cell12 + Cell23 + Cell34 + Cell45 + Cell51 = (2+3+4+5+1) =15.



Similar Reads

Travelling Salesman Problem (TSP) using Reduced Matrix Method
Given a set of cities and the distance between every pair of cities, the problem is to find the shortest possible route that visits every city exactly once and returns to the starting point.  Examples: Input:  Output: 80Explanation: An optimal path is 1 - 2 - 4 - 3 - 1. Dynamic Programming Approach: This approach is already discussed in Set-1 of th
15+ min read
Approximate solution for Travelling Salesman Problem using MST
We introduced Travelling Salesman Problem and discussed Naive and Dynamic Programming Solutions for the problem in the previous post,. Both of the solutions are infeasible. In fact, there is no polynomial time solution available for this problem as the problem is a known NP-Hard problem. There are approximate algorithms to solve the problem though.
3 min read
Travelling Salesman Problem implementation using BackTracking
Travelling Salesman Problem (TSP): Given a set of cities and distance between every pair of cities, the problem is to find the shortest possible route that visits every city exactly once and returns back to the starting point.Note the difference between Hamiltonian Cycle and TSP. The Hamiltonian cycle problem is to find if there exist a tour that v
9 min read
Travelling Salesman Problem using Dynamic Programming
Travelling Salesman Problem (TSP): Given a set of cities and the distance between every pair of cities, the problem is to find the shortest possible route that visits every city exactly once and returns to the starting point. Note the difference between Hamiltonian Cycle and TSP. The Hamiltonian cycle problem is to find if there exists a tour that
13 min read
Bitmasking and Dynamic Programming | Travelling Salesman Problem
In this post, we will be using our knowledge of dynamic programming and Bitmasking technique to solve one of the famous NP-hard problem "Traveling Salesman Problem".Before solving the problem, we assume that the reader has the knowledge of DP and formation of DP transition relationBitmasking in DPTraveling Salesman problem To understand this concep
15+ min read
Hungarian Algorithm for Assignment Problem | Set 2 (Implementation)
Given a 2D array, arr of size N*N where arr[i][j] denotes the cost to complete the jth job by the ith worker. Any worker can be assigned to perform any job. The task is to assign the jobs such that exactly one worker can perform exactly one job in such a way that the total cost of the assignment is minimized. Example Input: arr[][] = {{3, 5}, {10,
7 min read
Hungarian Algorithm for Assignment Problem | Set 1 (Introduction)
Let there be n agents and n tasks. Any agent can be assigned to perform any task, incurring some cost that may vary depending on the agent-task assignment. It is required to perform all tasks by assigning exactly one agent to each task and exactly one task to each agent in such a way that the total cost of the assignment is minimized. Example: You
15+ min read
Traveling Salesman Problem using Branch And Bound
Given a set of cities and distance between every pair of cities, the problem is to find the shortest possible tour that visits every city exactly once and returns to the starting point. For example, consider the graph shown in figure on right side. A TSP tour in the graph is 0-1-3-2-0. The cost of the tour is 10+25+30+15 which is 80.We have discuss
15+ min read
Traveling Salesman Problem using Genetic Algorithm
AuPrerequisites: Genetic Algorithm, Travelling Salesman ProblemIn this article, a genetic algorithm is proposed to solve the travelling salesman problem. Genetic algorithms are heuristic search algorithms inspired by the process that supports the evolution of life. The algorithm is designed to replicate the natural selection process to carry genera
15+ min read
Proof that traveling salesman problem is NP Hard
Pre-requisite: Travelling Salesman Problem, NP Hard Given a set of cities and the distance between each pair of cities, the travelling salesman problem finds the path between these cities such that it is the shortest path and traverses every city once, returning back to the starting point. Problem - Given a graph G(V, E), the problem is to determin
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg