计算模型与算法技术:9-Greedy Technique

上传人:努力****83 文档编号:192576699 上传时间:2023-03-07 格式:PPT 页数:36 大小:4.51MB
收藏 版权申诉 举报 下载
计算模型与算法技术:9-Greedy Technique_第1页
第1页 / 共36页
计算模型与算法技术:9-Greedy Technique_第2页
第2页 / 共36页
计算模型与算法技术:9-Greedy Technique_第3页
第3页 / 共36页
资源描述:

《计算模型与算法技术:9-Greedy Technique》由会员分享,可在线阅读,更多相关《计算模型与算法技术:9-Greedy Technique(36页珍藏版)》请在装配图网上搜索。

1、L o g oL o g o1Computational Model and Algorithmic TechniqueSouth China University of TechnologyDr.Han HuangL o g oL o g oCopyright 2007 Pearson Addison-Wesley.All rights reserved.L o g oL o g oGreedy TechniqueConstructs a solution to an optimization problem piece by piece through a sequence of choi

2、ces that are:vfeasiblevlocally optimalvirrevocableFor some problems,yields an optimal solution for every instance.For most,does not but can be useful for fast approximations.L o g oL o g oApplications of the Greedy StrategyvOptimal solutions:change making for“normal”coin denominations minimum spanni

3、ng tree(MST)single-source shortest paths simple scheduling problems Huffman codesvApproximations:traveling salesman problem(TSP)knapsack problem other combinatorial optimization problemsL o g oL o g oGreedy solution isvoptimal for any amount and“normal set of denominationsv may not be optimal for ar

4、bitrary coin denominationsGreedy TechniqueL o g oL o g oChange-Making ProblemGiven unlimited amounts of coins of denominations d1 dm,give change for amount n with the least number of coinsExample:d1=25c,d2=10c,d3=5c,d4=1c and n=48cL o g oL o g oMinimum Spanning Tree(MST)vSpanning tree of a connected

5、 graph G:a connected acyclic subgraph of G that includes all of Gs verticesvMinimum spanning tree of a weighted,connected graph G:a spanning tree of G of minimum total weightExample:cdba62431L o g oL o g oPrims MST algorithmvStart with tree T1 consisting of one(any)vertex and“grow”tree one vertex at

6、 a time to produce MST through a series of expanding subtrees T1,T2,TnvOn each iteration,construct Ti+1 from Ti by adding vertex not in Ti that is closest to those already in Ti(this is a“greedy”step!)vStop when all vertices are includedL o g oL o g oL o g oL o g oExample(graph ant its spanning tree

7、)bdca1523bdca123bdca153bdca152graphw(T1)=6w(T2)=9w(T3)=8T1 is the minimum spanning treeL o g oL o g oExample 1v1 C,D$700 2 C,E$800 v3 D,A$1200 4 A,B$900 vTotal:$3600 DCABE$900$1200$1300$1600$1000$700$800$2200$2000$1400L o g oL o g oExample 2v 1 b,f 1 v 2 a,b 2 v 3 f,j 2 v 4 a,e 3v 5 i,j 3v 6 f,g 3v

8、7 c,g 2v 8 c,d 1v 9 g,h 3v 10 h,l 3v 11 k,l 1abcefgkjidhl23143333112534243abcfgkjidhl21333112323eL o g oL o g oNotes about Prims algorithmvProof by induction that this construction actually yields MST vNeeds priority queue for locating closest fringe vertexvEfficiency O(n2)for weight matrix represen

9、tation of graph and array implementation of priority queue O(m log n)for adjacency list representation of graph with n vertices and m edges and min-heap implementation of priority queueL o g oL o g oAnother greedy algorithm for MST:KruskalsvSort the edges in nondecreasing order of lengthsv“Grow”tree

10、 one edge at a time to produce MST through a series of expanding forests F1,F2,Fn-1vOn each iteration,add the next edge on the sorted list unless this would create a cycle.(If it would,skip the edge.)L o g oL o g oL o g oL o g oExample 3v 1 c,d 1 v 2 k,l 1 v 3 b,f 1 v 4 c,g 2v 5 a,b 2v 6 f,j 2v 7 b,

11、c 3v 8 j,k 3v 9 g,h 3v 10 i,j 3v 11 a,e 4abcefgkjidhl231433331abcfgkjidhl2133311253424312323eL o g oL o g oExamplecdba42613cdba1cdba21cdba213L o g oL o g oNotes about Kruskals algorithmvAlgorithm looks easier than Prims but is harder to implement(checking for cycles!)vCycle checking:a cycle is creat

12、ed iff added edge connects vertices in the same connected componentvUnion-find algorithms see section 9.2L o g oL o g oShortest paths Dijkstras algorithmSingle Source Shortest Paths Problem:Given a weighted connected graph G,find shortest paths from source vertex sto each of the other verticesDijkst

13、ras algorithm:Similar to Prims MST algorithm,with a different way of computing numerical labels:Among vertices not already in the tree,it finds vertex u with the smallest sum dv+w(v,u)where v is a vertex for which shortest path has been already found on preceding iterations(such vertices form a tree

14、)dv is the length of the shortest path form source to v w(v,u)is the length(weight)of edge from v to u.L o g oL o g oL o g oL o g oExample 4vfor i:=1 to nv L(vi):=vL(a):=0 vS:=be0zacd4105826321L o g oL o g oExample 4vu:=a vertex not in S with L(u)minimalvS:=S U ube0zacd41058263214(a)2(a)L o g oL o g

15、 oExample 4vif L(u)+w(u,v)L(v)thenv L(v):=L(u)+w(u,v)be0zacd41058263213(a,c)2(a)10(a,c)12(a,c)L o g oL o g oExample 4vif L(u)+w(u,v)L(v)thenv L(v):=L(u)+w(u,v)be0zacd41058263213(a,c)2(a)8(a,c)12(a,c)L o g oL o g oExample 4vif L(u)+w(u,v)L(v)thenv L(v):=L(u)+w(u,v)be0zacd41058263213(a,c)2(a)8(a,c,b)1

16、0(a,c,b,d)14(a,c,b,d)L o g oL o g oExample 4vif L(u)+w(u,v)L(v)thenv L(v):=L(u)+w(u,v)be0zacd41058263213(a,c)2(a)8(a,c,b)10(a,c,b,d)13(a,c,b,d,e)L o g oL o g oExample 4vThe shortest path is a,c,b,d,e,z with length 13.be0zacd41058263213(a,c)2(a)8(a,c,b)10(a,c,b,d)13(a,c,b,d,e)L o g oL o g oExample d4

17、 a(-,0)b(a,3)c(-,)d(a,7)e(-,)ab4e37625cabd4ce374625abd4ce374625abd4ce374625 b(a,3)c(b,3+4)d(b,3+2)e(-,)d(b,5)c(b,7)e(d,5+4)c(b,7)e(d,9)e(d,9)d4abdce374625L o g oL o g oNotes on Dijkstras algorithmvDoesnt work for graphs with negative weightsvApplicable to both undirected and directed graphsvEfficien

18、cy O(|V|2)for graphs represented by weight matrix and array implementation of priority queue O(|E|log|V|)for graphs represented by adj.lists and min-heap implementation of priority queuevDont mix up Dijkstras algorithm with Prims algorithm!L o g oL o g oCoding ProblemCoding:assignment of bit strings

19、 to alphabet charactersCodewords:bit strings assigned for characters of alphabetTwo types of codes:vfixed-length encoding(e.g.,ASCII)vvariable-length encoding(e,g.,Morse code)Prefix-free codes:no codeword is a prefix of another codewordProblem:If frequencies of the character occurrences are known,wh

20、at is the best binary prefix-free code?L o g oL o g oHuffman codesvAny binary tree with edges labeled with 0s and 1s yields a prefix-free code of characters assigned to its leavesvOptimal binary tree minimizing the expected(weighted average)length of a codeword can be constructed as followsHuffmans

21、algorithmInitialize n one-node trees with alphabet characters and the tree weights with their frequencies.Repeat the following step n-1 times:join two binary trees with smallest weights into one(as left and right subtrees)and make its weight equal the sum of the weights of the two trees.Mark edges l

22、eading to left and right subtrees with 0s and 1s,respectively.L o g oL o g oExamplecharacter AB C D _frequency 0.35 0.1 0.2 0.2 0.15codeword 11 100 00 01 101average bits per character:2.25for fixed-length encoding:3compression ratio:(3-2.25)/3*100%=25%L o g oL o g o0.1B0.15_0.2C0.2D0.35A0.250.1B0.15_0.2C0.2D0.35A1.2.L o g oL o g o0.250.1B0.15_0.35A3.0.2C0.2D0.44.0.250.1B0.15_0.35A0.60.2C0.2D0.4L o g oL o g o5.0.250.1B0.15_0.35A0.60.2C0.2D0.41.010011100L o g oL o g o36C l i c k t o e d i t c o m p a n y s l o g a n .

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!