Best-first search

On this page 7

Best-first search is a family of graph search algorithms that always expands the most promising frontier node next, where promise is measured by an evaluation function f(n). In artificial intelligence, and specifically in the field of Symbolic AI & reasoning, it is the general template behind greedy best-first search, A* search and many planning and puzzle solvers that explore an explicit state space.

The idea separates what to explore from how to explore. The search procedure stays the same, while the evaluation function encodes domain knowledge, usually through a heuristic h(n) that estimates the remaining cost to a goal. Choosing that function determines whether the algorithm is fast but unreliable or slower but provably optimal.

How best-first search expands the frontier

Best-first search works by keeping a priority queue of frontier nodes ordered by f(n) and repeatedly removing the node with the lowest evaluation value. When that node is a goal, the search stops; otherwise its successors are generated, scored and inserted into the queue. A separate closed set records states that have already been expanded, which prevents the algorithm from repeating work in graphs with cycles.

The loop is the same as in uninformed search, except for the ordering rule. Breadth-first search orders by depth and uniform-cost search orders by path cost g(n), so both can be described as special cases of the best-first template with a particular choice of f(n).

The table below lists the components that every implementation must define.

ComponentRoleTypical implementation
FrontierHolds generated but unexpanded nodesBinary heap or bucket queue
Evaluation functionRanks frontier nodesf(n) = h(n) or f(n) = g(n) + h(n)
Closed setRecords expanded statesHash table keyed by state
Successor functionGenerates neighbor states and step costsOperators of the problem model
Goal testDecides when to stopPredicate on a state
Core components of a best-first search

Variants defined by the evaluation function

The behavior of best-first search depends almost entirely on the choice of f(n). Greedy best-first search uses f(n) = h(n) and rushes toward states that look close to the goal, while A* search uses f(n) = g(n) + h(n) and balances the cost already paid against the estimated cost remaining.

VariantEvaluation functionCompleteOptimal
Uniform-cost searchg(n)Yes, with positive step costsYes
Greedy best-first searchh(n)Only in finite spaces with a closed setNo
A* searchg(n) + h(n)Yes, with positive step costsYes, with an admissible heuristic
Weighted A*g(n) + w times h(n), w > 1Yes, with positive step costsWithin a factor of w
Recursive best-first searchg(n) + h(n), linear memoryYesYes, with an admissible heuristic
Common evaluation functions and their guarantees

A heuristic is admissible when it never overestimates the true remaining cost, and consistent when its estimate drops by no more than the step cost along any edge. Consistency guarantees that A* never needs to reopen a closed node, which simplifies implementation and keeps the number of expansions predictable.

Best-first search compared with other search strategies

Best-first search sits between uninformed search, which ignores domain knowledge, and local search, which keeps only one or a few states. Its strength is the use of problem-specific guidance; its main cost is the memory needed to store the frontier.

StrategyUses a heuristicMemory useTypical role
Breadth-first searchNoExponential in depthShortest paths in unit-cost graphs
Depth-first searchNoLinear in depthExhaustive enumeration with low memory
Best-first searchYesExponential in the worst caseInformed path finding and planning
Hill climbingYesConstantLocal optimization without backtracking
Beam searchYesBounded by beam widthApproximate search with a memory limit
Best-first search against neighboring strategies

In classical planning, best-first search is the standard engine of heuristic planners. Systems that read a PDDL or STRIPS model compute domain-independent heuristics, such as relaxed-plan estimates, and then run greedy or weighted variants over the resulting state space. In adversarial search, by contrast, minimax and alpha–beta pruning dominate because the opponent’s choices change the evaluation.

Designing heuristics and choosing a variant

The quality of the heuristic matters more than any implementation detail. A more informed admissible heuristic expands fewer nodes, and a common way to build one is to solve a relaxed problem exactly, for example by ignoring walls on a grid map or ignoring delete effects in a planning task.

Practitioners usually pick a variant by weighing solution quality against search time and memory, as the decision table shows.

SituationRecommended choiceReason
Optimal cost is mandatoryA* with an admissible heuristicGuarantees the cheapest solution
Any solution is acceptable quicklyGreedy best-first searchFewest expansions in many domains
Near-optimal within a known boundWeighted A*Trades bounded quality for speed
Memory is severely limitedIDA* or recursive best-first searchLinear memory at the cost of re-expansions
Time budget is fixedAnytime weighted A*Returns improving solutions until stopped
Choosing a best-first variant

Tie-breaking also deserves attention. When many nodes share the same f-value, preferring the node with the higher g(n), which is the one deeper in the search, often reaches the goal sooner without affecting optimality.

Limitations and common mistakes

The main practical limitation is memory consumption, because the frontier and closed set can grow exponentially with solution depth. Large planning or puzzle problems often exhaust memory long before they exhaust time, which is why memory-bounded variants exist.

  • Inadmissible heuristics in A* silently remove the optimality guarantee.
  • Greedy search without a closed set can loop forever in graphs with cycles.
  • Plateaus in heuristic values make greedy search behave like blind search.
  • Inconsistent heuristics force nodes to be reopened and can increase expansions sharply.
  • Expensive heuristics may cost more per node than they save in expansions.

A frequent misconception is that best-first search always finds the best solution. The name refers to expanding the best-looking node, not to the quality of the result; only specific variants, such as A* with an admissible heuristic, carry an optimality guarantee.

In everyday language

Best-first search is a way to look for a route through many choices. At every step, it picks the option that looks most promising. It keeps a list of places it could try next. It always opens the one with the best score.

Think of finding a café in a new city. You walk toward the street that seems closest to it. If that street is a dead end, you go back to the next best option on your list. A good guess saves time. A bad guess sends you the wrong way. The method is only as smart as the score it uses.

Seen in the real world

A warehouse operator runs a fleet of autonomous mobile robots on a grid map of roughly 200 by 150 cells. Each robot must reach a picking station while avoiding shelves and blocked aisles. The routing software models the floor as a graph and runs A* with Manhattan distance as the heuristic, because robots move only along aisles and the estimate never exceeds the real distance.

Uniform-cost search on the same map expanded thousands of cells per request, which was too slow when 60 robots replanned at once. A* typically expanded only a small fraction of that number and still returned the shortest available route. During peak hours, the team switched to weighted A* with a weight of 1.5, accepting routes that are at most 50 percent longer in exchange for faster replanning, and observed that most returned routes were in fact optimal or nearly so.