国外算法设计与分析课件14.ppt_第1页
国外算法设计与分析课件14.ppt_第2页
国外算法设计与分析课件14.ppt_第3页
国外算法设计与分析课件14.ppt_第4页
国外算法设计与分析课件14.ppt_第5页
已阅读5页,还剩30页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、GreedyAlgorithms(I),Greed,forlackofabetterword,isgood!Greedisright!Greedworks!-MichaelDouglas,U.S.actorintheroleofGordonGecko,inthefilmWallStreet,1987,Maintopics,IdeaofthegreedyapproachChange-makingproblemMinimumspanningtreeproblemPrimsalgorithmKruskalsalgorithmPropertiesofminimumspanningtree(additi

2、vepart)Bottleneckspanningtree(additivepart),ExpectedOutcomes,Studentshouldbeabletosummarizetheideaofthegreedytechniquelistseveralapplicationsofthegreedytechniqueapplythegreedytechniquetochange-makingproblemdefinetheminimumspanningtreeproblemdescribeideasofPrimandKruskalsalgorithmsforcomputingminimum

3、spanningtreeprovethecorrectnessofthetwoalgorithmsanalyzethetimecomplexitiesofthetwoalgorithmsunderdifferentdatastructuresprovethevariouspropertiesofminimumspanningtree,Glossary,Greedy:贪婪的Change-makingproblem:找零钱问题Cashier:收银员Denomination:货币单位,面额Quarter,dime,nickel,penny:2角5分,1角,5分,1分(美国硬币单位)Irrevocab

4、le:不可取消的,不可逆的Spanningtree:生成树,支撑树,AnticipatorySet:ChangeMakingProblem,Howtomake48centsofchangeusingcoinsofdenominationsof25(1quartercoin),10(1dimecoin),5(1nickelcoin),and1(1pennycoin)sothatthetotalnumberofcoinsisthesmallest?Theidea:Takethecoinwithlargestdenominationwithoutexceedingtheremainingamount

5、ofcents,makethelocallybestchoiceateachstep.Isthesolutionoptimal?Yes,theproofisleftasexercise.,GeneralChange-MakingProblem,Givenunlimitedamountsofcoinsofdenominationsd1dm,givechangeforamountnwiththeleastnumberofcoins.Doesthegreedyalgorithmalwaysgiveanoptimalsolutionforthegeneralchange-makingproblem?W

6、egivethefollowingexample,Example:d1=7c,d2=5c,d3=1c,andn=10c,notalwaysproducesanoptimalsolution.infact,forsomeinstances,theproblemmaynothaveasolutionatall!considerinstanced1=7c,d2=5c,d3=3c,andn=11c.But,thisproblemcanbesolvedbydynamicprogramming,pleasetrytodesignanalgorithmforthegeneralchange-makingpr

7、oblemafterclass.,GreedyAlgorithms,Agreedyalgorithmmakesalocallyoptimalchoicestepbystepinthehopethatthischoicewillleadtoagloballyoptimalsolution.Thechoicemadeateachstepmustbe:FeasibleSatisfytheproblemsconstraintslocallyoptimalBethebestlocalchoiceamongallfeasiblechoicesIrrevocableOncemade,thechoicecan

8、tbechangedonsubsequentsteps.Dogreedyalgorithmsalwaysyieldoptimalsolutions?Example:changemakingproblemwithadenominationsetof7,5and1,andn=10?,ApplicationsoftheGreedyStrategy,Optimalsolutions:someinstancesofchangemakingMinimumSpanningTree(MST)Single-sourceshortestpathsHuffmancodesApproximations:Traveli

9、ngSalesmanProblem(TSP)Knapsackproblemotheroptimizationproblems,Forsomeproblems,yieldsanoptimalsolutionforeveryinstance.Formost,doesnotbutcanbeusefulforfastapproximations.,MinimumSpanningTree(MST),SpanningtreeofaconnectedgraphGisaconnectedacyclicsubgraph(tree)ofGthatincludesallofGsvertices.Note:aspan

10、ningtreewithnverticeshasexactlyn-1edges.MinimumSpanningTreeofaweighted,connectedgraphGisaspanningtreeofGofminimumtotalweight.Example:,MSTProblem,Givenaconnected,undirected,weightedgraphG=(V,E),findaminimumspanningtreeforit.ComputeMSTthroughBruteForce?Kruskal:1956,Prim:1957,BruteforceGenerateallpossi

11、blespanningtreesforthegivengraph.Findtheonewithminimumtotalweight.FeasibilityofBruteforcePossibletoomanytrees(exponentialfordensegraphs),ThePrimsAlgorithm,IdeaofthePrimsalgorithmPseudo-codeofthealgorithmCorrectnessofthealgorithm(important)Timecomplexityofthealgorithm,IdeaofPrim,Growasingletreebyrepe

12、atedlyaddingtheleastcostedge(greedystep)thatconnectsavertexintheexistingtreetoavertexnotintheexistingtreeIntermediatesolutionisalwaysasubtreeofsomeminimumspanningtree.,PrimsMSTalgorithm,Startwithatree,T0,consistingofonevertex“Grow”treeonevertex/edgeatatimeConstructaseriesofexpandingsubtreesT1,T2,Tn-

13、1.AteachstageconstructTifromTi-1byaddingtheminimumweightedgethatconnectingavertexintree(Ti-1)toavertexnotyetinthetreethisisthe“greedy”step!Algorithmstopswhenallverticesareincluded,PseudocodeofthePrim,ALGORITHMPrim(G)/Primsalgorithmforconstructingaminimumspanningtree/Input:AweightedconnectedgraphG=(V

14、,E)/Output:ET,thesetofedgescomposingaminimumspanningtreeofGVTv0/v0canbearbitrarilyselectedETfori1to|V|-1dofindaminimum-weightedgee*=(v*,u*)amongalltheedges(v,u)suchthatvisinVTanduisinV-VTVTVTu*ETETe*returnET,Anexample,ThePrimsalgorithmisgreedy!,Thechoiceofedgesaddedtocurrentsubtreesatisfyingthethree

15、propertiesofgreedyalgorithms.Feasible,eachedgeaddedtothetreedoesnotresultinacycle,guaranteethatthefinalETisaspanningtreeLocaloptimal,eachedgeselectedtothetreeisalwaystheonewithminimumweightamongalltheedgescrossingVTandV-VTIrrevocable,onceanedgeisaddedtothetree,itisnotremovedinsubsequentsteps.,Correc

16、tnessofPrim,ProvebyinductionthatthisconstructionprocessactuallyyieldsMST.T0isasubsetofallMSTsSupposethatTi-1isasubsetofsomeMSTT,weshouldprovethatTiwhichisgeneratedfromTi-1isalsoasubsetofsomeMST.Bycontradiction,assumethatTidoesnotbelongtoanyMST.Letei=(u,v)betheminimumweightedgefromavertexinTi-1toaver

17、texnotinTi-1usedbyPrimsalgorithmtoexpandingTi-1toTi,accordingtoourassumption,eicannotbelongtoMSTT.AddingeitoTresultsinacyclecontaininganotheredgee=(u,v)connectingavertexuinTi-1toavertexvnotinit,andw(e)w(ei)accordingtothegreedyPrimsalgorithm.RemovingefromTandaddingeitoTresultsinanotherspanningtreeTwi

18、thweightw(T)w(T),indicatingthatTisaminimumspanningtreeincludingTiwhichcontradicttoassumptionthatTidoesnotbelongtoanyMST.,CorrectnessofPrim,ImplementationofPrim,HowtoimplementthestepsinthePrimsalgorithm?Firstidea,labeleachvertexwitheither0or1,1representsthevertexinVT,and0otherwise.Traversetheedgesett

19、ofindanminimumweightedgewhoseendpointshavedifferentlabels.Timecomplexity:O(VE)ifadjacencylinkedlistandO(V3)foradjacencymatrixForsparsegraphs,useadjacencylinkedlistAnyimprovement?,Notations,T:theexpandingsubtree.Q:theremainingvertices.Ateachstage,thekeypointofexpandingthecurrentsubtreeTistoDeterminew

20、hichvertexinQisthenearestvertextoT.Qcanbethoughtofasapriorityqueue:Thekey(priority)ofeachvertex,keyv,meanstheminimumweightedgefromvtoavertexinT.KeyvisifvisnotlinkedtoanyvertexinT.Themajoroperationistotofindanddeletethenearestvertex(v,forwhichkeyvisthesmallestamongallthevertices)Removethenearestverte

21、xvfromQandadditandthecorrespondingedgetoT.Withtheoccurrenceofthataction,thekeyofvsneighborswillbechanged.,ToremembertheedgesoftheMST,anarrayisintroducedtorecordtheparentofeachvertex.ThatisvisthevertexintheexpandingsubtreeTthatisclosesttovnotinT.,AdvancedPrimsAlgorithm,ALGORITHMMST-PRIM(G,w,r)/w:weig

22、ht;r:root,thestartingvertexforeachuVGdokeyuuNIL/u:theparentofukeyr0QVG/Nowthepriorityqueue,Q,hasbeenbuilt.whileQdouExtract-Min(Q)/removethenearestvertexfromQforeachvAdju/updatethekeyforeachofvsadjacentnodes.doifvQandw(u,v)keyvthenvukeyvw(u,v),TimeComplexityofPrimsalgorithm,Needpriorityqueueforlocati

23、ngthenearestvertexUseunorderedarraytostorethepriorityqueue:Efficiency:(n2)Usebinarymin-heaptostorethepriorityqueueEfficiency:Forgraphwithnverticesandmedges:O(mlogn)UseFibonacci-heaptostorethepriorityqueue:Efficiency:Forgraphwithnverticesandmedges:O(nlogn+m),KruskalsMSTAlgorithm,EdgesareinitiallysortedbyincreasingweightStartwithanemptyforestF0“grow”MSToneedgeatatimethroughaseriesofexpandingforestsF1,F2,Fn-1intermediatestagesusuallyhaveforestoftrees(notconnected)ateachstageaddminimumweightedgeamo

温馨提示

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

评论

0/150

提交评论