Programming Fundamentals · Search and Decision

Beam Search: Look a Few Steps Ahead Before Choosing

Last lesson left a trap: greedy always grabs the biggest option in front of it, so one misstep can ruin the whole path. The fix is almost cutely simple—don’t lock in yet; keep several candidate paths walking forward together, then compare total scores at the end. That’s Beam Search. The word lattice below puts three strategies on the same stage.

Three paths on the word lattice

The start is fixed as “This spot's”, then 4 steps right, 3 candidate words each step. Click the three buttons in order, and watch three things: greedy (red) grabs probability-0.5 “food” on step one, but the path narrows after; Beam=2 (blue) keeps two paths alive and greys out the pruned ones; Beam=3 even keeps step-one’s 0.2 “decor”—whose cumulative score wins? And how far apart are the compute counters at the bottom right?

Click “Go greedy” first—watch the red line lock in step by step
Path scoreboard
😤 Greedy · Cumulative
Candidates scored: –
🔦 Beam=2 · Cumulative
Candidates scored: –
🔦🔦 Beam=3 · Cumulative
Candidates scored: –
Three cumulative scores lined up: 0.072 < 0.098 < 0.101. Greedy was baited by step-one “food” (0.5) into a path that ran out of steam; Beam=2 kept an extra path so step-one’s 0.3 “value” laughed last; Beam=3 was fiercer still, kept even 0.2 “decor,” and dug up the global best. But watch compute: 12 → 21 → 30 candidates, nearly 3×—beam width is a “trade compute for quality” knob; how far you twist it depends on the power bill you can afford.
Concept card · Don’t put all your eggs in one basket
🧺

The whole idea of Beam Search

At each step you don’t pick one path—you keep the top-k highest-scoring paths (k = beam width) and walk them all forward; at the finish you compare cumulative totals and hand in the winner. When k=1 it collapses to greedy; when k is infinite it’s exhaustive search of every path—Beam Search is the slider between greedy and exhaustive search.

🎛

Width = the compute-for-quality knob

Each +1 on k means a whole extra row of candidates every step. Translation systems often use k=4~10: beyond that, quality gains shrink while the bill rises linearly. The engineering question was never “can we be more optimal?”—it’s “is this bit of optimality worth the compute?” The BFS/DFS fight in the maze lesson is the same choice in different clothes.

AI connection · Where you’ve seen it

🌍 The classic decoder for machine translation and speech recognition. When translating a sentence, picking “The” vs “A” for the first word can sway how fluent the whole line feels—greedy often produces sentences where every word is “right” but the glue feels off. Beam Search keeps several openings alive, then picks the highest whole-sentence probability; it was the default in the neural-MT era. Speech recognition is the same: keep near-homophone candidates first, and let later context split “facts” from “fax.”

🧠 Same intuition as “reasoning models think first, then answer.” The Absolute Beginner Part covered deep-thinking models: before answering they generate a long thinking trace, try a few lines of thought, self-reject, then pick the best reply. That philosophy lines up with Beam Search—explore a few more paths before you “put pen to paper,” trading extra compute for a better final answer. The difference is reasoning models explore in natural language and are far more flexible—but the “compute for quality” ledger is the same one as those three counters on the lattice.

What this lesson wants to share