Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can every recursive algorithm be improved with dynamic programming?

I am a first year undergraduate CSc student who is looking to get into competitive programming.

Recursion involves defining and solving sub problems. As I understand, top down dynamic programming (dp) involves memoizing the solutions to sub problems to reduce the time complexity of the algorithm.

Can top down dp be used to improve the efficiency of every recursive algorithm with overlapping sub problems? Where would dp fail to work and how can I identify this?

like image 304
shortstheory Avatar asked Oct 03 '15 05:10

shortstheory


People also ask

Is all recursion dynamic programming?

Is Dynamic Programming Just Recursion? Dynamic programming and recursion are things completely different. While dynamic programming can use recursion techniques, recursion itself doesn't have anything similar to dynamic programming.

How does dynamic programming make an inefficient recursive algorithm more efficient?

How does dynamic programming make an inefficient recursive algorithm more efficient? By keeping track of previously computed recursive calls, and using the stored values when possible instead of initiating redundant recursive calls.

Are all recursive algorithms inefficient?

Recursive algorithms are often inefficient for small data, due to the overhead of repeated function calls and returns. For this reason efficient implementations of recursive algorithms often start with the recursive algorithm, but then switch to a different algorithm when the input becomes small.


1 Answers

The short answer is: Yes.

However, there are some constraints. The most obvious one is that recursive calls must overlap. I.e. during the execution of an algorithm, the recursive function must be called multiple times with the same parameters. This lets you truncate the recursion tree by memoization. So you can always use memoization to reduce the number of calls.

However, this reduction of calls comes with a price. You need to store the results somewhere. The next obvious constraint is that you need to have enough memory. This comes with a not-so obvious constraint. Memory access always requires some time. You first need to find where the result is stored and then maybe even copy it to some location. So in some cases, it might be faster to let the recursion calculate the result instead of loading it from somewhere. But this is very implementation-specific and can even depend on the operating system and hardware setup.

like image 177
Nico Schertler Avatar answered Oct 01 '22 00:10

Nico Schertler