The problem with searching in every direction
A* adds that idea.
f = g + h
Every node carries three numbers:
- g(n) — the actual cost of the best known path from the start to n. This is exactly Dijkstra’s distance.
- h(n) — the heuristic: an estimate of the left cost from n to the goal.
- f(n) = g(n) + h(n) — the estimated total cost of a route through n.
A* is then exactly Dijkstra with the priority queue ordered by f instead of g. Set h(n) = 0 everywhere and you get Dijkstra back exactly — A* is a strict extension, not a different algorithm.
Admissible, steady, and why it stays best
A heuristic is admissible if it never overestimates the true left cost. That single property is what guarantees A* finds a best path.
The reason: if h never overestimates, then f(n) never overestimates the cost of the best route through n. So when the goal is popped with total cost f, no unexplored node can be hiding a cheaper route. Any such node would have had a smaller f and been popped first.
Overestimate, and that breaks. An inflated h can make the true best route look worse than a bad one, and A* will return the bad one. The common heuristics on a grid are admissible by construction:
- Manhattan |dx| + |dy| — admissible when movement is 4-directional, because that is the exact distance with no obstacles.
- Euclidean √(dx² + dy²) — admissible always. Since a straight line is the shortest doable route, but weak (too low) on 4-directional grids.
- Zero — admissible and useless: this is Dijkstra.
Note that Manhattan is not admissible if diagonal movement is allowed, because the diagonal route is shorter than |dx| + |dy|. That mismatch is the most common way people accidentally break optimality.
Choosing the heuristic is the whole design
The heuristic decides how much better than Dijkstra A* is, and getting it wrong is the usual failure.
Too weak (close to 0) and A* degenerates towards Dijkstra, exploring almost all of it.
Not admissible and the result is suboptimal. The classic mistake on a 4-directional grid is using Euclidean distance: it underestimates correctly. So it is admissible. But Manhattan distance is a tighter underestimate and so explores far less. Conversely, using Manhattan distance when diagonal movement is allowed overestimates and breaks optimality.
Too expensive to compute and the per-node saving is lost. h is evaluated for every node examined.
The practical rule: use the tightest admissible estimate that is cheap to compute, and match it to the actual movement rules.
For road networks the straight-line distance is admissible but weak, because roads are not straight. Production routing engines use landmark-based heuristics: precompute distances from a few hundred landmark nodes, and use the triangle inequality to derive much tighter bounds. That is one of the techniques that makes continental route planning interactive.
Guided tests
- See the search become directional. Press Run Algorithm and watch the explored region. It stretches toward the goal rather than spreading in a circle — that elongation is h doing its job.
- Turn the heuristic off. Set Heuristic to the zero or Dijkstra option and press Reset Grid, then run again. The explored area balloons into an even blob. The path found is the same length; the work to find it is far greater.
Where it is used
- Game pathfinding. The default algorithm for moving units around a map, often on a grid or navigation mesh.
- Robotics. Motion planning over occupancy grids, often with variants handling smooth space.
- Route planning. Real navigation systems use A* with landmark heuristics, or contraction hierarchies which precompute shortcuts.
- Puzzle solving. Sliding tiles, Rubik's cube and similar. Where the heuristic is a relaxation of the puzzle. For the 15-puzzle, the sum of Manhattan distances of each tile from its home.
- Word ladders and state-space search often, where the graph is made on demand.
- Automated planning in AI, where the state space is enormous and heuristics are important.
The puzzle case illustrates a general technique for inventing heuristics: solve a relaxed version of the problem. Ignoring that tiles block each other gives the Manhattan-distance heuristic. This is admissible because the real puzzle can only be harder.