I know that every program that can be solved using dynamic programming can be solved using recursion, but is the vice versa also possible? If possible then how will the time complexity differ?
-
It can't really be answered unless you say what "solving a program using dynamic programming" means to you. In general it's not a precisely defined thing. For example, what would it mean for a program that sorts an array to use dynamic programming?Paul Hankin– Paul Hankin2016-07-14 02:50:13 +00:00Commented Jul 14, 2016 at 2:50
-
I asked it in general like if printing permutations of a string which can be done using recursion. can it be done using dynamic programming?Zephyr– Zephyr2016-07-14 04:59:13 +00:00Commented Jul 14, 2016 at 4:59
3 Answers
is the vice versa also possible?
Yes.
On the other hand, if you were actually meaning to ask:
is the vice versa also true?
Then reasonably speaking the answer is No. Not all problems that can be solved with recursive algorithms can reasonably be solved with dynamic programming. We only need to come up with one problem to highlight this: sorting. It is easy to solve the problem of sorting with a recursive algorithm, but there does not seem to be a reasonable algorithm to solve the problem of sorting with dynamic programming. Unfortunately I have to resort to using the weasel word "reasonable" here, because you could forcefully use dynamic programming in some manner to solve the problem of sorting, in a very awkward and inefficient way.
The question regarding the time complexity can't be answered. It depends on the problem at hand, and how applicable dynamic programming would be in solving the problem.
4 Comments
A problem can be solved using dynamic programming if (1) it has optimal sub-structure, i.e. it is recursive, and (2) it has overlapping sub problems. Therefore, any recursive problem that has not overlapping sub problems cannot be solved using dynamic programming. Example: insertion sort, if defined recursively as sorted_list[0:n] = sorted_list[0] + sorted_list[1:n].
Comments
Dynamic Programming is a time-vs-complexity tradeoff. You store some information in tables so you do not need to recompute them if you need to do the same thing again.
If your problem naturally decomposes into subproblems that often repeat then dynamic programming is a good idea. On the other hand, if there are no recurring subproblems then there is no point in using dynamic programming.