Decomposition, Greedy Methods & Randomness

Hard problems often become manageable by exposing recursive state, proving a local choice, or accepting a controlled probability. Each shortcut needs a proof boundary and an adversarial test.

Section 4 of 5 · 6 units · about 25 hours

0/6

By the end you can Prove or refute one unseen greedy rule before coding, implement a pruned search with a sound bound, and empirically verify one randomized guarantee with fixed seeds.

Units

Build it

Mini project · about 8 to 12 hours

A proved greedy, a pruned search and a sampler check

Prove one greedy rule and refute a tempting alternative before coding. Implement a backtracking search whose prunes are each sound, and verify a weighted sampler with a statistical test over seeds you choose.

  1. 1State the LC 1029 Two City Scheduling rule, sorting by the difference between the two costs and sending the first half to one city, and prove it with an exchange argument before coding.
  2. 2Refute the alternative that sends each person to their cheaper city until one city fills, searching over all splits with brute force. Keep the minimized counterexample and name the information the refuted order ignores.
  3. 3Solve LC 698 Partition to Equal Sum Subsets by backtracking over buckets, stating each prune as a claim: order, early failure and skipping an equivalent bucket. Prove the skip sound by symmetry.
  4. 4Differential-test the pruned search against the unpruned one on small random inputs, recording how each prune changes the node count. Then drop the bucket backtracking for a first-fit variant and let the test find a counterexample.
  5. 5Solve LC 528 Random Pick with Weight with prefix sums and a binary search. Check the distribution with a chi-square test against the weights, keeping the check as a pytest over seeds you choose.
  6. 6Plant the off-by-one in the index mapping and show the test catches it, with counts shifting onto an index that should never appear. Keep the pytest passing on the correct sampler and failing on the planted one.
What you hand in
  • The LC 1029 exchange proof with the refuted rule and its counterexample.
  • The LC 698 pruned search with soundness claims, node counts and the caught unsound variant.
  • The LC 528 sampler with its chi-square test and the planted off-by-one.
How to check it
How it's graded

Greedy proof

Pass: The exchange argument is written before code, LC 1029 is accepted, and the refuted rule has a minimized counterexample.

Great: The proof handles ties, and the write-up explains what information the refuted order ignores.

Pruned search

Pass: Every prune has a one-line soundness claim, and the pruned and unpruned searches agree on random inputs.

Great: Node counts per prune are recorded, and the differential test catches the first-fit variant.

Randomized check

Pass: The chi-square test states its seeds, trials and decision rule, and the planted off-by-one fails it.

Great: The pytest passes across the chosen seeds on the correct sampler and fails on the planted one for every seed.

Resources