Traveling Salesman Problem TSP Implementation
Traveling Salesman Problem TSP Implementation
Traveling Salesman Problem TSP Implementation
geeksforgeeks.org/traveling-salesman-problem-tsp-implementation
Difficulty Level :
Medium
Last Updated :
31 Jan, 2023
1/19
Examples:
Output of Given Graph:
10 + 25 + 30 + 15 := 80
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.
C++
#include <bits/stdc++.h>
2/19
using namespace std;
#define V 4
vector<int> vertex;
if (i != s)
vertex.push_back(i);
do {
int current_pathweight = 0;
int k = s;
current_pathweight += graph[k][vertex[i]];
k = vertex[i];
current_pathweight += graph[k][s];
// update minimum
} while (
next_permutation(vertex.begin(), vertex.end()));
return min_path;
// Driver Code
3/19
int main()
{ 10, 0, 35, 25 },
{ 15, 35, 0, 30 },
int s = 0;
return 0;
Java
import java.util.*;
class GFG{
static int V = 4;
// implementation of traveling
// Salesman Problem
int s)
ArrayList<Integer> vertex =
4/19
new ArrayList<Integer>();
if (i != s)
vertex.add(i);
// Hamiltonian Cycle.
do
int current_pathweight = 0;
int k = s;
for (int i = 0;
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;
5/19
// Function to swap the data
ArrayList<Integer> data,
data.set(left, data.get(right));
data.set(right, temp);
return data;
// both inclusive
ArrayList<Integer> data,
data.set(left++,
data.get(right));
data.set(right--, temp);
6/19
// Return the updated array
return data;
ArrayList<Integer> data)
{
if (data.size() <= 1)
return false;
if (data.get(last) <
data.get(last + 1))
break;
last--;
if (last < 0)
7/19
return false;
// to the pivot
if (data.get(i) >
data.get(last))
nextGreater = i;
break;
// the pivot
data = swap(data,
nextGreater, last);
data.size() - 1);
// next_permutation is done
return true;
// Driver Code
8/19
{10, 0, 35, 25},
int s = 0;
System.out.println(
travllingSalesmanProblem(graph, s));
Python3
V =4
vertex = []
for i in range(V):
if i != s:
vertex.append(i)
min_path = maxsize
next_permutation=permutations(vertex)
for i in next_permutation:
9/19
current_pathweight = 0
k =s
for j in i:
current_pathweight += graph[k][j]
k =j
current_pathweight += graph[k][s]
# update minimum
return min_path
# Driver Code
if __name__ == "__main__":
s =0
print(travellingSalesmanProblem(graph, s))
C#
// C# program to implement
using System;
using System.Collections.Generic;
class GFG {
static int V = 4;
10/19
static int travllingSalesmanProblem(int[, ] graph,
int s)
if (i != s)
vertex.Add(i);
// Hamiltonian Cycle.
do {
int current_pathweight = 0;
int k = s;
k = vertex[i];
// update minimum
min_path
= Math.Min(min_path, current_pathweight);
} while (findNextPermutation(vertex));
return min_path;
// right indices
11/19
int right)
data[left] = data[right];
data[right] = temp;
return data;
data[left++] = data[right];
data[right--] = temp;
return data;
// integer array
12/19
// next_permutation is not possible
if (data.Count <= 1)
return false;
break;
last--;
if (last < 0)
return false;
// to the pivot
nextGreater = i;
break;
// the pivot
13/19
// Return true as the
// next_permutation is done
return true;
// Driver Code
int[, ] graph
{ 10, 0, 35, 25 },
{ 15, 35, 0, 30 },
int s = 0;
Console.WriteLine(
travllingSalesmanProblem(graph, s));
Javascript
const V = 4;
// implementation of traveling
// Salesman Problem
14/19
// from source vertex
if (i !== s) {
vertex.push(i);
// store minimum weight
// Hamiltonian Cycle.
do {
let current_pathweight = 0;
// compute current path weight
let k = s;
current_pathweight += graph[k][vertex[i]];
k = vertex[i];
current_pathweight += graph[k][s];
// update minimum
} while (findNextPermutation(vertex));
return min_path;
15/19
}
data[left] = data[right];
data[right] = temp;
// Return the updated array
return data;
// both inclusive
data[left++] = data[right];
data[right--] = temp;
// Return the updated array
return data;
16/19
// If the given dataset is empty
if (data.length <= 1) {
return false;
// find the longest non-increasing
break;
last--;
// If there is no increasing pair
if (last < 0) {
return false;
// Find the rightmost successor
// to the pivot
nextGreater = i;
17/19
break;
// the pivot
// 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));
Output
80
18/19
set.
Auxiliary Space: O(n) as we are using a vector to store all the
vertices.
Related Articles
1.
Travelling Salesman Problem (TSP) using Reduced Matrix Method
2.
Proof that traveling salesman problem is NP Hard
3.
Traveling Salesman Problem using Genetic Algorithm
4.
Traveling Salesman Problem using Branch And Bound
5.
Travelling Salesman Problem implementation using BackTracking
6.
Approximate solution for Travelling Salesman Problem using MST
7.
Travelling Salesman Problem using Hungarian method
8.
Travelling Salesman Problem | Greedy Approach
9.
Travelling Salesman Problem using Dynamic Programming
10.
Bitmasking and Dynamic Programming | Travelling Salesman
Problem
Article Contributed By :
Nishant_Singh
@Nishant_Singh
Vote for difficulty
Current difficulty :
Medium
Report Issue
@geeksforgeeks
, Some rights reserved
19/19