:::success

  • 局部最优也是全局最优时才能 work
  • 贪心不一定得到最优结果,而是最优解的近似解,但花费时间一般比找最优解少
  • 贪心背后往往有一个更复杂的动态规划解法 :::

    Activity Selection Problem

    problem:选择尽可能多的相容的活动

image.png

Solutions

:::info DP 解法
Greedy Method - 图2是完全处于(起始时间都在其间)Greedy Method - 图3结束以后到Greedy Method - 图4开始之前的集合,设Greedy Method - 图5Greedy Method - 图6中最多容纳的活动。
我们定义最优子结构为:从Greedy Method - 图7中选出Greedy Method - 图8Greedy Method - 图9Greedy Method - 图10也有最多的相容活动。
状态状态转移方程为:
Greedy Method - 图11
复杂度为Greedy Method - 图12。 ::: :::info 贪心解法
image.png
image.png
复杂度为Greedy Method - 图15 :::

Theorem

image.png

Thinking

image.png
不采取之前二分的做法而是从左到右的来计算。Greedy Method - 图18表示从Greedy Method - 图19Greedy Method - 图20这些活动中最多能容纳的数量,Greedy Method - 图21表示距离Greedy Method - 图22最近的与其相容的活动(也即Greedy Method - 图23前最晚结束的活动),因此我们的选择是放入(会导致一些之前被放入的活动被踢出去)或者不放入Greedy Method - 图24,在这两种选择中取最大。
image.png
如果有权重,贪心就没办法做了,因为考虑不了权重。

Huffman Codes

:::danger

  • 霍夫曼树的待编码字符只出现在叶子上,也即编码是前缀的(没有任何编码是其他编码的前n位) ::: image.png
    image.png