1

In this approach smaller subproblems are computed and results are cached, then we compute the bigger subproblems for which we use the already computed optimized values of the smaller subproblems from a table which cached the earlier computed values. So, is this approach recursive or iterative?

Deba
  • 429
  • 3
  • 8
  • 18
  • 1
    Neither nor. DP is just a algorithmic technique; it doesn't imply anything about the actual implementation. The logic is recursive (solve larger problems by solving the same problem scaled down) if that's what you mean? –  Jan 10 '17 at 20:40

1 Answers1

2

The approch we use in dynamic programming is actually inductive. One can turn constructive inductive proofs either to a recursive algorithm or to an iterative algorithm. It is just matter of taste. E.g. memoization is a recursive implementation, while for every memoized algorithm there is an iterative approach.

Simple example is the fibonacci number. One can write it iterative:

Fib (n)
{
    F_1=F_2=1;
    For i =3..n 
         F_i = F_i-1 + F_i-2;
   Return F_n;
}

One can write it recursively:

  Define array F of size n;
  F [1]=F [2]=1;
  Fib (n)
  {
       If (F [n-1]==0)
            F [n-1] = Fib (n-1);

      If (F [n-2]==0)
            F [n-2] = Fib (n-2);
     F [n]= F[n-2]+F [n-1];
     Return  F[n];
  }

Both of them are dynamic programing and they have same order. In some circumstances writing it recursive is easier. In some cases iterative is faster.

Codor
  • 17,447
  • 9
  • 29
  • 56
Saeed Amiri
  • 22,252
  • 5
  • 45
  • 83
  • I think this is a very good explanation. One more point: some programming languages do not directly support recursive calls, so a straightforward recursive implementation is impossible. Furthermore, apparently most people consider the respective inductive formulations to show a 'better understanding' of the problem. I heared the argument that the 'sequence of evaluations' would matter, as 'one needs to evaluate the base values first' - however, this is also the case in a recursive implementation. – Codor Jan 11 '17 at 12:17
  • I am not talking only about dynamic programming but, specifically, about the one of two ways of it: the Bottom UP approach. Why I think that the bottom up approach is of iterative nature (and not recursive) is there is another characteristic of recursion : we start from the main problem to be solved and after each recursion we move one step toward the base condition. But in case of this bottom up approach, we are starting from the base condition, caching the results and use them to evaluate the forthcoming results. So isn’t it of iterative nature rather than of recursive? – Deba Jan 11 '17 at 14:59
  • @Deba, as I said it is basically inductive. One can approach induction by recursive approach: solve the biggest one by reducing its size to the base case, here base case is small instances or the base cases of the induction. Another one can approach it bottom up: Having base cases of the problem, we know how to solve a bit bigger instance, solve those bit bigger instances. So in this case recursion and bottom up approaches are just tools to implement the induction. It is maybe more natural looking to say it is bottom up, but this is just matter of taste. – Saeed Amiri Jan 11 '17 at 15:45