Sub topic 12
Tie-Breaking and Its Effect on Performance
This chapter assumes you have already built the heap-based A from the implementation chapter and know what admissible and consistent heuristics are. It shows why the order in which A pops nodes with equal f-values can change the number of expanded nodes many times over, while the returned path cost stays the same.
Why Ties Are Common
On grid maps with unit step costs, f-values come in a small set of integers, so many nodes share the same one. On an empty 4-connected grid with Manhattan distance, every cell inside the rectangle between start and goal has the same f, equal to the true path cost. All of those cells are candidates for expansion. A* must pick among them somehow, and the heap's secondary key makes that choice.
The implementation chapter used a counter as the secondary key. The counter stops Python from comparing nodes directly, and it also makes ties first-in, first-out. Among equal f-values, A* then expands the oldest nodes first. Those nodes have the lowest g, so the search spreads out much like breadth-first search across the whole tied region.
Preferring Deeper Nodes
A better rule breaks ties in favour of larger g, which is the same as smaller h. Among equal-f nodes, the one with the largest g is closest to the goal. Expanding it first pushes the search along one optimal path instead of across all of them.
The program below runs both rules on an empty 20×20 grid and counts the expanded nodes.
import heapq
from itertools import count
INF = float('inf')
def astar_count(start, goal, neighbors, h, prefer_deep):
tie = count()
def entry(g_val, node):
secondary = -g_val if prefer_deep else 0
return (g_val + h(node), secondary, next(tie), node)
open_heap = [entry(0, start)]
g = {start: 0}
closed = set()
while open_heap:
node = heapq.heappop(open_heap)[-1]
if node == goal:
return g[goal], len(closed)
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, INF):
g[nxt] = new_g
heapq.heappush(open_heap, entry(new_g, nxt))
return INF, len(closed)
SIZE = 20
def open_grid(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 < SIZE and 0 <= nc < SIZE:
yield (nr, nc), 1
goal = (SIZE - 1, SIZE - 1)
h = lambda n: abs(n[0] - goal[0]) + abs(n[1] - goal[1])
print(*astar_count((0, 0), goal, open_grid, h, prefer_deep=False)) # 38 399
print(*astar_count((0, 0), goal, open_grid, h, prefer_deep=True)) # 38 38Both runs return cost 38. The FIFO version expands 399 of the 400 cells. The deep-first version expands only the 38 cells on one path.
Optimality Is Unaffected
With a consistent heuristic, A returns an optimal path under any tie-breaking rule, because every node popped at the minimum f already has its optimal g. Tie-breaking only decides how many equal-f nodes are expanded before the goal. It cannot make A return a worse path.
Common Errors
| Mistake | Effect |
|---|---|
| Heap entries of `(f, node)` with no counter | `TypeError` when nodes cannot be compared, or an arbitrary order based on node values |
| Breaking ties toward smaller g | Breadth-like spread across the tied region |
| Scaling h to break ties | Loses the strict optimality guarantee |
| Expecting gains on maps full of obstacles | Fewer ties, so the improvement is smaller |
Exercise
On an open grid, two open nodes have f = 30. Node A has g = 10 and node B has g = 25. Which node should the heap pop first under the "prefer deeper" rule, and could choosing the other one change the cost of the returned path if h is consistent?
Show answer
Pop B first, because it has the larger g and the smaller h, so it is closer to the goal. Choosing A instead could increase the number of expanded nodes, but it cannot change the returned path cost, because A* with a consistent heuristic is optimal under any tie-breaking rule.
Exercise
Why does the heap tuple keep the counter even after adding -g as the secondary key?
Show answer
Different nodes can share both f and g. Without the counter, Python would compare the node objects themselves, which raises a TypeError for types that cannot be ordered and otherwise gives an order based on node values instead of a predictable rule.