Best-first search and f(n) = g(n) + h(n)
◈ 3 cardsOrder 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:
- Expand A (); push B () and C ().
- C looks closer than B, so expand C next; push F ().
- 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\*.