Admissible Heuristic
Manhattan Distance
Pathfinding
Search Algorithms
Artificial Intelligence

Admissible Heuristic Manhattan Distance

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Manhattan distance is one of the standard heuristics for A* on grid maps. It is popular because it is cheap to compute and, under the right movement rules, it is admissible, which means it never overestimates the true remaining path cost.

Understanding Manhattan Distance

Manhattan distance is also called taxicab distance because it measures how far apart two points are if you can move only horizontally and vertically, like driving through a city laid out as a grid.

For points (x1, y1) and (x2, y2), the Manhattan distance is:

text
|x1 - x2| + |y1 - y2|

That value counts the minimum number of axis-aligned steps needed to line up the x coordinate and the y coordinate when each move changes only one coordinate by one unit.

For example, going from (1, 1) to (4, 5) requires:

  • 3 horizontal moves
  • 4 vertical moves

so the Manhattan distance is 7.

Admissibility of Manhattan Distance

A heuristic is admissible if it never returns a value larger than the actual cheapest remaining path cost.

Manhattan distance is admissible when the search space has these properties:

  • movement is limited to up, down, left, and right
  • each move has cost 1 or at least a constant lower-bounded cost that matches the heuristic scale
  • there are no special shortcuts such as teleporters or zero-cost moves that undercut the grid distance model

Why is it admissible in that setting. Because every valid path must somehow close the horizontal gap and the vertical gap. Obstacles may force a detour and make the true path longer, but they cannot make the path shorter than the number of required axis steps.

That gives Manhattan distance a very useful property:

  • it is often exact on open grids
  • it stays a lower bound even when walls force longer routes

That is exactly what A* wants from an admissible heuristic.

Implementation Example

Here is a minimal Python implementation of the heuristic:

python
1def manhattan(a, b):
2    return abs(a[0] - b[0]) + abs(a[1] - b[1])
3
4
5start = (1, 1)
6goal = (4, 5)
7print(manhattan(start, goal))

If you plug that into A*, the algorithm evaluates each state with:

text
f(n) = g(n) + h(n)

where:

  • g(n) is the cost already paid to reach node n
  • h(n) is the Manhattan estimate from n to the goal

That makes Manhattan distance a practical default for four-direction tile maps.

When Manhattan Distance Is Not Admissible

This is the part people often skip. Manhattan distance is not universally admissible. It depends on the movement rules.

For example, if diagonal moves are allowed at cost 1, Manhattan distance can overestimate. Going from (0, 0) to (1, 1) has Manhattan distance 2, but the true path cost is only 1 if one diagonal move is legal.

Likewise, if the map contains special mechanics such as:

  • teleporters
  • jump edges
  • zero-cost roads
  • movement costs lower than the heuristic assumes

then Manhattan distance may become too large and lose admissibility.

In those environments, a different heuristic or a teleporter-aware lower bound is needed.

Manhattan Distance and Consistency

In the ordinary four-direction unit-cost grid, Manhattan distance is not only admissible but also consistent. Consistency means the estimated cost does not drop by more than the cost of one action when moving to a neighbor.

That matters because consistent heuristics make A* easier to implement efficiently. In many textbook and production implementations, consistency is what lets the closed-set logic behave cleanly without repeated node reopening.

So in the standard grid setting, Manhattan distance is especially attractive because it gives you:

  • cheap computation
  • admissibility
  • consistency

That combination is why it appears so often in tutorials and games.

Common Pitfalls

The most common mistake is using Manhattan distance on a grid that allows diagonal movement with cheap diagonal cost. In that setting, Chebyshev or octile distance is usually the safer choice.

Another mistake is assuming a heuristic stays admissible when the map adds special mechanics such as teleporters or jump pads. The movement model changed, so the heuristic has to be reconsidered too.

People also sometimes scale Manhattan distance incorrectly. If each step costs 5, then the heuristic should be scaled to that cost model. Otherwise the estimate may become too weak or inconsistent with the search space.

Finally, do not confuse admissible with perfect. Manhattan distance can be admissible and still be much smaller than the true path cost when obstacles create large detours.

Summary

  • Manhattan distance is |x1 - x2| + |y1 - y2|.
  • It is admissible for four-direction grids when movement cost matches the heuristic assumptions.
  • Obstacles do not break admissibility because they can only make the true path longer.
  • It is not automatically admissible when diagonal moves or special shortcuts exist.
  • In the standard unit-cost grid, Manhattan distance is both admissible and consistent, which makes it a strong default A* heuristic.

Course illustration
Course illustration

All Rights Reserved.