1、 与分 马海 东北大学 分校2015.11.25School of Computer and Communication Engineer 1第 4 贪 学习 :理贪 贪 本 素活currency1选 , 载 , 夫 编码, 单 最 ,最小生成树School of Computer and Communication Engineer 2找 : 假 有顾在 市购买物 值 2.5 元, 支一张 20 元”民币, 找 案, 求找 顾 数 最少?10 元一张, 5 元一张, 1 元 张, 5 一张.共5张.School of Computer and Communication Engineer 3
2、贪 一步一步 最优案, 其中一步考虑眼前 最佳选空 行搜索 , 在局范 行优选 , 下一步搜索向不是为 找到全, 而是找一 行在一 下, 贪 找 行将是最优School of Computer and Communication Engineer 44.2 贪 本 素包含一列步, 一步 有一选, 做在当前看来最好 选希望通作局最优选 到全局最优选一个贪 是否生最优, 需 严格明School of Computer and Communication Engineer 5贪 生最优 条 贪选性最优子 School of Computer and Communication Engineer 6贪
3、选性质义 1. 一个优化 全局最优 通局最优选 到,则 为具有贪选性.currency1态划: 一步做一个选, 选 赖 子 贪 : 一步做一个选, 选不 赖 子一个 是否具有贪选性需 明School of Computer and Communication Engineer 7最优子 义 2. 一个优化 最优包 子 最优,则 其具有最最优子 .currency1态划: 最优子 , 子 性贪 : 最优子 , 贪选性School of Computer and Communication Engineer 8currency1态划与贪 包 0/1 包 School of Computer and
4、 Communication Engineer 9包(Knapsack) n物 一 包. 物i 是wi, 其 值为 vi, 包 为c. 何选入 包 物, 使 入 包中物 总 值最大?c 0, wi 0, vi 0, 1 i n, 求找一个n元向 (x1;x2;:;xn), 0 xi 1, 1 i n, 使 Pni=1wixi c, 而且,Pni=1vixi 到最大.School of Computer and Communication Engineer 100/1 包 在选入 包 物 , 物i有两选, 即入 包或不入 包. 不 将物i入 包多次, 也不 入分 物i. 此, 为 0-1 包 .
5、c 0, wi 0, vi 0, 1 i n, 求找一个n元 0-1 向 (x1;x2;:;xn), xi2f0;1g, 1 i n,使 Pni=1wixi c, 而且, Pni=1vixi 到最大.School of Computer and Communication Engineer 11包 贪 本步先 物单位 值vi=wi, 然后 贪选策略, 将尽 多 单位 值最 物入 包将 物全入 包后, 包 物总未 c, 则选单位 值次 物并尽 多地入 包此策略一 地 行下, 到 包为止School of Computer and Communication Engineer 12Fun Time
6、0-1 包 :假 n = 3, c = 50, w = 10;20;30, v = 60;100;120.贪 求:最大 值=?currency1态划 求:最大 值=?School of Computer and Communication Engineer 13Fun Time0-1 包 :假 n = 3, c = 50, w = 10;20;30, v = 60;100;120.贪 求:物1,2入 包, 值= 160;currency1态划 求:物2,3入 包, 值= 220,最优.School of Computer and Communication Engineer 140/1 包 c
7、urrency1态划求贪选 最 将 包, 分 置 包空 使物单位 值 生 化选 物 不选 物所 最案, 然后 作最好选( 子 )School of Computer and Communication Engineer 15贪 正确性明 (1) 明 所求 具有最优子 (2) 明 所求 具有贪选性(3) 按照贪选性 行局最优选School of Computer and Communication Engineer 164.1 活currency1 活currency1: S =f1;2;:;ng是n个活currency1 合, 个活currency1使用一个资 , 个资 为一个活currenc
8、y1使用个活currency1i有一个起 si 止 fi, si fi活currency1: sj fi或si fj, 活currency1i j School of Computer and Communication Engineer 17活currency1s i f i s j f js j f j si f iTimeTimeSchool of Computer and Communication Engineer 18活currency1 义活currency1合S =f1;2;:;ng, 活currency1 合F =fsi;fig, 1 i n,求S 最大活currency1合
9、.School of Computer and Communication Engineer 19Fun Time考虑 下活currency1合S:i 1 2 3 4 5 6 7 8 9 10 11si 1 3 0 5 3 5 6 8 8 2 12fi 4 5 6 7 9 9 10 11 12 14 16子fa3;a9;a11g为一活currency1合 最大活currency1合?School of Computer and Communication Engineer 20Fun Time考虑 下活currency1合S:i 1 2 3 4 5 6 7 8 9 10 11si 1 3 0
10、5 3 5 6 8 8 2 12fi 4 5 6 7 9 9 10 11 12 14 16- 子fa1;a4;a8;a11g为最大活currency1合- 子fa2;a4;a9;a11g为最大活currency1合School of Computer and Communication Engineer 21活currency1选 currency1态划 Sij: 活currency1ai 束之后开, 活currency1aj 开之前 束 活currency1合Aij: Sij 最大活currency1合, 包含活currency1ak子 为Sik Skj 最大活currency1合求Scho
11、ol of Computer and Communication Engineer 22子 求Aik = AijSikAkj = AijSkjAij = AikfakgAkjjAijj=jAikj+jAkjj+ 1School of Computer and Communication Engineer 23最优 公式ci;j: Sij 最优 大小ci;j =(0 if Sij = maxak2Sijfci;k +ck;j + 1g if Sij6= School of Computer and Communication Engineer 24活currency1选 currency1态划
12、考虑 下活currency1合S:i 1 2 3 4 5 6 7 8 9 10 11si 1 3 0 5 3 5 6 8 8 2 12fi 4 5 6 7 9 9 10 11 12 14 16School of Computer and Communication Engineer 25活currency1选 贪 为 选最多 活currency1, 次选fi最小 活currency1, 即, 使 后 选 多 活currency1|贪(Greedy)“.School of Computer and Communication Engineer 26活currency1选 贪 考虑 下活curre
13、ncy1合S:i 1 2 3 4 5 6 7 8 9 10 11si 1 3 0 5 3 5 6 8 8 2 12fi 4 5 6 7 9 9 10 11 12 14 16- 子fa1;a4;a8;a11g为最大活currency1合School of Computer and Communication Engineer 27GREEDY-ACTIVITY-SELECTOR greedy-activity-selector(S;F)1 n lengthS2 A f1g3 j 14 for i 2 to n5 if si fj6 then A Afig7 j i8 return ASchool
14、 of Computer and Communication Engineer 28复杂性分 (1) 活currency1 束 ,T(n) = (n)(2) 未 ,T(n) = (n) + (nlogn) = (nlogn)School of Computer and Communication Engineer 29最优子 性质明引理 1. S =fa1;a2;:;ang是n个活currency1合, si;fi是活currency1ai 起止 ,且f1 f2 : fn, S 活currency1选 个最优包 活currency1a1.Proof. A是一个最优, 按 束 A中活currency1,其第一个活currency1为 ak, 第 个活currency1是aj, k = 1,引理成.k6= 1, B = A fakgfa1g, 由 f1 fk sj,B中活currency1. 由 jBj=jAj, 所 B是一个最优, 且包 活currency1a1.School of Computer and Communication Engineer 30