Evaluating a Recurrence from the Bottom Up

A bottom-up evaluation records base values first and computes larger states in an order respecting all dependencies.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Calculate a small sequence
  2. Keep only needed states
  3. Derive the evaluation order from the dependencies
  4. Store only what later computations read

Calculate a small sequence

Let \(a_0=1\), \(a_1=2\), and \(a_n=a_{n-1}+a_{n-2}\). Then \(a_2=3\), \(a_3=5\), and \(a_4=8\). Overlapping subproblems explain why caching earlier terms saves work in a top-down version too.

Keep only needed states

This recurrence can discard every term older than the preceding two when only the last result is needed. If the caller wants all terms, preserve them as an output sequence. Return contracts govern which memory-saving choices are legal.[1]

Derive the evaluation order from the dependencies

To count ways to reach step five using jumps of one or two, let the count at zero be one for the empty path. Let the count at one be one. Every later path arrives from exactly one of the previous two steps. See overlapping subproblems.

Step    0  1  2  3  4  5
Count   1  1  2  3  5  8
Final count: 5 paths ending in +1, 3 ending in +2

Store only what later computations read

If the caller needs only the final count, two previous values suffice. A recurrence with transitions back up to \(k\) steps may need a larger window. To reconstruct a winning path in an optimization recurrence, store predecessor choices or compute them again later.

References

  1. Jeff Erickson, Algorithms ↩