7 routes42 sections324 units

Unit 5.3 · about 3 h

121 / 324

Solution reconstruction and rolling state

An optimum without a witness cannot be checked. Compression that deletes the decisions trades checkability for memory.

  • implement

You can now

  • Reconstruct an optimal witness by walking parent pointers back from the answer cell.
  • Decide whether rolling compression preserves dependencies and reconstruction, and state what is lost.
  • Implement length-only rolling rows and full-table recovery without mixing the two.

Covers

  • Solution reconstruction and independently checkable outputs
  • Rolling state and dependency-preserving compression

Study, in this order

3 steps

  1. Backtracking the LCS table
    DSA Handbook - dynamic programming

    Recovering the actual subsequence: backtrack from \((m, n)\) with one tie-break used in both passes; \(O(\min(m, n))\) space via row reuse

  2. Rolling compression and its cost
    DSA Handbook - dynamic programming

    Rolling-pair compression: from \(O(n)\) to \(O(1)\) space, including the warning that compression makes traceback impossible

  3. Linear-space recovery by splitting
    Erickson lecture note D - Advanced Dynamic Programming

    Section D.1, Saving Space: Divide and Conquer: forward-backward split with the Half table to recover the edit sequence in \(O(m + n)\) space

Focus on

  • Fill and backtrack must share one tie-break. Mixed comparators walk a path the fill never scored.
  • A rolling row keeps only the previous row, so the parent chain is gone. Length-only compression and reconstruction exclude each other without a Hirschberg split.
  • Backtrack cost is \(O(m + n)\): every step decrements \(i\), \(j\), or both, so no cell is revisited.

Reproduce closed-book

Closed-book in Python 3.13: fill the LCS table for ABCBDAB vs BDCAB (\(7 \times 5\)) and backtrack to length 4: up-on-ties gives BCAB and left-on-ties gives BDAB, both valid. Then run the rolling length-only form on the same pair and confirm 4.

Traps

  • Keeping one row and then trying to backtrack through it. Old rows are gone; recovery needs the full table or a Hirschberg split.
  • Using strict greater-than in the fill and greater-or-equal in the backtrack. Either comparator is fine alone; mixing them walks an unscored path.
  • Reading text1[i] instead of text1[i-1] inside a 1-indexed table. The one-character test (a, a) raises IndexError at \(i = 1\) and exposes the shift.

Practice

LeetCode 583 - Delete Operation for Two Strings

Reduce to LCS with \(\text{answer} = m + n - 2k\); check \((\text{sea}, \text{eat}) = 2\) and \((\text{leetcode}, \text{etco}) = 4\), then submit.

After submitting, compare with the DSA Handbook lcs reduction section for LC 583.

Self-test

  1. Why does the rolling LCS return the length but not the string?

  2. Two valid LCS strings exist for one input. Is that a bug?

  3. What does the Hirschberg split add over rolling rows?

  4. House Robber rolling pair: can you still name the robbed houses?

Completion criterion

Stop when

Your backtrack returns length 4 plus a valid witness on the textbook pair, and your rolling form agrees on the length.

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

Leads to

Comes after

Resources for this unit

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

Also in this section

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