See how the heuristic guides A* toward the goal, exploring fewer nodes than Dijkstra
A* is an informed search algorithm that combines the actual distance travelled so far with an estimated remaining distance to decide which node to explore next.
The heuristic h(n) estimates the remaining cost from node n to the goal. It must be admissible — it must never overestimate the true remaining distance. If it does, A* might miss the optimal path. Common admissible heuristics include straight-line (Euclidean) distance and Manhattan distance.
Why does A* explore fewer nodes? Dijkstra picks the node with the lowest g(n) — distance from the start. A* picks the node with the lowest f(n) = g(n) + h(n), which factors in how close a node is to the goal. This steers the search in the right direction, skipping nodes that are far from the goal. A* also stops as soon as the goal is reached, while Dijkstra must visit every node.
Special case: If h(n) = 0 for all nodes, the heuristic gives no guidance and A* degrades to Dijkstra exactly.
Explores all directions equally. Picks the node with the smallest g(n). Must visit every reachable node. No knowledge of where the goal is.
Explores toward the goal. Picks the node with the smallest f(n) = g(n) + h(n). Stops when the goal is reached. Uses a heuristic to skip irrelevant nodes.