Sub topic 11
Comparing Heuristics: Dominance and Nodes Expanded
This chapter assumes you already know how A* ranks nodes by f(n) = g(n) + h(n), what makes a heuristic admissible and consistent, and the standard grid heuristics from the previous chapter. The question here is practical. If two heuristics are both admissible, which one should you use?
What dominance means
Heuristic h2 dominates h1 if both are admissible and h2(n) ≥ h1(n) for every node n. A heuristic that dominates another is better informed. It sits closer to the true remaining cost h*(n) and never goes above it.
Dominance matters because of how A decides what to expand. With a consistent heuristic, A expands every node with f(n) < C, where C is the optimal path cost. That condition is the same as h(n) < C − g(n). A larger h makes the condition harder to meet, so fewer nodes qualify. Every node that A expands under h2 (ignoring ties at f = C*) is also expanded under h1.
On a 4-connected grid, Manhattan distance is never smaller than Euclidean distance, and both are admissible. Manhattan dominates Euclidean, and both dominate h = 0.
Measuring it: nodes expanded
The simplest way to compare heuristics is to count expansions on the same problem. The program below is self-contained. It runs A* three times on a grid with a wall.
import heapq
import math
from itertools import count
def astar_count(start, goal, neighbors, h):
tie = count()
open_heap = [(h(start), next(tie), start)]
g = {start: 0}
closed = set()
while open_heap:
_, _, node = heapq.heappop(open_heap)
if node in closed:
continue
if node == goal:
return g[goal], len(closed)
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
heapq.heappush(open_heap, (new_g + h(nxt), next(tie), nxt))
return float('inf'), len(closed)
ROWS, COLS = 20, 20
walls = {(r, 10) for r in range(0, 15)}
def neighbors(cell):
r, c = cell
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nr, nc = r + dr, c + dc
if 0 <= nr < ROWS and 0 <= nc < COLS and (nr, nc) not in walls:
yield (nr, nc), 1
start, goal = (0, 0), (0, 19)
heuristics = {
"zero": lambda n: 0,
"euclidean": lambda n: math.hypot(n[0] - goal[0], n[1] - goal[1]),
"manhattan": lambda n: abs(n[0] - goal[0]) + abs(n[1] - goal[1]),
}
for name, h in heuristics.items():
cost, expanded = astar_count(start, goal, neighbors, h)
print(f"{name:10s} cost={cost} expanded={expanded}")All three runs report the same optimal cost. The expansion counts fall from zero to Euclidean to Manhattan. The exact numbers depend on tie-breaking, which a later chapter covers.
Effective branching factor
Raw counts depend on the problem size. To compare across problems, use the effective branching factor b\. If A expands N nodes and finds a solution at depth d, b\* is the branching factor a uniform tree of depth d would need to contain N + 1 nodes:
N + 1 = 1 + b\ + (b\)² + … + (b\*)^d
def effective_branching_factor(n_expanded, depth, tol=1e-6):
lo, hi = 1.0, float(max(n_expanded, 2))
while hi - lo > tol:
mid = (lo + hi) / 2
total = sum(mid ** i for i in range(depth + 1))
if total < n_expanded + 1:
lo = mid
else:
hi = mid
return (lo + hi) / 2
print(round(effective_branching_factor(52, 5), 2)) # about 1.92A good heuristic pushes b\ toward 1. A b\ that stays fairly constant across problem sizes tells you the heuristic scales well.
Exercise
On a 4-connected grid with unit step costs, does Chebyshev distance max(|dx|, |dy|) dominate Manhattan distance, or the other way around? Which should you use?
Show answer
Manhattan dominates Chebyshev, because |dx| + |dy| ≥ max(|dx|, |dy|) and both are admissible for 4-connected moves. Use Manhattan. It expands no more nodes than Chebyshev, apart from tie effects.
Exercise
A* expands 52 nodes and finds a solution at depth 5. Another heuristic solves the same problem with 20 expansions. Which has the lower effective branching factor, and what does that tell you?
Show answer
The 20-expansion heuristic has the lower b (about 1.55 against about 1.92). For a fixed depth, fewer expansions always means a lower b. It shows the heuristic steers the search more directly toward the goal.