Difference Between Dynamic Programming and Greedy Algorithms

Greedy algorithm is a special case of dynamic programming, which is a generalization of greedy algorithm

Dynamic programming is similar to the exhaustive method, it just records the previous results and does not need to perform repeated calculations. The previous results (state equation) can be used, and the general complexity is higher than greedy

The greedy algorithm only considers the current every time, and does not guarantee that the global optimum can be obtained, only the current optimum is guaranteed. It generally needs to be proved before application, and the general complexity is relatively low

 

Example of dynamic programming: scraping money problem, dp[i] = min(dp[i-v1]+1,dp[i-v2]+1...)

Example of greed: The second question of stocks, that is, you can trade unlimited times. At this time, you only need to consider the situation of the two days before and after, you can buy and sell if you can make a profit, and add the difference in the result. If you can’t make a profit, consider the next time.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324460745&siteId=291194637