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

Return: #Include #Include #Define N 4 #Define VISITED - ALL (1 N) - 1

The document discusses using dynamic programming and bitwise operators to solve the traveling salesman problem (TSP) in N cities. It presents two implementations, a shorter version using bitmasks and a longer version using an array to track visited cities. Both implementations calculate the minimum cost path and output the path.
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)
19 views3 pages

Return: #Include #Include #Define N 4 #Define VISITED - ALL (1 N) - 1

The document discusses using dynamic programming and bitwise operators to solve the traveling salesman problem (TSP) in N cities. It presents two implementations, a shorter version using bitmasks and a longer version using an array to track visited cities. Both implementations calculate the minimum cost path and output the path.
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

//using dynamic programming and bitwise operators (Shorter code version)

#include <stdio.h>
#include <limits.h>

#define N 4
#define VISITED_ALL (1<<N) - 1

int dist[N][N] = {
{0, 400, 500, 300},
{400, 0, 300, 500},
{500, 300, 0, 400},
{300, 500, 400, 0}
};

int dp[16][4];
int next[16][4];

int min(int a, int b) {


return a < b ? a : b;
}

int tsp(int mask, int pos) {


if(mask == VISITED_ALL) {
return dist[pos][0];
}
if(dp[mask][pos] != -1) {
return dp[mask][pos];
}

int ans = INT_MAX;


int cityIndex = -1;

// Try to go to an unvisited city


for(int city = 0; city < N; city++) {
if((mask & (1 << city)) == 0) {
int newAns = dist[pos][city] + tsp(mask | (1 << city), city);
if(newAns < ans) {
ans = newAns;
cityIndex = city;
}
}
}

next[mask][pos] = cityIndex;
return dp[mask][pos] = ans;
}

void findPath(int mask, int pos) {


if(mask == VISITED_ALL) {
printf("A");
return;
}

int nextCity = next[mask][pos];


printf("%c-->", 'A' + nextCity);
findPath(mask | (1 << nextCity), nextCity);
}

int main() {
for(int i = 0; i < (1<<N); i++) {
for(int j = 0; j < N; j++) {
dp[i][j] = -1;
next[i][j] = -1;
}
}

printf("The minimum cost is %d\n", tsp(1, 0));


printf("The path is: A-->");
findPath(1, 0);
printf("\n");

return 0;
}

//using dynamic programming and array (Longer code version but easier to understand)
#include <stdio.h>
#include <limits.h>

#define N 4

int dist[N][N] = {
{0, 1, 2, 1},
{1, 0, 1, 3},
{3, 1, 0, 2},
{1, 2, 1, 0}
};

int dp[1<<N][N];
int next[1<<N][N];

int min(int a, int b) {


return a < b ? a : b;
}

int tsp(int visited[], int pos, int count) {


if(count == N) {
return dist[pos][0];
}

int mask = 0;
for(int i = 0; i < N; i++) {
if(visited[i]) {
mask |= (1 << i);
}
}

if(dp[mask][pos] != -1) {
return dp[mask][pos];
}

int ans = INT_MAX;


int cityIndex = -1;

for(int city = 0; city < N; city++) {


if(!visited[city]) {
visited[city] = 1; // Mark as visited
int newAns = dist[pos][city] + tsp(visited, city, count + 1);
visited[city] = 0;
if(newAns < ans) {
ans = newAns;
cityIndex = city;
}
}
}

next[mask][pos] = cityIndex;
return dp[mask][pos] = ans;
}

void findPath(int visited[], int pos, int count) {


if(count == N) {
printf("A");
return;
}

int mask = 0;
for(int i = 0; i < N; i++) {
if(visited[i]) {
mask |= (1 << i);
}
}

int nextCity = next[mask][pos];


printf("%c-->", 'A' + nextCity);
visited[nextCity] = 1;
findPath(visited, nextCity, count + 1);
visited[nextCity] = 0;
}

int main() {
int visited[N] = {0};
visited[0] = 1;

for(int i = 0; i < (1<<N); i++) {


for(int j = 0; j < N; j++) {
dp[i][j] = -1;
next[i][j] = -1;
}
}

printf("The minimum cost is %d\n", tsp(visited, 0, 1));


printf("The path is: A-->");
findPath(visited, 0, 1);
printf("\n");

return 0;
}

//Output:
//The minimum cost is 4
//The path is: A-->D-->C-->B-->A

You might also like