The A* algorithm, stripped of all the code, is fairly simple. There are two sets, OPEN and CLOSED. The OPEN set contains those nodes that are candidates for examining. Initially, the OPEN set contains just one element: the starting position. The CLOSED set contains those nodes that have already been examined. Initially, the CLOSED set is empty. Graphically, the OPEN set is the “frontier” and the CLOSED set is the “interior” of the visited areas. Each node also keeps a pointer to its parent node so that we can determine how it was found.
There is a main loop that repeatedly pulls out the best node
n in OPEN (the node with the lowest
f value) and examines it. If
n is the goal, then we’re done. Otherwise, node
n is removed from OPEN and added to CLOSED. Then, its neighbors
n' are examined. A neighbor that is in CLOSED has already been seen, so we don’t need to look at it (*). A neighbor that is in OPEN is scheduled to be looked at, so we don’t need to look at it now (*). Otherwise, we add it to OPEN, with its parent set to
n. The path cost to
g(n'), will be set to
I go into a lot more detail here, with interactive diagrams.
(*) I’m skipping a small detail here. You do need to check to see if the node’s
g value can be lowered, and if so, you re-open it.
OPEN = priority queue containing START CLOSED = empty set while lowest rank in OPEN is not the GOAL: current = remove lowest rank item from OPEN add current to CLOSED for neighbors of current: cost = g(current) + movementcost(current, neighbor) if neighbor in OPEN and cost less than g(neighbor): remove neighbor from OPEN, because new path is better if neighbor in CLOSED and cost less than g(neighbor): ** remove neighbor from CLOSED if neighbor not in OPEN and neighbor not in CLOSED: set g(neighbor) to cost add neighbor to OPEN set priority queue rank to g(neighbor) + h(neighbor) set neighbor's parent to current reconstruct reverse path from goal to start by following parent pointers
(**) This should never happen if you have an consistent admissible heuristic. However in games we often have inadmissible heuristics.
See Python and C++ implementations here.
If your game has situations in which the start and goal are not connected at all by the graph, A* will take a long time to run, since it has to explore every node connected from the start before it realizes there’s no path. Calculate the Connected Components first and only use A* if the start and goal are in the same region.
The main loop of A* reads from a priority queue, analyzes it, and inserts nodes back into the priority queue. In addition, it tracks which nodes have been visited. To improve performance, consider:
- Can you decrease the size of the graph? This will reduce the number of nodes that are processed, both those on the path and those that don’t end up on the final path. Consider navigation meshes instead of grids. Consider hierarchical map representations.
- Can you improve the accuracy of the heuristic? This will reduce the number of nodes that are not on the final path. The closer the hheuristic to the actual path length (not the distance), the fewer nodes A* will explore. Consider these heuristics for grids. Consider ALT (A*, Landmarks, Triangle Inequality) for graphs in general (including grids).
- Can you make the priority queue faster? Consider other data structures for your priority queue. Consider processing nodes in batches, as fringe search does. Consider approximate sorting.
- Can you make the heuristic faster? The heuristic function is called for every open node. Consider caching its result. Consider inlining the call to it.
For grid maps, see these suggestions.
These demos run in your browser:
- I have written an introduction to A* with interactive demos.
- I have written Flash demos for square grids, hexagonal grids, and triangular grids. The Actionscript 3 code for these demos is available here (see Pathfinder.as for the main algorithm, and Graph.as for the abstract interface to graphs).
- This site has demos of A*, Breadth-First Search, Dijkstra’s Algorithm, and Greedy Best-First Search on road maps (not grids)
- This library has lots of optimizations for grid maps.
- James Macgill’s Java applet.
- This interactive demo lets you choose A* or Dijkstra’s Algorithm.
- This demo is nice and also has code available.
- This page describes Jump Point Search and also has an online demo.
- This Actionscript A* tutorial has a demo near the end.
- This A* demo and this Jump Point Search demo use Unity.
If you’re planning to implement graph search yourself, here’s my guide to Python and C++ implementations.
I’ve collected some links to source code but haven’t looked into these projects and can’t make specific recommendations:
- C++: 
- Java: 
- Python: 
- Objective C + Cocos2D: parts  and 
- Lua: more
- Ruby: 
- C#: [priority queue helper class]
- Unity: 
- Assembly: 
- Actionscript 3 (Flash): 
- Flex (Flash): 
- Go: 
- Prolog: 
- Processing: 
What’s the first thing you’ll think of using for the OPEN and CLOSED sets? If you’re like me, you probably thought “array”. You may have thought “linked list”, too. There are many different data structures we can use, but to pick one we should look at what operations are needed.
There are three main operations we perform on the OPEN set: the main loop repeatedly finds the best node and removes it; the neighbor visiting will check whether a node is in the set; and the neighbor visiting will insert new nodes. Insertion and remove-best are operations typical of a priority queue.
The choice of data structure depends not only on the operations but on the number of times each operations runs. The membership test runs once for each neighbor for each node visited. Insertion runs once for each node being considered. Remove-best runs once for each node visited. Most nodes that are considered will be visited; the ones that are not are the fringe of the search space. When evaluating the cost of operations on these data structures, we need to consider the maximum size of the fringe (F).
In addition, there’s a fourth operation, which is relatively rare but still needs to be implemented. If the node being examined is already in the OPEN set (which happens frequently), and if its
f value is better than the one already in the OPEN set (which is rare), then the value in the OPEN set must be adjustment. The adjustment operation involves removing the node (which is not the best
f) and re-inserting it. These two steps may be optimized into an increase-priority operation that moves the node (this is also called decrease-key).
My recommendation: The best generic choice is a binary heap. If you have a binary heap library available, use it. If not, start with sorted arrays or unsorted arrays, and switch to binary heaps if you want more performance. If you have more than 10,000 elements in your OPEN set, then consider more complicated structures such as a bucketing system.
Unsorted arrays or linked lists
The simplest data structure is an unsorted array or list. Membership test is slow, O(F) to scan the entire structure. Insertion is fast, O(1) to append to the end. Finding the best element is slow, O(F) to scan the entire structure. Removing the best element is O(F) for arrays and O(1) for linked lists. The increase-priority operation is O(F) to find the node and O(1) to change its value.
To make remove-best fast, we can keep the array sorted. Membership is then O(log F), since we can use binary search. Insertion is slow, O(F) to move all the elements to make space for the new one. Finding the best is fast, O(1) since it’s already at the end. And removing the best is O(1) if we make sure the best sorts to the end of the array. The increase-priority operation is O(log F) to find the node and O(F) to change its value/position.
Make sure the array is sorted so that the best element is at the end.
Sorted linked lists
With sorted arrays, insertion is slow. If we use a linked list, we can make that fast. Membership in the linked list is slow, O(F) to scan the list. Insertion is fast, O(1) to insert a new link, but it was O(F) to find the right position for it. Finding the best remains fast, O(1) because the best is at the end. Removing the best also is O(1). The increase-priority operation is O(F) to find the node and O(1) to change its value/position.
A binary heap (not to be confused with a memory heap) is a tree structure that is stored in an array. Unlike most trees, which use pointers to refer to children, the binary heap uses indexing to find children.
In a binary heap, membership is O(F), as you have to scan the entire structure. Insertion is O(log F) and remove-best is O(log F).
The increase-priority operation is tricky, with O(F) to find the node and surprisingly, only O(log F) to increase-priority it. Unfortunately most priority queue libraries don’t include this operation. Fortunately, it’s not strictly necessary. So I recommend not worrying about it unless you absolutely need to. Instead of increasing priority, insert a new element into the priority queue. You’ll potentially end up processing the node twice but that’s relatively cheap compared to implementing increase-priority.
You can combine a hash table or indexed array for membership and a priority queue for managing priorities; see the hybrid section below.
As part of my newer A* tutorial, I have a complete A* implementation in Python and C++ using binary heaps for the priorities and hash tables for the for membership. I do not implement increase-priority and explain why in the optimization section.
A variant of the binary heap is a d-ary heap, which has more than 2 children per node. Inserts and increase-priority become a little bit faster, but removals become a little bit slower. They may have better cache performance.
I have never needed anything other than a binary heap for my own pathfinding projects.
Sorted skip lists
Searching an unsorted linked list is slow. We can make that faster if we use a skip list instead of a linked list. With a skip list, membership is fast if you have the sort key: O(log F). Insertion is O(1) like a linked list if you know where to insert. Finding the best node is fast if the sort key is
f, O(1), and removing a node is O(1). The increase-priority operation involves finding a node, removing it, and reinserting it.
If we use skip lists with the map location as the key, membership is O(log F), insertion is O(1) after we’ve performed the membership test, finding the best node is O(F), and removing a node is O(1). This is better than unsorted linked lists in that membership is faster.
If we use skip lists with the
f value as the key, membership is O(F), insertion is O(1), finding the best node is O(1), and removing a node is O(1). This is no better than sorted linked lists.
If the set of nodes is finite and reasonably sized, we can use a direct indexing structure, where an index function
i(n) maps each node
n to an index into an array. Unlike the unsorted and sorted arrays, which have a size corresponding to the largest size of OPEN, with an indexed array the array size is always
max(i(n)) over all
n. If your function is dense (i.e., there are no indices unused), then
max(i(n)) will be the number of nodes in your graph. Whenever your map is a grid, it’s easy to make the function dense.
i(n) is O(1), membership test is O(1), as we merely have to check whether
Array[i(n)] contains any data. Insertion is O(1), as we just set
Array[i(n)]. Find and remove best is O(numnodes), since we have to search the entire structure. The increase-priority operation is O(1).
Indexed arrays take up a lot of memory to store all the nodes that are not in the OPEN set. An alternative is to use a hash table, with a hash function
h(n) that maps each node
n into a hash code. Keep the hash table twice as big as N to keep the chance of collisions low. Assuming
h(n) is O(1), membership test is expected O(1), insertion is expected O(1), and remove best is O(numnodes), since we have to search the entire structure. The increase-priority operation is O(1).
Hash tables are best for set membership but not for managing priorities. In my my newer A* tutorial, I use hash tables for membership and binary heaps for priorities. I combined the OPEN and CLOSED sets into one, which I call VISITED.
Heaps are a tree-based structure with expected O(log F) time operations. However, the problem is that with A*, the common behavior is that you have a low cost node that is removed (causing O(log F) behavior, since values have to move up from the very bottom of the tree) followed by low cost nodes that are added (causing O(log F) behavior, since these values are added at the bottom and bubble up to the very top). The expected case behavior of heaps here is equivalent to the worst case behavior. We may be able to do better if we find a data structure where expected case is better, even if the worst case is no better.
Splay trees are a self adjusting tree structure. Any access to a node in the tree tends to bring that node up to the top. The result is a “caching” effect, where rarely used nodes go to the bottom and don’t slow down operations. It doesn’t matter how big your splay tree is, because your operations are only as slow as your “cache size”. In A*, the low cost nodes are used a lot, and the high cost nodes aren’t used for a long time, so those high cost nodes can move to the bottom of the tree.
With splay trees, membership, insertion, remove-best, and increase-priority are all expected O(log F), worst case O(F). Typically however, the caching keeps the worst case from occurring. Dijkstra’s algorithm and A* with an underestimating heuristic however have some peculiar characteristics that may keep splay trees from being the best. In particular,
f(n') >= f(n) for nodes
n and neighboring node
n'. When this happens, it may be that the insertions all occur on one side of the tree and end up putting it out of balance. I have not tested this.
There’s another good data structure that may be better than heaps. Often, you can restrict the range of values that would be in the priority queue. Given a restricted range, there are often better algorithms. For example, sorting can be done on arbitrary values in O(N log N) time, but when there is a fixed range the bucket or radix sorts can perform sorting in O(N) time.
We can use HOT Queues (Heap On Top queues) to take advantage of
f(n') >= f(n) where
n' is a neighbor of
n. We are removing the node
n with minimal
f(n), and inserting neighbors
f(n) <= f(n') <= f(n) + delta where
delta <= C. The constant
C is the maximum change in cost from one point to an adjacent point. Since
f(n) was the minimal
f value in the OPEN set, and everything being inserted is
+ delta, we know that all
f values in the OPEN set are within a range of
0 .. delta. As in bucket/radix sort, we can keep “buckets” to sort the nodes in the OPEN set.
With HOT queues, the topmost bucket uses a binary heap and all other buckets are unsorted arrays. Membership test is O(F) because we don’t know which bucket the node is in. Insertion and remove-best in the top bucket are O(log (F/K)). Insertion into other buckets is O(1), remove-best never occurs! If the top bucket empties, then we need to convert the next bucket, an unsorted array, into a binary heap. It turns out this operation (“heapify”) can be run in O(F/K) time. The increase-priority operation is best treated as a O(F/K) removal followed by an O(log (F/K)) or O(1) insertion.
In A*, many of the nodes we put into OPEN we never actually need. HOT Queues are a big win because the elements that are not needed are inserted in O(1) time. Only elements that are needed get heapified (which is not too expensive). The only operation that is more than O(1) is node deletion from the heap, which is only O(log (F/K)).
In addition, if
C is small, we can just set K = C, and then we do not even need a heap for the smallest bucket, since all the nodes in a bucket have the same
f value. Insertion and remove-best are both O(1) time! One person reported that HOT queues are as fast as heaps for at most 800 nodes in the OPEN set, and are 20% faster when there are at most 1500 nodes. I would expect that HOT queues get faster as the number of nodes increases.
A cheap variant of a HOT queue is a two-level queue: put good nodes into one data structure (a heap or an array) and put bad nodes into another data structure (an array or a linked list). Since most nodes put into OPEN are “bad”, they are never examined, and there’s no harm putting them into the big array.
Fibonacci heaps are good priority queues for A*, in theory. However, in practice they’re not used. A pairing heap can be thought of as a simplified Fibonacci heap. They are said to work well in practice; I have never used them.
Data Structure Comparison
It is important to keep in mind that we are not merely looking for asymptotic (“big O”) behavior. We also want to look for a low constant. To see why, consider an algorithm that is O(log F) and another that is O(F), where F is the number of elements in the heap. It may be that on your machine, an implementation of the first algorithm takes 10,000 * log(F) seconds, while an implementation of the second one takes 2 * F seconds. For F = 256, the first would take 80,000 seconds and the second would take 512 seconds. The “faster” algorithm takes more time in this case, and would only start to be faster when F > 200,000.
You cannot merely compare two algorithms. You should also compare the implementations of those algorithms. You also have to know what size your data might be. In the above example, the first implementation is faster for F > 200,000, but if in your game, F stays under 30,000, then the second implementation would have been better.
None of the basic data structures is entirely satisfactory. Unsorted arrays or lists make insertion very cheap and membership and removal very expensive. Sorted arrays or lists make membership somewhat cheap, removal very cheap and insertion very expensive. Binary heaps make insertion and removal somewhat cheap, but membership is very expensive. Splay trees make everything somewhat cheap. HOT queues make insertions cheap, removals fairly cheap, and membership tests somewhat cheap. Indexed arrays make membership and insertion very cheap, but removals are incredibly expensive, and they can also take up a lot of memory. Hash tables perform similarly to indexed arrays, but they can take up a lot less memory in the common case, and removals are merely expensive instead of extremely expensive.
For a good list of pointers to more advanced priority queue papers and implementations, see Lee Killough’s Priority Queues page.
To get the best performance, you will want a hybrid data structure. For my A* code, I used an indexed array for O(1) membership test and a binary heap for O(log F) insertion and O(log F) remove-best. For increase-priority, I used the indexed array for an O(1) test whether I really needed to perform the change in priority (by storing the
g value in the indexed array), and then in those rare cases that I did need to increase priority, I used the O(F) increase-priority on the binary heap. You can also use the indexed array to store the location in the heap of each node; this would give you O(log F) for increase-priority.
In my newer A* tutorial, I use a hash table for membership and a binary heap for priorities. I further simplified by combining OPEN and CLOSED into the same set, which I call Visited.
Interactive (especially real-time) games introduce requirements that affect your ability to compute the best path. It may be more important to get any answer than to get the best answer. Still, all other things being equal, a shorter path is better than a longer one.
In general, computing the part of the path close to the starting point is more important than the path close to the goal. The principle of immediate start: get the unit moving as soon as possible, even along a suboptimal path, and then compute a better path later. In real-time games, the latency of A* is often more important than its throughput.
Units can be programmed to follow either their instincts (simple movement) or their brains (a precalculated path). Units will follow their instincts unless their brains tell them otherwise. (This approach is used in nature and also in Rodney Brook’s robot architecture.) Instead of calculating all the paths at once, limit the game to finding one path every one, two, or three game cycles. Then let the units start walking according to instinct (which could simply be moving in a straight line towards the goal), and come back later to find paths for them. This approach allows you to even out the cost of pathfinding so that it doesn’t occur all at once.
It is possible to exit early from the A* main loop and get a partial path. Normally, the loop exits when it finds the goal node. However, at any point before that, it can return a path to the currently best node in OPEN. That node is our best chance of getting to the goal, so it’s a reasonable place to go.
Candidates for early exit include having examined some number of nodes, having spent some number of milliseconds in the A* algorithm, or exploring a node some distance away from the starting position. When using path splicing, the spliced path should be given a smaller limit than a full path.
If few objects need pathfinding services or if the data structures used to store the OPEN and CLOSED sets are small, it can be feasible to store the state of the algorithm, exit to the game loop, then continue where A* left off.
Path requests do not arrive evenly distributed. A common situation in a real-time strategy game is for the player to select multiple units and order them to move to the same goal. This puts a high load on the pathfinding system.
In this situation, it is very likely that the path found for one will be useful for other units. One idea is to find a path
P from the center of the units to the center of the destinations. Then, use most of that path for all the units, but replace the first 10 steps and the last 10 steps by paths that are found for each individual unit. Unit i will receive a path from its starting location to
P, followed by the shared path
P[10..len(P)-10], followed by a path from
P[len(P)-10] to the destination.
The paths found for each unit are short (approximately 10 steps on average), and the long path is shared. Most of the path is found only once and shared among all the units. However, the user may not be impressed if he sees all the units moving on the same path. To improve the appearance of the system, make the units follow slightly different paths. One way to do this is to alter the paths themselves, by choosing adjacent locations.
Another approach is to make the units aware of each other (perhaps by picking a “leader” unit randomly, or by picking the one with the best sense of what’s going on), and only find a path for the leader. Then use a flocking algorithm to make them move in a group.
There are some variants of A* that handle a moving destination, or updated knowledge of the destination. Some of them might be adapted to handle multiple units going to the same destination, by performing A* in reverse (finding a path from the destination to the unit).
If the map contains few obstacles, but instead contains terrain of varying costs, then an initial path can be computed by treating terrain as being cheaper than normal. For instance, if grasslands are cost 1, hills are cost 2, and mountains are cost 3, then A* will consider walking through 3 grasslands to avoid 1 mountain. Instead, compute an initial path by treating grasslands as 1, hills as 1.1, and mountains as 1.2. A* will then spend less time trying to avoid the mountains, and it will find a path quicker. (It approximates the benefits of an exact heuristic.) Once a path is known, the unit can start moving, and the game loop can continue. When spare CPU is available, compute a better path using the real movement costs.