# include # include # include # include "tsp_greedy.h" /******************************************************************************/ int *tsp_greedy ( int n, double *distance ) /******************************************************************************/ /* Purpose: tsp_greedy() seeks an optimal traveling salesperson path by the greedy 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 *distance: the NxN table of pairwise distances between cities. Output: int *path: a list of the order in which the N cities should be visited. */ { double cost; double cost_best; int i; int *path; int *path_best; int start; printf ( "\n" ); printf ( "tsp_greedy():\n" ); printf ( " For the traveling salesperson problem, find a greedy\n" ); printf ( " solution by choosing a starting city, and then constructing\n" ); printf ( " the tour by always moving to the nearest unvisited city.\n" ); /* Initialize: the best cost so far is Infinity; the best tour so far is 1, 2, ..., n. */ cost_best = HUGE_VAL; path_best = ( int * ) malloc ( n * sizeof ( int ) ); for ( i = 0; i < n; i++ ) { path_best[i] = i; } /* Try starting at city START and taking greedy steps. */ for ( start = 0; start < n; start++ ) { path = path_greedy ( n, distance, start ); cost = path_cost ( n, distance, path ); if ( cost < cost_best ) { for ( i = 0; i < n; i++ ) { path_best[i] = path[i]; } cost_best = cost; printf ( " Route starting at city %d has cost = %g\n", start, cost_best ); } free ( path ); } return path_best; } /******************************************************************************/ double path_cost ( int n, double *distance, int *p ) /******************************************************************************/ /* Purpose: path_cost() evaluates the cost of a round trip. Licensing: This code is distributed under the MIT license. Modified: 02 June 2026 Author: John Burkardt Input: int n: the number of cities. double DISTANCE(N,N), the city to city distance table. int P(N), a permutation of 1:N, the route. 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 + distance [ p[from] + p[to] * n ]; from = to; } return cost; } /******************************************************************************/ int *path_greedy ( int n, double *distance, int start ) /******************************************************************************/ /* Purpose: path_greedy() finds a greedy route for a given start. Licensing: This code is distributed under the MIT license. Modified: 02 June 2026 Author: John Burkardt Input: int n: the number of cities. double DISTANCE(N,N), the city to city distance table. int start: the starting city. Output: int P[N]: the greedy route that starts at START. */ { double *d; double dmin; int from; int i; int j; int k; int *p; int to; /* Make a copy of the distance table that we can modify. */ d = ( double * ) malloc ( n * n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { for ( j = 0; j < n; j++ ) { d[i+j*n] = distance[i+j*n]; } } for ( i = 0; i < n; i++ ) { d[i+i*n] = HUGE_VAL; } /* At FROM, find nearest unvisited city TO. */ p = ( 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. */ p[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 p; }