Heuristic Mathematics

What is a Heuristic?

A heuristic is an educated guess about the cost to reach the goal from any given node. It is the component that makes A* "informed," effectively acting as a compass guiding the algorithm toward the target rather than blindly exploring in all directions.

Key Properties of Heuristics

Common Heuristics: A Deeper Look

1. Manhattan Distance (L1 Norm)

Formula: h(n) = |x₁ - x₂| + |y₁ - y₂|

Used specifically for grids allowing only 4-directional movement (up, down, left, right). It represents the distance traveled if you could only move along grid axes. Think of walking in a city with a grid of streets.

2. Euclidean Distance (L2 Norm)

Formula: h(n) = √[(x₁ - x₂)² + (y₁ - y₂)²]

This is the "straight-line" distance between two points. It is best suited for environments allowing movement in any direction. However, in a grid, Euclidean distance is often not admissible if the actual movement is restricted to grid steps, because the true cost (grid distance) might be higher than the straight-line distance.

3. Chebyshev Distance

Formula: h(n) = max(|x₁ - x₂|, |y₁ - y₂|)

Used for environments allowing 8-directional movement (including diagonals) where diagonal movement costs the same as cardinal movement (e.g., in a game like Chess, where a King moves diagonally for cost 1).

4. Octile Distance

Formula: h(n) = max(dx, dy) + (√2 - 1) * min(dx, dy)

A more accurate heuristic for 8-directional movement where diagonal movement has a cost of √2 (approx 1.414) while cardinal movement costs 1.

Worked Example: Comparing Heuristics

Suppose you are at (0,0) and the goal is at (3,3). The cost of a cardinal step is 1. The cost of a diagonal step is √2 (≈1.414).

Notice how different heuristics estimate the same distance differently based on allowed movement. Selecting the wrong heuristic for your movement rules will lead to suboptimal paths or inefficient searches.

Tie-Breaking and Optimization

If multiple nodes have the same f(n) value, A* might explore them all unnecessarily. You can force A* to prioritize nodes closer to the straight-line path by slightly increasing the heuristic estimate, effectively breaking ties in favor of a more direct route: h'(n) = h(n) * (1 + p), where p is a very small value (e.g., 0.001). This can significantly reduce the number of explored nodes in open areas.

Choosing the Right Heuristic

Movement Type Best Heuristic
4-directionalManhattan
8-directional (Diagonal cost 1)Chebyshev
8-directional (Diagonal cost 1.414)Octile
Any directionEuclidean