# Travelling Salesman Problem in C

The Travelling Salesman Problem (TSP) is a well-known optimization issue in the areas of mathematics and computer science. One way to put it is as follows: Find the shortest route that visits each city exactly once, travels the distance between each pair of cities, and then returns to the starting city. Numerous practical applications of the issue exist, including logistics planning, circuit design, and vehicle routing. In this post, we'll look at how to use dynamic programming to solve the Travelling Salesman Problem in C and present a sample program and its results.

### Travelling Salesman Problem (TSP) Using Dynamic Programming

Dynamic programming is a potent tool for solving optimization problems by dividing optimization problems into smaller subproblems and storing the solutions to these subproblems to prevent repeating calculations. The steps listed below can also be used to solve the TSP using dynamic programming:

### Definition of a subproblem:

Now let's define our subproblems: dp[mask][i], where 'mask' is a binary number denoting the collection of visited cities and 'i' denotes the current city. The value of dp[mask][i] will be the lowest price to go to every city in the "mask" and end at the city "i".

### Primary Case:

When 'mask' only has one city (i.e., when only one bit is set in the binary encoding of 'mask'), initialize dp[mask][i] to a high value (signifying infinite).

### Relation of Recurrence:

Iterate through all cities 'j' not included in the 'mask' for each subproblem dp[mask][i], and figure out how much it would cost to travel to city 'j' from any city 'k' in the 'mask' that has already been there. Add the lowest cost to dp[mask | (1 j)][j].

### Final Response:

The final response will be dp[(1 n) - 1)]'s minimum value.[i], with 'n' denoting the total number of cities.

### Recursive Equation

The recursive equation for dynamic programming's solution of the TSP is as follows:

In this case, cost[i][j] denotes the separation between the cities 'i' and 'j'.

dp[mask][i]: It indicates the lowest cost of travelling to every city listed in the binary mask "mask", starting with the city "i". The 'mask' is a binary number, with the jth bit set to 1 if city j has been visited and 0 otherwise.

mask ^ (1 << j): By switching the jth bit in the "mask", this expression creates a new mask and essentially marks city "j" as visited. It is accomplished by combining a left-shift operation with the XOR operator ().

The price of travelling from the city 'i' to city 'j' is cost[i][j]. It is taken from the cost matrix, which includes the travel times between every pair of cities.

### Time Complexity

The dynamic programming approach, where 'n' is the number of cities, takes O(n2 * 2n) time to solve the TSP. It is because there are 2n potential subsets of cities, and we must iterate over 'n' cities for each subset in order to determine the minimal cost.

### Example:

Let's take a program for Travelling Salesman Problem in C:

Output:

```Enter the number of cities: 4
Enter the cost matrix:
0 10 15 20
```