高级数据结构与算法分析
本文是ADS期末复习所做整理包含课程重点内容,习题错题等
5-8 贪心算法 (Greedy Algorithm)
Make the best decision at each stage, under some greedy criterion
Key Points
- Doesn’t guarantee an optimal solution, however, it’s quite like the heuristics method. (Woeks when the local optimum is the global optimal)
- 跟Local Search的区别是Greedy Algorithm是在逐步地构建一个解,局部搜索是每次在局部去找更好的解(每次都是一个新的解)
Examples
Activity Selection Problem
- By GA: ends first first
O(NlogN) O ( N l o g N )
- By DP:
- By GA: ends first first
c1,j={
1max{
c1,j−1,c1,k(j)+1}if j=1if j>1(1) (1) c 1 , j = { 1 i f j = 1 m a x { c 1 , j − 1 , c 1 , k ( j ) + 1 } i f j > 1
If each activity has a weight:
c1,j={
1max{
c1,j−1,c1,k(j)+w