# Traveling Salesman with Held and Karp Algorithm

I am well aware of the DP solution to the traveling salesman problem; also known as the Held and Karp algorithm for TSP.

I have implemented it with bitmask, and it's something like this:

int TSP(int pos, int bitmask) {
return dist[pos][0];              // Completing the round trip

for (int i = 0; i <= K; i++) {
if (i != pos && (bitmask & (1 << i)) == 0)
}

return memo[pos][bitmask] = answer;     // Storing the best dist for the set of traveled cities and untraveled ones.


This algorithm is quite fast; computation of 15 cities is relatively fast enough. However, I notice that it could be further improved to accommodate around 20 cities.

1) If the dist matrix is symmetrical, perhaps we can make use of this property to prevent repeated calculations. (e.g a->b->c->d->a == a->d->c->b->a)

2) Using both a upper and lower bound to prune. The above algorithm is able to get its first possible optimal solution in a very short time, might be able to use that.

I have tried to improve the algorithm based on the aforementioned two principles. However, I don't get a better algorithm.

Am I making a futile attempt at improving something impossible? What do you think?

• People have been developing efficient algorithms for TSP for ages. If you want a really fast implementation, use one of these. There is a high chance that whatever simple improvements you've thought of has also crossed these people's minds, and has been implemented if possible. – Yuval Filmus Oct 27 '13 at 16:46
• Is this related to the large number of other questions we've had about Euclidean TSP recently? – David Richerby Oct 27 '13 at 19:55