Best First Search

The pure heuristic search — follow the gradient downhill, no cost awareness. The precursor to A* and the foil to Dijkstra

The Simplest Explanation

Best First Search = always expand the node that looks closest to the goal.

You have a heuristic h(n)h(n) that guesses distance to goal. At each step, pick the unvisited node with smallest hh. That's it. No gg (cost so far), no ff. Just hh.

Remember This

Best First = sort Open by hh alone. It's Dijkstra's mirror: Dijkstra looks only backward (gg), Best First looks only forward (hh). A* does both.

The Algorithm (from Lecture 31)

Same skeleton as DFS/BFS, one change: Open is a priority queue ordered by hh.

  1. Open = [(S, nil, h(S))] sorted by hh; Closed = []
  2. While Open not empty:
    • Pick lowest-hh node NN from Open
    • If GoalTest(NN) → return path
    • Move NN to Closed
    • For each neighbor MM of NN:
      • Compute h(M)h(M)
      • If MM not on Open/Closed → insert sorted by hh

Node representation upgrades from pairs to triples (node, parent, h).

Heuristics for Route Finding (Lecture 31)

Manhattan distance (grid): |x1-x2| + |y1-y2| steps. Integer, counts grid moves. Euclidean distance: sqrt((dx)^2 + (dy)^2) straight line. Real-valued.

On a map where edge cost is constant (e.g., 5 per move), Manhattan is admissible if it never exceeds true shortest-path cost. In exams, a tile is 10×10 and edge cost is 5 — Manhattan with unit 10 can overestimate when diagonal shortcuts exist via edge costs smaller than grid steps. The 2023 papers ask exactly this.

Comparison Table

PropertyBest FirstDijkstra (gg)A* (f=g+hf=g+h)BFS (hops)
Orders Open byhhggg+hg+hdepth
Looks forward?YesNoYesNo
Looks backward?NoYesYesNo
Guarantees shortest?NoYes (if c≥0c≥0)Yes (if h≤h∗h≤h^*)Only if uniform cost
SpaceO(bd)O(b^d)O(bd)O(b^d)O(bd)O(b^d)O(bd)O(b^d)

Why Not Optimal?

Best First chases the heuristic downhill and may find a non-optimal path that looked close:

Best First picks A (h=10 < 30), finds path cost 55. The optimal is via B, cost 10. It ignored gg.

Exam Trace: 23T2 AN Pattern

Map with 10×10 grid, S(0,20), A(20,0), B(20,20), C(40,0), D(10,40), E(30,40), F(30,60), G(50,40). Alphabetical tie-break.

AlgorithmPath FoundReasoning
DFSS,A,C,GGoes deep via alphabetical order
Best FirstS,D,F,GPicks smallest hh at each step (D closest to G by Manhattan)
A*S,B,E,GBalances g+hg+h
B&BS,A,C,GCheapest partial path (like Dijkstra with pruning)

Best First finds S-D-F-G because h(D)h(D) is smallest from S, then h(F)h(F) from D, etc. — purely heuristic-driven.

Hill Climbing vs Best First (Lecture 32)

Hill ClimbingBest First
MemoryO(1)O(1) — only current node + neighborsO(bd)O(b^d) — full Open
Backtracking?No — if stuck, terminates (local optimum)Yes — via Open
Complete?NoYes (finite space)
When stuckLocal max, even if global far awayEventually explores whole space
QuizBest First Search orders Open by...
QuizWhy does Best First NOT guarantee optimality?