浙江大学DS05Ch10aA高级数据结构.ppt

上传人:max****ui 文档编号:11545509 上传时间:2020-04-28 格式:PPT 页数:12 大小:450.50KB
返回 下载 相关 举报
浙江大学DS05Ch10aA高级数据结构.ppt_第1页
第1页 / 共12页
浙江大学DS05Ch10aA高级数据结构.ppt_第2页
第2页 / 共12页
浙江大学DS05Ch10aA高级数据结构.ppt_第3页
第3页 / 共12页
点击查看更多>>
资源描述
CHAPTER10ALGORITHMDESIGNTECHNIQUES,1GreedyAlgorithms,OptimizationProblems:Givenasetofconstrainsandanoptimizationfunction.Solutionsthatsatisfytheconstrainsarecalledfeasiblesolutions.Afeasiblesolutionforwhichtheoptimizationfunctionhasthebestpossiblevalueiscalledanoptimalsolution.,TheGreedyMethod:Makethebestdecisionateachstage,undersomegreedycriterion.Adecisionmadeinonestageisnotchangedinalaterstage,soeachdecisionshouldassurefeasibility.,1/12,1GreedyAlgorithms,2/12,Note:Greedyalgorithmworksonlyifthelocaloptimumisequaltotheglobaloptimum.Greedyalgorithmdoesnotguaranteeoptimalsolutions.However,itgenerallyproducessolutionsthatareverycloseinvalue(heuristics)totheoptimal,andhenceisintuitivelyappealingwhenfindingtheoptimalsolutiontakestoomuchtime.,1GreedyAlgorithms,1.HuffmanCodesforfilecompression,ExampleSupposeourtextisastringoflength1000thatcomprisesthecharactersa,u,x,andz.Thenitwilltake?bitstostorethestringas1000one-bytecharacters.,8000,Noticethatwehaveonly4distinctcharactersinthatstring.Henceweneedonly2bitstoidentifythem.,Wemayencodethesymbolsasa=00,u=01,x=10,z=11.Forexample,aaaxuaxzisencodedas0000001001001011.Thenthespacetakenbythestringwithlength1000willbe2000bits+spaceforcodetable./*logCbitsareneededinastandardencodingwhereCisthesizeofthecharacterset*/,frequency:=numberofoccurrencesofasymbol.,Instringaaaxuaxz,f(a)=4,f(u)=1,f(x)=2,f(z)=1.,Thesizeofthecodedstringcanbereducedusingvariable-lengthcodes,forexample,a=0,u=110,x=10,z=111.,3/12,Discussion9:InwhatcasethattherearenotlikelytobeanysavingsevenusingHuffmancodes?,1GreedyAlgorithms,Representationoftheoriginalcodeinabinarytree/*trie*/,IfcharacterCiisatdepthdiandoccursfitimes,thenthecostofthecode=difi.,Cost(aaaxuaxz0000001001001011)=24+21+22+21=16,Representationoftheoptimalcodeinabinarytree,Cost(aaaxuaxz00010110010111)=14+31+22+31=14,Theanswerisaaaxuaxz(witha=0,u=110,x=10,z=111).Whatmakesthisdecodingmethodwork?,Thetrickis:Nocodeisaprefixofanother.,Now,witha=0,u=110,x=10,z=111andthestring00010110010111,canyoudecodeit?,4/12,Discussion10:Whatmustthetreelooklikeifwearetodecodeunambiguously?,1GreedyAlgorithms,HuffmansAlgorithm(1952),voidHuffman(PriorityQueueheap,intC)considertheCcharactersasCsinglenodebinarytrees,andinitializethemintoaminheap;for(i=1;iC;i+)createanewnode;/*begreedyhere*/deleterootfromminheapandattachittoleft_childofnode;deleterootfromminheapandattachittoright_childofnode;weightofnode=sumofweightsofitschildren;/*weightofatree=sumofthefrequenciesofitsleaves*/insertnodeintominheap;,T=O(?),ClogC,5/12,1GreedyAlgorithms,Cost=310+215+212+53+44+213+51=146,6/12,ResearchProject4HuffmanCodes(30),AsaprofessorwhogivesthefinalexamproblemonHuffmancodes,Iamencounteringabigproblem:theHuffmancodesareNOTunique.Thestudentsaresubmittingallkindsofcodes,andIneedacomputerprogramtohelpmedeterminewhichonesarecorrectandwhichonesarenot.Detailedrequirementscanbedownloadedfrom,7/12,2.ASimpleSchedulingProblem,GivenNjobsj1,j2,jN,theirrunningtimest1,t2,tN,andoneprocessor.,Schedulejobstominimizetheaveragecompletiontime./*assumenonpreemptivescheduling*/,TheSingleProcessorCase,1GreedyAlgorithms,8/12,1GreedyAlgorithms,Example,Schedule1,Tavg=(15+23+26+36)/4=25,Schedule2,Tavg=(3+11+21+36)/4=17.75,Ingeneral:,9/12,Bestschedulingistomaketiknondecreasing.,1GreedyAlgorithms,TheMultiprocessorCaseNjobsonPprocessors,AnOptimalSolution,MinimizingtheFinalCompletionTime,NPHard,10/12,1GreedyAlgorithms,FlowShopSchedulingasimplecasewith2processors,Consideramachineshopthathas2identicalprocessorsP1andP2.WehaveNjobsJ1,.,JNthatneedtobeprocessed.EachjobJicanbebrokeninto2tasksji1andji2.,Ascheduleisanassignmentofjobstotimeintervalsonmachinessuchthat,jijmustbeprocessedbyPjandtheprocessingtimeistij.,Nomachineprocessesmorethanonejobatanytime.,ji2maynotbestartedbeforeji1isfinished.,Constructaminimum-completion-time2machinescheduleforagivensetofNjobs.,Letai=ti10,andbi=ti2.,ExampleGivenN=4,T1234=?,40,11/12,Discussion11:Whatifai=0?,1GreedyAlgorithms,【Proposition】Anoptimalscheduleexistsifminbi,ajminbj,aiforanypairofadjacentjobsJiandJj.Allthescheduleswiththispropertyhavethesamecompletiontime.,AlgorithmSorta1,.,aN,b1,.,bNintonon-decreasingsequence;m=minimumelement;doif(m=ai),ExampleGivenN=4,T=O(NlogN),12/12,Sortingb2a1a2b1a3b3a4b4,J2,J1,J3,J4,T1342=?,38,
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 课件教案


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

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


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