Search/Informed Search and A*

Lesson 2.31,945 words

Informed Search and A*

An informed search uses a heuristic h(n)h(n), an estimate of the cost from a node to the goal, to decide what to expand next. Greedy best-first search follows the heuristic blindly and gives up optimality; A* corrects it by ranking nodes on f(n)=g(n)+h(n)f(n) = g(n) + h(n), and is optimal when the heuristic is admissible (tree search) or consistent (graph search).

╌╌╌╌

An uninformed search knows only the problem definition: the start state, the actions, the goal test, and the step costs. It cannot tell a promising node from a hopeless one, so it expands the frontier by a fixed rule — shallowest first, cheapest-path first — and pays for that ignorance in nodes expanded. An informed search brings in problem-specific knowledge beyond the definition itself, and uses it to expand fewer nodes on the way to a solution.1

The knowledge arrives as a single number attached to each node.

For route-finding across Romania, the natural heuristic is the straight-line distance from a city to the destination: it is easy to compute, it correlates with actual road distance, and it can never exceed it because a straight line is the shortest path between two points. Notice what the definition does not require — cannot be read off the problem statement (you need a map with coordinates), and knowing that it is useful takes a little experience. That is the whole point: the heuristic is the channel through which extra knowledge enters the search.

This lesson develops the two informed strategies and proves the guarantees of the central one, A*. It leaves open the practical question of where good heuristics come from — relaxed problems, pattern databases, dominance — which is the subject of the companion lesson, Heuristic Functions and Memory-Bounded Search.

Both strategies in this lesson are instances of one scheme, best-first search: keep the frontier in a priority queue and always expand the node with the lowest value of an evaluation function , which we read as a cost estimate — small means promising. Underneath, this reuses the priority-queue graph search that uniform-cost search uses, with substituted for to order the queue. Everything below comes down to one choice: what is ?

Best-first search orders the frontier by an evaluation function . Greedy search sets ; A* sets ; uniform-cost search is the case .

The most direct use of a heuristic is to trust it completely. Greedy best-first search expands the node that looks closest to the goal, ranking the frontier by the heuristic alone:

On the Romania map with and Bucharest as the goal, greedy search starting from Arad expands Sibiu first (closest of Arad's neighbours to Bucharest), then Fagaras (closest of Sibiu's), and Fagaras generates Bucharest. It reaches the goal having expanded only nodes on a single downhill run toward it — the search cost is minimal.2

But the path it returns, Arad–Sibiu–Fagaras–Bucharest, is km longer than the route through Rimnicu Vilcea and Pitesti. Greedy search is not optimal. The name is the diagnosis: at each step it grabs the node that gets closest to the goal right now, with no accounting for the cost already accumulated on the path. A short first hop toward the goal that commits you to a long detour later is invisible to a function that ignores .

Greedy search chases the smallest and takes Arad-Sibiu-Fagaras (h drops 366, 253, 176, 0), a route 32 km longer than the optimal Arad-Sibiu-Rimnicu-Pitesti path it never considers.

Greedy tree search is also incomplete, in the same way depth-first search is. Getting from Iasi to Fagaras, the heuristic points first at Neamt (closest to Fagaras), which is a dead end; the true first move is to Vaslui, a step that is farther from the goal by the heuristic. A greedy tree search that keeps re-expanding Neamt (returned to the frontier each time Iasi is reached) loops forever. The graph-search version, which refuses to re-expand a state, is complete in finite spaces but not in infinite ones. Worst-case time and space for the tree version are with the maximum depth, though a good heuristic cuts this sharply in practice.

A* search fixes greedy search's blind spot by adding back the cost already paid. It evaluates a node by combining , the cost to reach , with , the estimated cost to get from to the goal:

Since is the actual path cost from the start to and estimates the remaining cost, estimates the cost of the cheapest solution that passes through . Ranking the frontier by therefore expands, at each step, the node that lies on the currently most promising complete path. This is more than a sensible heuristic: provided meets a mild condition, A* is both complete and optimal. The algorithm is identical to uniform-cost search except that it orders the queue by rather than alone — and uniform-cost search is just the special case , in which A* has no heuristic guidance and reduces to Dijkstra's shortest-path expansion.3

Algorithm:A*-Search\textsc{A*-Search} — best-first search on f(n)=g(n)+h(n)f(n) = g(n) + h(n)
  1. 1
    input: a problem with start state s0s_0, goal test, step costs cc, heuristic hh
  2. 2
    n0n_0 \gets node for s0s_0 with g(n0)=0g(n_0) = 0
  3. 3
    frontier \gets priority queue ordered by f(n)=g(n)+h(n)f(n) = g(n) + h(n), containing n0n_0
  4. 4
    explored \gets empty set
  5. 5
    loop do
  6. 6
    if frontier is empty then return failure
  7. 7
    nn \gets pop the node with lowest ff from frontier
  8. 8
    if nn passes the goal test then return the path to nn
  9. 9
    add state of nn to explored
  10. 10
    for each action aa available at nn do
  11. 11
    nn' \gets child of nn via aa, with g(n)g(n)+c(n,a,n)g(n') \gets g(n) + c(n, a, n')
  12. 12
    if state of nn' is not in explored and not in frontier then
  13. 13
    add nn' to frontier
  14. 14
    else if nn' reaches a frontier state at higher gg then
  15. 15
    replace that frontier node with nn'

The condition A* needs for tree search is that be admissible.

Admissible heuristics are optimistic by nature: they always think the goal is at least as close as it really is. Because is the exact cost along the current path and , an admissible makes a lower bound on the true cost of any solution through . Straight-line distance is admissible for exactly this reason — no road is shorter than the straight line, so can never overshoot.

Here is the argument that admissibility gives optimality for tree search. Suppose a suboptimal goal node , with cost , sits on the frontier alongside a node that lies on an optimal path to the true goal , with optimal cost . Then

using admissibility for the first inequality. So has strictly smaller than , and A* expands before it would ever select . Every node on the optimal path is expanded ahead of any suboptimal goal, so the first goal A* removes from the frontier must be an optimal one.

A* tree search for Bucharest with . Every node carries . Bucharest first appears on the frontier at (via Fagaras), but Pitesti's is smaller, so A* expands Pitesti first and finds the cheaper route -- it will not settle for the 450 solution.

A traced A* expansion

The figure names the -values; the trace shows how the priority queue produces them. Take the Bucharest search from Arad with , whose values (in km, straight-line to Bucharest) are , , , , , , , , and . Each row selects the lowest- frontier node and lists what it generates with , , and .

StepSelect ()Generates: state, Frontier by
Arad ()Sibiu ; Timisoara ; Zerind Sibiu , Timisoara , Zerind
Sibiu ()Rimnicu ; Fagaras ; Oradea Rimnicu , Fagaras , Timisoara , Zerind , Oradea
Rimnicu ()Pitesti ; Craiova Fagaras , Pitesti , Timisoara , …
Fagaras ()Bucharest Pitesti , Timisoara , Zerind , Bucharest , …
Pitesti ()Bucharest Bucharest , Timisoara , Zerind , …
Bucharest ()— goal selected— return

The decisive rows are and . In step a Bucharest node appears at (reached via Fagaras), but A* does not stop: Pitesti at is smaller and sits ahead of it in the queue. Expanding Pitesti in step generates a second Bucharest node at , cheaper than the first, and it jumps to the front. Only in step , when Bucharest at is selected, does A* return — Arad-Sibiu-Rimnicu-Pitesti-Bucharest, cost . The suboptimal goal sat on the frontier the whole time and was never selected, exactly as the admissibility argument promised: , so the node on the optimal path is expanded first. Note also Timisoara at and Zerind at : both have , so A* never expands either — that is pruning in action.

For graph search, where we discard repeated states, admissibility alone is not quite enough; A* needs the slightly stronger property of consistency (also called monotonicity).

This is a form of the triangle inequality: the side from to the goal cannot exceed the sum of the other two sides, and . Every consistent heuristic is admissible (take a whole path of steps and the inequalities telescope to ), though the converse can fail; still, essentially every admissible heuristic one meets in practice is also consistent, included.

Consistency buys the key structural fact: is nondecreasing along any path. If is a successor of , then , so

with the inequality from consistency. So the sequence of nodes A* expands has nondecreasing . Combined with the graph-search rule that the first time we expand a state we have already found the cheapest path to it, this makes the first goal node A* selects an optimal solution: goal nodes have , so their equals their true cost, and any later goal is at least as expensive.

The contour picture

Nondecreasing lets us picture A* geometrically. Because never drops along a path, we can draw contours in the state space, exactly like the level curves of a topographic map: the region forms one band, a larger band containing it, and so on. A* expands the frontier in concentric bands of increasing -cost, fanning outward from the start.

A* fans out in contours of increasing -cost from the start (Arad). With a perfect heuristic the contours stretch into a narrow corridor along the optimal path to the goal (B); with (uniform-cost search) they are circular. A* expands every node with and no node with .

Let be the cost of the optimal solution. The contour picture makes two facts immediate:

  • A* expands every node with .
  • A* expands no node with ; it may expand some nodes right on the goal contour before selecting a goal.

The second fact is pruning: A* safely ignores whole subtrees whose exceeds (Timisoara, a child of Arad in the Bucharest search, is never expanded) without any risk to optimality. With uniform-cost search the bands are circular around the start; a more accurate heuristic stretches them toward the goal and narrows them onto the optimal path — that narrowing is precisely the work the heuristic does. Among all algorithms of this kind that use the same heuristic, A* is optimally efficient: no such algorithm is guaranteed to expand fewer nodes, because any algorithm that skips a node with risks missing the optimal solution.

The catch is memory. Like every graph search, A* keeps all generated nodes, and for hard problems the number of nodes on the goal contour is still exponential in the solution length. A* usually exhausts memory long before it runs out of time.

A* is only as good as its heuristic — the narrower those contours, the less work it does — which raises the question its optimality proof left open: where does a good come from, and what do you do when even A*'s memory bill is too high? This continues in Heuristic Functions and Memory-Bounded Search.

Footnotes

  1. Russell & Norvig, Artificial Intelligence: A Modern Approach (3rd ed.), §3.5 — Informed (Heuristic) Search Strategies: best-first search as an instance of the general tree/graph search that orders the frontier by an evaluation function , with the heuristic as the channel for problem-specific knowledge.
  2. Russell & Norvig, §3.5.1 — Greedy best-first search: ; efficient but neither optimal (the 32-km-longer Bucharest route) nor complete for tree search (the Iasi–Fagaras loop).
  3. Russell & Norvig, §3.5.2 — A* search: ; optimality via admissibility (tree search) and consistency/monotonicity with nondecreasing and the contour argument (graph search); optimal efficiency and the memory drawback.

╌╌ END ╌╌