递归和动态编程

时间:2016-07-13 20:30:40

标签: algorithm recursion dynamic-programming

我知道每个可以使用动态编程解决的程序都可以使用递归来解决,但反之亦然吗?如果可能的话,时间复杂度会有何不同?

3 个答案:

答案 0 :(得分:2)

  反之亦然?

另一方面,如果你真的有意思问:

  

反之亦然?

然后合理地说答案是否定的。并非所有可以通过递归算法解决的问题都可以通过动态编程合理地解决。我们只需要提出一个问题来强调这一点:排序。使用递归算法很容易解决排序问题,但似乎没有合理的算法来解决动态编程排序问题。不幸的是,我不得不在这里使用狡猾的词“合理”,因为你可以用某种方式强制使用动态编程来解决排序问题,这是一种非常尴尬和低效的方式。

关于时间复杂性的问题无法回答。这取决于手头的问题,以及动态编程在解决问题方面的适用性。

答案 1 :(得分:0)

如果(1)具有最佳子结构,即它是递归的,并且(2)具有重叠子问题,则可以使用动态编程解决问题。因此,没有重叠子问题的任何递归问题都无法使用动态编程来解决。例如:插入排序,如果递归定义为sorted_list [0:n] = sorted_list [0] + sorted_list [1:n]。

答案 2 :(得分:-1)

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.