Open In App

Traveling Salesman Problem (TSP) Implementation

Last Updated : 31 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Travelling Salesman Problem (TSP) : Given a set of cities and distances 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 visits every city exactly once. Here we know that Hamiltonian Tour exists (because the graph is complete) and in fact, many such tours exist, the problem is to find a minimum weight Hamiltonian Cycle. 
For example, consider the graph shown in the figure on the right side. A TSP tour in the graph is 1-2-4-3-1. The cost of the tour is 10+25+30+15 which is 80.
The problem is a famous NP-hard problem. There is no polynomial-time known solution for this problem. 
 

Examples: 

Output of Given Graph:
minimum weight Hamiltonian Cycle :
10 + 25 + 30 + 15 := 80

In this post, the implementation of a simple solution is discussed.

  1. Consider city 1 as the starting and ending point. Since the route is cyclic, we can consider any point as a starting point.
  2. Generate all (n-1)! permutations of cities.
  3. Calculate the cost of every permutation and keep track of the minimum cost permutation.
  4. Return the permutation with minimum cost.

Below is the implementation of the above idea 

C++




// CPP program to implement traveling salesman
// problem using naive approach.
#include <bits/stdc++.h>
using namespace std;
#define V 4
 
// implementation of traveling Salesman Problem
int travllingSalesmanProblem(int graph[][V], int s)
{
    // store all vertex apart from source vertex
    vector<int> vertex;
    for (int i = 0; i < V; i++)
        if (i != s)
            vertex.push_back(i);
 
    // store minimum weight Hamiltonian Cycle.
    int min_path = INT_MAX;
    do {
 
        // store current Path weight(cost)
        int current_pathweight = 0;
 
        // compute current path weight
        int k = s;
        for (int i = 0; i < vertex.size(); i++) {
            current_pathweight += graph[k][vertex[i]];
            k = vertex[i];
        }
        current_pathweight += graph[k][s];
 
        // update minimum
        min_path = min(min_path, current_pathweight);
 
    } while (
        next_permutation(vertex.begin(), vertex.end()));
 
    return min_path;
}
 
// Driver Code
int main()
{
    // matrix representation of graph
    int graph[][V] = { { 0, 10, 15, 20 },
                       { 10, 0, 35, 25 },
                       { 15, 35, 0, 30 },
                       { 20, 25, 30, 0 } };
    int s = 0;
    cout << travllingSalesmanProblem(graph, s) << endl;
    return 0;
}


Java




// Java program to implement
// traveling salesman problem
// using naive approach.
import java.util.*;
class GFG{
     
static int V = 4;
 
// implementation of traveling
// Salesman Problem
static int travllingSalesmanProblem(int graph[][],
                                    int s)
{
  // store all vertex apart
  // from source vertex
  ArrayList<Integer> vertex =
            new ArrayList<Integer>();
   
  for (int i = 0; i < V; i++)
    if (i != s)
      vertex.add(i);
 
  // store minimum weight
  // Hamiltonian Cycle.
  int min_path = Integer.MAX_VALUE;
  do
  {
    // store current Path weight(cost)
    int current_pathweight = 0;
 
    // compute current path weight
    int k = s;
     
    for (int i = 0;
             i < vertex.size(); i++)
    {
      current_pathweight +=
              graph[k][vertex.get(i)];
      k = vertex.get(i);
    }
    current_pathweight += graph[k][s];
 
    // update minimum
    min_path = Math.min(min_path,
                        current_pathweight);
 
  } while (findNextPermutation(vertex));
 
  return min_path;
}
 
// Function to swap the data
// present in the left and right indices
public static ArrayList<Integer> swap(
              ArrayList<Integer> data,
              int left, int right)
{
  // Swap the data
  int temp = data.get(left);
  data.set(left, data.get(right));
  data.set(right, temp);
 
  // Return the updated array
  return data;
}
   
// Function to reverse the sub-array
// starting from left to the right
// both inclusive
public static ArrayList<Integer> reverse(
              ArrayList<Integer> data,
              int left, int right)
{
  // Reverse the sub-array
  while (left < right)
  {
    int temp = data.get(left);
    data.set(left++,
             data.get(right));
    data.set(right--, temp);
  }
 
  // Return the updated array
  return data;
}
   
// Function to find the next permutation
// of the given integer array
public static boolean findNextPermutation(
                      ArrayList<Integer> data)
  // If the given dataset is empty
  // or contains only one element
  // next_permutation is not possible
  if (data.size() <= 1)
    return false;
 
  int last = data.size() - 2;
 
  // find the longest non-increasing
  // suffix and find the pivot
  while (last >= 0)
  {
    if (data.get(last) <
        data.get(last + 1))
    {
      break;
    }
    last--;
  }
 
  // If there is no increasing pair
  // there is no higher order permutation
  if (last < 0)
    return false;
 
  int nextGreater = data.size() - 1;
 
  // Find the rightmost successor
  // to the pivot
  for (int i = data.size() - 1;
           i > last; i--) {
    if (data.get(i) >
        data.get(last))
    {
      nextGreater = i;
      break;
    }
  }
 
  // Swap the successor and
  // the pivot
  data = swap(data,
              nextGreater, last);
 
  // Reverse the suffix
  data = reverse(data, last + 1,
                 data.size() - 1);
 
  // Return true as the
  // next_permutation is done
  return true;
}
 
// Driver Code
public static void main(String args[])
{
  // matrix representation of graph
  int graph[][] = {{0, 10, 15, 20},
                   {10, 0, 35, 25},
                   {15, 35, 0, 30},
                   {20, 25, 30, 0}};
  int s = 0;
  System.out.println(
  travllingSalesmanProblem(graph, s));
}
}
 
// This code is contributed by adityapande88


Python3




# Python3 program to implement traveling salesman
# problem using naive approach.
from sys import maxsize
from itertools import permutations
V = 4
 
# implementation of traveling Salesman Problem
def travellingSalesmanProblem(graph, s):
 
    # store all vertex apart from source vertex
    vertex = []
    for i in range(V):
        if i != s:
            vertex.append(i)
 
    # store minimum weight Hamiltonian Cycle
    min_path = maxsize
    next_permutation=permutations(vertex)
    for i in next_permutation:
 
        # store current Path weight(cost)
        current_pathweight = 0
 
        # compute current path weight
        k = s
        for j in i:
            current_pathweight += graph[k][j]
            k = j
        current_pathweight += graph[k][s]
 
        # update minimum
        min_path = min(min_path, current_pathweight)
         
    return min_path
 
 
# Driver Code
if __name__ == "__main__":
 
    # matrix representation of graph
    graph = [[0, 10, 15, 20], [10, 0, 35, 25],
            [15, 35, 0, 30], [20, 25, 30, 0]]
    s = 0
    print(travellingSalesmanProblem(graph, s))


C#




// C# program to implement
// traveling salesman problem
// using naive approach.
using System;
using System.Collections.Generic;
 
class GFG {
 
    static int V = 4;
 
    // implementation of traveling Salesman Problem
    static int travllingSalesmanProblem(int[, ] graph,
                                        int s)
    {
        List<int> vertex = new List<int>();
 
        for (int i = 0; i < V; i++)
            if (i != s)
                vertex.Add(i);
 
        // store minimum weight
        // Hamiltonian Cycle.
        int min_path = Int32.MaxValue;
 
        do {
            // store current Path weight(cost)
            int current_pathweight = 0;
            int k = s;
 
            // compute current path weight
            for (int i = 0; i < vertex.Count; i++) {
                current_pathweight += graph[k, vertex[i]];
                k = vertex[i];
            }
 
            current_pathweight += graph[k, s];
 
            // update minimum
            min_path
                = Math.Min(min_path, current_pathweight);
 
        } while (findNextPermutation(vertex));
 
        return min_path;
    }
 
    // Function to swap the data resent in the left and
    // right indices
    public static List<int> swap(List<int> data, int left,
                                 int right)
    {
        // Swap the data
        int temp = data[left];
        data[left] = data[right];
        data[right] = temp;
 
        // Return the updated array
        return data;
    }
 
    // Function to reverse the sub-array starting from left
    // to the right both inclusive
    public static List<int> reverse(List<int> data,
                                    int left, int right)
    {
        // Reverse the sub-array
        while (left < right) {
            int temp = data[left];
            data[left++] = data[right];
            data[right--] = temp;
        }
 
        // Return the updated array
        return data;
    }
 
    // Function to find the next permutation of the given
    // integer array
    public static bool findNextPermutation(List<int> data)
    {
        // If the given dataset is empty
        // or contains only one element
        // next_permutation is not possible
        if (data.Count <= 1)
            return false;
        int last = data.Count - 2;
 
        // find the longest non-increasing
        // suffix and find the pivot
        while (last >= 0) {
            if (data[last] <  data[last + 1])
                break;
            last--;
        }
 
        // If there is no increasing pair
        // there is no higher order permutation
        if (last < 0)
            return false;
        int nextGreater = data.Count - 1;
 
        // Find the rightmost successor
        // to the pivot
        for (int i = data.Count - 1; i > last; i--) {
            if (data[i] >  data[last]) {
                nextGreater = i;
                break;
            }
        }
 
        // Swap the successor and
        // the pivot
        data = swap(data, nextGreater, last);
 
        // Reverse the suffix
        data = reverse(data, last + 1, data.Count - 1);
 
        // Return true as the
        // next_permutation is done
        return true;
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        //   matrix representation of graph
        int[, ] graph
            = new int[4, 4] { { 0, 10, 15, 20 },
                              { 10, 0, 35, 25 },
                              { 15, 35, 0, 30 },
                              { 20, 25, 30, 45 } };
        int s = 0;
        Console.WriteLine(
            travllingSalesmanProblem(graph, s));
    }
}
 
// This code is contributed by Tapesh(tapeshdua420)


Javascript




// JavaScript program to implement
// traveling salesman problem
// using naive approach.
const V = 4;
 
// implementation of traveling
// Salesman Problem
const travllingSalesmanProblem = (graph, s) => {
 
    // store all vertex apart
    // from source vertex
    let vertex = [];
    for (let i = 0; i < V; i++) {
        if (i !== s) {
            vertex.push(i);
        }
    }
     
    // store minimum weight
    // Hamiltonian Cycle.
    let min_path = Number.MAX_VALUE;
    do {
     
        // store current Path weight(cost)
        let current_pathweight = 0;
         
        // compute current path weight
        let k = s;
         
        for (let i = 0; i < vertex.length; i++) {
            current_pathweight += graph[k][vertex[i]];
            k = vertex[i];
        }
        current_pathweight += graph[k][s];
         
        // update minimum
        min_path = Math.min(min_path, current_pathweight);
    } while (findNextPermutation(vertex));
    return min_path;
}
 
// Function to swap the data
// present in the left and right indices
const swap = (data, left, right) => {
 
    // Swap the data
    let temp = data[left];
    data[left] = data[right];
    data[right] = temp;
     
    // Return the updated array
    return data;
}
 
// Function to reverse the sub-array
// starting from left to the right
// both inclusive
const reverse = (data, left, right) => {
 
    // Reverse the sub-array
    while (left < right) {
        let temp = data[left];
        data[left++] = data[right];
        data[right--] = temp;
    }
     
    // Return the updated array
    return data;
}
 
// Function to find the next permutation
// of the given integer array
const findNextPermutation = (data) => {
 
    // If the given dataset is empty
    // or contains only one element
    // next_permutation is not possible
    if (data.length <= 1) {
        return false;
    }
    let last = data.length - 2;
     
    // find the longest non-increasing
    // suffix and find the pivot
    while (last >= 0) {
        if (data[last] < data[last + 1]) {
            break;
        }
        last--;
    }
     
    // If there is no increasing pair
    // there is no higher order permutation
    if (last < 0) {
        return false;
    }
    let nextGreater = data.length - 1;
     
    // Find the rightmost successor
    // to the pivot
    for (let i = data.length - 1; i > last; i--) {
        if (data[i] > data[last]) {
            nextGreater = i;
            break;
        }
    }
     
    // Swap the successor and
    // the pivot
    data = swap(data, nextGreater, last);
     
    // Reverse the suffix
    data = reverse(data, last + 1, data.length - 1);
     
    // Return true as the
    // next_permutation is done
    return true;
}
 
// Driver Code
const graph = [[0, 10, 15, 20], [10, 0, 35, 25], [15, 35, 0, 30], [20, 25, 30, 0]];
let s = 0;
console.log(travllingSalesmanProblem(graph, s));
 
// This code is contributed by ishankhandelwals.


Output

80

Time complexity:  O(n!) where n is the number of vertices in the graph. This is because the algorithm uses the next_permutation function which generates all the possible permutations of the vertex set. 
Auxiliary Space: O(n) as we are using a vector to store all the vertices.



Previous Article
Next Article

Similar Reads

Traveling Salesman Problem (TSP) in Python
The Traveling Salesman Problem (TSP) is a classic algorithmic problem in the fields of computer science and operations research. It involves finding the shortest possible route that visits a set of cities and returns to the origin city. The challenge of TSP lies in its NP-hard nature, meaning that as the number of cities increases, the problem beco
4 min read
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
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
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
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
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 | Greedy Approach
Given a 2D matrix tsp[][], where each row has the array of distances from that indexed city to all the other cities and -1 denotes that there doesn't exist a path between those two indexed cities. The task is to print minimum cost in TSP cycle.Examples: Input: tsp[][] = {{-1, 10, 15, 20}, {10, -1, 35, 25}, {15, 35, -1, 30}, {20, 25, 30, -1}}; Below
10 min read
Travelling Salesman Problem using Hungarian method
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
5 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
Article Tags :
Practice Tags :