VOOZH about

URL: https://www.geeksforgeeks.org/dsa/approximate-solution-for-travelling-salesman-problem-using-mst/

⇱ Approximate solution for Travelling Salesman Problem using MST - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Approximate solution for Travelling Salesman Problem using MST

Last Updated : 23 Jul, 2025

Given a 2d matrix cost[][] of size n where cost[i][j] denotes the cost of moving from city i to city j. The task is to complete a tour from city 0 (0-based index) to all other towns such that we visit each city exactly once and then return to city 0 at minimum cost.

Note: There is a difference between the Hamiltonian Cycle and TSP. The Hamiltonian cycle problem is to find if there exists a tour that visits every city exactly once. Here we know that the Hamiltonian Tour exists (because the graph is complete) and, many such tours exist, the problem is to find a minimum weight Hamiltonian Cycle. 

Examples:

Input: cost[][] = [[0, 111], [112, 0]]
Output: 223
Explanation: We can visit 0->1->0 and cost = 111 + 112 = 223.

Input: cost[][] = [[0, 1000, 5000], [5000, 0, 1000], [1000, 5000, 0]]
Output: 3000
Explanation: We can visit 0->1->2->0 and cost = 1000 + 1000 + 1000 = 3000.

We introduced Travelling Salesman Problem and discussed Naive and Dynamic Programming Solutions for the problem. 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. The approximate algorithms work only if the problem instance satisfies Triangle-Inequality. 

What is Triangle Inequality?

The least distant path to reach a vertex j from i is always to reach j directly from i, rather than through some other vertex k (or vertices), i.e., dis(i, j) is always less than or equal to dis(i, k) + dist(k, j). The Triangle-Inequality holds in many practical situations. 

Using Minimum Spanning Tree - 2 Approximate Algorithm

When the cost function satisfies the triangle inequality, we can design an approximate algorithm for TSP that returns a tour whose cost is never more than twice the cost of an optimal tour. The idea is to use Minimum Spanning Tree (MST). Following is the MST based algorithm. 

Algorithm:

  1. Let 1 be the starting and ending point for salesman. 
  2. Construct MST from with 1 as root using Prim's Algorithm.
  3. List vertices visited in preorder walk of the constructed MST and add 1 at the end. 

Let us consider the following example. The first diagram is the given graph. The second diagram shows MST constructed with 1 as root. The preorder traversal of MST is 1-2-4-3. Adding 1 at the end gives 1-2-4-3-1 which is the output of this algorithm.

👁 Euler1

👁 MST_TSP

In this case, the approximate algorithm produces the optimal tour, but it may not produce optimal tour in all cases. 

How is algorithm 2-approximate?

The cost of the output produced by the above algorithm is never more than twice the cost of best possible output. Let us see how is this guaranteed by the above algorithm. 

Let us define a term full walk to understand this. A full walk is lists all vertices when they are first visited in preorder, it also list vertices when they are returned after a subtree is visited in preorder. The full walk of above tree would be 1-2-1-4-1-3-1. 

Following are some important facts that prove the 2-approximateness. 

  • The cost of best possible Travelling Salesman tour is never less than the cost of MST. (The definition of MST says, it is a minimum cost tree that connects all vertices). 
  • The total cost of full walk is at most twice the cost of MST (Every edge of MST is visited at-most twice) 
  • The output of the above algorithm is less than the cost of full walk. In above algorithm, we print preorder walk as output. In preorder walk, two or more edges of full walk are replaced with a single edge. For example, 2-1 and 1-4 are replaced by 1 edge 2-4. So if the graph follows triangle inequality, then this is always true. 

From the above three statements, we can conclude that the cost of output produced by the approximate algorithm is never more than twice the cost of best possible solution. 

Below is given the implementation:


Output
3000

Time Complexity: O(n ^ 3), the time complexity of triangleInequality() function is O(n ^ 3) as we are using 3 nested loops, and all other functions are working in O(n ^ 2), and O(n ^ 2 * log n) time complexity, thus the overall time complexity will be O(n ^ 3).
Space Complexity: O(n ^ 2), to store the adjacency list, and creating MST.

Using Christofides Algorithm - 1.5 Approximate Algorithm

The Christofides algorithm or Christofides–Serdyukov algorithm is an algorithm for finding approximate solutions to the travelling salesman problem, on instances where the distances form a metric space (they are symmetric and obey the triangle inequality).It is an approximation algorithm that guarantees that its solutions will be within a factor of 3/2 of the optimal solution length

Algorithm:

  • Create a minimum spanning tree T of G.
  • Let O be the set of vertices with odd degree in T. By the handshaking lemma, O has an even number of vertices.
  • Find a minimum-weight perfect matching M in the subgraph induced in G by O.
  • Combine the edges of M and T to form a connected multigraph H in which each vertex has even degree.
  • Form an Eulerian circuit in H.
  • Make the circuit found in previous step into a Hamiltonian circuit by skipping repeated vertices (shortcutting).

Below is given the implementation:


Output
3000

Time Complexity: O(n ^ 3), the time complexity of triangleInequality() function is O(n ^ 3) as we are using 3 nested loops, and all other functions are working in O(n ^ 2), and O(n ^ 2 * log n) time complexity, thus the overall time complexity will be O(n ^ 3).
Space Complexity: O(n ^ 2), to store the adjacency list, and creating MST.

Comment
Article Tags: