2

I am studying Dynamic Programming using both iterative and recursive functions. With recursion, the trick of using Memoization the cache results will often dramatically improve the time complexity of the problem.

When evaluating the space complexity of the problem, I keep seeing that time O() = space O(). This is because we will have to cache all the results, but once we cache them it is O(1) to retrieve

Examples of this are

My question is, with Memoization, will the space & time complexity always be the same ? If not, why not ?

Edit - Clarification

This question is regarding the Space complexity of the recursive solution. I have solved the iterative solution using smaller space complexity before, i.e. keeping only a single row in the matrix. However for the recursive solution, I have never seen a recursive approach where items are deleted from the memoization table. I suppose you could combine recursion + iteration to only keep a single row in the memoization, but I have not seen an example where you start at the pure end, and only keep limited space.

Fairly Nerdy
  • 131
  • 1
  • 3

2 Answers2

3

There are plenty examples for which you

  • need $\omega(1)$ time to compute each table entry or
  • do not need to keep all table entries.

An example for the former would be CYK; for the latter memoized Fibonacci and Bellman-Ford.

Raphael
  • 72,336
  • 29
  • 179
  • 389
2

This is not a general rule. It is very possible for a dynamic programming algorithm to have greater time complexity than space complexity (but obviously not the other way around, since we spend at least $O(1)$ time per instance).

For instance, you mention that the dice sum problem can be solved in $O(D*T)$ time and space (if we let $D$ be the number of dice and $T$ be the target sum). It can also be solved in $O(D*T)$ time and $O(T)$ space, simply by noting that the computation of the values follows a specific pattern and that to compute the answer for $n$ dice, you only need the answers for $n$ and $n-1$ dice (and you can forget the answers for $n-2,n-3,\ldots$ dice).

Tom van der Zanden
  • 13,238
  • 1
  • 35
  • 54
  • Tom, have solved the Dice problem with that Space complexity iteratively by building up a matrix and only keeping a row, but not recursively with memoization. Are you saying that recursively with memoization you can only keep a partial set of the data table ? How would you set up the algorithm ? – Fairly Nerdy Jun 04 '16 at 15:50