浙江大学DS05Ch10aA高级数据结构课件.ppt
《浙江大学DS05Ch10aA高级数据结构课件.ppt》由会员分享,可在线阅读,更多相关《浙江大学DS05Ch10aA高级数据结构课件.ppt(12页珍藏版)》请在三一办公上搜索。
1、CHAPTER 10ALGORITHM DESIGN TECHNIQUES,1 Greedy Algorithms,Optimization Problems:Given a set of constrains and an optimization function.Solutions that satisfy the constrains are called feasible solutions.A feasible solution for which the optimization function has the best possible value is called an
2、optimal solution.,The Greedy Method:Make the best decision at each stage,under some greedy criterion.A decision made in one stage is not changed in a later stage,so each decision should assure feasibility.,1/12,1 Greedy Algorithms,2/12,Note:Greedy algorithm works only if the local optimum is equal t
3、o the global optimum.Greedy algorithm does not guarantee optimal solutions.However,it generally produces solutions that are very close in value(heuristics)to the optimal,and hence is intuitively appealing when finding the optimal solution takes too much time.,1 Greedy Algorithms,1.Huffman Codes for
4、file compression,Example Suppose our text is a string of length 1000 that comprises the characters a,u,x,and z.Then it will take?bits to store the string as 1000 one-byte characters.,8000,Notice that we have only 4 distinct characters in that string.Hence we need only 2 bits to identify them.,We may
5、 encode the symbols as a=00,u=01,x=10,z=11.For example,aaaxuaxz is encoded as 0000001001001011.Then the space taken by the string with length 1000 will be 2000 bits+space for code table./*log C bits are needed in a standard encoding where C is the size of the character set*/,frequency:=number of occ
6、urrences of a symbol.,In string aaaxuaxz,f(a)=4,f(u)=1,f(x)=2,f(z)=1.,The size of the coded string can be reduced using variable-length codes,for example,a=0,u=110,x=10,z=111.,3/12,Discussion 9:In what case that there are not likely to be any savings even using Huffman codes?,1 Greedy Algorithms,Rep
7、resentation of the original code in a binary tree/*trie*/,If character Ci is at depth di and occurs fi times,then the cost of the code=di fi.,Cost(aaaxuaxz 0000001001001011)=24+21+22+21=16,Representation of the optimal code in a binary tree,Cost(aaaxuaxz 00010110010111)=14+31+22+31=14,The answer is
8、aaaxuaxz(with a=0,u=110,x=10,z=111).What makes this decoding method work?,The trick is:No code is a prefix of another.,Now,with a=0,u=110,x=10,z=111 and the string 00010110010111,can you decode it?,4/12,Discussion 10:What must the tree look like if we are to decode unambiguously?,1 Greedy Algorithms
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 浙江大学 DS05Ch10aA 高级 数据结构 课件
链接地址:https://www.31ppt.com/p-5451179.html