skip to content

For A* on a game grid, how do you choose between Manhattan, octile and Euclidean heuristics, and how does tie-breaking cut node expansions?

level: middleimportance: must knowfreq 72%

answer

  1. match the estimate to allowed moves
  2. exact cost on an empty map
  3. diagonal costs √2, not 1 or 2
  4. many nodes share one f value
  5. prefer the node nearer the goal

basics

~20 s

Use the heuristic that equals unobstructed travel cost under your movement rules: Manhattan for 4-way grids, octile for 8-way grids with √2 diagonals, Euclidean for any-angle movement. Break equal-f ties toward the node nearer the goal.

solid answer

~50 s

A heuristic should be as large as possible without overestimating, and the best cheap estimate is the exact cost on an empty map under the same movement rules, since obstacles only add cost. On a 4-connected grid that is Manhattan, `dx + dy`; on an 8-connected grid with straight cost 1 and diagonal cost √2 it is octile, `max(dx, dy) + (√2 - 1) * min(dx, dy)`; for any-angle movement on a navmesh it is Euclidean. Euclidean on a grid stays admissible but underestimates, so A* expands more nodes; Manhattan on an 8-way grid overestimates diagonal travel, so it is fast but can return longer paths. On open ground many open nodes share the same `f = g + h`; breaking ties toward larger `g` (smaller `h`) makes A* dive toward the goal instead of flooding the whole equal-`f` band, without changing the cost of the path it returns.

code

pseudocode · 18 lines
pseudocode
// dx, dy: absolute cell differences between a node and the goal
function manhattan(dx, dy):      // 4-connected, straight step costs 1
    return dx + dy

function octile(dx, dy):         // 8-connected, straight 1, diagonal sqrt(2)
    return max(dx, dy) + (sqrt(2) - 1) * min(dx, dy)

function chebyshev(dx, dy):      // 8-connected, diagonal step also costs 1
    return max(dx, dy)

function euclidean(dx, dy):      // any-angle movement, e.g. on a navmesh
    return sqrt(dx * dx + dy * dy)

// open-list order: lower f first; on equal f, larger g first
function comesFirst(a, b):
    if a.f != b.f:
        return a.f < b.f
    return a.g > b.g

go deeper

for a junior

Recall what the heuristic estimates, the remaining cost to the goal, and which distance goes with 4-way, 8-way and any-angle movement.

for a middle

Derive the octile formula from diagonal-then-straight steps, explain under- versus over-estimation, and show how preferring larger g on equal f cuts expansions on open ground.

for a senior

Diagnose a slow or odd-looking search from its symptoms: a flood of equal-f nodes, a heuristic broken by cheap terrain, or Manhattan used on a diagonal grid.

for a principal

Decide where bounded-suboptimal search is acceptable, for example weighted heuristics for distant units, and how the team measures path quality against search cost.

## What the heuristic is for A* orders its open list by `f = g + h`, where `g` is the cost already paid from the start and `h` is an **estimate** of the cost still to go. Two properties matter. An **admissible** heuristic never overestimates the true remaining cost, which keeps the returned path optimal; why that holds, and the stronger consistency property, belong with the general study of shortest-path algorithms. Within the admissible ones, a **larger** estimate is better: the closer `h` is to the true cost, the fewer nodes A* expands. On a game map the best practical choice is therefore the heuristic that is **exact on an empty map** under the game's movement rules, because obstacles can only make the real cost higher. ## Matching the heuristic to the movement model Let `dx` and `dy` be the absolute cell differences between a node and the goal. | Movement rule | Exact empty-map cost | Name | |---|---|---| | 4-connected, straight step costs 1 | `dx + dy` | Manhattan | | 8-connected, diagonal costs √2 | `max(dx, dy) + (√2 - 1) * min(dx, dy)` | Octile | | 8-connected, diagonal costs 1 | `max(dx, dy)` | Chebyshev | | Any angle (navmesh, any-angle search) | `sqrt(dx^2 + dy^2)` | Euclidean | The octile formula follows from the cheapest way across open ground: take `min(dx, dy)` diagonal steps of cost √2, then `max(dx, dy) - min(dx, dy)` straight steps of cost 1. That totals `√2 * min + max - min`, which is `max + (√2 - 1) * min`. For `dx = 3, dy = 1` it gives `3 + 0.414 = 3.414`: one diagonal and two straight steps. ## What goes wrong with a mismatched heuristic - **Euclidean on a grid** is admissible, because no grid path is shorter than the straight line, but it underestimates the octile or Manhattan cost. A* still finds an optimal path, only after expanding more nodes. - **Manhattan on an 8-way grid with √2 diagonals** overestimates: one diagonal step costs √2 but Manhattan charges 2. A* becomes greedier and faster but can return a path that is longer than optimal. - **Squared Euclidean**, chosen to skip a square root, grows with the square of distance while `g` grows linearly. Far from the goal it swamps `g`, A* behaves like greedy best-first search, and paths can be badly non-optimal. - **Terrain costs**: if the cheapest cell costs less than 1 (a road at 0.5), an empty-map heuristic computed with unit costs overestimates wherever a road exists. Scale it by the cheapest step cost to stay admissible, and accept that it becomes looser. ## Weighted heuristics: trading optimality for speed Sometimes speed matters more than the last few percent of path length, for distant units or background traffic. **Weighted A*** uses `f = g + w * h` with `w > 1`. With an admissible `h`, the returned path costs at most `w` times the optimum, so `w = 1.2` caps the detour at 20%, while expansions often fall sharply on open maps. The weight is a tuning value, not a constant: measure expansions and path quality on the game's own maps. ## Tie-breaking On an open grid with an exact heuristic, every node on every optimal path has the same `f`, and so do many nodes beside them. If ties are broken arbitrarily, or in favour of older nodes, A* expands a broad band of equal-`f` nodes before reaching the goal. Common fixes: 1. **Prefer larger `g` on equal `f`.** The node with larger `g` has smaller `h`, so it is nearer the goal, and A* pushes forward along one optimal path. Because only the order among equal-`f` nodes changes, the returned path cost stays optimal. 2. **Scale `h` by `(1 + p)`** with a small `p`, chosen below the minimum step cost divided by the expected longest path. It breaks ties toward the goal at the cost of a very slight overestimate. 3. **Prefer nodes near the straight line** from start to goal, using a cross-product term. It changes which of several equal-cost paths is returned, which mostly makes paths look straighter. ## A checklist for the interview - State the movement model before naming the heuristic. - Derive the octile formula from diagonal-then-straight movement. - Say which mismatches cost speed (underestimate) and which cost optimality (overestimate). - Name tie-breaking toward larger `g` as the cheap win on open maps.

  • What does weighting the heuristic, f = g + w * h with w > 1, buy and cost?
    It makes A* greedier: nodes near the goal are expanded first, which often cuts expansions sharply on open maps. With an admissible heuristic, the returned path costs at most w times the optimum, so w = 1.2 bounds the detour at 20%. It suits distant or low-priority agents whose routes nobody inspects closely.
  • Why is squared Euclidean distance a poor heuristic?
    It grows with the square of distance while g grows linearly, so far from the goal it dominates f and A* behaves like greedy best-first search: few expansions, but paths can be far from optimal. Beyond about one step it exceeds the real open-ground cost, so it is not admissible.
  • How do terrain costs change the heuristic you can use?
    The estimate must assume the cheapest possible terrain all the way, so multiply the empty-map distance by the lowest cell cost. If roads cost 0.5 and grass 1, an unscaled octile overestimates near roads. The scaled version stays admissible but gets looser as the gap between cheapest and typical cost grows, so expansions rise.

  • Euclidean is always the best heuristic because it is the true straight-line distance.
  • Manhattan is safe on an 8-way grid because it never overestimates.
  • Squared Euclidean is a cheaper drop-in for Euclidean.
  • Breaking ties between equal-f nodes can make A* return a costlier path.
  • A larger heuristic weight always gives better paths faster.