Problem Solving by Search (9hrs) · Introduction to AI

A* algorithm

Watch lectures

A* Algorithm — Beginner-Friendly Notes

1. What A* Is (the most important informed search)

Combine actual cost so far g(n)g(n) + heuristic estimate to goal h(n)h(n):

f(n)=g(n)+h(n)f(n) = g(n) + h(n)
  • g(n)g(n) = cheapest cost from start to nn found so far.
  • h(n)h(n) = estimated cost from nn to goal (must be admissible — next topic).
  • f(n)f(n) = estimated total cost through nn to goal. A* always expands smallest f(n)f(n) first.

A* = UCS (gg) + Greedy (hh) — cost-aware and goal-directed.

2. How It Works

Priority queue by f(n)f(n). Like UCS but with heuristic guidance.

frontier = priority queue by f(n) = g(n) + h(n)
push initial with f = h(initial)
while frontier not empty:
  node = pop smallest f(n)
  if goal_test(node): return path  # optimal if h is admissible
  for child:
    g(child) = g(node) + step_cost
    f(child) = g(child) + h(child)
    push/update child in frontier

Needs visited/closed set with best gg handling.

3. Why Optimal (when h is admissible — see next topic)

If h(n)h(n) never overestimates true cost h∗(n)h*(n), f(n)f(n) never overestimates total true cost, so first goal popped has optimal ff = optimal cost. Formal proof in textbook, but intuition: A* won't skip a cheaper path because its ff would still look cheaper.

4. Properties

Property Value (with admissible hh)
Complete Yes (finite branching, positive costs)
Optimal Yes if hh admissible; optimal and optimally efficient (expands fewest nodes among optimal algorithms for same hh)
Time O(bd)O(b^d) worst, but much less with good hh
Space O(bd)O(b^d) — still memory heavy like BFS (main drawback)

5. Heuristic Examples

  • 8-puzzle: h1h1 = misplaced tiles, h2h2 = Manhattan distance (sum of tile distances) — h2h2 dominates h1h1.
  • Map: straight-line distance.
  • Grid pathfinding: Manhattan/Euclidean distance.

6. When to Use

  • When you have a good admissible heuristic and need optimal + fast.
  • Default choice for pathfinding, puzzles, planning.

7. One-Line Summary

A* expands nodes by f(n)=g(n)+h(n)f(n)=g(n)+h(n) and is complete and optimal when hh is admissible, combining cost-so-far with heuristic guidance to find optimal paths efficiently.

8. Quick Self-Check

  1. What is f(n)f(n) for a node with g=5g=5, h=3h=3?
  2. Why is A* optimally efficient vs UCS?
AI LearningWeb Platform

Coursify — Personal AI Course Generator

Struggling with a difficult subject or concept? Turn any engineering topic into a structured course with interactive lessons, quizzes, and research-backed explanations.

← All Introduction to AI modules