# include # include # include # include "tsp_greedy.h" int main ( ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: tsp_greedy_test() tests tsp_greedy(). Licensing: This code is distributed under the MIT license. Modified: 02 June 2026 Author: John Burkardt */ { double cost; double d; double *distance; double five_d[5*5] = { 0.0, 3.0, 4.0, 2.0, 9.0, 3.0, 0.0, 4.0, 6.0, 3.0, 4.0, 4.0, 0.0, 5.0, 8.0, 2.0, 6.0, 5.0, 0.0, 6.0, 9.0, 3.0, 8.0, 6.0, 0.0 }; int five_n = 5; int from; int n; double nine_d[9*9] = { 0, 4, 100, 100, 100, 100, 7, 100, 100, 4, 0, 9, 100, 100, 100, 11, 20, 100, 100, 9, 0, 6, 2, 100, 100, 100, 100, 100, 100, 6, 0, 10, 5, 100, 100, 100, 100, 100, 2, 10, 0, 15, 100, 1, 5, 100, 100, 100, 5, 15, 0, 100, 100, 12, 7, 11, 100, 100, 100, 100, 0, 1, 100, 100, 20, 100, 100, 1, 100, 1, 0, 3, 100, 100, 100, 100, 5, 12, 100, 3, 0 }; int nine_n = 9; int *path; int test; int to; timestamp ( ); printf ( "\n" ); printf ( "tsp_greedy_test():\n" ); printf ( " C version\n" ); printf ( " Test tsp_greedy().\n" ); for ( test = 0; test <= 1; test++ ) { if ( test == 0 ) { n = five_n; distance = five_d; path = tsp_greedy ( n, distance ); } else if ( test == 1 ) { n = nine_n; distance = nine_d; path = tsp_greedy ( n, distance ); } /* Report. */ printf ( "\n" ); printf ( " The best greedy itinerary found:\n" ); printf ( "\n" ); printf ( " Step From To Distance\n" ); printf ( "\n" ); cost = 0.0; for ( from = 0; from < n; from++ ) { to = ( ( from + 1 ) % n ); d = distance [ path[from] + path[to] * n ]; printf ( " %4d %2d %2d %14.6g\n", from, path[from], path[to], d ); cost = cost + d; } printf ( " ---- -- -- --------------\n" ); printf ( " cost: %14.6g\n", cost ); free ( path ); } /* Terminate. */ printf ( "\n" ); printf ( "tsp_greedy_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ 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 }