Tail recursion and dynamic programming are the same?

I programmed Fibonacci numbers using tail recursion, and it seems that the idea behind it is the same as dynamic programming. So are they? or rather, is there some similarity between them? If not, when do cases happen when they differ?

+4
source share
3 answers

The terms themselves, although related, are not equivalent in any way:

  • Dynamic programming is a problem-solving methodology that can be implemented with or without tail recursion. In general, this requires "memoization".

  • Tail recursion is a common way to implement a dynamic programming algorithm because it specifically applies memoization logic to a specific field.

+3
source

Dynamic programming is usually a more efficient method for performing the same task as in tail recursion. The main difference is that dynamic programming stores the results already calculated so that if the same operation appears, instead of having to run the code again, the value is simply looked through. This takes up more space / memory, but leads to a faster algorithm.

In the case of a Fibonacci, a dynamic programming solution would be to constantly add the last two elements to the array to get the next. A tail recursion solution will calculate each fibonacci value from scratch.

+1
source

I understand why you are asking this question. The idea of โ€‹โ€‹dynamic programming is to break the problem down into smaller problems, and this is the same as many recursive functions (tail or not).

These are really apples for comparing oranges that contain a lot of good answers, but here is one thing that makes you understand why it is so difficult to compare two ideas: in some languages, including Java, you technically cannot use tail recursion. As you may know, the tail recursive function does not store the result stack from its calls and uses them later. However, in Java, stack tracing is supported for each method call. This uses stack space and can overflow if it is executed too many times. Therefore, any Java methods that may appear to be tail recursive are not really.

0
source

Source: https://habr.com/ru/post/1436919/


All Articles