Memra

Best-first search and f(n) = g(n) + h(n)

◈ 3 cards

Order the frontier by a heuristic with a priority queue, and read the evaluation function: g is cost-so-far, h is estimated cost-to-go.

Best-first search

Best-first search uses the same open / closed bookkeeping as BFS and DFS, but it orders the open list differently. BFS orders open as a FIFO queue (by level); DFS as a LIFO stack (by recency). Best-first orders open as a priority queue — by a heuristic estimate of closeness to the goal. Each iteration pops the most promising state, regardless of which level it sits on, giving an opportunistic search that jumps to wherever the heuristic points.

Crucially — unlike hill-climbing — best-first keeps every generated state on open. If the heuristic leads it down a dead end, the next-best state is still waiting on the queue, so it can recover.

The evaluation function

The state's priority is its evaluation function

with two parts that pull in different directions:

  • — the actual cost so far: the path length (or summed edge cost) from the start to . It biases the search toward shallower states and stops it from diving forever down one deep branch.
  • — the heuristic estimate of the remaining cost from to the goal. It pulls the search toward states that look close to the goal.

Drop and you are back to greedy hill-climbing-like behaviour (no depth penalty). Drop (set ) and you have uniform-cost / breadth-first search (blind to where the goal is). The balance of the two is the whole game — and it sets up A\* in the next lesson.

Worked example: greedy best-first (order by h alone)

Take a small graph A→{B,C}, B→D, C→F, D→F, with heuristic values . Ordering the open list by only:

  1. Expand A (); push B () and C ().
  2. C looks closer than B, so expand C next; push F ().
  3. F has the best , expand F — goal.

Expansion order A C F, path A → C → F. The heuristic steered the search straight down the C branch and never expanded B or D. Swap the priority key from to and this same machinery becomes A\*.

Ah=5Bh=4Ch=2Dh=3Fh=0Expansion order A C F; B is left sitting on open.
Greedy best-first: the priority key is h(n) alone. A is expanded first and pushes B (h = 4) and C (h = 2); C wins the pop, pushes F (h = 0), and F is popped as the goal. B is generated but never expanded, so D is never even reached — the dashed edges are the part of the space the heuristic steered around.
NORMAL ~/memra/learn/comp-456/best-first-evaluation-function utf-8 LF