Heuristic Functions

GATE Relevance: High

Prerequisites

🎯 Learning Objectives

  • Define what a heuristic function is and its role in informed search
  • Understand the h(n) notation and the requirement that h(goal) = 0
  • Derive heuristics from relaxed problems
  • Compute Manhattan, Euclidean, and Hamming distances for 8-puzzle
  • Explain dominance between heuristics and why it matters

1. What Is a Heuristic Function?

Heuristic Function h(n)

A heuristic function h(n) is an estimate of the cost of the cheapest path from the state at node n to a goal state. It encodes domain-specific knowledge that guides the search algorithm toward promising regions of the search space.

The word “heuristic” comes from the Greek heuriskein, meaning “to find” or “to discover.” Think of a heuristic as an educated guess about how far you are from the goal.

Key Properties

  • h(n) ≥ 0 for all nodes n (non-negative)
  • h(goal) = 0 — at a goal state the estimated remaining cost is zero
  • h(n) should be easy to compute (otherwise it defeats the purpose of speeding up the search)
  • A better heuristic means fewer nodes expanded during search
Uninformed vs. Informed

Uninformed search (BFS, DFS, UCS) uses no domain knowledge beyond the problem definition. Informed search uses a heuristic h(n) to estimate distance to the goal, enabling the algorithm to prioritize more promising nodes and dramatically reduce the search space.

2. Using Domain Knowledge to Build Heuristics

The power of an informed search algorithm lies entirely in the quality of its heuristic. A good heuristic captures the structure of the problem. There are several systematic ways to derive heuristics:

2.1 Relaxed Problems

A relaxed problem is obtained by removing one or more constraints from the original problem. The exact cost of solving the relaxed problem is an admissible heuristic for the original problem (it never overestimates, because the relaxed problem is strictly easier).

8-Puzzle Example

Original rule: A tile can move from square A to square B if A is adjacent to B and B is blank.

  • Relaxation 1: A tile can move from A to B if A is adjacent to B. (Drop the “B is blank” constraint.) → This gives the Manhattan distance heuristic.
  • Relaxation 2: A tile can move from A to B. (Drop both constraints.) → This gives the Misplaced tiles (Hamming distance) heuristic.

2.2 Common Heuristic Functions for the 8-Puzzle

h1: Hamming Distance (Misplaced Tiles)

Count the number of tiles that are not in their goal position. Each misplaced tile contributes exactly 1 to the heuristic, regardless of how far away it is.

h2: Manhattan Distance

For each tile, compute the number of moves (horizontal + vertical) needed to reach its goal position. Sum over all tiles. Manhattan distance = ∑ |xcurrent − xgoal| + |ycurrent − ygoal| for each tile.

Euclidean Distance

For each tile, compute the straight-line distance to its goal position: √((xc−xg)² + (yc−yg)²). Sum over all tiles. This is admissible but generally less informative than Manhattan distance for grid problems since tiles move in discrete steps.

3. Dominance

Dominance

Heuristic h2 dominates h1 if h2(n) ≥ h1(n) for every node n, and both are admissible. A dominating heuristic is always better (or at least as good) for search because it expands fewer (or equal) nodes.

For the 8-puzzle, Manhattan distance (h2) dominates Hamming distance (h1) because every misplaced tile must move at least 1 step, so h2(n) ≥ h1(n). Experimental data confirms this:

Search Cost (avg nodes) d = 12 d = 24
IDS (no heuristic)3,644,035~54 billion
A* with h1 (Hamming)22739,135
A* with h2 (Manhattan)731,641
Combining Heuristics

If you have multiple admissible heuristics h1, h2, …, hk, you can always use: h(n) = max(h1(n), h2(n), …, hk(n)). This composite heuristic is still admissible and dominates each individual heuristic.

4. Worked Examples

Example 1: Computing Heuristics for an 8-Puzzle State

Consider the following 8-puzzle state and goal:

Current State:        Goal State:
 7 | 2 | 4            1 | 2 | 3
-----------           -----------
 5 |   | 6            4 | 5 | 6
-----------           -----------
 8 | 3 | 1            7 | 8 |  

h1 (Hamming/Misplaced tiles):

  • Tile 7: at (0,0), goal (2,0) — misplaced ✓
  • Tile 2: at (0,1), goal (0,1) — correct ✘
  • Tile 4: at (0,2), goal (1,0) — misplaced ✓
  • Tile 5: at (1,0), goal (1,1) — misplaced ✓
  • Tile 6: at (1,2), goal (1,2) — correct ✘
  • Tile 8: at (2,0), goal (2,1) — misplaced ✓
  • Tile 3: at (2,1), goal (0,2) — misplaced ✓
  • Tile 1: at (2,2), goal (0,0) — misplaced ✓

h1 = 6 (6 tiles are misplaced)

h2 (Manhattan distance):

  • Tile 7: |0−2| + |0−0| = 2
  • Tile 2: |0−0| + |1−1| = 0
  • Tile 4: |0−1| + |2−0| = 3
  • Tile 5: |1−1| + |0−1| = 1
  • Tile 6: |1−1| + |2−2| = 0
  • Tile 8: |2−2| + |0−1| = 1
  • Tile 3: |2−0| + |1−2| = 3
  • Tile 1: |2−0| + |2−0| = 4

h2 = 2+0+3+1+0+1+3+4 = 14

Note: h2 = 14 ≥ h1 = 6, confirming Manhattan dominates Hamming.

Example 2: Straight-Line Distance Heuristic (Romania Map)

For the classic route-finding problem (Arad to Bucharest on the Romania map), the straight-line distance (SLD) to Bucharest is commonly used as h(n).

City            h_SLD (km to Bucharest)
Arad            366
Zerind          374
Sibiu           253
Timisoara       329
Fagaras         176
Rimnicu Vilcea  193
Pitesti         100
Bucharest       0

This heuristic is admissible because the straight-line distance never overestimates the actual road distance (you cannot drive shorter than a straight line).

Why it works: SLD captures the geographic structure of the problem. Cities closer to Bucharest “as the crow flies” are generally closer by road too, so expanding them first leads the algorithm toward the goal efficiently.

Example 3: Verifying Dominance

Consider two heuristics for a grid pathfinding problem (4-connected grid, unit cost per step):

  • ha(n) = max(|xn−xg|, |yn−yg|) — Chebyshev distance
  • hb(n) = |xn−xg| + |yn−yg| — Manhattan distance

For a node at (1,1) with goal at (4,3):

  • ha = max(|1−4|, |1−3|) = max(3, 2) = 3
  • hb = |1−4| + |1−3| = 3 + 2 = 5

In general, |Δx| + |Δy| ≥ max(|Δx|, |Δy|) always holds. So hb dominates ha on a 4-connected grid. A* with hb will expand fewer or equal nodes.

Important: On an 8-connected grid (diagonal moves allowed at cost 1), the Chebyshev distance ha is the exact cost and thus the better heuristic. Context matters!

5. GATE Practice Points

⚠ Common GATE Mistakes
  • Forgetting that h(goal) must equal 0
  • Including the blank tile when computing Hamming/Manhattan for 8-puzzle (the blank is not a tile)
  • Confusing “admissible” with “consistent” — they are related but different (see next section)
  • Thinking a larger heuristic value is always better — it must also be admissible (never overestimate)

⚡ Quick Facts for GATE

  • Heuristic functions enable informed search to outperform uninformed search by orders of magnitude
  • Relaxed problems always yield admissible heuristics
  • Manhattan distance dominates misplaced tiles for the sliding puzzle
  • max(h1, h2) is admissible if both h1 and h2 are admissible
  • h(n) = 0 is the trivially admissible heuristic — it makes A* behave like UCS

Summary

  • A heuristic h(n) estimates the remaining cost to reach the goal from node n.
  • h(goal) = 0 and h(n) ≥ 0 for all n.
  • Heuristics are derived from domain knowledge, commonly via relaxed problems.
  • Manhattan distance, Hamming distance, and Euclidean distance are standard examples for grid/puzzle problems.
  • Dominance: h2 dominates h1 if h2(n) ≥ h1(n) ∀ n and both are admissible.
  • max(h1, …, hk) yields the strongest combined admissible heuristic.

References

  • Russell, S. & Norvig, P. Artificial Intelligence: A Modern Approach, 4th Edition — Chapter 3.6
  • Pearl, J. Heuristics: Intelligent Search Strategies for Computer Problem Solving