The idea: a cheap guess of the moves left
The 8-puzzle has eight numbered tiles and one blank on a 3 × 3 board. A move slides a tile next to the blank into it, and every move costs 1. A* search needs a heuristic h(n): a quick guess of how many moves are still needed from board n. A good guess lets A* skip most boards. This page compares two classic guesses on the same boards and counts the work each one saves.
The canvas shows the node being expanded (left), its up to four children (middle), and the frontier sorted by f = g + h (right). The table below keeps the result of each search on the chosen board. Pick a board and a search and press Search, or run a demo. The first few expansions are drawn one by one; after that the search is fast-forwarded and only the counts move.
Two heuristics, one board
h1 = misplaced tiles: count the tiles that are not on their goal cell. The blank does not count. h2 = Manhattan distance: for each tile, add the rows plus columns between its cell and its home cell (Demo 1: count h1 and h2 on board B).
Both are admissible: they never guess more than the real number of moves. A misplaced tile needs at least one move, and a tile k cells away needs at least k moves. Since every misplaced tile is at least 1 cell away, h2 ≥ h1 on every board. We say h2 dominates h1. With two admissible heuristics, the larger one never makes A* expand more nodes (apart from ties at the final f).
Admissible and consistent, seen in one move
A heuristic is consistent if for every move from n to n′ with cost c, h(n) ≤ c + h(n′). The guess may drop along a path, but never by more than the move cost. On the 8-puzzle this is easy to see. A move changes the position of one tile by one cell. That tile gets one step closer to home or one step farther, so h2 changes by exactly 1. Only that tile can become home or leave home, so h1 changes by at most 1 (Demo 2: one move changes h2 by exactly 1).
A consistent heuristic that is 0 at the goal is also admissible. With it, f never decreases along a path, so the first time A* pops a board it already has the cheapest path to it. That is why the page's A* never reopens an expanded board.
Counting the work
The page counts three things. A node is expanded when it is popped and its children are made. A node is generated when it is put on the frontier (the start not counted). States stored is how many different boards the search kept in memory. The effective branching factor b* is the branching factor a uniform tree of depth d would need to hold N = generated nodes: b* + b*² + … + b*d = N. Closer to 1 is better.
The rule for ties matters for exact counts. Here the frontier pops the lowest f, and among equal f the entry that was generated first. The blank tries up, down, left, right in that order. With h = 0 this is exactly breadth-first search (Demo 3 walks A* with h2 through board A step by step).
| search | board A (6 moves) | board B (18 moves) |
|---|---|---|
| h = 0 (breadth-first) | 78 expanded, 136 generated, b* 2.03 | 29081 expanded, 41102 generated, b* 1.72 |
| A* with h1 | 11 expanded, 21 generated, b* 1.37 | 1706 expanded, 2766 generated, b* 1.46 |
| A* with h2 | 7 expanded, 14 generated, b* 1.25 | 241 expanded, 400 generated, b* 1.28 |
All three return the same depth, because all three are admissible. On board B, h1 cuts the work by a factor of 17 and h2 by a factor of 120 (Demos 4 and 5). The gap grows with depth, because a smaller b* is raised to a higher power.
IDA*: an f-limit instead of a frontier
A* with h2 still stores 396 boards on board B, and on harder boards memory runs out before time does. IDA* (iterative deepening A*) keeps only the current path. Each round is a depth-first search that cuts every node with f = g + h above a limit. The first limit is h(start). After a failed round, the next limit is the smallest f that was cut, so no cheaper solution can be skipped. On board B the limits are 14, 16 and 18, with 18 + 88 + 201 = 307 expansions, a bit more than A*'s 241, but it never holds more than 19 boards (Demo 6: IDA* on board B). The limits rise by 2 because each move changes g by 1 and h2 by ±1, so f always changes by 0 or 2. See uniform-cost search and iterative deepening for the depth-limit version.
When the guess is too high
Use 2·h2 instead (weighted A* with weight 2). It can overestimate, so it is not admissible. A* then trusts the guess too much: it rushes toward boards that look close and stops at the first goal it pops. On board B it expands only 88 boards instead of 241, but returns a 20-move solution instead of 18 (Demo 7: 2·h2 overestimates). That trade is sometimes worth it on big puzzles, where any solution soon is better than the best one late.
Half the boards cannot be solved
Read the tiles row by row, skip the blank, and count inversions: pairs where a larger number comes before a smaller one. A sideways move does not change the reading order. An up or down move lifts one tile over the two tiles between, which changes the count by 0 or ±2. So the parity (odd or even) never changes. The goal has 0 inversions. A board with an odd count can never reach it, and only 9!/2 = 181440 boards can.
Every search on the page checks this first. Board C is board B with tiles 1 and 5 swapped; its search stops at once (Demo 8: board C cannot be solved). Without the check, A* would expand all 181440 boards it can reach before it gives up.
See also A* search for f = g + h on an ordinary graph, Dijkstra vs A* for how a heuristic changes the search shape, grid pathfinding for heuristics on a map, and TSP heuristics, where "heuristic" means a fast rule that may miss the optimum.
See also Variable Elimination, exact inference in a Bayesian network, from the same AI course.
What the page leaves out
Stronger heuristics: linear conflict, pattern databases and their additive versions; how to invent heuristics from relaxed problems; bidirectional search; the 15-puzzle, where breadth-first search is hopeless and IDA* with pattern databases is the standard; tie-breaking toward larger g, which often expands fewer nodes; reopening nodes when a heuristic is admissible but not consistent.