How is dynamic programming different from greedy algorithms?

前端 未结 7 943
故里飘歌
故里飘歌 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:31

    The difference between DP and greedy is DP will look for the global optimal at each subproblem, but greedy will only look for the local optimal. So, this about this scenario:

    Suppose you are climbing a mountain, and you want to climb as high as possible. The road on the mountain has several branches, and at each intersection you need to decide which branch to take, which is the subproblem of this climbing problem (the goal is the same, only the start point is different)

    For the greedy algorithm, you will always choose whichever one seems more steeply up. This is a local optimal decision and is not guaranteed to lead to the best result

    For the DP, at each intersection, you should already know the highest altitude each branch will lead you to (suppose your evaluation order is reversed, a.k.a from end points to the starting point), and choose the one with the largest altitude. This decision is build upon the global optimum of the future subproblems and there for will be globally optimum for this subproblem

提交回复
热议问题