How is dynamic programming different from greedy algorithms?

前端 未结 7 958
故里飘歌
故里飘歌 2020-12-12 13:40

In the book I am using Introduction to the Design & Analysis of Algorithms, dynamic programming is said to focus on the Principle of Optimality

7条回答
  •  孤街浪徒
    2020-12-12 14:28

    The concepts of greedy and dynamic solutions are not mutually exclusive and I think this is causing a lot of confusion in most answers. I believe amit's answer stresses on the most important property: a greedy solution makes decisions based on local information. As a consequence a greedy solution may end up finding a local optimum instead of a global one. Dynamic solutions break a problem into smaller subproblems and then aggregate the result to obtain the answer for a bigger more complex problem. So - is it possible that a problem is both dynamic and greedy? The answer is - yes it is possible. An example would be Dijkstra's algorithm. For this algorithm you make a greedy choice on each step and yet you reduce the problem to a simpler subproblem.

    Still there are also examples of greedy algorithms that are not DP-s: say hill climbing is a greedy algorithm that does not break a problem into multiple subproblems - it only always solves one. There are also examples of DPs that are not greedy - e.g. computing the n-th Fibonacci number using memoization is not greedy.

提交回复
热议问题