3.5.13Graphs

A - algorithm — heuristic, admissibility, consistency — important for GNC

2,102 words10 min readdifficulty · medium

WHY does A* exist?


WHAT are the pieces?


HOW does A* relate to Dijkstra and Greedy? (the 80/20 core)

Algorithm Priority key Behaviour
Dijkstra f=gf=g exact, slow, blind
Greedy best-first f=hf=h fast, not optimal
A* f=g+hf=g+h optimal (if hh admissible) + fast
Figure — A -  algorithm — heuristic, admissibility, consistency — important for GNC

DERIVATION 1 — Why admissibility ⇒ optimal path

Step 1. Let CC^* be the true optimal cost. Suppose A* is about to pop a goal node G2G_2 whose path cost is g(G2)>Cg(G_2) > C^* (a suboptimal goal). We show this is impossible. Why this step? If we prove A* can never pop a worse goal first, the first popped goal must be optimal.

Step 2. Because the true optimal path exists, somewhere on the open list (frontier) there is a node nn lying on an optimal path. Why? Any path from start to goal that hasn't been fully expanded has at least one "boundary" node sitting on the frontier.

Step 3. For that nn: f(n)=g(n)+h(n)g(n)+h(n)=C.f(n)=g(n)+h(n)\le g(n)+h^*(n)=C^*. Why this step? h(n)h(n)h(n)\le h^*(n) is admissibility, and since nn is on an optimal path, g(n)+h(n)g(n)+h^*(n) equals the optimal total cost CC^*.

Step 4. But G2G_2 is a goal so h(G2)=0h(G_2)=0, hence f(G2)=g(G2)>Cf(G_2)=g(G_2)>C^*. Combining: f(n)C<f(G2)f(n)\le C^* < f(G_2).

Step 5. A* pops the smallest ff first, so nn would be popped before G2G_2. Contradiction. ∎ Why this matters: the very first goal A* removes from the queue is guaranteed optimal — admissibility is exactly the condition that buys this guarantee.


DERIVATION 2 — Why consistency ⇒ admissibility (and never re-expand)

Step A: consistent ⇒ admissible. Take any path n=v0v1vk=goaln=v_0\to v_1\to\dots\to v_k=\text{goal}. Apply consistency along it: h(vi)c(vi,vi+1)+h(vi+1).h(v_i)\le c(v_i,v_{i+1})+h(v_{i+1}). Sum from i=0i=0 to k1k-1 (telescoping the hh terms), with h(vk)=0h(v_k)=0: h(n)i=0k1c(vi,vi+1)=cost of that path.h(n)\le \sum_{i=0}^{k-1} c(v_i,v_{i+1}) = \text{cost of that path}. Why this step? It holds for every path, in particular the cheapest one, so h(n)h(n)h(n)\le h^*(n). Done — consistency is stronger than admissibility.

Step B: ff is non-decreasing along expansions. For an edge uvu\to v: f(v)=g(v)+h(v)=g(u)+c(u,v)+h(v)g(u)+h(u)=f(u),f(v)=g(v)+h(v)=g(u)+c(u,v)+h(v)\ge g(u)+h(u)=f(u), using consistency c(u,v)+h(v)h(u)c(u,v)+h(v)\ge h(u). Why this matters: ff never decreases as we go deeper. So when a node is popped its gg is already final → A never needs to re-open a closed node*. With a merely-admissible (but inconsistent) heuristic you may have to re-expand, which is slower.


Worked Example 1 — grid with Manhattan distance

Grid, 4-directional moves cost 1 each. Heuristic h=h= Manhattan distance =x1x2+y1y2=|x_1-x_2|+|y_1-y_2|.

  • Why admissible? Each move changes Manhattan distance by at most 1 and costs 1, so the straight count never overestimates the true number of moves.
  • Why consistent? For neighbours c(u,v)=1c(u,v)=1 and h(u)h(v)1=c(u,v)|h(u)-h(v)|\le 1=c(u,v), so h(u)c(u,v)+h(v)h(u)\le c(u,v)+h(v). ✔

Start (0,0)(0,0), goal (2,2)(2,2):

  • h(0,0)=4h(0,0)=4, f=0+4=4f=0+4=4.
  • Moving to (1,0)(1,0): g=1g=1, h=3h=3, f=4f=4. Same ff — A* keeps moving goalward, never expanding (1,0)(-1,0) whose f=2+5=7f=2+5=7. Why this step? The away-from-goal node has higher ff, so A* simply ignores it — that's the speedup over Dijkstra.

Worked Example 2 — inadmissible heuristic breaks optimality

Nodes SAGS\to A\to G: c(S,A)=1,c(A,G)=1c(S,A)=1,\,c(A,G)=1 (total 2). Also SGS\to G direct =2.5=2.5. Set a bad h(A)=5h(A)=5 (overestimate; true is 1).

  • f(via A at A)=1+5=6f(\text{via }A \text{ at }A)=1+5=6. f(G direct, on open)=2.5+0=2.5f(G\text{ direct, on open})=2.5+0=2.5.
  • A* pops the direct GG first → returns cost 2.52.5, missing the true optimum 2. Why this matters: overestimating made the good path look expensive. Steel-man fix: clamp/lower hh so hhh\le h^*.

Worked Example 3 — Euclidean heuristic for a drone (GNC)

Continuous space, moves along grid but the true robot can fly diagonally; cost = Euclidean length. Use h=h= straight-line (Euclidean) distance.

  • Straight line is the shortest possible path ⇒ hhh\le h^* ⇒ admissible.
  • It also satisfies the triangle inequality ⇒ consistent. Why GNC cares: admissible Euclidean hh guarantees the planner returns the genuinely shortest flyable route, while exploring a narrow cone toward the target — crucial when compute is limited on-board.


Recall Feynman: explain to a 12-year-old

Imagine you're in a maze looking for the exit. Dijkstra explores in every direction equally — slow. A* is smarter: at each spot you also peek and estimate "how far is the exit roughly from here?" (like looking at a straight-line distance). You always go investigate the spot that seems to give the shortest total trip = steps already walked plus the peek-guess. The one rule: your peek must never overpromise — never say "exit is super close" when it isn't. If your guess is always a little cautious (never too rosy), A* is guaranteed to find the truly shortest way out, just much faster.


Flashcards

What is f(n) in A*?
f(n)=g(n)+h(n)f(n)=g(n)+h(n) — cost-so-far plus estimated cost-to-go.
Definition of admissible heuristic
0h(n)h(n)0\le h(n)\le h^*(n); never overestimates true remaining cost.
Definition of consistent heuristic
h(u)c(u,v)+h(v)h(u)\le c(u,v)+h(v) for every edge, with h(goal)=0h(\text{goal})=0 (triangle inequality).
Does consistency imply admissibility?
Yes — telescoping consistency along any path gives h(n)h(n)h(n)\le h^*(n). (One-way; admissible need not be consistent.)
Which property guarantees A* returns an optimal path?
Admissibility.
Which property guarantees A* never re-expands a closed node?
Consistency (it makes ff non-decreasing along any path).
A* with h(n)=0 reduces to what?
Dijkstra's algorithm.
A* using only f=h (ignoring g) is what?
Greedy best-first search — fast but not optimal.
Why can an overestimating heuristic break optimality?
It makes the true cheapest path look expensive, so A* may pop a worse goal first.
What is Weighted A*?
f=g+whf=g+w\cdot h with w>1w>1: faster, bounded-suboptimal (≤ w × optimal), generally inadmissible.
Why is Euclidean distance admissible in continuous space?
A straight line is the shortest possible path, so it never overestimates.

Connections

  • Dijkstra's Algorithm — A* with h=0h=0.
  • Greedy Best-First Search — A* with gg dropped.
  • Heuristic Functions — Manhattan, Euclidean, Chebyshev.
  • Priority Queue / Min-Heap — data structure ordering by ff.
  • Triangle Inequality — the geometric root of consistency.
  • Weighted A* and Bounded Suboptimality
  • GNC Path Planning — rovers, drones, spacecraft.
  • Bellman Optimality — relation g(v)=g(u)+c(u,v)g(v)=g(u)+c(u,v).

Concept Map

add heuristic

estimates cost to goal

start to n

expand smallest f

never overestimates

guarantees when h admissible

implies

only h gives

fast but not

used by

Dijkstra blind ripples

Heuristic h n

g n cost so far

f n = g + h

A* algorithm

Admissible h ≤ h*

Consistent triangle ineq

Optimal shortest path

Greedy best-first f=h

GNC path planning

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, A* basically Dijkstra ka smart version hai. Dijkstra har direction me equally search karta hai — slow, kyunki usko pata hi nahi goal kahan hai. A* me hum ek heuristic h(n)h(n) add karte hain jo guess karta hai "yahan se goal kitni door hai". Phir har node ka score banta hai f(n)=g(n)+h(n)f(n)=g(n)+h(n), jahan gg = ab tak chala raasta ka cost aur hh = aage ka guess. A* hamesha sabse chhote ff wala node pehle expand karta hai — isliye search goal ki taraf "pull" hoti hai aur bahut kam nodes explore hote hain.

Sabse important rule: heuristic admissible honi chahiye, matlab h(n)h(n)h(n)\le h^*(n) — kabhi over-estimate mat karo, thoda optimistic raho. Agar tum jhooth bol do ki "goal bahut paas hai" jabki nahi hai, to A* galat (non-optimal) raasta de sakta hai. Ye humne Example 2 me dekha. Consistent heuristic ek step aage hai: h(u)c(u,v)+h(v)h(u)\le c(u,v)+h(v) (triangle inequality) — isse ff kabhi neeche nahi girta path ke along, to A* kisi closed node ko dobara expand nahi karta, aur fast rehta hai. Yaad rakho: consistency se admissibility apne aap aa jaati hai (C → A), par ulta zaroori nahi.

GNC (Guidance, Navigation, Control — drones, rovers, spacecraft) me ye crucial hai kyunki on-board compute limited hota hai aur shortest, safe path real-time me chahiye. Euclidean (straight-line) distance ek mast heuristic hai: straight line hamesha shortest hoti hai, isliye automatically admissible aur consistent — guaranteed optimal route, kam exploration ke saath. Agar speed zyada chahiye aur thoda suboptimal chalega, to Weighted A* (f=g+wh, w>1f=g+w\,h,\ w>1) use karte hain.

Go deeper — visual, from zero

Test yourself — Graphs

Connections