算法设计与分析基础第三版PPTch06PPT课件_第1页
算法设计与分析基础第三版PPTch06PPT课件_第2页
算法设计与分析基础第三版PPTch06PPT课件_第3页
算法设计与分析基础第三版PPTch06PPT课件_第4页
算法设计与分析基础第三版PPTch06PPT课件_第5页
已阅读5页,还剩39页未读 继续免费阅读

下载本文档

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

文档简介

.,1,TransformandConquer,Thisgroupoftechniquessolvesaproblembyatransformationtoasimpler/moreconvenientinstanceofthesameproblem(instancesimplification)adifferentrepresentationofthesameinstance(representationchange)adifferentproblemforwhichanalgorithmisalreadyavailable(problemreduction),.,2,Instancesimplification-Presorting,Solveaproblemsinstancebytransformingitintoanothersimpler/easierinstanceofthesameproblemPresortingManyproblemsinvolvinglistsareeasierwhenlistissorted,e.g.searchingcomputingthemedian(selectionproblem)checkingifallelementsaredistinct(elementuniqueness)Also:Topologicalsortinghelpssolvingsomeproblemsfordags.Presortingisusedinmanygeometricalgorithms.,.,3,Howfastcanwesort?,Efficiencyofalgorithmsinvolvingsortingdependsonefficiencyofsorting.Theorem(seeSec.11.2):log2n!nlog2ncomparisonsarenecessaryintheworstcasetosortalistofsizenbyanycomparison-basedalgorithm.Note:Aboutnlog2ncomparisonsarealsosufficienttosortarrayofsizen(bymergesort).,.,4,Searchingwithpresorting,Problem:SearchforagivenKinA0.n-1Presorting-basedalgorithm:Stage1SortthearraybyanefficientsortingalgorithmStage2ApplybinarysearchEfficiency:(nlogn)+O(logn)=(nlogn)Goodorbad?Whydowehaveourdictionaries,telephonedirectories,etc.sorted?,.,5,ElementUniquenesswithpresorting,Presorting-basedalgorithmStage1:sortbyefficientsortingalgorithm(e.g.mergesort)Stage2:scanarraytocheckpairsofadjacentelementsEfficiency:(nlogn)+O(n)=(nlogn)BruteforcealgorithmCompareallpairsofelementsEfficiency:O(n2)Anotheralgorithm?Hashing,.,InstancesimplificationGaussianElimination,Given:Asystemofnlinearequationsinnunknownswithanarbitrarycoefficientmatrix.Transformto:Anequivalentsystemofnlinearequationsinnunknownswithanuppertriangularcoefficientmatrix.Solvethelatterbysubstitutionsstartingwiththelastequationandmovinguptothefirstone.a11x1+a12x2+a1nxn=b1a1,1x1+a12x2+a1nxn=b1a21x1+a22x2+a2nxn=b2a22x2+a2nxn=b2an1x1+an2x2+annxn=bnannxn=bn,.,GaussianElimination(cont.),Thetransformationisaccomplishedbyasequenceofelementaryoperationsonthesystemscoefficientmatrix(whichdontchangethesystemssolution):fori1ton-1doreplaceeachofthesubsequentrows(i.e.,rowsi+1,n)byadifferencebetweenthatrowandanappropriatemultipleofthei-throwtomakethenewcoefficientinthei-thcolumnofthatrow0,.,ExampleofGaussianElimination,Solve2x1-4x2+x3=63x1-x2+x3=11x1+x2-x3=-3Gaussianelimination2-4162-4163-1111row2(3/2)*row105-1/2211-1-3row3(1/2)*row103-3/2-6row3(3/5)*row22-41605-1/2200-6/5-36/5Backwardsubstitutionx3=(-36/5)/(-6/5)=6x2=(2+(1/2)*6)/5=1x1=(66+4*1)/2=2,.,PseudocodeandEfficiencyofGaussianElimination,Stage1:Reductiontoanupper-triangularmatrixfori1ton-1doforji+1tondoforkiton+1doAj,kAj,k-Ai,k*Aj,i/Ai,i/improve!Stage2:Backsubstitutionsforjndownto1dot0forkj+1tondott+Aj,k*xkxj(Aj,n+1-t)/Aj,jEfficiency:(n3)+(n2)=(n3),.,10,SearchingProblem,Problem:Givena(multi)setSofkeysandasearchkeyK,findanoccurrenceofKinS,ifanySearchingmustbeconsideredinthecontextof:filesize(internalvs.external)dynamicsofdata(staticvs.dynamic)Dictionaryoperations(dynamicdata):find(search)insertdelete,.,11,TaxonomyofSearchingAlgorithms,ListsearchingsequentialsearchbinarysearchinterpolationsearchTreesearchingbinarysearchtreebinarybalancedtrees:AVLtrees,red-blacktreesmultiwaybalancedtrees:2-3trees,2-3-4trees,BtreesHashingopenhashing(separatechaining)closedhashing(openaddressing),.,12,BinarySearchTree,Arrangekeysinabinarytreewiththebinarysearchtreeproperty:,K,K,Example:5,3,1,10,12,7,9,.,13,DictionaryOperationsonBinarySearchTrees,SearchingstraightforwardInsertionsearchforkey,insertatleafwheresearchterminatedDeletion3cases:deletingkeyataleafdeletingkeyatnodewithsinglechilddeletingkeyatnodewithtwochildrenEfficiencydependsofthetreesheight:log2nhn-1,withheightaverage(randomfiles)beabout3log2nThusallthreeoperationshaveworstcaseefficiency:(n)averagecaseefficiency:(logn)Bonus:inordertraversalproducessortedlist,.,14,BalancedSearchTrees,Attractivenessofbinarysearchtreeismarredbythebad(linear)worst-caseefficiency.Twoideastoovercomeitare:torebalancebinarysearchtreewhenanewinsertionmakesthetree“toounbalanced”AVLtreesred-blacktreestoallowmorethanonekeypernodeofasearchtree2-3trees2-3-4treesB-trees,.,15,Balancedtrees:AVLtrees,DefinitionAnAVLtreeisabinarysearchtreeinwhich,foreverynode,thedifferencebetweentheheightsofitsleftandrightsubtrees,calledthebalancefactor,isatmost1(withtheheightofanemptytreedefinedas-1),Tree(a)isanAVLtree;tree(b)isnotanAVLtree,.,16,Rotations,Ifakeyinsertionviolatesthebalancerequirementatsomenode,thesubtreerootedatthatnodeistransformedviaoneofthefourrotations.(Therotationisalwaysperformedforasubtreerootedatan“unbalanced”nodeclosesttothenewleaf.),SingleR-rotation,DoubleLR-rotation,.,17,Generalcase:SingleR-rotation,.,18,Generalcase:DoubleLR-rotation,.,19,AVLtreeconstruction-anexample,ConstructanAVLtreeforthelist5,6,8,3,2,4,7,.,20,AVLtreeconstruction-anexample(cont.),.,21,AnalysisofAVLtrees,h1.4404log2(n+2)-1.3277averageheight:1.01log2n+0.1forlargen(foundempirically)SearchandinsertionareO(logn)DeletionismorecomplicatedbutisalsoO(logn)Disadvantages:frequentrotationscomplexityAsimilaridea:red-blacktrees(heightofsubtreesisallowedtodifferbyuptoafactorof2),.,22,MultiwaySearchTrees,DefinitionAmultiwaysearchtreeisasearchtreethatallowsmorethanonekeyinthesamenodeofthetree.DefinitionAnodeofasearchtreeiscalledann-nodeifitcontainsn-1orderedkeys(whichdividetheentirekeyrangeintonintervalspointedtobythenodesnlinkstoitschildren):Note:Everynodeinaclassicalbinarysearchtreeisa2-node,k1k2kn-1,k1,k1,k2),kn-1,.,23,2-3Tree,DefinitionA2-3treeisasearchtreethatmayhave2-nodesand3-nodesheight-balanced(allleavesareonthesamelevel)A2-3treeisconstructedbysuccessiveinsertionsofkeysgiven,withanewkeyalwaysinsertedintoaleafofthetree.Iftheleafisa3-node,itssplitintotwowiththemiddlekeypromotedtotheparent.,.,24,2-3treeconstructionanexample,Constructa2-3treethelist9,5,8,3,2,4,7,.,25,Analysisof2-3trees,log3(n+1)-1hlog2(n+1)-1Search,insertion,anddeletionarein(logn)Theideaof2-3treecanbegeneralizedbyallowingmorekeyspernode2-3-4treesB-trees,.,26,HeapsandHeapsort,DefinitionAheapisabinarytreewithkeysatitsnodes(onekeypernode)suchthat:Itisessentiallycomplete,i.e.,allitslevelsarefullexceptpossiblythelastlevel,whereonlysomerightmostkeysmaybemissingThekeyateachnodeiskeysatitschildren,.,27,Illustrationoftheheapsdefinition,aheap,notaheap,notaheap,Note:Heapselementsareorderedtopdown(alonganypathdownfromitsroot),buttheyarenotorderedlefttoright,.,28,SomeImportantPropertiesofaHeap,Givenn,thereexistsauniquebinarytreewithnnodesthatisessentiallycomplete,withh=log2nTherootcontainsthelargestkeyThesubtreerootedatanynodeofaheapisalsoaheapAheapcanberepresentedasanarray,.,29,HeapsArrayRepresentation,Storeheapselementsinanarray(whoseelementsindexed,forconvenience,1ton)intop-downleft-to-rightorderExample:Leftchildofnodejisat2jRightchildofnodejisat2j+1Parentofnodejisatj/2Parentalnodesarerepresentedinthefirstn/2locations,9,1,5,3,4,2,.,30,Step0:InitializethestructurewithkeysintheordergivenStep1:Startingwiththelast(rightmost)parentalnode,fixtheheaprootedatit,ifitdoesntsatisfytheheapcondition:keepexchangingitwithitslargestchilduntiltheheapconditionholdsStep2:RepeatStep1fortheprecedingparentalnode,HeapConstruction(bottom-up),.,31,ExampleofHeapConstruction,Constructaheapforthelist2,9,7,6,5,8,.,32,Pseudopodiaofbottom-upheapconstruction,.,33,Stage1:ConstructaheapforagivenlistofnkeysStage2:Repeatoperationofrootremovaln-1times:Exchangekeysintherootandinthelast(rightmost)leafDecreaseheapsizeby1Ifnecessary,swapnewrootwithlargerchilduntiltheheapconditionholds,Heapsort,.,34,Sortthelist2,9,7,6,5,8byheapsortStage1(heapconstruction)Stage2(root/maxremoval)19765896825729865776825|929865786725|99286575672|899682577652|89265|789625|78952|678952|67892|56789,ExampleofSortingbyHeapsort,.,35,Stage1:Buildheapforagivenlistofnkeysworst-caseC(n)=Stage2:Repeatoperationofrootremovaln-1times(fixheap)worst-caseC(n)=Bothworst-caseandaverage-caseefficiency:(nlogn)In-place:yesStability:no(e.g.,11),2(h-i)2i=2(nlog2(n+1)(n),i=0,h-1,#nodesatleveli,i=1,n-1,2log2i(nlogn),AnalysisofHeapsort,.,36,ApriorityqueueistheADTofasetofelementswithnumericalprioritieswiththefollowingoperations:findelementwithhighestprioritydeleteelementwithhighestpriorityinsertelementwithassignedpriority(seebelow)HeapisaveryefficientwayforimplementingpriorityqueuesTwowaystohandlepriorityqueueinwhichhighestpriority=smallestnumber,PriorityQueue,.,37,InsertionofaNewElementintoaHeap,Insertthenewelementatlastpositioninheap.Compareitwithitsparentand,ifitviolatesheapcondition,exchangethemContinuecomparingthenewelementwithnodesupthetreeuntiltheheapconditionissatisfiedExample:Insertkey10Efficiency:O(logn),.,38,HornersRuleForPolynomialEvaluation,Givenapolynomialofdegreenp(x)=anxn+an-1xn-1+a1x+a0andaspecificvalueofx,findthevalueofpatthatpoint.Twobrute-forcealgorithms:p0pa0;power1forindownto0dofori1tondopower1powerpower*xforj1toidopp+ai*powerpowerpower*xreturnppp+ai*powerreturnp,.,39,HornersRule,Example:p(x)=2x4-x3+3x2+x-5=x(2x3-x2+3x+1)-5=x(x(2x2-x+3)+1)-5=x(x(x(2x-1)+3)+1)-5SubstitutionintothelastformulaleadstoafasteralgorithmSamesequenceofcomputationsareobtainedbysimplyarrangingthecoefficientinatableandproceedingasfollows:coefficients2-131-5x=3,.,40,HornersRulepseudocode,EfficiencyofHornersRule:#multiplications=#additions=nSyntheticdivisionofofp(x)by(x-x0)Example:Letp(x)=2x4-x3+3x2+x-5.Findp(x):(x-3),.,41,Computingan(revisited),Left-to-rightbinaryexponentiationInitializeproductaccumulatorby1.Scannsbinaryexpansionfromlefttorightanddothefollowing:Ifthecurrentbinarydigitis0,squaretheaccumulator(S);ifthebinarydigitis1,squaretheaccumulatorandmultiplyitbya(SM).Example:Computea13.Here,n=13=11012binaryrep.of13:1101SMSMSSMaccumula

温馨提示

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

评论

0/150

提交评论