




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年快递绿色运输路线规划操作竞赛考核试卷
- 2025年新能源行业储能系统钒电池效率提升技术优化考核试卷
- 2025年物流数字化转型公共数据开放利用合规考核试卷
- 104.工业机器人故障树分析维护考核试卷
- 2025年中药饮片进口检验标准中医药现代化合规考核试卷
- 小学数学结构化教学的现实路径
- 考点攻克人教版八年级上册物理《物态变化》同步练习试题(解析卷)
- 考点解析人教版八年级物理上册第4章光现象-光的色散章节测试试题
- 综合解析苏科版九年级物理上册《机械能和内能》专题训练试题(解析卷)
- 考点解析人教版八年级物理上册第4章光现象定向测评试卷(解析版含答案)
- 工业皮带专业知识培训课件
- 新生儿患者安全知识培训课件
- 2025至2030全球及中国便携式风扇行业发展趋势分析与未来投资战略咨询研究报告
- 2025年救护车司机驾驶员资格考试考前真题训练题库及答案
- 公路工程重大风险安全管控方案
- 《市场监管部门标识规范》编制说明
- 学校工作汇报会议
- 2025广东深圳市福田区选用劳务派遣人员308人笔试历年参考题库附带答案详解
- 纪委编外考试试题及答案
- 江苏定向考试题目及答案
- 中国民生银行2024年校园招聘考试笔试历年考试真题资料
评论
0/150
提交评论