# include # include # include # include "tsp_nearest.h" /******************************************************************************/ void tsp_nearest ( int n, double *x, int *order_best, double *cost_best ) /******************************************************************************/ /* Purpose: tsp_nearest() seeks an optimal traveling salesperson path by the nearest neighbor method. Licensing: This code is distributed under the MIT license. Modified: 02 June 2026 Author: John Burkardt Input: int n: the number of cities. double *x[n*2]: the coordinates of the cities. Output: int order_best[n]: a list of the order in which the N cities should be visited. double cost_best: the length of the round trip. */ { double cost; int i; int *order; int start; /* Initialize: the best cost so far is Infinity; the best tour so far is 1, 2, ..., n. */ *cost_best = HUGE_VAL; for ( i = 0; i < n; i++ ) { order_best[i] = i; } /* Try starting at city START and moving to the nearest unvisited city. */ for ( start = 0; start < n; start++ ) { order = tsp_tour_nearest ( n, x, start ); cost = tsp_tour_cost ( n, x, order ); if ( cost < *cost_best ) { for ( i = 0; i < n; i++ ) { order_best[i] = order[i]; } *cost_best = cost; printf ( " Route starting at city %d has cost = %g\n", start, *cost_best ); } free ( order ); } return; } /******************************************************************************/ double tsp_tour_cost ( int n, double *x, int *order ) /******************************************************************************/ /* Purpose: tsp_tour_cost() evaluates the cost of a round trip. Licensing: This code is distributed under the MIT license. Modified: 18 June 2026 Author: John Burkardt Input: int n: the number of cities. double x[n,2]: the city locations. int order[n]: the order in which the cities are visited. Output: double cost: the cost of the route. */ { double cost; int from; int to; cost = 0.0; from = n - 1; for ( to = 0; to < n; to++ ) { cost = cost + sqrt ( pow ( x[order[from]*2] - x[order[to]*2], 2 ) + pow ( x[order[from]*2+1] - x[order[to]*2+1], 2 ) ); from = to; } return cost; } /******************************************************************************/ int *tsp_tour_nearest ( int n, double *x, int start ) /******************************************************************************/ /* Purpose: tsp_path_nearest() finds a nearest neighbor route for a given start. Licensing: This code is distributed under the MIT license. Modified: 07 July 2026 Author: John Burkardt Input: int n: the number of cities. double x[n*2]: the city to city distance table. int start: the starting city. Output: int order[n]: the route that starts at START. */ { double *d; double dmin; int from; int i; int j; int k; int *order; int to; /* Create the distance table. */ d = ( double * ) malloc ( n * n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { for ( j = 0; j < n; j++ ) { d[i+j*n] = sqrt ( pow ( x[i*2] - x[j*2], 2 ) + pow ( x[i*2+1] - x[j*2+1], 2 ) ); } } for ( i = 0; i < n; i++ ) { d[i+i*n] = HUGE_VAL; } /* At FROM, find nearest unvisited city TO. */ order = ( int * ) malloc ( n * sizeof ( int ) ); for ( j = 0; j < n; j++ ) { if ( j == 0 ) { to = start; } else { to = 0; dmin = d[from+to*n]; for ( k = 1; k < n; k++ ) { if ( d[from+k*n] < dmin ) { to = k; dmin = d[from+to*n]; } } } /* Add TO to path, and move there. */ order[j] = to; from = to; /* Reset D so we can't revisit city TO. */ for ( i = 0; i < n; i++ ) { d[i+to*n] = HUGE_VAL; } } free ( d ); return order; }