0

According to wiki it will take (N-1)! to calculate a tour with N cities. I found a better way to do it but I can't do the math to calculate just how much I improved it. I can tell you that on my home pc I been able to solve 20 cities map in less than 1 hour. 20! = 2.43290200e+18. Here is what I did:

When searching a route of N cities (lets give them a names: City(1), City(2), City(3)... City(N)) with the brout algorithm, you will first perform this test: City(1), City(2), City(3), City(4)... City(N) and some time after, this one: City(1), City(3), City(2), City(4)... City(N). I am claiming that the second calculation is unnecessary. If I calculated just once the shortest route for City(4) ... City(N) I can use it for my second calculation and determine which route is better.

Using this trick I can reduce the number of calculation that I am doing for the K city by: (N - k) which is the number of options that I can shouse who will be the first city, multiply (N - K - 1)! which is the number of options that I have to choose the rest of the cities, and minus the first time, that I need to perform the full calculation. So it will be (N - K)!. And you need to sum it for all the K's starting from k = 3 to k = N - 2.

This is as far as I went,(which is not to far)... I hope you be able to help me to calculate this.

Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
  • I think this is a known trick to improve tsp so that it could handle 1 or 2 more cities comparing to the O(n^2*2^n) algorithm. mind posting your code here ? – noooooooob Feb 10 '14 at 04:43
  • @noooooooob Also posted it [here](http://cs.stackexchange.com/questions/16076/traveling-salesman-heldkarp-algorithm-big-improvement) – Ilya Gazman Feb 10 '14 at 08:03

1 Answers1

1

Storing and reusing results you've already calculated is the basic idea behind dynamic programming, for the TSP there are dynamic programming algorithms that runs with O[(N^2)*(2^N)] time, which will yield quicker result than your algorithm (you'll be able to solve problems with 25 vertices within minutes...)

See: Dynamic Programming for the TSP

Ron Teller
  • 1,880
  • 1
  • 12
  • 23
  • I don't think thats solving 20 vertices with O[(N^2)*(2^N)] is possible. 2^20 * 20 * 20 =~400M. When I were using my algorithm so 20 vertices is less then 100K and it takes almost hour. I know that it less than 100K because I calculated the number of times I compared between cities. – Ilya Gazman Oct 09 '13 at 15:06
  • Performing 400M basic operations should take less than a second on an average computer, there's no way your performing only 100K operations and it takes you almost an hour, you should check your runtime calculation. – Ron Teller Oct 09 '13 at 15:30
  • Yes but there is nothing basic in comparing Cities. Of course atomic action can vary between different algorithms, but yet I think it's impossible to go to 20 with algorithm you offer. – Ilya Gazman Oct 09 '13 at 15:54
  • @Babidu I know it's possible as i've implemented this algorithm several times. – Ron Teller Oct 09 '13 at 17:07
  • @Babidu: Can you post how your solution is only 100k combinations with 20 cities? – Micromega Oct 10 '13 at 11:23