# Traveling Salesman Problem using backtracking in C

I have previously shown the Cheapest-Link, Nearest-Neigbour, and Repetitive-Nearest Neighbour algorithms for the Traveling Salesman Problem. These are all greedy algorithms that give an approximate result. The Traveling Salesman Problem is NP-complete, so an exact algorithm will have exponential running time unless $$P=NP$$. However, we can reduce the search space for the problem by using backtracking.

This is an implementation of TSP using backtracking in C. It searches the permutation space of vertices, fixing the start of each tour at vertex 0. This means that the last edge is always the one that connects the second-last edge to vertex 0, so it is not necessary to find this edge by permutation. The function traveling_salesman() takes a graph in the form of a matrix of distances (adjmat), the number of vertices (order), and the address of a pointer to an array of unsigned integers used as an output parameter (best_tour). It returns the cost of the best tour, and assigns an array containing the vertices of the tour in order to *best_tour.

#include <stdlib.h>
#include <string.h>

static void swap(unsigned int *a, unsigned int *b)
{
unsigned int temp = *a;
*a = *b;
*b = temp;
}

static void traveling_salesman_recursive(const unsigned int **adjmat, unsigned int order,
unsigned int *best_tour, unsigned int *best_tour_cost, unsigned int *partial_tour,
unsigned int *partial_tour_cost, unsigned int level)
{
if (level == order - 1) {
/* Add last two edges to complete the tour */
unsigned int tour_cost = *partial_tour_cost
+ adjmat[partial_tour[order - 2]][partial_tour[order - 1]]
if (*best_tour_cost == 0 || tour_cost < *best_tour_cost) {
/* Best tour so far */
memcpy(best_tour, partial_tour, order * sizeof(unsigned int));
*best_tour_cost = tour_cost;
}
}
else {
unsigned int i;
for (i = level; i < order; i++) {
if (*best_tour_cost == 0
|| *partial_tour_cost + adjmat[partial_tour[level - 1]][partial_tour[i]]
< *best_tour_cost)
{
/* Next permutation */
swap(&partial_tour[level], &partial_tour[i]);
unsigned int cost = adjmat[partial_tour[level - 1]][partial_tour[level]];
*partial_tour_cost += cost;
partial_tour, partial_tour_cost, level + 1);
*partial_tour_cost -= cost;
swap(&partial_tour[level], &partial_tour[i]);
}
}
}
}

unsigned int traveling_salesman(const unsigned int **adjmat, unsigned int order,
unsigned int **best_tour)
{
unsigned int i;
unsigned int best_tour_cost = 0;
unsigned int partial_tour_cost = 0;
unsigned int *partial_tour = malloc(order * sizeof(unsigned int));
*best_tour = malloc(order * sizeof(unsigned int));
if (partial_tour == NULL || *best_tour == NULL) {
free(partial_tour);
free(*best_tour);
*best_tour = NULL;
return 0;
}
for (i = 0; i < order; i++) {
partial_tour[i] = i;
}
&partial_tour_cost, 1);
free(partial_tour);
return best_tour_cost;
}


Here is an example program:

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
unsigned int r1[] = {0, 5, 7, 9, 10};
unsigned int r2[] = {4, 0, 11, 3, 7};
unsigned int r3[] = {3, 1, 0, 4, 5};
unsigned int r4[] = {6, 5, 7, 0, 11} ;
unsigned int r5[] = {13, 2, 8, 3, 0} ;
const size_t order = 5; /* Vertices */
const unsigned int *adjmat[] = { r1, r2, r3, r4, r5 };
unsigned int *best_tour;
unsigned int cost = traveling_salesman(adjmat, order, &best_tour);
unsigned int i;
printf("Best tour cost: %u\n", cost);
printf("Vertices:\n");
for (i = 0; i < order; i++) {
printf("%u ", best_tour[i]);
}
putchar('\n');
printf("Edge weights:\n");
for (i = 0; i < order - 1; i++) {
}
free(best_tour);
return 0;
}


The output:

Best tour cost: 23
Vertices:
0 2 4 1 3
Edge weights:
7 5 2 3 6


# Repetitive Nearest Neighbour Algorithm for TSP in C

The Repetitive Nearest Neighbour Algorithm (RNNA) is a refinement of the Nearest Neighbour Algorithm. It uses the same greedy strategy of going to the nearest unvisited neighbour at each step, but instead of constructing just one tour, the RNNA constructs a tour starting from every vertex of the graph, and then selects the one with the lowest total length to return as the solution.

Here is the implementation in C:

#include <stdlib.h>

typedef struct {
unsigned int first;
unsigned int second;
unsigned int weight;
} weighted_edge;

/* Check if the tour already contains an edge */
static unsigned int tour_contains(const weighted_edge *tour, unsigned int t,
const weighted_edge *edge)
{
unsigned int contains = 0;
unsigned int i;
for (i = 0; i < t && !contains; i++) {
contains = tour[i].first == edge->first
&& tour[i].second == edge->second;
}
return contains;
}

/* Find the edge to v's nearest neighbour not in the tour already */
static unsigned int nearest_neighbour_edge(const weighted_edge *edges, unsigned int size,
const weighted_edge *tour, unsigned int t, unsigned int v)
{
unsigned int min_distance = 0;
unsigned int nearest_neighbour;
unsigned int i;
for (i = 0; i < size; i++) {
if ((edges[i].first == v || edges[i].second == v)
&& (min_distance == 0 || edges[i].weight < min_distance)
&& !tour_contains(tour, t, &edges[i]))
{
min_distance = edges[i].weight;
nearest_neighbour = i;
}
}
return nearest_neighbour;
}

weighted_edge *repetitive_nearest_neighbour_tsp(const weighted_edge *edges, unsigned int size,
unsigned int order)
{
unsigned int best_tour_distance = 0;
weighted_edge *best_tour = NULL;
unsigned int v;
for (v = 0; v < order; v++) {
unsigned int t;
unsigned int distance = 0;
weighted_edge *tour = malloc(order * sizeof(weighted_edge));
if (tour == NULL) {
return NULL;
}
for (t = 0; t < order; t++) {
unsigned int e = nearest_neighbour_edge(edges, size, tour, t, v);
tour[t] = edges[e];
distance += edges[e].weight;
v = edges[e].first == v ? edges[e].second : edges[e].first;
}
if (best_tour_distance == 0 || distance < best_tour_distance) {
best_tour_distance = distance;
free(best_tour);
best_tour = tour;
}
else {
free(tour);
}
}
return best_tour;
}


Here is an example program in C using the same graph as I used for the Nearest Neighbour and Cheapest-Link algorithms:

#include <stdio.h>
#include <stdlib.h>

void weighted_edge_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)++;
}

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;

weighted_edge_connect(edges, 0, 1, 25, &i);
weighted_edge_connect(edges, 0, 2, 19, &i);
weighted_edge_connect(edges, 0, 3, 19, &i);
weighted_edge_connect(edges, 0, 4, 16, &i);
weighted_edge_connect(edges, 0, 5, 28, &i);
weighted_edge_connect(edges, 1, 2, 24, &i);
weighted_edge_connect(edges, 1, 3, 30, &i);
weighted_edge_connect(edges, 1, 4, 27, &i);
weighted_edge_connect(edges, 1, 5, 17, &i);
weighted_edge_connect(edges, 2, 3, 18, &i);
weighted_edge_connect(edges, 2, 4, 20, &i);
weighted_edge_connect(edges, 2, 5, 23, &i);
weighted_edge_connect(edges, 3, 4, 19, &i);
weighted_edge_connect(edges, 3, 5, 32, &i);
weighted_edge_connect(edges, 4, 5, 41, &i);

tour = repetitive_nearest_neighbour_tsp(edges, size, order);
print_edges(tour, order);

free(tour);
free(edges);
return 0;
}


Here is the output:

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


# Nearest Neighbour Algorithm for TSP in C

The Nearest Neighbour Algorithm is the simplest greedy approximate algorithm for the TSP.

The steps are:

1. Pick a starting vertex
2. Go to its nearest neighbour in the graph
3. Repeat, only going to unvisited vertices
4. When all vertices have been visited, stop

Here it is in C:

#include <stdlib.h>

typedef struct {
unsigned int first;
unsigned int second;
unsigned int weight;
} weighted_edge;

/* Check if the tour already contains an edge */
static unsigned int tour_contains(const weighted_edge *tour, unsigned int t,
const weighted_edge *edge)
{
unsigned int contains = 0;
unsigned int i;
for (i = 0; i < t && !contains; i++) {
contains = tour[i].first == edge->first
&& tour[i].second == edge->second;
}
return contains;
}

/* Find the edge to v's nearest neighbour not in the tour already */
static unsigned int nearest_neighbour_edge(const weighted_edge *edges, unsigned int size,
const weighted_edge *tour, unsigned int t, unsigned int v)
{
unsigned int min_distance = 0;
unsigned int nearest_neighbour;
unsigned int i;
for (i = 0; i < size; i++) {
if ((edges[i].first == v || edges[i].second == v)
&& (min_distance == 0 || edges[i].weight < min_distance)
&& !tour_contains(tour, t, &edges[i]))
{
min_distance = edges[i].weight;
nearest_neighbour = i;
}
}
return nearest_neighbour;
}

weighted_edge *nearest_neighbour_tsp(const weighted_edge *edges, unsigned int size,
unsigned int order)
{
unsigned int t, v = 0;
weighted_edge *tour = malloc(order * sizeof(weighted_edge));
if (tour == NULL) {
return NULL;
}
for (t = 0; t < order; t++) {
unsigned int e = nearest_neighbour_edge(edges, size, tour, t, v);
tour[t] = edges[e];
v = edges[e].first == v ? edges[e].second : edges[e].first;
}
}


Here is an example program that solves the TSP for the same graph as I used for the Cheapest-Link Algorithm:

#include <stdio.h>
#include <stdlib.h>

/* Connect two edges */
void weighted_edge_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)++;
}

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;

weighted_edge_connect(edges, 0, 1, 25, &i);
weighted_edge_connect(edges, 0, 2, 19, &i);
weighted_edge_connect(edges, 0, 3, 19, &i);
weighted_edge_connect(edges, 0, 4, 16, &i);
weighted_edge_connect(edges, 0, 5, 28, &i);
weighted_edge_connect(edges, 1, 2, 24, &i);
weighted_edge_connect(edges, 1, 3, 30, &i);
weighted_edge_connect(edges, 1, 4, 27, &i);
weighted_edge_connect(edges, 1, 5, 17, &i);
weighted_edge_connect(edges, 2, 3, 18, &i);
weighted_edge_connect(edges, 2, 4, 20, &i);
weighted_edge_connect(edges, 2, 5, 23, &i);
weighted_edge_connect(edges, 3, 4, 19, &i);
weighted_edge_connect(edges, 3, 5, 32, &i);
weighted_edge_connect(edges, 4, 5, 41, &i);

tour = nearest_neighbour_tsp(edges, size, order);
print_edges(tour, order);

free(tour);
free(edges);
return 0;
}


The output:

(0, 4, 16) (3, 4, 19) (2, 3, 18) (0, 2, 19) (0, 3, 19) (1, 3, 30)


# Cheapest-Link Algorithm for TSP in C

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:

1. Doesn’t close the circuit
2. 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) {
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++) {
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 */
{
degrees[edges[e].first]++;
degrees[edges[e].second]++;
}
}
e++;
}
/* Edges were not correct */
free(tour);
free(degrees);
return NULL;
}
}
free(degrees);
}


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);


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