// tsp.c traveling salesman problem, NP Complete // test on 9 x,y random locations, 9! = 362,880 // arbitrary start and end, not closed loop // will be different start and end for heuristic and complete #include #include #include #include #include "permute.h" // problem data static const int n = 9; typedef struct {int id; double x; double y;} loc; static loc locs[9]; static int soln[9]; static int avail[9]; static double total; static int debug = 0; static void used(int i, int na) { // delete i from avail that has n items by compressing int j, k; if(debug) printf("used(%d)\n", i); for(j=0; j0) { printf("avail %d ", avail[0]); for(i=1; i0; i--) soln[i] = soln[i-1]; soln[0] = jk; used(soln[0], n-k); dist = dist0; } total += dist; k++; prsoln(k, dist); } printf("heuristic solution total distance=%f\n", total); // find minimum total distance for all permutations for(i=0; i