What's the difference between recursion and corecursion?

What is the difference between dynamic programming and recursion?

  • It seems to me that dynamic programming is quite similar to recursion. After all, the optimal substructure of the problem is often expressed in terms of similar, smaller subproblems. My guess is that dynamic programming leverage some sort of caching strategies, so the computation of smaller subproblems become constant look-ups rather than repeated (redundant) subproblem computations.

  • Answer:

    Once you have come up with a recursive solution to a problem you would want to prevent computing the same values multiple times. You plug in a cache and it becomes a D.P solution. With sufficient practice you will learn to compute the subproblems in a well contrived manner so as to avoid recursive calls completely.

Apurv Verma at Quora Visit the source

Was this solution helpful to you?

Other answers

During recursion, there may exist a case where same sub-problems are solved multiple times. Consider the example of calculating nth fibonacci number. fibo(n) = fibo(n-1) + fibo(n-2) ffibo(n-1) = fibo(n-2) + fibo(n-3) fibo(n-2) = fibo(n-3) + ffibo(n-4) ................................. ................................ ................................ fibo(2) = fibo(1) + fibo(0) In the first three steps, it can be clearly seen that fibo(n-3) is calculated twice. If one goes deeper into recursion, he/she may find repeating the same sub-problems again and again. Benefit of DP over Recursion: DP is basically a memorization technique which uses a table to store the results of sub-problem so that if same sub-problem is encountered again in future, it could directly return the result instead of re-calculating it. Follow the below link for more details: http://www.geeksforgeeks.org/dynamic-programming-set-1/

Aashish Barnwal

Recursion is just a function calling itself. It is not related to dynamic programming, like Recursion vs Iteration. You can use recursion in your dynamic programming as well. Coming to dynamic programming, most DP problems can be solved by top-down recursion or bottoms up approach caching intermediate results a technique which is called 'memo-ization' . You can cache results while using recursion as well.  One major difference using these two methods to solve a problem is that in 'recursive' top down approach you need not calculate all the sub-problems, you only calculate the sub-problems you need for the final solution eliminating some sub-problems altogether.  In the bottoms-up approach you calculate all the sub-problems whether they are used in the final solution or not. This difference might not be relevant in all the problems. One example i can think of is the coin change problem. http://stackoverflow.com/questions/1986828/coin-changing-algorithm Here using DP you calculate all the solution for all the values from 1 up to the value for which the solution is needed . Using recursion for the same value , you would calculate solutions for only some of the sub problems.

Manoj Krovvidi

Both are completely different although you might encounter cases where you can avoid recursive calls just by storing the results of previous recursive calls, thereby avoiding a function call by trading off some memory. e.g. fibonacci number. see 's answer. Recursive solution where a subproblem is solved more than once,  can be made to run faster by using DP technique. There is no need of DP in recursive solutions where you don't have to solve the same subproblem more than once. e.g. binary search. Recursion helps you break down your problem into subproblems which are easier to solve. Dynamic programming is a technique where you store the result of previous calculation to avoid calculating the same once again. As a side note, dynamic programming is useful in cases where the given problem has optimal substructure property. That means given problem can be obtained by using optimal solutions of its subproblems. e.g. shortest path problem.

Ajaya Agrawal

The fundamental difference is the approach to solve the problem. In recursion you break the problem top-down but in dynamic programming you approach it from the bottom-up and also use some mechanism to store intermediate results. The difficulty with dynamic programming is that its solution are not very obvious you have to think in a very different way to understand it. The simplest example I can think of  is the factorial computation: In recursion to find the Fac(n) you will break the problem down knowing that Fac(0) is 1 In Dynamic programming you start with Fac(0) then storing it and using it for computing Fac(1) and so on.. Similar problems you can see is Maximum sum sub vector

Himanshu Maurya

You must look at this blog post.. this is exactly defining the dynamic programmin hallmarks http://techieme.in/dynamic-programming-longest-common-subsequence/

Dharmendra Prasad

There are two types of Dynamic Programming: Bottom Up and Top Down. I assume you're asking about the latter. Top down Dynamic Programming is essentially recursion, but enhanced with memoization. You "memoize" the computed values in a lookup table (usually an array), to avoid having to recompute those values again in the future; you simply return the value in the lookup table.

Evan Putra Limanto

Well, I found out that people have a lot of trouble understanding dynamic programming. Hence, I have written a blog post regarding the same to explain it in the best possible way and simple language. Click https://tp-iiita.quora.com/Dynamic-Programming-Part-1 to view the blog post. You can raise your concern in comments if you face any doubts after reading the tutorial.

Prateek Gupta

Dynamic programming is just recursion plus a little bit of common sense. Recursion means that you express the value of a function in terms of other values of that function (or as an easy-to-process base case). Where the common sense comes in is that you implement your function in such a way that the recursive calls are done in advance, and stored for easy access. Let me illustrate with an example. Here are two functions to compute binomial coefficients; the first uses recursion, the second used dynamic programming. Recursion: int binom(int a, int b) {      if (b==0 || b==a) return 1;      else return binom(a-1,b-1)+binom(a-1,b); } Dynamic Programming: int binomDP(int a, int b) {      int binom[a+1][a+1];      for (int i=0; i<=a; i++) {           for (int j=0; j<=i; j++) {                if (j==0 || j==i) binom[i][j] = 1;                else binom[i][j] = binom[i-1][j-1] + binom[i-1][j];           }      }      return binom[a][b]; } As you can see, both functions do exactly the same thing, in exactly the same way. But the second one is much faster (at the expense of using a lot more memory), because the "recursive calls" happen in constant time (they just access an array).

Anonymous

Dynamic programming is a special case of recursion. In the recursion, if there are subproblems that are solved repeatedly, you store the results instead of recalculating them. This act of remembering is called Memoization. This whole technique is called Dynamic Programming.

Mayank Jaiswal

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.