A* is the most widely used informed search algorithm. It combines the cost already paid with the estimated cost to go, and is optimal when the heuristic is admissible.

The Evaluation Function

A expands the frontier node with the lowest f(n) = g(n) + h(n), where g(n) is the actual cost from the start to n and h(n) is the estimated cost from n to the goal; thus f(n) is the estimated cost of the cheapest solution through n. The frontier is a priority queue ordered by f; when a node is reached again by a cheaper path, its g and parent are updated. The algorithm stops when a goal node is removed* from the frontier, not merely generated.

Optimality

A\ is complete (with finite branching and positive step costs) and optimal if h(n) is admissible (tree search) or consistent (graph search). The reason: with an admissible h, the f value along any path never overestimates the true cost, so the first goal removed from the frontier has the lowest cost; any better goal would have a smaller f and would have been expanded first. A is also optimally efficient: no other optimal algorithm using the same heuristic is guaranteed to expand fewer nodes. Its weakness is memory: it stores all generated nodes, so the space is O(bᵈ) in the worst case; variants IDA\ (iterative deepening on f) and RBFS* reduce memory.

Definition: A* search. A best-first search that expands the node with the smallest f(n) = g(n) + h(n), where g is the path cost so far and h an admissible heuristic estimate, and returns an optimal path.

Effect of the Heuristic

If h(n) = 0 for all n, A reduces to uniform-cost search; the closer h is to the true cost (while still admissible), the fewer nodes are expanded; a perfect heuristic expands only nodes on the optimal path. A heuristic that dominates another (h₂ ≥ h₁ everywhere) expands no more nodes. Weighted A with f = g + w·h (w > 1) trades optimality for speed.

A* on a Grid

For a grid with 4-direction moves and unit cost, h = Manhattan distance |x − xg| + |y − yg| is admissible and consistent. In games and robotics A* finds shortest paths around obstacles, and it is the algorithm behind many map and navigation systems.

Table 2.4: A compared with other searches*

Property Uniform-cost Greedy A*
Uses g Yes No Yes
Uses h No Yes Yes
Optimal Yes No Yes, with admissible h
Speed Slow Fast Fast with good h

Example 2.4: A* trace

Problem: Use the graph and heuristics of the previous topic (h(S) = 6, h(A) = 5, h(B) = 3, h(C) = 3, h(G) = 0) and find the optimal path from S to G with A*.

Solution:

  1. Frontier {S: f = 0 + 6 = 6}. Expand S: A (g = 1, f = 6), B (g = 4, f = 7).
  2. Expand A (f = 6): B via A has g = 3, f = 6 (better than 7, update); C has g = 6, f = 9.
  3. Expand B (f = 6): C via B has g = 4, f = 7 (better than 9, update).
  4. Expand C (f = 7): G has g = 7, f = 7. Expand G: goal removed.

Result: Optimal path S → A → B → C → G with cost 7, correcting greedy's cost 8.

import heapq
g = {"S": {"A": 1, "B": 4}, "A": {"S": 1, "B": 2, "C": 5},
     "B": {"S": 4, "A": 2, "C": 1}, "C": {"A": 5, "B": 1, "G": 3},
     "G": {"C": 3}}
h = {"S": 6, "A": 5, "B": 3, "C": 3, "G": 0}
best, pq = {"S": 0}, [(6, 0, "S", ["S"])]
while pq:
    f, c, n, path = heapq.heappop(pq)
    if n == "G":
        print("A* path", path, "cost", c)
        break
    for m, w in g[n].items():
        if c + w < best.get(m, 99):
            best[m] = c + w
            heapq.heappush(pq, (c + w + h[m], c + w, m, path + [m]))

Output

A* path ['S', 'A', 'B', 'C', 'G'] cost 7

Common mistakes

  • Stopping when the goal is first generated instead of when it is removed from the frontier.
  • Using an inadmissible heuristic, such as one that overestimates.
  • Forgetting to update g when a cheaper path to a frontier node is found.
  • Ignoring A*'s memory use on large problems.

Key takeaways

  • A* uses f(n) = g(n) + h(n) and is optimal for admissible (tree) or consistent (graph) h.
  • A* reduces to uniform-cost search when h = 0.
  • A more informed (dominating) heuristic expands fewer nodes.
  • Memory is A*'s main limit; IDA* reduces it.

Practice questions

  1. Write the evaluation function of A*.
  2. Under what condition is A* optimal?
  3. Give an admissible heuristic for grid path finding.

Answers to the practice questions

  1. f(n) = g(n) + h(n).
  2. When the heuristic is admissible for tree search, or consistent for graph search.
  3. Manhattan distance to the goal.

Sources

  • Official BCA syllabus Artificial Intelligence, course code 26BCA404DS04, Unit II