Heuristic Search

Is A * just Dijkstra with A heuristic?

A* is almost Dijkstra's algorithm with a heuristic added. Learn how the two differ, when A* reduces to Dijkstra, and why the heuristic matters for speed.

On this page
  1. How each algorithm scores nodes
  2. Why A* becomes Dijkstra
  3. Where the two differ in practice
  4. What can go wrong
  5. A grid example
  6. Summary

Mostly, yes. A* is Dijkstra’s algorithm with a heuristic added to the way it chooses which node to expand next. If the heuristic is zero for every state, A* behaves exactly like Dijkstra’s algorithm. A useful heuristic usually makes A* faster by steering it toward the goal.

How each algorithm scores nodes

Both algorithms keep a frontier of nodes that have been reached but not yet expanded. At each step, they expand the node with the lowest score. The difference is in the score.

Algorithm Score for each node Heuristic needed?
Dijkstra’s algorithm g(n) No
A* g(n) + h(n) Yes, though h(n) can be zero

Here g(n) is the cheapest known cost from the start to node n. For A*, h(n) is the estimated cost from n to the goal.

Why A* becomes Dijkstra

If h(n) is zero for every node, then A*’s score is just g(n). That is exactly Dijkstra’s score, so Dijkstra’s algorithm is a special case of A*. The extra term is what separates them.

Where the two differ in practice

  • Purpose. Dijkstra’s algorithm, in its basic form, finds the shortest path from one start node to every other reachable node. It can stop once the target is reached. A* is aimed at one goal from the beginning.
  • Search shape. Dijkstra’s search spreads outward from the start in rings of similar cost. A* leans toward the goal, so it often examines fewer nodes.
  • Requirements. In their usual forms, both need edge costs that are not negative. A* also needs its heuristic to be admissible, meaning it never overestimates the true remaining cost, if it is to be sure of finding the cheapest path.

What can go wrong

If the heuristic overestimates the remaining cost, A* can skip a cheap route and return a longer one. Suppose the heuristic says the goal is 10 steps away when the true cost is 4. The search may pass over the cheap route because it looks expensive. Some systems accept this trade-off on purpose to save time, but then the answer is no longer guaranteed to be the shortest.

A grid example

Imagine a grid where you can move up, down, left or right one square at a time. The start is at the bottom left and the goal is at the top right. Manhattan distance, which counts the horizontal and vertical steps to the goal, is a common heuristic here. It never overestimates on this grid, so A* finds a shortest path.

Dijkstra’s algorithm also finds a shortest path, but it often examines more squares, because it does not know which direction the goal lies in. The exact number depends on the grid and on where the obstacles are.

Summary

A* is Dijkstra’s algorithm with a heuristic added to each node’s score. With a zero heuristic, the two are the same. With a good heuristic that never overestimates, A* usually does less work. For a wider view of how A* fits among other methods, see the overview of types of heuristic search in artificial intelligence.

The complete guide to heuristic search

Types of Heuristic Search in Artificial Intelligence
Read the guide

Keep exploring

Guides from other topics

All topics →