Machine Learning Overview
Search as Reasoning
Introduction to Search
Search is a form of reasoning used when the world is deterministic and problems require planning, problem-solving, or thinking — not just reflexive mapping from percepts to actions. Examples include solving a Rubik's cube or finding the shortest path from point A to point B.
The Bitter Lesson (Rich Sutton) argues that general methods that leverage computation are ultimately the most effective. Search and learning are identified as the two methods that scale arbitrarily with compute. While 1950s symbolic AI relied solely on search and failed, modern AI combines learned models with search to tackle challenging problems.
Formalizing Search Problems
A search problem is an abstraction that formally defines a problem using the following components:
Start state: The initial state from which search begins.
Successors function: Given a state, returns a list of possible steps. Each step consists of:
Action (a label)
Cost (e.g., time)
Next state (the state reached after taking the action)
Is end function: Given a state, returns whether it is a goal state.
The objective is to find a solution: a sequence of actions that minimizes total cost from the start state to any end state.
Example: Travel Search Problem
A street with blocks numbered 1 through n. From block i, you can:
Walk: to block i+1, cost 1 minute (if i<n)
Take a magic tram: to block 2i, cost 2 minutes (if 2i≤n)
Search problem definition:
Start state: block 1
Successors at state i: walk (cost 1, next i+1) and tram (cost 2, next 2i), with bounds checks
Is end: return true if state i=n
State Design: Incorporating Constraints
State must contain all information needed to evaluate future actions, costs, and successors. Keeping the state small is important for computational efficiency.
Constraint: Limited Tram Tickets
If only a fixed number of tickets are available, the state must include both location and remaining tickets.
Start state:
TravelState(location=1, tickets=initial_tickets)Successors for
TravelState(loc, t):Walk: cost 1, next state
TravelState(loc+1, t)(always allowed)Tram: cost 2, next state
TravelState(2*loc, t-1)(only if t>0)
Is end: check if location equals destination (ignores tickets — multiple end states possible)
Constraint: No Tram Twice in a Row
The state must also record the last action taken (e.g., boolean last_action_was_tram). Then in successors, the tram action is only allowed if the last action was not tram.
Exhaustive Search and Future Cost
The Future Cost Recurrence
Define future cost f(s) as the minimum cost from state s to any end state. The recurrence is:
The solution to the original problem is f(start), along with the sequence of actions that achieves it.
Exhaustive Search Algorithm
Exhaustive search implements the recurrence directly via recursion, trying all possible sequences of actions. It returns a solution (cost + action sequence).
Pseudo-idea:
If state is end, return solution with cost 0 and empty steps.
Otherwise, for each successor, recursively compute the future solution from the next state.
Choose the successor with minimum total cost (cost+future_cost).
Time complexity: Exponential in the number of states (worst case). Memory complexity: Linear in solution length (the recursion stack).
Problem: Cycles
If the search graph contains cycles, exhaustive search may not terminate (infinite recursion). Workaround: incorporate a step counter into the state (so each state is unique) and define a maximum step threshold. Any state exceeding the threshold is given infinite cost.
Dynamic Programming (Memoization)
Dynamic programming (DP) is exhaustive search plus caching. Before computing f(s), check a cache (dictionary) to see if f(s) was already computed. If so, return the cached value. After computing, store it in the cache.
Three changes to exhaustive search:
Add a cache (e.g., dictionary mapping states to solutions).
At the start of the future-solution function, if state is in cache, return cached solution.
After computing the best solution, store it in cache.
Benefits
Time complexity: Reduces exponential to polynomial (usually number of states) — states are never explored more than once.
Memory complexity: Requires storing solutions for all states (linear in number of states).
When to Use DP
Useful when many paths converge to the same state (e.g., lattice structures). If every path is unique (like tree), DP gives no speedup.
Only practical when the number of states fits in memory. For very large state spaces (e.g., 1012), DP is infeasible.
Approximate Search
When exact search is intractable (e.g., state space too large), heuristic methods are used. Two common algorithms are best-of-n and beam search.
Best-of-N
Idea: Generate n random solutions (rollouts) and return the best one.
A policy is a function mapping a state to an action (deterministic or random). A rollout applies the policy from the start state until an end state (or a step limit) is reached.
Policy example: Uniform random – at each state, choose uniformly among available actions.
Rollout: execute policy, accumulate actions and cost.
Best-of-n algorithm:
Perform n rollouts (independent, can be parallelized).
Return the solution with minimum total cost.
Properties:
As n→∞, converges to the optimal solution if the policy has positive probability on all actions (but may require exponentially many rollouts).
Embarrassingly parallel — no communication between workers.
Often used with an informed policy (e.g., a language model) to make it practical.
Beam Search
Idea: Maintain a set of up to k (beam width) partial solutions at each step. At each iteration, extend all partial solutions by one action, then keep only the k best (by total cost).
Beam width k=1 is greedy search.
As k→∞, beam search becomes exhaustive search (bad idea – memory also exponential).
Deterministic (unlike best-of-n's stochastic sampling).
Algorithm outline:
Initialize candidates = [start state (empty solution)].
For step in 1..max_steps:
For each candidate, if it reached an end state, keep it; otherwise, for each successor, create a new candidate by appending the step.
Sort all new candidates by total cost.
Prune to keep only the top k (lowest cost).
Return the best candidate that reached an end state.
Properties:
Provides not only the best approximate solution but also alternatives (top k).
Memory usage is O(k⋅max steps) – controlled by beam width.
Useful when there is no good policy for sampling.
Test-Time Compute in Language Models
Language models produce a probability distribution over next tokens given a prefix. Combining search with learned costs (negative log probabilities) enables finding better responses through computation at test time.
Search problem formulation for language model generation:
State: a string consisting of the prompt plus the generated prefix.
Actions: choose the next token.
Cost: −logP(next token∣current state) — minimizing cost maximizes probability.
Is end: when a termination condition is met (e.g., end-of-sequence token or incomplete expression). Optionally, a verifier assigns a large negative cost (bonus) if the response is correct.
Best-of-N with Language Model Policy
A policy derived from the language model samples tokens according to the predicted distribution.
Rollout: starting from the prompt, repeatedly sample the next token using the policy until stopping condition.
Collect n rollouts; return the one with lowest total cost (highest probability, possibly plus verifier bonus).
This is a simple yet effective method: best-of-n sampling with a small model can compete with a much larger model.
Synergy of Search and Learning
Costs are learned from data (e.g., language model probabilities learned via supervised learning).
Search finds the best sequence given those costs.
Combining learned models with search is a powerful recipe for building robust AI systems.
This lecture covered the formalization of search problems, exact algorithms (exhaustive search, dynamic programming), and approximate algorithms (best-of-n, beam search). The key takeaway is that search remains relevant today, especially when coupled with learning.

Ask anything
We've read this whole study set, so questions about it get the best answers.