How to implement an A algorithm?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
A* is a shortest-path algorithm that combines the exact path cost so far with a heuristic estimate of the remaining distance. It is popular because it is much more directed than Dijkstra’s algorithm while still producing optimal paths when the heuristic is admissible.
Core Idea of A*
For each node n, A* tracks:
- '
g(n): the exact cost from the start ton' - '
h(n): the heuristic estimate fromnto the goal' - '
f(n) = g(n) + h(n)'
The algorithm always expands the open node with the lowest f score.
If h never overestimates the true remaining cost, A* is optimal. In grid pathfinding, Manhattan distance is a common heuristic when movement is limited to four directions.
Data Structures You Need
A practical implementation uses:
- a priority queue for the open set
- a map of best-known
gscores - a
came_frommap for path reconstruction - a closed set or stale-entry check to avoid useless work
Here is a compact Python implementation for a 2D grid:
Here 0 means walkable and 1 means blocked.
Example Usage
The output is the shortest path if one exists.
Choosing the Heuristic
The heuristic is what makes A* efficient. Good heuristics point the search toward the goal without overestimating.
Common choices:
- Manhattan distance for four-direction grids
- Euclidean distance for continuous or eight-direction movement
- domain-specific lower bounds in routing or planning systems
If the heuristic is always zero, A* collapses into Dijkstra’s algorithm.
Why Path Reconstruction Matters
Many beginners get the search to reach the goal but forget to store how each node was reached. Without came_from, you know the cost but not the actual path.
That is why every time a better route to a neighbor is found, the predecessor mapping must be updated.
Common Pitfalls
The biggest mistake is using a heuristic that overestimates the true remaining cost. That can make A* faster, but it can also destroy optimality.
Another issue is failing to update a node when a cheaper path is found later. A* correctness depends on keeping the best-known g score.
People also often ignore stale priority-queue entries. In simple implementations this is acceptable, but you must still rely on the best g score rather than trusting every popped queue entry blindly.
Finally, choose movement costs and heuristic units consistently. If they do not measure the same thing, the search behavior becomes distorted.
Summary
- A* combines exact path cost and heuristic estimate through
f(n) = g(n) + h(n). - Use a priority queue,
gscores, and a predecessor map. - Choose an admissible heuristic if you need optimal paths.
- Store predecessors so you can reconstruct the final route.
- Keep the heuristic and movement cost model aligned with the problem domain.
Related reading
- How to implement classic sorting algorithms in modern C?
- How to implement depth first search for graph with a non-recursive approach
- How to implement dfs using recursion?
- How to implement distributed algorithm of leader election using JBotsim library
- How to implement fast bigint division?
- How to implement lock-free skip list
- How to implement Ologn decrease-key operation for min-heap based Priority Queue?
- How to implement Prim's algorithm with a Fibonacci heap?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.