Skip to content
StackDecode
AI & ML Atlas
Artificial IntelligencealgorithmInteractive simulation

A* Search Algorithm

Finds the cheapest path by combining the cost so far with a guess of the cost still to go.

The idea

A* always expands the node with the lowest f = g + h, where g is the exact cost from the start and h is a heuristic estimate of the cost to the goal. If h never overestimates (it is "admissible"), the first time A* reaches the goal the path is guaranteed to be cheapest. A good heuristic lets it ignore most of the map; a bad one (h = 0) turns it back into Dijkstra.

f(n) = g(n) + h(n)

Watch it run

This is a real computation, not a drawing. Pause, step, change the settings, or try new random data.

SG
  • current
  • frontier
  • explored
  • path

Walls are dark, brown cells are mud (cost 3), everything else costs 1. Find a route from S to G.

1/42

Compare modes on the same map: A* and UCS find the cheapest route, greedy is fast but can be fooled.

Step by step

  1. 1Put the start node in an open list with f = 0 + h(start).
  2. 2Take the open node with the smallest f and move it to the closed set.
  3. 3If it is the goal, walk the parent links back to rebuild the path.
  4. 4Otherwise look at each neighbour: compute g = g(current) + step cost and f = g + h.
  5. 5Add the neighbour to the open list, or update it if this route is cheaper, then repeat from step 2.

Where it is used

Game and robot path-finding, route planners, puzzle solvers.

Watch out for

An inadmissible heuristic (one that overestimates) can return a path that is not the cheapest. Memory use can also grow large.

Related