Heuristic Search

Types of Heuristic Search in Artificial Intelligence

A plain-language guide to the main types of heuristic search in AI, including best-first search, A*, hill climbing, beam search and simulated annealing.

On this page
  1. Heuristic search compared with blind search
  2. Best-first search and greedy search
  3. A* search
  4. Memory-saving versions of A*
  5. Local search methods
  6. Where heuristics come from
  7. A worked example: the 8-puzzle
  8. Choosing a method
  9. Summary

Heuristic search is a way of solving problems by exploring options in an order guided by an estimate of how close each option is to a goal. That estimate is the heuristic. The main types include best-first search (including its greedy form), A* and its memory-saving versions, and local search methods such as hill climbing, simulated annealing and beam search. Each type makes a different trade-off between solution quality, speed and memory use.

Blind, or uninformed, search explores a problem without any sense of how far a state is from the goal. Breadth-first search and depth-first search are standard examples. They are systematic and easy to reason about, but on large problems they can examine far more states than needed.

Heuristic, or informed, search adds a heuristic function, usually written h(n), which estimates the cost of the cheapest path from state n to a goal. The estimate does not have to be perfect. It only has to point the search in a useful direction. On a road map, the straight-line distance to the destination is a common example.

Best-first search keeps a frontier of states it has found but not yet explored, and it always expands the one with the best score. Greedy best-first search uses only the heuristic as its score: f(n) = h(n). It tends to head straight toward the goal, which often makes it fast. However, it only looks at the estimate, so it can follow a promising route that turns out to be a dead end or a longer path. It does not guarantee the best solution.

A* scores each state with f(n) = g(n) + h(n). Here g(n) is the cost already paid to reach the state, and h(n) is the estimated cost still to come. Because A* counts both, it balances the cost so far against the expected cost to the goal.

Two properties of a heuristic matter here:

  • Admissible means h(n) never overestimates the true cost from n to the goal. With an admissible heuristic, A* finds an optimal solution, meaning the cheapest path.
  • Consistent, also called monotone, means the estimate never drops by more than the cost of the step between two neighboring states. A consistent heuristic is always admissible too. It also lets the standard version of A* that keeps track of visited states avoid revisiting them.

A heuristic that is more informed usually means less work. If two admissible heuristics h1 and h2 satisfy h2(n) ≥ h1(n) for every state, h2 is said to dominate h1. A* using h2 does not expand more states than A* using h1, apart from ties.

The main drawback of A* is memory. It keeps many states in storage, so on large problems it can run out of memory before it finds a solution.

Memory-saving versions of A*

  • Iterative deepening A* (IDA*) runs a depth-first search again and again, each time with a larger limit on the f-cost. Its memory grows with the length of the path rather than with the size of the frontier, but it may revisit states, which costs time.
  • Recursive best-first search (RBFS) follows the logic of best-first search using memory that grows only with the depth of the search. It remembers the best alternative f-value so it can return to another route if the current one turns out worse.
  • Simplified memory-bounded A* (SMA*) works within a fixed amount of memory. When that memory is full, it forgets the least promising states it has stored.

Local search methods

Local search works differently from A*. It keeps track of only a current state and moves to a neighboring state. It does not usually remember the path it took, so it suits problems where the goal is a good arrangement, such as a schedule or a layout, rather than a route.

  • Hill climbing moves to the neighbor with the best value and stops when no neighbor is better. It is simple and fast, but it can get stuck. It may settle on a local maximum, which is a peak lower than the highest one. It can also stall on a plateau, a flat area where nearby states look equally good, or on a ridge, a narrow upward path that single moves struggle to follow. Random restarts and occasional random moves are common fixes.
  • Simulated annealing sometimes accepts a worse neighbor, which helps it escape a local maximum. The chance of accepting a worse move falls as a control value called the temperature is lowered over time. The name comes from annealing in metallurgy, where metal is heated and then cooled slowly.
  • Beam search keeps only the k best states at each step, where k is the beam width. A wider beam covers more options but uses more memory. A beam width of one behaves much like hill climbing.

Where heuristics come from

  • Distance formulas. On grids and maps, a distance formula estimates the remaining cost. It should match the allowed moves. Manhattan distance adds the horizontal and vertical steps and fits grids where moves go in four directions. Euclidean distance is the straight-line distance. Chebyshev distance is the larger of the horizontal and vertical steps and fits grids where diagonal moves cost the same as straight ones.
  • Relaxed problems. Solve a simpler version of the problem with some rules removed. Removing rules can only make a solution cheaper or leave it the same, so the cost of the simpler version is an admissible heuristic for the original problem.
  • Pattern databases. Store the exact cost of solving a simplified part of the problem ahead of time and use it as the estimate. When the stored patterns do not overlap, these estimates do not overestimate.
  • Learned heuristics. Some systems learn the estimate from data or experience rather than having a person design it. Learned estimates usually come without the guarantee that admissibility gives, so they need careful testing.

A worked example: the 8-puzzle

The 8-puzzle is a sliding tile puzzle with eight numbered tiles on a 3-by-3 board and one blank space. Two heuristics are commonly compared:

  • Misplaced tiles counts the tiles that are not in their goal positions. It is admissible because each misplaced tile needs at least one move.
  • Manhattan distance adds up, for each tile, the horizontal and vertical steps to its goal position. It is admissible because one move shifts one tile by one square.

Every misplaced tile is at least one step from its goal, so the Manhattan total is never smaller than the misplaced-tiles count. That makes Manhattan distance the more informed heuristic of the two, and A* using it usually expands fewer states.

Choosing a method

These are general rules of thumb rather than fixed rules:

  • If you need the shortest path and have enough memory, use A* with an admissible heuristic.
  • If memory is tight, consider IDA*, RBFS or SMA*.
  • If the route does not matter and a good-enough answer is fine, consider hill climbing, simulated annealing or beam search.
  • If speed matters more than finding the best answer, consider greedy best-first search.

The choice of heuristic often matters as much as the choice of algorithm. A well-chosen admissible heuristic makes most of these methods faster, while a poor one can make them slow or misleading.

Heuristics also matter in game-playing programs. A minimax search that stops at a fixed depth relies on an evaluation function to score the positions it reaches, which plays a similar role to a heuristic. Readers interested in that side of the topic can also look at adversarial search in artificial intelligence examples.

The same idea appears in machine learning. The no free lunch rule in machine learning says that, averaged over all possible problems, no single method does better than the others. The best method therefore depends on the problem.

Summary

Heuristic search is less about one best method than about matching the method to the problem. Start with two questions: what do you need from the answer, and how much time and memory do you have? A* suits problems where the best path matters and memory allows it. Local methods suit problems where a good state is enough.

Keep exploring

Guides from other topics

All topics →