Thursday, August 11, 2011

Brute force with traveling salesman algorithm?

Given that you have the distances between every pair of cities represented somehow (a map data structure would work), and uming a single given starting city, the problem boils down to generating all permutations of the remaining cities (besides the starting city). Then it is a simple matter to take each of those permutations (with the starting city prepended) and add up their cost (distance), by adding up the distances between successive cities in the list. If there is a requirement to return to the starting city, then add one more distance, from the last city on the list back to the starting city. The permutation with the least distance wins.

No comments:

Post a Comment