# Travelling Salesman Problem in Java

In Java, Travelling Salesman Problem is a problem in which we need to find the shortest route that covers each city exactly once and returns to the starting point. Hamiltonian Cycle is another problem in Java that is mostly similar to Travelling Salesman Problem.

The main difference between TSP and the Hamiltonian cycle is that in Hamiltonian Cycle, we are not sure whether a tour that visits each city exactly once exists or not, and we have to determine it. In the Travelling Salesman Problem, a Hamiltonian cycle is always present because the graph is complete, and the problem is to find a Hamiltonian cycle with minimum weight.

Let's take an example of the Travelling Salesman Problem to understand the problem. In the above graph, a, b, c, d, e, f, and g are the cities and we have to find the Hamiltonian cycle having minimum weight. There are several Hamiltonian Cycle and some of them are as follows:

1. a -> b -> c -> d -> e -> f-> g -> a
2. a -> c -> b -> d -> e -> f -> g -> a
3. a -> g -> c -> e -> f-> d -> b -> a

The weight of each Hamiltonian cycle can be different, and now our task is to find that route having a minimum distance.

### Steps to implement Travelling Salesman Problem

These are the following steps, which we use to implement the TSP program in Java:

1. We consider a city as the starting and ending point. We can use any city as a starting point because the route is cyclic.
2. In the DFS way, we start traversing from the source to its adjacent nodes.
3. Find the cost of each traversal and keep track of minimum cost and keep on updating the value of minimum cost stored value.
4. In the end, return the permutation with minimum cost.

Let's implement the Java code of TSP by using the above steps:

TSPExample.java

Output  ### Feedback   