Sub topic 9
Why A* Returns Optimal Paths
This chapter assumes you know the A loop from earlier chapters: the heap ordered by f = g + h, the closed set, and path reconstruction. It also assumes you can tell an admissible heuristic from a consistent one. The question here is when A can promise that the first path it returns is the cheapest, and when it can promise to return a path at all.
Completeness: Will A* Find a Path?
A* is complete when both of these conditions hold:
- Every node has finitely many neighbors.
- Every edge costs at least some fixed ε > 0.
With these conditions, only finitely many nodes have f below any given value. A* expands nodes in roughly increasing f, so it reaches the goal after finitely many expansions. Zero-cost cycles break this guarantee, because g never grows around such a cycle. An infinite graph with shrinking edge costs breaks it too.
Optimality: The Core Argument
Suppose h is admissible. Let C be the optimal cost, and suppose A pops the goal with some cost C > C*. Take any optimal path. Before the goal is popped, some node n on that path sits on the open list with its optimal g value. Then:
f(n) = g(n) + h(n) ≤ g(n) + h(n) = C < C
Because n has a smaller f than the goal entry, the heap pops n first. This repeats along the optimal path, so the goal is popped with cost C before the costly entry can come out. The argument depends on testing for the goal when a node is popped*, not when it is generated. A goal test at generation time can return a costly path while a cheaper one is still waiting in the heap.
Where the Closed Set Changes Things
The argument above is enough for tree search. With a closed set, it also needs every node to be closed with its optimal g. Consistency provides that. If h(n) ≤ c(n, n') + h(n') holds for every edge, f never decreases along a path. The first time a node is popped, its g is therefore already optimal, and skipping later entries for that node is safe.
An admissible heuristic that is not consistent gives no such guarantee. The graph below shows the failure.
Here h(B) = 5 is admissible, because the true cost from B to G is 5. It is inconsistent, because c(B, A) + h(A) = 2 < 5.
import heapq
from itertools import count
def astar(start, goal, neighbors, h):
tie = count()
open_heap = [(h(start), next(tie), start)]
g = {start: 0}
parent = {start: None}
closed = set()
while open_heap:
_, _, node = heapq.heappop(open_heap)
if node == goal:
path = []
while node is not None:
path.append(node)
node = parent[node]
return path[::-1], g[goal]
if node in closed:
continue
closed.add(node)
for nxt, cost in neighbors(node):
new_g = g[node] + cost
if new_g < g.get(nxt, float('inf')):
g[nxt] = new_g
parent[nxt] = node
heapq.heappush(open_heap, (new_g + h(nxt), next(tie), nxt))
return None, float('inf')
edges = {'S': [('A', 4), ('B', 1)], 'B': [('A', 1)], 'A': [('G', 4)], 'G': []}
neighbors = lambda n: edges[n]
bad_h = {'S': 0, 'A': 1, 'B': 5, 'G': 0}
print(astar('S', 'G', neighbors, bad_h.get)[1]) # 8
print(astar('S', 'G', neighbors, lambda n: 0)[1]) # 6A* closes A with g = 4 because f(A) = 5 is less than f(B) = 6. It then finds g(A) = 2 through B. The new entry for A is skipped because A is already closed, so G keeps g = 8.
Summary of Guarantees
| Heuristic | Tree search | Graph search with closed set |
|---|---|---|
| Admissible only | Optimal | Optimal only if closed nodes are reopened |
| Consistent | Optimal | Optimal, and each node is expanded at most once |
| Overestimates | No guarantee | No guarantee |
Exercise
In the example graph, change h(B) from 5 to 2 and leave everything else unchanged. Is h now consistent, and what cost does astar return?
Show answer
Yes. Every edge now satisfies h(n) ≤ c(n, n') + h(n'): S→A gives 0 ≤ 5, S→B gives 0 ≤ 3, B→A gives 2 ≤ 2, and A→G gives 1 ≤ 4. B (f = 3) is popped before A (f = 5), so A is first closed with g = 2. The returned cost is 6.
Exercise
Why must A* test for the goal when the node is popped rather than when it is pushed?
Show answer
When the goal is pushed, a cheaper path to it may still be on the open list as a node with smaller f. Only at pop time does the optimality argument guarantee that no remaining entry can lead to a cheaper goal.