You're offline — cached pages and worlds still work
Drishti Innovations logo
Drishti Innovations

A* Pathfinding & Heuristics

Comprehensive notes, formulas, and practice questions for A* Pathfinding & Heuristics.

What you'll learn

  • What graph search is and why it matters for AI
  • The A* evaluation function f(n) = g(n) + h(n)
  • What makes a heuristic admissible
  • How A* uses open and closed sets
  • Manhattan vs Euclidean heuristics and when to use each
  • How A* compares to Dijkstra's algorithm and BFS

Key concepts

Graph Search

Many AI problems — navigation, puzzle solving, route planning — can be modelled as finding a path through a graph of nodes (states) and edges (transitions).

Goal: Find the optimal (lowest-cost) path from a start node to a goal node.

The A* Evaluation Function

A* assigns a score to every node it considers:

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

TermMeaning
f(n)Total estimated cost of the cheapest path through node n
g(n)Actual cost from the start node to node n (known)
h(n)Heuristic estimate of cost from n to the goal (estimated)

A* always expands the node with the lowest f(n) next.

Admissible Heuristic

A heuristic h(n) is admissible if it never overestimates the true cost to the goal.

h(n) ≤ h(n)* for all n, where h*(n) is the true optimal cost.

If h is admissible, A* is guaranteed to find the optimal path.

  • Admissible: straight-line distance (never longer than the actual road distance)
  • Not admissible: an estimate that sometimes exceeds the true cost

Open and Closed Sets

SetContents
Open setNodes discovered but not yet fully explored (priority queue ordered by f)
Closed setNodes already expanded (no need to revisit)

A Algorithm:*

  1. Add start node to the open set with f = h(start).
  2. Remove the node with lowest f from the open set.
  3. If it is the goal → return the path.
  4. Otherwise: add it to the closed set; expand its neighbours.
  5. For each neighbour: calculate g, h, f; if not in closed set, add/update in open set.
  6. Repeat from step 2.

Manhattan vs Euclidean Heuristics

HeuristicFormulaGrid type
Manhattanh =x₁−x₂
Euclideanh = √[(x₁−x₂)² + (y₁−y₂)²]Any direction (diagonal movement allowed)

Manhattan distance is preferred for grid maps where diagonal movement is not allowed (e.g., many game maps, tile-based worlds). It is always admissible in this setting.

Euclidean distance is admissible when diagonal movement is permitted.

Comparison with Dijkstra and BFS

AlgorithmHeuristic?Finds optimal path?Speed
BFSNo (uniform cost)Yes (for unweighted graphs)Slow (explores in all directions equally)
DijkstraNoYes (for weighted graphs)Moderate (explores by cost, no goal direction)
A*Yes (h guides search)Yes (if h is admissible)Fast (focuses search toward goal)

Key insight: Dijkstra is A* with h(n) = 0. A* with a perfect heuristic (h = h*) expands the minimum number of nodes possible.

Example: Grid Pathfinding

S . . . .
. # # . .
. . . # .
. . . . G

S = Start, G = Goal, # = Wall

  • BFS: explores many wrong directions before finding G.
  • Dijkstra: finds optimal path but explores widely.
  • A* (Manhattan): focuses search toward G efficiently, visiting far fewer nodes.

Quick check

  1. Write the A* evaluation function and explain what each term (f, g, h) represents. Why does A* expand the lowest-f node first?

  2. A heuristic h(n) for a road navigation problem estimates travel time as "straight-line distance ÷ speed limit." Is this admissible? Justify your answer.

  3. On a grid where only up/down/left/right moves are allowed, why is Manhattan distance preferred over Euclidean distance as a heuristic?

  4. How does Dijkstra's algorithm differ from A*? Under what condition does A* reduce to Dijkstra's algorithm?

  5. An A* search with an inadmissible heuristic finds a path quickly but the path is not optimal. Explain why an overestimating heuristic causes this problem.

Key Takeaways (TL;DR)

  • What you'll learn
  • Key concepts
  • Quick check

Master this topic with Drishti OS

Get unlimited mock tests, AI-powered mentorship, and complete video courses when you join.

Start Free Practice