Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Traveling Salesman Problem I wrote a code for TSP. Can someone look at my code and make it better? It is supposed to be easy

Traveling Salesman Problem

I wrote a code for TSP. Can someone look at my code and make it better?

It is supposed to be easy to change into assembly language.

The shortest path is found starting from City 1 and returning to it while visiting other cities only once.

The coordinates of the cities : City-1(0 ,0), City-2(2,6), City-3(8,4), City-4(7,2), City-5(1,6), City-6(4,9), City-7(3,2)

You need to find the tour in terms of the sequence of the cities visited and the traveling distance.

*There is one more condition that City-3 must be visited before City-7.

The code should be in C and I hope for it to be easy to change to assembly language!

My CODE

#include  #include  #define POSITION_MAX 256 double weight(int x, int y, int x_, int y_) { int a = (x - x_); int b = (y - y_); return sqrt(a*a + b*b); } double TSP(int cur, double* cur_x, double * cur_y, int visited, double memo[][POSITION_MAX]) { if (visited == (1 << 7) - 1) { // all_visited then memo[cur][visited] = weight(cur_x[cur], cur_y[cur], cur_x[1], cur_y[1]); return memo[cur][visited]; // return distance (cur_x, cur_y to 1 1) } if (memo[cur][visited] != 0) // Reuse the cache return memo[cur][visited]; memo[cur][visited] = 987654321; for (int i = 1; i <= 7; i++) { if (visited & (1 << (i - 1))) // if the city_i is already visited, then continue continue; if (cur == 3 && i != 7) continue; int next = visited | (1 << (i - 1)); // mark visited double tmp = weight(cur_x[cur], cur_y[cur], cur_x[i], cur_y[i]) + TSP(i, cur_x, cur_y, next, memo); // recursion for D.P. if (memo[cur][visited] > tmp) { memo[cur][visited] = tmp; // Memoization } } return memo[cur][visited]; } void path(double map_x[], double map_y[], double memo[][POSITION_MAX], double distance) { int visited = 1; int cur = 1; printf("From 1 visited, "); while (1) { for (int i = 1; i <= 7; i++) { if (visited & (1 << (i - 1))) continue; int next = visited | (1 << (i - 1)); // mark visited if (roundf(distance - weight(map_x[cur], map_y[cur], map_x[i], map_y[i])) == roundf(memo[i][next])) { cur = i; distance = memo[i][next]; visited |= (1 << (i - 1)); printf("%d visited, ", i); } } if (visited == (1 << 7) - 1) break; } printf("To 1 "); } int main() { double map_x[] = {-1, 0,2,8,7,1,4,3 }; double map_y[] = {-1, 0,6,4,2,6,9,2 }; double memo[8][POSITION_MAX] = { 0, }; double distance = TSP(1, map_x, map_y, 1, memo); printf("The salesman has to walk at least %.2f ", distance); path(map_x, map_y, memo, distance); getchar(); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Finance The Role Of Data Analytics In Manda Due Diligence

Authors: Ps Publishing

1st Edition

B0CR6SKTQG, 979-8873324675

More Books

Students also viewed these Databases questions

Question

sharing of non-material benefits such as time and affection;

Answered: 1 week ago