views:

75

answers:

2

I have the ability to calculate the best route between a start and end point using A*. Right now, I am including waypoints between my start and end points by applying A* to the pairs in all permutations of my points.

Example:

I want to get from point 1 to point 4. Additionally, I want to pass through points 2 and 3.

I calculate the permutations of (1, 2, 3, 4):

1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1

Then, for each permutation, I calculate the A* route from the first to the second, then append it to the route from the second to the third, then the third to the fourth.

When I have this calculated for each permutation, I sort the routes by distance and return the shortest.

Obviously, this works but involves a lot of calculation and totally collapses when I have 6 waypoints (permutations of 8 items is 40320 :-))

Is there a better way to do this?

+2  A: 

First of all, you should store all intermediate calculations. Once you calculated the route from 1 to 2, you should never recalculate it again, just look up in a table. Second, if your graph is undirected, a route from 2 to 1 has exactly the same distance as a route from 1 to 2, so you should not recalculate it either.

And finally, in any case you will have an algorithm that is exponential to the number of points you need to pass. This is very similar to the traveling salesman problem, and it will be exactly this problem if you include all available points. The problem is NP-complete, i.e. it has complexity, exponential to the number of waypoints.

So if you have a lot of points that you must pass, exponential collapse is inevitable.

Delynx
Memoization FTW!
Sarah Vessels
You can see [the wikipedia article](http://en.wikipedia.org/wiki/Traveling_Salesman_Problem) on the Traveling Salesman problem.Note that there are meta-heuristics you can try. Personally, I had fun implementing [ant colony optimization](http://en.wikipedia.org/wiki/Ant_colony_optimization)
Justin L.
+1  A: 
Eric