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.

Board B, 5 1 4 / 8 blank 3 / 2 7 6, drawn twice next to the goal. Left: all 8 tiles are orange because none is on its home cell, so h1 = 8. Right: each tile carries its Manhattan distance, tiles 1 to 8 give 1 + 3 + 1 + 3 + 2 + 1 + 1 + 2 = 14, so h2 = 14. The real shortest solution has 18 moves
On board B, h1 = 8 and h2 = 14; the shortest solution has 18 moves. Both guesses are too low, and h2 is closer.

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).

Board B with h2 = 14 in the middle and its four children around it. The blank moving up gives h2 = 15, down 15, left 13, right 15. Each arrow is labelled cost 1 and change of h2 plus or minus 1
Every move from board B changes h2 by exactly 1 (up +1, down +1, left −1, right +1), so h(n) ≤ 1 + h(n′): h2 is consistent.

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).

searchboard A (6 moves)board B (18 moves)
h = 0 (breadth-first)78 expanded, 136 generated, b* 2.0329081 expanded, 41102 generated, b* 1.72
A* with h111 expanded, 21 generated, b* 1.371706 expanded, 2766 generated, b* 1.46
A* with h27 expanded, 14 generated, b* 1.25241 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.

Board B, 5 1 4 / 8 blank 3 / 2 7 6, read as 5 1 4 8 3 2 7 6, has 12 inversions, even, solvable. Board C swaps tiles 5 and 1: read as 1 5 4 8 3 2 7 6 it has 11 inversions, odd, unsolvable
Swapping two tiles changes the inversion count by an odd number: board B has 12 (solvable), board C has 11 (unsolvable).

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.

On the mind map

Open the Graph Algorithms mind mapGraph AlgorithmsTraversal: visit every reachable vertexTraversalConnectivity and order: DFS reveals the structureConnectivity and orderShortest paths: relax edges until distances settleShortest pathsHeuristic search: aim the search at the goalHeuristic searchSpanning trees: connect all vertices, no cycleSpanning treesFlow, matching, coloring: assign and push along edgesFlow, matching, coloringComplexity: easy to check vs easy to findComplexityGraphs at scale: rank and group huge graphsGraphs at scaleA* searchA* searchf = g + h; admissible h stays optimal8-puzzle heuristicsManhattan beats misplaced: 241 vs 1706Dijkstra vs A*Dijkstra vs A*heuristic expands fewer verticesGrid pathfindingGrid pathfindingA*, BFS, greedy, JPS on walls and mudGrid pathfinders raceGrid pathfinders racegreedy is fast but not always shortestD* Lite replanningD* Lite replanningrepair the last search, not restartTheta* any-angleTheta* any-angleline of sight lets parents skip cellsPathfinding playgroundPathfinding playgrounddraw walls, pick an algorithmOpen the Graph Algorithms mind mapGraph Algorithmsaim the search at the goalHeuristic searchA* searchA* searchf = g + h; admissible h stays optimal8-puzzle heuristicsManhattan beats misplaced: 241 vs 1706Dijkstra vs A*Dijkstra vs A*heuristic expands fewer verticesGrid pathfindingGrid pathfindingA*, BFS, greedy, JPS on walls and mudGrid pathfinders raceGrid pathfinders racegreedy is fast but not always shortestD* Lite replanningD* Lite replanningrepair the last search, not restartTheta* any-angleTheta* any-angleline of sight lets parents skip cellsPathfinding playgroundPathfinding playgrounddraw walls, pick an algorithm
This page on the Graph Algorithms mind map · 39 pages in 8 branches · open it to see the whole map and test yourself in recall mode.