版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Greedy AlgorithmsGreedy Algorithms2Greedy Methods (描述描述1)*解最佳化問題的演算法, 其解題過程可看成是由一連串的決策步驟所組成, 而每一步驟都有一組選擇要選定.*一個 greedy method 在每一決策步驟總是選定那目前看來最好 的選擇.*Greedy methods 並不保證總是得到最佳解, 但在有些問題卻可以得到最佳解.Greedy Algorithms3Greedy Methods (描述描述2)*Greedy 演算法經常是非常有效率且簡單的演算; 但 但較難證明其正確性 (與 DP 演算法比較).*很多 heuristic a
2、lgorithms 都採用 greedy methods 的策略.Greedy Algorithms4一個活動選擇問題一個活動選擇問題 (定義定義)假設有 n 個 活動 提出申請要使用一個場地, 而這場地在同一時間點時最多只能讓一個活動使用. 問題是:從這 n 個活動選一組數量最多, 且可以在這場地舉辦的活動集.1234567891110時間軸假設活動 i, 其提出申請使用場地的時段為一半關半開的區間 si, fi), 並以符號 Ii 代表.Greedy Algorithms5一個活動選擇問題一個活動選擇問題 (設計設計 1)*Let P(A) denote the problem with
3、A as the given set of proposed activities and S denote an optimal solution of P(A). For any activity i in A, we have 1.i S S is an optimal solution of P(A i ).2.i S S i is an optimal solution of P(AN i ) but not necessary an optima solution of P(A i ).iN i :N i =j A: Ij Ii Greedy Algorithms6一個活動選擇問題
4、一個活動選擇問題 (設計設計 2)*What kind of activity i in A will be contained in an optimal solution of P(A) : an activity with1. minimum fi si or 2.minimum |N i | or3. minimum fi or 4.minimum si.Answer : .Proof : Let f1 = min fi and S be an optimal solution of P(A). If 1 S then there is one and only one activit
5、y in S, say j, such that Ij I1 . Then S j 1 is also an optimal solution.Greedy Algorithms7一個活動選擇問題一個活動選擇問題 (程式程式+例子例子)Greedy-ASP(s, f, n) /* f1 f2 fn*/ Ans = 1; for(i=2, j=1; in; i+) if(s i f j ) Ans = Ans i ; j = i; 1234567891110timeInput: isifi11423530645753865976108811981210913111214 Greedy Algor
6、ithms8Greedy 演算法的要素演算法的要素 *Optimal substructure (a problem exhibits optimal substructure if an optimal solution to the problem contains within it optimal solutions to subproblems)*Greedy-choice property*Priority queue or sortingGreedy Algorithms9Knapsack Problem (Greedy vs. DP)Given n items:weights:
7、 w1 w2 wnvalues: v1 v2 vna knapsack of capacity W Find the most valuable load of the items that fit into the knapsackExample:item weight value Knapsack capacity W=16 1 2 $20 2 5 $30 3 10 $50 4 5 $10Greedy Algorithms10Knapsack ProblemGiven n items:weights: w1 w2 wnvalues: v1 v2 vna knapsack of capaci
8、ty W Ti, j: the optimal solution using item 1,.,i with weight at most j. If wi j Ti, j = Ti-1, j; otherwise Ti, j = max Ti-1, j, wi +Ti-1, j - wi. How good is this method?Greedy Algorithms110-1 and Fractional Knapsack Problem*Constraints of 2 variants of the knapsack problem: 0-1 knapsack problem: e
9、ach item must either be taken or left behind.Fractional knapsack problem: the thief can take fractions of items.*The greedy strategy of taking as mush as possible of the item with greatest vi / wi only works for the fractional knapsack problem.Greedy Algorithms12Huffman CodesAlphabet: a b c d e f Fr
10、equency in a file 45 13 12 16 9 5 Fixed-length codeword 000 001 010 011 100 101Variable-length codeword0 101 100 111 1101 1100file length 1 = 300; file length 2 = 224Compression ratio = (300224)/300100% 25%*A very effective technique for compressing data*Consider the problem of designing a binary ch
11、aracter code*Fixed length code vs. variable-length code, e.g.:Greedy Algorithms13Prefix Codes & Coding Trees*We consider only codes in which no codeword is also a prefix of some other codeword.*The assumption is crucial for decoding variable-length code (using a binary tree). E.g.:a:45c:12b:13d:
12、16 f: 5e: 9 1001011100Greedy Algorithms14Optimal Coding Trees*For a alphabet C, and its corresponding coding tree T, let f(c) denote the frequency of c C in a file, and let dT(c) denote the depth of cs leaf in T. (dT(c) is also the length of the codeword for c.)*The size required to encode the file
13、is thus: B(T) = c C f(c) dT(c) *We want to find a coding tree with minimum B(T). Greedy Algorithms15Observation 1*Any optimal coding tree for C, |C| 1, must be a full binary tree, in which every nonleaf node has two children. E.g.: for the fixed-length code:a:45c:12b:13d:16 f: 5e: 9 Greedy Algorithm
14、s16Observation 2*Assume C = c1, c2, , cn, and f(c1) f(c2) f(cn). Then there exists an optimal coding tree T such that : c1 c2 T:and dT(c1) = dT(c2) = maxc C dT(c)Greedy Algorithms17Observation 3*If is T an optimal coding tree for C , then T is an optimal coding tree for C c1, c2 c with f(c) = f(c1)
15、+ f(c2). c1 c2 T: c1 c2T: cGreedy Algorithms18Huffmans Algorithm (例例)a:45c:12b:13d:16f: 5e: 9a:45c:12b:13d:16f: 5e: 914a:45c:12b:13d:16f: 5e: 91425Greedy Algorithms19Huffmans Algorithm (例例-續續1)a:45c:12b:13d:16f: 5e: 91425d:16f: 5e: 91430c:12b:1325a:45Greedy Algorithms20Huffmans Algorithm (例例-續續2)d:16f: 5e: 91430c:12b:1325a:45a:4555d:16f: 5e: 91430c:12b:1325Greedy Algorithms21Huffmans Algorithm (例例-續續3)a:4555d:16f: 5e: 91430c:12b:1325a:45c:12b:13d:1625f: 5e: 914305
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 温故知新 2026年秋季初一历史部编版上学期期中测试卷(含答案)
- 超越自我 2026年秋季初二语文部编版10月月考试卷(含答案)
- 命题风向标 2027届广西壮族自治区历史初三沪教版查缺补漏专项训练(含答案)
- 2027年河北省语文中考苏教版高分冲刺模拟卷(含答案)
- 2027年山东省道德与法治九年级冲刺模拟卷(含答案)
- 快速提分 2026年秋季初二历史人教版上学期期末测试卷(含答案)
- 2027年陕西省道德与法治中考模拟演练卷(含答案)
- 2027年江苏省语文九年级真题变式卷(含答案)
- 2026 湖北事业编社会工作岗 高频考题试卷 含答案解析
- 2026 水利岗面试易错题集 含答案
- 2026年企业文化企业建设知识竞赛-中国电信知识竞赛历年参考题库含答案解析
- 2026高考议论文范文19篇(完整版含真题立意+考场高分作文)
- ESG管理制度手册
- 《演唱 郊游》课件2025-2026学年冀少版三年级下册音乐
- 2026年乡村医生真题【历年真题】附答案详解
- 2026年C1驾照三力测试模拟题库
- 早产儿母乳喂养知识培训
- 《计算机程序设计员》教学大纲-初中级
- 急性脑卒中静脉溶栓桥接动脉取栓方案
- 银座运营管理制度手册
- 500kV变压器保护及并联电抗器保护技术规范
评论
0/150
提交评论