4. 贪心算法

What

A greedy algorithm is an algorithmic paradigm that follows the problem solving heuristic of making the locally optimal choice at each stage with the hope of finding a global optimum. In many problems, a greedy strategy does not in general produce an optimal solution, but nonetheless a greedy heuristic may yield locally optimal solutions that approximate a global optimal solution in a reasonable time.
说白了,贪心算法是分步走,每一个找到最优解。总体的找到解不一定是最优解。

Why

Compare to Dynamic Programming

  1. A greedy algorithm is effective powerful, beacuse it isn't pay attention to global result.
  2. More then appropriate NP(Non-determine Problem) 问题

How to design

  1. 贪心算法使用组合优化问题,该问题满足优化原则
  2. 求解过程是多步判断过程,最终的判断序列对应问题的最优解
  3. 判断一句某种短视的贪心选择原则,原则的好坏决定了算法的成败
  4. 贪心法必须进行正确性证明
Greedy Select
input : S=set{1,2 ... n},活动开始时间Si,截止时间Fi
output : A, it is one of S
A = {1}
j = 1
for i =2 to n do
  if Si > Fj
  then A = A + {i}
    j = i
return A

Sample

  1. Generate Minimum Tree
Prim
input : Graphic G = < Vector, Edge, Weight>
output : Tree T
S = {1} // start
T = null
while V - S != null do
  temp = V - S
  j = selectMinimalWeightOfEdge(temp)
  T = T + ei
  S = S + j
Kruskal
input : Graphic G = < Vector, Edge, Weight>
output : Tree T
1. Sorting edges by Weight
2. T = null
3. repeat
4.   e = Minimal edge
5.   if e is not into continue branch T
6.   then T = T + e
7.   E = E - e
8. Until T include n-1 edge
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一条狗大摇大摆地走进了我的家门。它耷拉着脑袋,在门左侧的树绕了一圈,往前走了几步,又调头回来,再次绕了一圈。东嗅西...
    喝醉酒的少校阅读 1,228评论 4 9
  • 赏八月萍踪,澈彩虹蜻影。 其一刻也, 可食杉果,担石泉, 意马南山,心猿寺庐, 岂非人生之妙时? 咏诗歌赋以为乐!...
    摩羯星一号阅读 304评论 3 3
  • 在很长的一段时间里,我都以为随波逐流是人生最安全的状态。小时候,家属院里做游戏,妈妈就跟我说跟着大孩子玩会比较安...
    July鲸鱼阅读 495评论 0 4
  • 为纪念新华书店成立八十周年,北流市新华书店有限公司在中共北流市委宣传部的策划下于2017年4月21日晚成功地...
    樊偉阅读 391评论 0 0
  • 瞬间容颜 在穿梭迅速的每一个瞬间, 仅留一熟悉而清澈的容颜。2009年5月14 �����U
    甜蜜蜜_b52a阅读 128评论 0 0