#include #include #include #define MAX_SIZE 100 #define INFINITY 1000000000 void gencosts ( int C[][MAX_SIZE], int n ) { int i, j; for (i=0; i A[1]) break; if (l < n) mincostexh(C,n,A,k+1,cost,bestcost,nleaves,ncalls); cost -= C[A[k-1]][A[k]]; t = A[k]; A[k] = A[j]; A[j] = t; } } void mincostgp ( int C[][MAX_SIZE], int n, int A[], int k, int cost, int m, int *bestcost, int *nleaves, int *ncalls ) { int j, l, t; int estimate; ++(*ncalls); if (k == n-1) { handleleaf(C,n,A,cost,bestcost); ++(*nleaves); return; } for (j=k; j A[1]) break; if (l < n) { estimate = cost + m * (n-k); if (estimate < *bestcost) mincostgp(C,n,A,k+1,cost,m,bestcost,nleaves,ncalls); } cost -= C[A[k-1]][A[k]]; t = A[k]; A[k] = A[j]; A[j] = t; } } void mincostlp1 ( int C[][MAX_SIZE], int n, int A[], int k, int cost, int *bestcost, int *nleaves, int *ncalls ) { int j, l, t, m, r, s; int estimate; ++(*ncalls); if (k == n-1) { handleleaf(C,n,A,cost,bestcost); ++(*nleaves); return; } for (j=k; j A[1]) break; if (l < n) { estimate = cost; m = INFINITY; for (s=k+1; s A[1]) break; if (l < n) { estimate = 2 * cost; /* You only leave A[k] */ m = INFINITY; for (s=k+1; s 1) n = atoi(argv[1]); else scanf("%d", &n); if ((n < 3) || (n > MAX_SIZE)) { fprintf(stderr, "Invalid number of cities: %d\n", n); exit(1); } srand((unsigned int)time(NULL)); gencosts(C,n); for (i=0;i 12) printf(" Not done\n"); else { printf("\n"); bestcost = INFINITY; nleaves = ncalls = 0; mincostexh(C,n,A,1,0,&bestcost,&nleaves,&ncalls); printf(" Minimum cost computed = %d\n", bestcost); printf(" Number of leaves explored = %d\n", nleaves); printf(" Number of calls made = %d\n", ncalls); } printf("\n+++ Search with global pruning strategy:\n"); m = INFINITY; for (i=0; i