PythiloLesson 5 of 8 · progress kept in this browser

Beam Search

Greedy search discards every alternative immediately. Keep a bounded set of complete prefixes, sum log probabilities and prune globally after each step.

import math
greedy_path = math.log(0.6)+math.log(0.5)
alternative = math.log(0.4)+math.log(1.0)
print(alternative > greedy_path)
Your task

beam_search(transitions, start, steps, width) returns the highest log-score token path (including start) after exactly steps expansions. transitions maps a token to (next_token, probability) pairs; all expanded tokens have successors and probabilities > 0. Keep width paths; ties use lexicographic path order.