A* is the classical best-first graph-search algorithm: it expands nodes in order of f(n) = g(n) + h(n), where g is the cost-to-come and h is an admissible heuristic estimating the cost-to-go. With an admissible and consistent heuristic, A* returns an optimal path and is optimally efficient (no other algorithm with the same heuristic expands fewer nodes). This implementation uses 8-connectivity with the octile heuristic and runs in single-digit milliseconds on a 30×30 grid against four environments shared with three other planners.
f(n) = g(n) + h(n)
g(n) = shortest known cost from start to n
h(n) = heuristic estimate of cost from n to goal
(admissible: h(n) ≤ true cost-to-go, never over-estimates)
For 8-connected grids the octile distance is the tight admissible heuristic, equal to the shortest 8-connected path on an empty grid:
octile(a, b) = (√2 − 1) · min(|Δx|, |Δy|) + max(|Δx|, |Δy|)
Loop: pop the node with smallest f from a min-heap, mark it expanded, relax all 8 neighbours, push improved neighbours back into the heap. Diagonal moves cost √2; cardinal moves cost 1.
Cyan dots are the cells A* expanded. The dark line is the recovered optimal path. The expansion frontier is biased toward the goal because of the heuristic; without h(n) (Dijkstra) the frontier would expand uniformly outward.
| Environment | Plan time | Path length | Expanded nodes |
|---|---|---|---|
| 0: scattered rectangles | 1.07 ms | 39.94 | 140 |
| 1: maze channels | 2.54 ms | 55.70 | 428 |
| 2: bottleneck | 2.88 ms | 42.87 | 296 |
| 3: cluttered | 0.50 ms | 38.77 | 73 |
A* is the path-length floor against which the other three planners on this site compare themselves: optimal by construction, fastest by 1–3 orders of magnitude, but constrained to the grid. Sub-millisecond plans on the cluttered environment because the heuristic prunes most of the search space.
A* is a global, complete, optimal planner on discrete grids. The other three planners on this site sit at different points in the design space and the cross-planner comparison below makes the trade-offs explicit: