# include # include # include # include # include # include "tsp_nearest.h" int main ( ); void timestamp ( ); void tsp_display ( char *prefix, int n, double *x2 ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: tsp_nearest_test() tests tsp_nearest(). Licensing: This code is distributed under the MIT license. Modified: 08 July 2026 Author: John Burkardt */ { double cost; double d; int i; int i1; int i2; int j1; int j2; int n = 48; int order[48]; char prefix[] = "att48"; static double x[48*2] = { 6734, 1453, 2233, 10, 5530, 1424, 401, 841, 3082, 1644, 7608, 4458, 7573, 3716, 7265, 1268, 6898, 1885, 1112, 2049, 5468, 2606, 5989, 2873, 4706, 2674, 4612, 2035, 6347, 2683, 6107, 669, 7611, 5184, 7462, 3590, 7732, 4723, 5900, 3561, 4483, 3369, 6101, 1110, 5199, 2182, 1633, 2809, 4307, 2322, 675, 1006, 7555, 4819, 7541, 3981, 3177, 756, 7352, 4506, 7545, 2801, 3245, 3305, 6426, 3173, 4608, 1198, 23, 2216, 7248, 3779, 7762, 4595, 7392, 2244, 3484, 2829, 6271, 2135, 4985, 140, 1916, 1569, 7280, 4899, 7509, 3239, 10, 2676, 6807, 2993, 5185, 3258, 3023, 1942 }; double *x2; timestamp ( ); printf ( "\n" ); printf ( "tsp_nearest_test():\n" ); printf ( " C version\n" ); printf ( " Test tsp_nearest(), which uses the nearest neighbor method\n" ); printf ( " to construct a round trip for the traveling salesman problem.\n" ); tsp_nearest ( n, x, order, &cost ); /* Report. */ printf ( "\n" ); printf ( " The best itinerary found:\n" ); printf ( "\n" ); printf ( " Step From To Distance\n" ); printf ( "\n" ); for ( i1 = 0; i1 < n; i1++ ) { i2 = ( ( i1 + 1 ) % n ); j1 = order[i1]; j2 = order[i2]; d = sqrt ( pow ( x[j1*2] - x[j2*2], 2 ) + pow ( x[j1*2+1] - x[j2*2+1], 2 ) ); printf ( " %4d %2d %2d %14.6g\n", i1, j1, j2, d ); cost = cost + d; } printf ( " ---- -- -- --------------\n" ); printf ( " cost: %14.6g\n", cost ); /* Create the circuit data with transposition, reordering and wrap-around. */ x2 = ( double * ) malloc ( ( n + 1 ) * 2 * sizeof ( double ) ); for ( i2 = 0; i2 <= n; i2++ ) { if ( i2 < n ) { i = order[i2]; } else { i = order[0]; } x2[i2*2] = x[i*2]; x2[i2*2+1] = x[i*2+1]; } /* Send the data to the plotter. */ tsp_display ( prefix, n + 1, x2 ); free ( x2 ); /* Terminate. */ printf ( "\n" ); printf ( "tsp_nearest_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ void tsp_display ( char *prefix, int n, double *x2 ) /******************************************************************************/ /* Purpose: tsp_display() plots cities and a tour connecting them. Discussion: If a "round trip" is desired, then the x2 array must repeat the first city at the end. Licensing: This code is distributed under the MIT license. Modified: 12 June 2026 Author: John Burkardt Input: char *prefix: a string that defines the name of the problem. It is also used to create file names needed for input to gnuplot. int n: the number of cities. double x[n*2]: the x and y coordinates of the cities. */ { char command_filename[80]; FILE *command; FILE *data; char data_filename[80]; int i; /* Create the data file. */ strcpy ( data_filename, prefix ); strcat ( data_filename, "_data.txt" ); data = fopen ( data_filename, "wt" ); for ( i = 0; i < n; i++ ) { fprintf ( data, "%14.6g %14.6g\n", x2[2*i], x2[2*i+1] ); } fclose ( data ); printf ( " Created data file '%s'\n", data_filename ); /* Create the command file. */ strcpy ( command_filename, prefix ); strcat ( command_filename, "_commands.txt" ); command = fopen ( command_filename, "wt" ); fprintf ( command, "# %s\n", command_filename ); fprintf ( command, "#\n" ); fprintf ( command, "# Usage:\n" ); fprintf ( command, "# gnuplot < %s\n", command_filename ); fprintf ( command, "#\n" ); fprintf ( command, "set term png\n" ); fprintf ( command, "set output '%s.png'\n", prefix ); fprintf ( command, "set xlabel '<--- X --->'\n" ); fprintf ( command, "set ylabel '<--- Y --->'\n" ); fprintf ( command, "set title '%s'\n", prefix ); fprintf ( command, "set grid\n" ); fprintf ( command, "unset key\n" ); fprintf ( command, "set style data lines\n" ); fprintf ( command, "plot '%s' using 1:2 lw 3 linecolor rgb 'blue', \\\n", data_filename ); fprintf ( command, " '%s' using 1:2 with points pointtype 7 pointsize 3 linecolor rgb 'green'\n", data_filename ); fprintf ( command, "quit\n" ); fclose ( command ); printf ( " Created command file '%s'\n", command_filename ); return; } /******************************************************************************/ void timestamp ( ) /******************************************************************************/ /* Purpose: timestamp() prints the current YMDHMS date as a time stamp. Example: 17 June 2014 09:45:54 AM Licensing: This code is distributed under the MIT license. Modified: 01 May 2021 Author: John Burkardt */ { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; time_t now; now = time ( NULL ); tm = localtime ( &now ); strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); printf ( "%s\n", time_buffer ); return; # undef TIME_SIZE }