版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
3.1简介递归算法是由Prof.JohnMcCarthy(麻省理工学院→Stanford大学)提出来的,1970得到应用,那时是出算法的高峰期。今天,几年所有的通用语言都支持递归。递归和归纳法之间的关系非常密切它们以及与它们相关的证明可互相代替。3.2递归例程3.2.1ActivationFrameandRecursiveProcedureCallsThebasicunitofstorageforanindividualprocedureinvocationatruntimeiscalledanactivationframe.Itprovidestoragespacefortheprocedure’slocalvariables,actualparameters,andcompiler“temporaryvariables”,includingthereturnvalue,iftheprocedurereturnsavalue.Italsoprovidesstoragespaceforotherbookkeeping(簿记)needs,suchasreturnaddress,whichtellswhatinstructiontheprogramshouldexecuteafterthisprocedureexits.Thusitprovidesa“frameofreference”inwhichtheprocedureexecutesforthisinvocationonly.Thecompilergeneratescodetoallocationspaceinaregionofstoragecalledtheframestackaspartofthecodethatimplementsaprocedurecall.Thisspaceisreferencedbyaspecialregistercalledtheframepointer,sothatasthisprocedureinvocationexecutes,itknowswhereitslocalvariables,inputparameters,andreturnvaluearestored.Eachprocedureinvocationthatisactivehasauniqueactivationframe.Aprocedureinvocationisactivefromthetimeitisentereduntilitexits.Ifrecursionoccurs,allinvocationsoftherecursiveprocedurethatareactivesimultaneouslyhavedistinctframes.Asaprocedureinvocation(recursiveornot)exits,itsactivationframeisautomaticallyde-allocatedsothatthespacecanbeusedbysomefuturefunctioninvocation.Ahandexecutionofcodethatdepictsthestatesofactivationframesiscalledanactivationtrace.Fibonaccifunctionintfib(intn){
intf,f1,f2;if(n<2)f=n;else{f1=fib(n-1);f2=fib(n-2);f=f1+f2;}returnf;}FIBONACCI递归程程序,其执行(包括激活等)过程见书PP104页。Lemma3.1Inacomputationwithoutwhileorforloops,butpossiblywithrecursiveprocedurecalls,thetimethatanyparticularactivationframeisonthetopoftheframestackisO(L),whereListhenumberoflinesintheprocedurethatcontaineitherasimplestatementoraprocedurecall.Theorem3.2Inacomputationwithoutwhileorforloops,butpossiblywithrecursiveprocedurecalls,thetotalcomputationtimeis(C),whereCisthetotalnumberofprocedurecalls(includefunctioncallsasprocedurecalls)thatoccurduringthecomputation.ActivationTreeExampleandFigureofActivationTreeTheparentofeachothernodeisjustthenodewhoseactivationframewastopoftheframeframestackatthetimethisonewascreated.Thechildrenofeachnodeappearlefttorightintheorderinwhichtheiractivationframeswerecreated.P106Apreordertraversaloftheactivationtreevisitseachactivationframeinorderofitscreation,andthenumberofnodesinthetreeisproportionaltothetotalexecutiontime.3.2.2HintsforRecursion–Method991.procedurep假定理想情况下问题的规模n在0..100之间,即n至多为100.subroutine,p99,除假定理想情况下问题的规模在0..99之间外,其它情况(前置条件、参数、代码等)和p完全一样。3.2.2HintsforRecursion–Method992.写出程序的非递归部分,使之尽可能小。特别注意边界条件3最终,测试,选择一个规模,不必是100,可以是一个小的常数。Recursion注意点Thesubproblemsizeislessthanp’sproblemsize.Thesubproblemsizeisnotbelowtheminimum(0,forthisdiscussion)3.Thesubproblemsatisfiesallotherpreconditionofp99(whicharethesameasthepreconditionofp)例问题:删除表L中第一个出现的元素x,表可能为空表表L中有100,有一个子程序99可用,如果我们能设法使问题即表中的元素个数减小一个,则便可。第二个提示:L可能是空表;还有一种情况,x可能出现在表L中第一个位置程序IntListdelete(IntListL,intx){
IntList
newL,fixedL;if(L==nil)
newL=L;elseif(x==first(L)
newL=rest(L);else{
fixedl=delete99(rest(L),x);
newL=cons(first(L),fixedL);}returnnewL;}
3.3WhatIsaProofxW[A(x)C(x)]几个简单的结论及其证明参见P109双栏证明格式P1104数学归纳法例用数学归纳法证明:P113InductionproofschemaxW[A(x)C(x)]包含下列几个部分:Theproofisbyinductiononx,<descriptionofx>Thebasecaseis…<base-case><Proofofgoalstatementwithbase-casesubstitutedintoit,thatis,C(base-case)>For<x>greaterthan<base-case>,assumethat[A(x)C(x)]holdsforally∈Wsuchthaty<x<Proofofthegoalstatement,C(x),exactlyasitappearsintheproposition>
InductionproofonaRecursiveProcedureDefinition3.2Externalnodesand2-treesAtreethatconsistsofanexternalnodeiscalledaleaf,anditdoesnothaveanysubtrees.Theothertypeofnodeiscalledaninternalnode,itmusthavetwochildren.!?Suchbinarytreesarecalled2-treesbecauseeachnodehastwochildrenornochildren.Definition3.2ExternalpathlengthIna2-treet,theexternalpathlengthoftisthesumofthelengthsofallthepathsfromtherootofttoanyexternalnodet.Thelengthpathlengthofapathisthenumberofitsedges.Alternatively,theexternalpathlengthcanbedefinedinductivelyasfollows:Theexternalpathlengthofaleafis0.Lettbeanonleaf,withleftsubtreeLandrightsubtreeR(eithermaybealeaf).Theexternalpathlengthoftisthe外部长度ofL+外结点数ofL+外部长度ofR+外结点数ofRLemma3.7Lettbeany2-tree.LeteplandmbethevaluesofthefieldeplandextNumrespectively,asreturnedbycalcEpl(t).ThenEplistheexternalpathlengthoft.Misthenumberofexternalnodesint.epl≥mlg(m)EplReturn
calcEpl(TwoTreet){
EplReturn
ansL,ansR;//returnedfromsubtrees
EplReturn
ans=newEplReturn();//toreturnif(tisaleaf){ans.epl=0;ans.extNum=1;}else{
ansL=calcEpl(leftSubtree(t));
ansR=calcEpl(rightSubtree(t));ans.epl=ansLepl+ansR.epl+ansL.extNum+ansR.extNum;
ans.extNum=ansL.extNum+ansR.extNum;}returnans;}用数学归纳法来证明定理推论3.8Theexternalpathlengtheplofa2-treewithninternalnodeshasthelowerbound:epl≥(n+1)lg(n+1)Proof:Every2-treewithninternalnodeshas(n+1)externalnodes.3.5证明程序的正确性3.5.1定义和术语Ablockisasectionofcodewithoneentrypointandoneexitpoint.Aprocedureisablockwithaname,soitcanbecalled.Itusuallyhasparameters,whicharedesignatedeitherasinputoroutput.Noparametersalsoisbothinputandoutput.Afunction,Localdata,Globaldata,Reference3.5.2基本控制结构顺序分支Procedurecall如果没使用循环语句,可以吗?YES!使用递归来取代循环,可以更简单地写任何一个算法。证明正确性前置条件后置条件specification引理3.9Ifallpreconditonsholdwhentheblockisentered,thenallpostconditionholdwhentheblockexits.引理3.10(sequencecorrectness)1.Thepreconditonsoftheblockimplythepreconditionsofblock1.2.Thepostconditonsofblock1implythepreconditionsofblock2.3.Thepostconditonsofblock2implythepostconditionsofblock.
引理3.11(Alternationcorrectness)1.Thepreconditonsoftheblockandthetruthofconditionimplythepreconditionsoftrueblock.2.Thepostconditionoftrue-blockandthetruthofcondition(atthetimetrue-blockisentered)implythepostconditionoftheblock.1.Thepreconditonsoftheblockandthefalsityofconditionimplythepreconditionsoffalse-block.2.Thepostconditionoffalse-blockandthefalsityofcondition(atthetimefalse-blockisentered)implythepostconditionoftheblock.引理3.12(Procedure-Call)1.Thepreconditonsoftheblockimplythepreconditionsofthecalledprocedurewithitsactualparameters.2.Thepostconditionofthecalledprocedurewithitsactualparametersimplythepostconditionoftheblock.Loop-FreeProcedure顺序搜索递归算法intseqSearchRec(int[]E,intm,intnum,intK){if(m>num)
ans=-1;elseif(E[m]==K)
ans=m;else
ans=seqSearchRec(E,m+1,num,K);returnans
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 菏泽山东菏泽东明县结合事业单位招聘征集部分普通高等院校本科毕业生秋季入伍11人笔试历年参考题库附带答案详解
- 滁州2025年安徽滁州来安县县级公立医院高层次人才引进6人笔试历年参考题库附带答案详解
- 海西2025年青海海南州职业技术学校招聘教师12人笔试历年参考题库附带答案详解
- 河北2025年河北省中医药科学院招聘7人笔试历年参考题库附带答案详解
- 来宾2025年广西来宾市公安局招聘辅警16人笔试历年参考题库附带答案详解
- 成都2025年四川成都武侯区招聘社区工作者和党建服务专员614人笔试历年参考题库附带答案详解
- 广西2025年广西体育局直属事业单位招聘笔试历年参考题库附带答案详解
- 宿州2025年安徽宿州灵璧县招聘编外高中教师70人笔试历年参考题库附带答案详解
- 九江2025年江西师范大学附属柴桑区实验学校(初中部小学部)-实验幼儿园选调教师48人笔试历年参考题库附带答案详解
- 2026-2032年中国集装箱用铝行业市场竞争态势及投资机会研判报告
- 2025年安徽阜阳市人民医院校园招聘42人笔试模拟试题参考答案详解
- 2024~2025学年江苏省扬州市树人集团九年级上学期期末语文试卷
- 2026届江苏省南京溧水区四校联考中考一模物理试题含解析
- 民用建筑热工设计规范
- 2025年黑龙江省公务员《申论(行政执法)》试题(网友回忆版)含答案
- 学堂在线 雨课堂 学堂云 唐宋词鉴赏 期末考试答案
- 2025至2030中国辐射监测仪表市场投资效益与企业经营发展分析报告
- 产品认证标志管理制度
- 广州西关大屋介绍
- 基于机器视觉的SLM金属3D打印设备视觉标定技术研究
- CJ/T 192-2017内衬不锈钢复合钢管
评论
0/150
提交评论