Sub topic 7
Admissible Heuristics: Never Overestimate
This chapter assumes you know the A* loop from the previous chapters: a heap ordered by f(n) = g(n) + h(n), a closed set, and path reconstruction. You should also be able to read Python and know how Dijkstra's algorithm computes shortest distances.
The definition
Let h(n) be the true cost of the cheapest path from node n to the goal. A heuristic h is admissible* if, for every node n:
- h(n) ≤ h*(n), and
- h(n) ≥ 0, which forces h(goal) = 0.
An admissible heuristic is optimistic. It may underestimate the remaining cost by any amount, but it never overestimates it. When h is admissible, A* returns an optimal path. A later chapter proves this.
What goes wrong when h overestimates
A* stops as soon as it pops the goal from the heap. If h inflates the estimate at a node on the best path, that node waits in the heap behind a worse route to the goal.
After A expands S, the heap holds A with f = 1 + 5 = 6 and G with f = 4 + 0 = 4. G comes out first, and A returns a path of cost 4 even though a path of cost 2 exists.
Admissible does not mean useful
The zero heuristic, h(n) = 0, is always admissible. With it, A becomes uniform-cost search and explores in every direction. The goal is a heuristic that stays below h while getting as close to it as possible. A tighter heuristic expands fewer nodes. The chapter on dominance makes this precise.
Checking admissibility empirically
You usually prove admissibility by argument. On a small map you can also test it directly. Compute h* for every node by running Dijkstra from the goal, then compare it with h. The code below runs Dijkstra outward from the goal, which is valid because the grid's edge costs are the same in both directions.
import heapq
import math
def true_costs(goal, neighbors):
dist = {goal: 0}
heap = [(0, goal)]
while heap:
d, node = heapq.heappop(heap)
if d > dist[node]:
continue
for nxt, cost in neighbors(node):
nd = d + cost
if nd < dist.get(nxt, float('inf')):
dist[nxt] = nd
heapq.heappush(heap, (nd, nxt))
return dist
def eight_neighbors(rows, cols):
def neighbors(cell):
r, c = cell
for dr in (-1, 0, 1):
for dc in (-1, 0, 1):
if (dr, dc) == (0, 0):
continue
nr, nc = r + dr, c + dc
if 0 <= nr < rows and 0 <= nc < cols:
yield (nr, nc), math.sqrt(2) if dr and dc else 1
return neighbors
def overestimates(h, goal, neighbors):
hstar = true_costs(goal, neighbors)
return sorted((n, h(n), c) for n, c in hstar.items() if h(n) > c + 1e-9)
goal = (0, 0)
nb = eight_neighbors(3, 3)
def manhattan(n):
return abs(n[0] - goal[0]) + abs(n[1] - goal[1])
def octile(n):
dx, dy = abs(n[0] - goal[0]), abs(n[1] - goal[1])
return (dx + dy) + (math.sqrt(2) - 2) * min(dx, dy)
bad = overestimates(manhattan, goal, nb)
print(len(bad)) # 4
print(bad[0]) # ((1, 1), 2, 1.4142135623730951)
print(len(overestimates(octile, goal, nb))) # 0Manhattan distance is admissible on a 4-connected grid. On an 8-connected grid it is not, because one diagonal step of cost √2 covers a Manhattan distance of 2. The octile distance accounts for diagonal moves and passes the check.
Common mistakes
| Mistake | Why it breaks admissibility |
|---|---|
| Manhattan distance with diagonal moves | A diagonal step costs less than the two orthogonal steps Manhattan counts |
| Unscaled distance when edge costs are below 1 | Example: road costs in hours while h measures kilometres |
| Adding a "penalty" for terrain to h | The true path may avoid that terrain |
| Nonzero h at the goal | h(goal) must be 0, so any positive value overestimates |
Exercise
On a 4-connected grid where each step costs 1, is h(n) = 2 × Manhattan(n, goal) admissible? What happens to A*'s result?
Show answer
No. On an open grid h equals the Manhattan distance, so doubling it overestimates at every node except the goal. A may return a suboptimal path. This is weighted A* with w = 2, which bounds the returned cost at twice the optimum.
Exercise
Movement is 4-connected, but every step costs 0.5. Is plain Manhattan distance admissible? If not, fix it.
Show answer
No. The true cost is at most 0.5 × Manhattan, so plain Manhattan overestimates by a factor of 2. Use h(n) = 0.5 × Manhattan(n, goal), which scales the distance by the minimum step cost.