167 - Amaankhanpathan - TSP DP PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Name :- AMAAN KHAN PATHAN

Roll No :- 167 Div :- B

Sub :- DAA Lab

Assignment No :- 07

Travelling Salesman Problem using DP


The traveling salesman problem (TSP) is a well-known problem in
computer science that asks for the shortest possible route that visits a set of cities
exactly once and returns to the starting city. One approach to solving the TSP is
dynamic programming, which involves breaking the problem down into smaller
subproblems and using the solutions to those subproblems to build up to the final
solution.

Here is an outline of the dynamic programming approach to solving the TSP:

1. Define a state space: The state space of the problem consists of all possible subsets of
cities that the salesman can visit, along with the city that the salesman is currently in.
For example, if there are n cities, then there are 2^n possible subsets of cities,
including the empty subset (representing the case where the salesman has visited no
cities yet).
2. Define a cost function: The cost function assigns a cost to each possible transition
between states. In this case, the cost of transitioning from state A (representing a
subset of cities) to state B (representing a subset of cities that includes city i) is the
minimum cost of any path that starts at the starting city, visits all cities in subset A
exactly once (not including city i), and ends at city i.
3. Fill in a table of costs: Starting with the smallest possible subsets (i.e., those
containing only the starting city), fill in a table of costs for all possible subsets of
cities. This can be done using a bottom-up approach, where the cost of each subset is
computed from the costs of its sub-subsets.
4. Find the optimal solution: Once the table of costs has been filled in, the optimal
solution can be found by starting at the starting city and repeatedly choosing the next
city with the lowest cost, until all cities have been visited. The cost of the optimal
solution is the sum of the costs of these transitions.

Time Complexity :-
The time complexity of the dynamic programming approach for TSP is O(n^2 * 2^n),
where n is the number of cities. While this approach is not practical for large TSP
instances, it can be used to solve smaller instances optimally.
Aim :- Design & Implement Travelling salespersons Problem using
Dynamic Programming. Also calculate the Time complexity for this
algorithm

Program Code :-
#include<iostream>

using namespace std;

int ary[10][10], completed[10], n, cost = 0;

void takeInput()

int i, j;

cout<< "Enter the number of villages: ";

cin>> n;

cout<< "\nEnter the Cost Matrix\n";

for(i = 0; i < n; i++)

cout<< "\nEnter Elements of Row: " << i + 1 << "\n";

for (j = 0; j < n; j++)

cin >> ary[i][j];

completed[i] = 0;

cout<< "\n\nThe cost list is:";

for (i = 0; i < n; i++)

cout<< "\n";

for (j = 0; j < n; j++)


cout << "\t" << ary[i][j];

int least(int c)

int i, nc = 999;

int min = 999, kmin;

for(i = 0; i < n; i++)

if((ary[c][i] != 0) && (completed[i] == 0))

if (ary[c][i] + ary[i][c] < min)

min = ary[i][0] + ary[c][i];

kmin = ary[c][i];

nc = i;

if(min != 999)

cost += kmin;

return nc;

void mincost(int city)

int i, ncity;

completed[city] = 1;
cout << city + 1 << "--->";

ncity = least(city);

if (ncity == 999)

ncity = 0;

cout << ncity + 1;

cost += ary[city][ncity];

return;

mincost(ncity);

int main()

takeInput();

cout << "\n\nThe Path is:\n";

mincost(0);

cout << "\n\nMinimum cost is " << cost << endl;

return 0;

}
Output :-

You might also like