About this visualization
A* expands the node with the smallest f(n) = g(n) + h(n): the cost already paid plus an estimate of the cost remaining. With an admissible heuristic it finds the cheapest path while exploring far fewer nodes than uniform-cost search. Build any graph, set h, and watch f values decide the order.
How it works
- g(n) is the real cost from the start; h(n) is the estimated cost to the goal.
- Expand the frontier node with the smallest f = g + h.
- If a cheaper path to a known node appears, update it (and re-open it if it was already expanded).
Formulas used
- Admissible: , the true cost to the goal
- Consistent:
Worked example
On the Romania map from Arad to Bucharest with straight-line distances as h, A* expands Arad (f = 366), Sibiu (393), Rimnicu (413), Fagaras (415) and Pitesti (417), then picks Bucharest (f = 418) via Arad → Sibiu → Rimnicu → Pitesti → Bucharest. That is the optimal path, found with 5 expansions where uniform-cost search needs 12.
Enter these numbers in the simulator above to see every step.
Complexity and properties
| Worst case | Exponential in the heuristic's error |
|---|---|
| Space |
Complete. Optimal when h is admissible (and consistent for graph search). The better h is, the fewer nodes are expanded.
When to use it
- Route planning in maps, games and robotics.
- Learning admissible and consistent heuristics.
- Comparing A* with Dijkstra and greedy search.
Frequently asked questions
What does an admissible heuristic mean?
h(n) never over-estimates the true cost from n to the goal. With an admissible heuristic A* is guaranteed to find an optimal path.
Is A* better than Dijkstra's algorithm?
With a good heuristic A* explores far fewer nodes. With h = 0 it becomes uniform-cost search (Dijkstra).
What is the time complexity of A*?
It is exponential in the worst case, but practical performance depends on how accurate the heuristic is. It keeps all generated nodes in memory, .
What is a consistent heuristic?
h(n) ≤ cost(n, m) + h(m) for every edge n → m. Consistency implies admissibility and means A* never needs to re-open nodes.