A* Algorithm — Beginner-Friendly Notes
1. What A* Is (the most important informed search)
Combine actual cost so far + heuristic estimate to goal :
- = cheapest cost from start to found so far.
- = estimated cost from to goal (must be admissible — next topic).
- = estimated total cost through to goal. A* always expands smallest first.
A* = UCS () + Greedy () — cost-aware and goal-directed.
2. How It Works
Priority queue by . 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 handling.
3. Why Optimal (when h is admissible — see next topic)
If never overestimates true cost , never overestimates total true cost, so first goal popped has optimal = optimal cost. Formal proof in textbook, but intuition: A* won't skip a cheaper path because its would still look cheaper.
4. Properties
| Property | Value (with admissible ) |
|---|---|
| Complete | Yes (finite branching, positive costs) |
| Optimal | Yes if admissible; optimal and optimally efficient (expands fewest nodes among optimal algorithms for same ) |
| Time | worst, but much less with good |
| Space | — still memory heavy like BFS (main drawback) |
5. Heuristic Examples
- 8-puzzle: = misplaced tiles, = Manhattan distance (sum of tile distances) — dominates .
- 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 and is complete and optimal when is admissible, combining cost-so-far with heuristic guidance to find optimal paths efficiently.
8. Quick Self-Check
- What is for a node with , ?
- Why is A* optimally efficient vs UCS?