How is dynamic programming different from greedy algorithms?

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

    DP algorithms uses the fact that (for some problems) - an optimal solution to a problem of size n is composed of an optimal solution to a problem of size n', and uses this to build the solution bottom-up, from the smallest problem to the required size.

    It fits the same principle of recursion very well (reduce the problem to a smaller sub-problem, and invoke recursively), and indeed - DP solutions are often represented as a recursive formula.

    Greedy algorithms are looking at a local point and doing some choice with the data at this point. For some problems (shortest path without negatve weights for example) - this local choice will lead to an optimal solution.

    A good example of the differences between the two approaches is for the shortest path problem:

    • Dijsktra's Algorithm is a greedy approach (At each step, chose the node that the path to it is currently minimized - the choice is done greedily based on the local state of the algorithm).
    • Bellman-Ford algorithm is a DP solution ("relax" ALL edges effectively reducing the problem)

提交回复
热议问题