7 routes42 sections324 units

Unit 5.8 · about 3 h

126 / 324

Recurrence proofs and differential validation

Samples pass and proofs fail. A differential harness plus a minimized counterexample is what catches a wrong recurrence.

  • derive
  • measure

You can now

  • 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

  • Inductive recurrence proofs and loop invariants
  • Exhaustive small-instance and differential validation

Study, in this order

4 steps

  1. The proof checklist and a model argument
    Algorithms - 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

  2. First property test with pytest
    Hypothesis - exhaustive and differential DP tests

    Write your first test and Combining Hypothesis with pytest

  3. Strategies for small instances
    Hypothesis - exhaustive and differential DP tests

    Strategies Reference: Numeric (integers) and Collections (lists) for generating small valid instances

  4. Replaying shrunken failures
    Hypothesis - exhaustive and differential DP tests

    The Hypothesis database; Using @example to run a specific input; Prefer @example over 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.

Reproduce closed-book

In Python 3.13 with Hypothesis 6.169 and pytest: @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.

Traps

  • 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.

Practice

The section mini-project covers this: a Hypothesis differential harness over memoized, tabulated and brute-force forms with retained shrunk failures and one repaired recurrence.

Self-test

  1. What does the edit-distance contradiction actually establish?

  2. Why three implementations instead of two?

  3. Hypothesis shrinks a failure to [1,0,0,1]. What do you do?

  4. A proof says by optimal substructure, the recurrence holds. What is missing?

Completion criterion

Stop when

Your harness passes 200 derandomized examples on the correct code and exhibits a minimized counterexample on the planted bug.

Mark passed, then open 5.9 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

  • Algorithms - dynamic programming
    Jeff Ericksonrequiredtheory
    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 tests
    Hypothesissupportpractice
    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 programming
    Handbook Academyprimarycourse
    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 slice
    AlgoMasterrequiredpractice
    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 problems
    NeetCodeoptionalassessment
    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 limit
    Python Software Foundationreferencereference
    Read sys.getrecursionlimit and sys.setrecursionlimit: the limit guards the C stack and its safe maximum is platform-dependent. Check the default with getrecursionlimit (1000 on CPython 3.13), raise it once for memoized DP, or switch to tabulation for deep inputs.
  • Erickson lecture note D - Advanced Dynamic Programming
    Jeff Ericksonreferencetheory
    Read Section D.1 (Saving Space: Divide and Conquer) for the Hirschberg Half-table split that recovers an optimal edit sequence in \(O(m + n)\) space.
  • Erickson lecture note J - Approximation Algorithms
    Jeff Ericksonreferencetheory
    Read Section J.1 (Load Balancing) for the Partition reduction that makes exact scheduling NP-hard, the GreedyLoadBalance heuristic, and Theorem J.1 with its factor-2 proof.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels