Skip to content
Advertisement

Stuck implementing Wikipedia’s A* (“A star”) algorithm

I’m implementing the A* search algorithm from this pseudo on wikipedia’s article:

JavaScript

I’m stuck on the line where I’m asked to retrive the node in openSet with the lowest f value. When has openSet been filled? With what? Should it just have start on the first run?

I also don’t undestand the reconstruct path pseudo:

JavaScript

How should that pseudo instructions be implemented?

JavaScript

Advertisement

Answer

The open set initially contains the node that you start your search from – the starting node.

JavaScript

As for the reconstruct path part – each time you process a node and find its neighbour X can be reached with a lower cost from the current node, you should set the came_from entry of X to the node you’re currently processing. Once you find the destination node, you reconstruct the path by following the came_from entries from the destination node until you reach to source node. You can implement this by modifying the Point class to have an additional field called came_from.

The only way to extract the node with the lowest value from the hash table is to iterate through the hash table. An alternative is to additionally have a tree map which will allow you to quickly find the element with the smallest value (or have a specialized heap, e.g. binary or Fibonacci heap which additionally allows you to decrease values of elements within the heap).

This is where I originally learned A*.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement