0% found this document useful (0 votes)
173 views3 pages

Travelling Sales Man Problem

The Travelling Salesman Problem (TSP) involves finding the shortest route for a salesman to visit each city in a list exactly once and return to the starting city. The document provides an example of a TSP tour with 4 cities and explains an algorithm to solve TSP by generating all permutations of cities and tracking the minimum cost permutation. It also includes a Java implementation that uses backtracking to recursively explore permutations and return the minimum weight Hamiltonian cycle.

Uploaded by

Yaseen Sh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
173 views3 pages

Travelling Sales Man Problem

The Travelling Salesman Problem (TSP) involves finding the shortest route for a salesman to visit each city in a list exactly once and return to the starting city. The document provides an example of a TSP tour with 4 cities and explains an algorithm to solve TSP by generating all permutations of cities and tracking the minimum cost permutation. It also includes a Java implementation that uses backtracking to recursively explore permutations and return the minimum weight Hamiltonian cycle.

Uploaded by

Yaseen Sh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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
Hamiltoninan cycle problem is to find if there exist 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. 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.
Algorithm:

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 minimum cost permutation.
4. Return the permutation with minimum cost.

Java Implementation:

// Java implementation of the approach

class GFG
{

// Function to find the minimum weight


// Hamiltonian Cycle

static int tsp(int[][] graph, boolean[] v,


int currPos, int n,
int count, int cost, int ans)
{

// If last node is reached and it has a link


// to the starting node i.e the source then
// keep the minimum value out of the total cost
// of traversal and "ans"
// Finally return to check for more possible values
if (count == n && graph[currPos][0] > 0)
{
ans = Math.min(ans, cost + graph[currPos][0]);
return ans;
}

// BACKTRACKING STEP
// Loop to traverse the adjacency list
// of currPos node and increasing the count
// by 1 and cost by graph[currPos,i] value
for (int i = 0; i < n; i++)
{
if (v[i] == false && graph[currPos][i] > 0)
{

// Mark as visited
v[i] = true;
ans = tsp(graph, v, i, n, count + 1,
cost + graph[currPos][i], ans);

// Mark ith node as unvisited


v[i] = false;
}
}
return ans;
}

// Driver code
public static void main(String[] args)
{

// n is the number of nodes i.e. V


int n = 4;

int[][] graph = {{0, 10, 15, 20},


{10, 0, 35, 25},
{15, 35, 0, 30},
{20, 25, 30, 0}};

// Boolean array to check if a node


// has been visited or not
boolean[] v = new boolean[n];

// Mark 0th node as visited


v[0] = true;
int ans = Integer.MAX_VALUE;

// Find the minimum weight Hamiltonian Cycle


ans = tsp(graph, v, 0, n, 1, 0, ans);

// ans is the minimum weight Hamiltonian Cycle


System.out.println(ans);
}
}

You might also like