A* path reconstruction

Started by
2 comments, last by Kris_A 18 years, 9 months ago
I implemented A* based on the wikipedia.org article. It states: "This path reconstruction from the stored closed nodes (see below) means it is not necessary to store the path-so-far within each node." But below that line there is no mention of how to reconstruct the path. So how do I reconstruct the path from the 'closed' priority queue without storing the path so far in each node?
Advertisement
You "traverse in reverse". Whenever you create a node, store its parent node. When you find the node that's at the target position, go into a loop that backtracks the list of nodes (using 'node->parent') until it reaches the first node, adding each node to a list. Then just reverse the list so the path is in the correct order (or if you like you can just go backwards through the list whenever you're making things actually follow the path)

if (node->x == target_x && node->y == target_x) {   while (node != first_node) {      list[list_pos++] = node;      node = node->parent;   }   reverse(list);}

Hope this helps!
that does, thank you very much.

although that solution is sufficient. is it possible to reconstruct the path without even storing the parent?
Not sure, but I would guess not. If you're trying to optimise for size, I think the best you can get away with is storing the parent as a reference to an array element rather than a pointer.

Edit: You could always try different things, like running the pathfinder in reverse, and restrict it to the nodes already created by the first run. Not sure what results that would produce, though.

This topic is closed to new replies.

Advertisement