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
- Admissible: A heuristic is admissible if
h(n) ≤ h*(n), whereh*(n)is the true minimum cost to reach the goal from node n. Essentially, it never overestimates the true cost. This is the crucial requirement to guarantee that A* finds the optimal (shortest) path. - Consistent (Monotonic): A heuristic is consistent if
h(n) ≤ cost(n, n') + h(n')for every node n and its successor n', withh(goal) = 0. This ensures that the first time A* reaches a node, it has found the shortest path to it, allowing us to avoid revisiting nodes and dramatically improving performance. - Efficiency: Since the heuristic is calculated for every explored node, it must be extremely fast to compute, ideally
O(1).
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).
- Manhattan: |0-3| + |0-3| = 3 + 3 = 6
- Euclidean: √[(0-3)² + (0-3)²] = √[9 + 9] = √18 ≈ 4.24
- Octile: max(3,3) + (1.414-1) * min(3,3) = 3 + (0.414 * 3) = 3 + 1.242 = 4.242
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-directional | Manhattan |
| 8-directional (Diagonal cost 1) | Chebyshev |
| 8-directional (Diagonal cost 1.414) | Octile |
| Any direction | Euclidean |