Unit 3 of 9 · about 3 hours · implement

Solution reconstruction and rolling state

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

You'll be able to

  • 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 outputsRolling state and dependency-preserving compression
1

Read

In this order, only the parts listed

0 of 3 done

  1. Backtracking the LCS tableDSA Handbook - dynamic programming

    Recovering the actual subsequence: backtrack from with one tie-break used in both passes; space via row reuse

  2. Rolling compression and its costDSA Handbook - dynamic programming

    Rolling-pair compression: from to space, including the warning that compression makes traceback impossible

  3. Linear-space recovery by splittingErickson 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 space

2

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 : every step decrements , , or both, so no cell is revisited.
Common mistakes · 3
  • 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 and exposes the shift.
3

Try it yourself

Closed-book, before checking anything

Closed-book in Python 3.13: fill the LCS table for ABCBDAB vs BDCAB () 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.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

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

    The backtrack walks rows in reverse, and the rolling form discarded every row but the last two. The parent pointers lived in the discarded rows.

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

    No. Ties in admit several optimal paths. Any one of them is a correct LCS; enumerating all is a different, exponential-output problem.

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

    The witness in linear space. Rolling rows give the length in space; the midpoint split recurses on both halves to also recover the sequence in space at a constant time factor.

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

    No. The pair kept values, not decisions. Keep a boolean choice array or the full dp column to reconstruct the set.

5

Practice

You're done when

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

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 6 more in the 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.

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

Also in this section

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

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

  • 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.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 AlgorithmsJeff Erickson · reference

    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.