← All questions
RareProgramming

Which methods can reduce memory use in dynamic programming?

Short answer
Compress a 2D table into a single working row · Keep only the required part of the table · Keep only a few recent values · Use sparse storage

Exercise

  1. Compress a 2D table into a single working row
  2. Keep only the required part of the table
  3. Recurse without memoization
  4. Replace dynamic programming with a greedy algorithm
  5. Keep only a few recent values
  6. Use sparse storage

Solution

Rolling storage requires limited dependencies. Sparse storage helps when few states are reachable.

In a grid, one row may suffice: take the untouched cell for the value above and the updated neighbour for the value to the left. Fibonacci needs just two earlier numbers. If only s states of a large table are reachable, store only those state-value entries. This accounts for choices 1, 2, 5 and 6.

r × c → c. G(r,c) = G(r−1,c) + G(r,c−1). F(i) = F(i−1) + F(i−2). s ≪ r × c → O(s).

First identify what the next transition reads. A cell may be overwritten only after its old value has been used for the last time. Recovering an entire path can require extra information even when computing its value does not.

Dynamic programming

Platforms

  • MAXIMUM

Download now and try it for free on your own tasks!

10 15 free credits • No credit card required
Version: Loading…

Choose the Windows app or the Chrome extension with phone controls.