The Traveling Salesman Problem (TSP) is NP-Complete, but there are a few greedy approximate algorithms that are efficient. One of them is the Cheapest Link Algorithm, which I describe here.

The algorithm works by repeatedly choosing the cheapest link in the graph that:

- Doesn’t close the circuit
- Doesn’t create a vertex with three edges coming out of it

These cheapest links are added to the tour until it needs one more edge to complete it, at which point condition (1) is removed so the cheapest link that does not create a vertex with three edges will then be added and the tour is complete.

Below is the implementation in C. To prevent closing the circuit early I used the graph cycle detection algorithm I described in an earlier post. To make sure there are no vertices with three edges, I keep track of the degrees of the vertices as the tour is built, and edges that connect vertices with degree 2 are rejected.

#include <stdlib.h> typedef struct { unsigned int first; unsigned int second; unsigned int weight; } weighted_edge; static int compare_weighted_edges(const weighted_edge *edge1, const weighted_edge *edge2) { return edge1->weight - edge2->weight; } static unsigned int cyclic_recursive(const weighted_edge *edges, unsigned int n, unsigned int *visited, unsigned int order, unsigned int vertex, unsigned int predecessor) { unsigned int i; unsigned int cycle_found = 0; visited[vertex] = 1; for (i = 0; i < n && !cycle_found; i++) { if (edges[i].first == vertex || edges[i].second == vertex) { /* Adjacent */ const unsigned int neighbour = edges[i].first == vertex ? edges[i].second : edges[i].first; if (visited[neighbour] == 0) { /* Not yet visited */ cycle_found = cyclic_recursive(edges, n, visited, order, neighbour, vertex); } else if (neighbour != predecessor) { /* Found a cycle */ cycle_found = 1; } } } return cycle_found; } unsigned int cyclic(const weighted_edge *edges, unsigned int n, unsigned int order) { unsigned int *visited = calloc(order, sizeof(unsigned int)); unsigned int cycle_found; if (visited == NULL) { return 0; } cycle_found = cyclic_recursive(edges, n, visited, order, 0, 0); free(visited); return cycle_found; } weighted_edge *cheapest_link_tsp(weighted_edge *edges, unsigned int size, unsigned int order) { unsigned int t, e = 0; weighted_edge *tour = malloc(order * sizeof(weighted_edge)); unsigned int *degrees = calloc(order, sizeof(unsigned int)); if (tour == NULL || degrees == NULL) { free(tour); free(degrees); return NULL; } /* Sort the edges by weight */ qsort(edges, size, sizeof(weighted_edge), (int(*)(const void *, const void *))compare_weighted_edges); /* Main algorithm */ for (t = 0; t < order; t++) { unsigned int added = 0; while (!added && e < size) { if (degrees[edges[e].first] < 2 && degrees[edges[e].second] < 2) { tour[t] = edges[e]; if (t == order - 1 /* It's the last edge */ || !cyclic(tour, t + 1, order)) /* It doesn't close the circuit */ { added = 1; degrees[edges[e].first]++; degrees[edges[e].second]++; } } e++; } if (!added) { /* Edges were not correct */ free(tour); free(degrees); return NULL; } } free(degrees); return tour; }

Here is an example program that finds a solution for the graph shown at the top:

#include <stdio.h> #include <stdlib.h> /* Connect two edges */ void connect(weighted_edge *edges, unsigned int first, unsigned int second, unsigned int weight, unsigned int *pos) { edges[*pos].first = first; edges[*pos].second = second; edges[*pos].weight = weight; (*pos)++; } static void print_edges(const weighted_edge *edges, unsigned int n) { unsigned int e; for (e = 0; e < n; e++) { printf("(%u, %u, %u) ", edges[e].first, edges[e].second, edges[e].weight); } putchar('\n'); } int main(void) { unsigned int i = 0; const unsigned int size = 15; /* Edges */ const unsigned int order = 6; /* Vertices */ weighted_edge *edges = malloc(size * sizeof(weighted_edge)); weighted_edge *tour; connect(edges, 0, 1, 25, &i); connect(edges, 0, 2, 19, &i); connect(edges, 0, 3, 19, &i); connect(edges, 0, 4, 16, &i); connect(edges, 0, 5, 28, &i); connect(edges, 1, 2, 24, &i); connect(edges, 1, 3, 30, &i); connect(edges, 1, 4, 27, &i); connect(edges, 1, 5, 17, &i); connect(edges, 2, 3, 18, &i); connect(edges, 2, 4, 20, &i); connect(edges, 2, 5, 23, &i); connect(edges, 3, 4, 19, &i); connect(edges, 3, 5, 32, &i); connect(edges, 4, 5, 41, &i); tour = cheapest_link_tsp(edges, size, order); print_edges(tour, order); free(tour); free(edges); return 0; }

Output:

(0, 4, 16) (1, 5, 17) (2, 3, 18) (0, 2, 19) (1, 4, 27) (3, 5, 32)