Lecture Notes — Lecture 20: Brute Force Workshop

Companion to the lecture plan and the worked examples. Read the Definitions before class, the Explanation after it; the practice questions and the lecture quiz close the loop.

Definitions

Explanation

Count before you enumerate. 10^4 PINs is seconds; 2^30 subsets is years. The workshop's rule: arithmetic first, code second. The cost curve is plotted from real timings.

Brute force is the correctness oracle. For every clever algorithm in Blocks III-IV, a brute-force version checks it on small inputs. 'Slow but surely right' is an asset, not an embarrassment.

Prune early, pray less. Applying constraints during generation (skip a PIN whose partial sum already exceeds 12) cuts whole subtrees. The difference between generate-then-filter and generate-with-constraints is measured live.

Structure the space. Enumerating triples as nested loops is fine when the depth is small and fixed; recursion (lecture 22) generalises it. Today's workshop stays nested.

Worst case vs typical case. Subset-sum brute force is 2^n worst case but often finds an answer early; average-case thinking is introduced here and formalised much later.

Examples

The lecture's worked examples live in examples.md; the case studies this lecture builds on are worked in class from the projector. The case sequence with difficulties:

Common misconceptions

Summary and key takeaways

  1. Count the space with arithmetic before writing any enumeration.
  2. Brute force is the correctness oracle for everything fancier.
  3. Prune during generation, not after.
  4. 2^n is the wall; the next blocks teach the doors around it.

Practice questions

Where this leads

Next lecture: Binary Search Everywhere. The quiz below checks this lecture's essentials before we build on them.