Recognizing Overlapping Recursive Subproblems
Recursive calls overlap when different branches request the same subproblem. Reusing its answer can avoid repeating an entire subtree of work.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace a small recurrence
With a Fibonacci-style recurrence, computing the value at five asks for four and three. Computing four then asks for three and two, so three is requested more than once. This differs from splitting independent halves in divide and conquer.
Name the subproblem key
For a recurrence depending only on the index, that index identifies an answer. If the answer depends on a remaining budget too, include it in the key. Dynamic programming states describe how to identify sufficient state.[1]
Identify identical states reached by different paths
For \(F(n)=F(n-1)+F(n-2)\) with base cases at zero and one, evaluating \(F(5)\) asks for \(F(3)\) directly and indirectly within \(F(4)\). Both requests represent the same mathematical subproblem. Mark identical input arguments in a trace to see where recursive memoization can remove duplicated evaluation.
F(5)
F(4)
F(3) ← first path
F(2)
F(3) ← second path
Distinguish overlap from a large family of different states
Two calls on separate parts of an array can have the same length and different contents. Equal length alone does not give the same answer. Use the complete information that determines the subproblem, such as an interval within one fixed input. Test two equal-length intervals with different values.