Unit 8 of 9 · about 3 hours · derive, measure
Recurrence proofs and differential validation
Samples pass and proofs fail. A differential harness plus a minimized counterexample is what catches a wrong recurrence.
You'll be able to
- Prove a recurrence by induction with the invariant, exchange step or cut property stated, not restated.
- Build a Hypothesis harness that compares memoized, tabulated and brute-force results on small inputs.
- Shrink a failure to a minimal counterexample and map it to the violated claim.
Covers
Read
In this order, only the parts listed
0 of 4 done
- The proof checklist and a model argumentAlgorithms - dynamic programming
Section 3.4 (pp. 105-107), the step checklist that ends "you have to prove that each of these steps is correct"; Section 3.7 Recursive Structure (pp. 111-112) as the model optimal-substructure argument
- First property test with pytestHypothesis - exhaustive and differential DP tests
Write your first test and Combining Hypothesis with pytest
- Strategies for small instancesHypothesis - exhaustive and differential DP tests
Strategies Reference: Numeric (integers) and Collections (lists) for generating small valid instances
- Replaying shrunken failuresHypothesis - exhaustive and differential DP tests
The Hypothesis database; Using
@exampleto run a specific input; Prefer@exampleover the database for correctness
Focus on
- A proof names the invariant and the step: last-column cases for edit distance, shorter-interval reads for OBST, both worlds for tree tuples.
- The harness generates small valid inputs, runs three implementations, and keeps the shrunk failure. Brute force is the oracle, not a fourth opinion.
- Derandomize the settings for the reproduce run so the example count and seed replay exactly.
Common mistakes · 3
- Testing only on statement samples. The parity variant passes both LC 198 examples (
[1,2,3,1]and[2,7,9,3,1]) and fails first on[1,0,0,1]; generate, do not hand-pick. - Comparing two optimized forms with no brute-force oracle. Agreement proves consistency, not correctness; the exhaustive baseline is the check.
- Generating invalid instances such as negative coins or empty grids. Constrain generation to valid inputs or failures indict the generator, not the recurrence.
Try it yourself
Closed-book, before checking anything
@given lists of ints 0-9 with length at most 8, max_examples = 200, derandomize = True. Compare memoized, tabulated and brute-force House Robber on every example: all 200 pass. Then plant the alternating-parity variant: Hypothesis shrinks the failure to [1,0,0,1] with parity 1 versus optimal 2.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.What does the edit-distance contradiction actually establish?Answer
That removing the last column of an optimal alignment leaves an optimal alignment of the prefixes. With the three possible last-column shapes, the three-way min considers every optimum, and each option it considers is a valid alignment.
2.Why three implementations instead of two?Answer
Memoized and tabulated share the recurrence, so a wrong recurrence passes their comparison. Brute force derives from the definition, so it disagrees exactly when the recurrence is wrong.
3.Hypothesis shrinks a failure to
[1,0,0,1]. What do you do?AnswerPin it with
@example([1, 0, 0, 1])so it runs on every test run, then map it to the claim: the alternating pick returns 1, while taking both ends and skipping the middle returns 2, which indicts the parity assumption.4.A proof says by optimal substructure, the recurrence holds. What is missing?Answer
The argument itself: which cases exhaust the optimum, why the tail is optimal for the subproblem, and why no invalid solution is admitted.
You're done when
Your harness passes 200 derandomized examples on the correct code and exhibits a minimized counterexample on the planted bug.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 6 more in the section
- Algorithms - dynamic programmingJeff Erickson · required
Use the dynamic-programming chapter to derive states and recurrences from first principles and to separate DP from greedy reasoning and shortest paths.
- Hypothesis - exhaustive and differential DP testsHypothesis · support
Generate small valid instances, compare memoized, tabulated and exhaustive results, and retain shrunk failures for wrong state, base case, order and compression defects.
Also in this section
- DSA Handbook - dynamic programmingHandbook Academy · primary
Study Part 9 in order from state and recurrence through sequence, grid, tree and bitmask families. For every example, close the page and reconstruct the state, transition and evaluation order.
- AlgoMaster DSA - dynamic programming sliceAlgoMaster · required
Attempt Coin Change, Longest Common Subsequence and House Robber III cold. Write the state, transition, base case and evaluation order first; compare against brute force on small inputs and reconstruct a witness when available.
- NeetCode - unseen mixed problemsNeetCode · optional
Use two tag-hidden mixed problems as the final transfer test. One must require rejecting a tempting greedy or insufficient-state solution with a counterexample.
- Python sys: recursion limitPython Software Foundation · reference
Read
sys.getrecursionlimitandsys.setrecursionlimit: the limit guards the C stack and its safe maximum is platform-dependent. Check the default withgetrecursionlimit(1000 on CPython 3.13), raise it once for memoized DP, or switch to tabulation for deep inputs. - Erickson lecture note D - Advanced Dynamic ProgrammingJeff Erickson · reference
Read Section D.1 (Saving Space: Divide and Conquer) for the Hirschberg Half-table split that recovers an optimal edit sequence in space.
- Erickson lecture note J - Approximation AlgorithmsJeff Erickson · reference
Read Section J.1 (Load Balancing) for the Partition reduction that makes exact scheduling NP-hard, the
GreedyLoadBalanceheuristic, and Theorem J.1 with its factor-2 proof.