版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Recursion The procedure of function call (stack application)rMain programsrrrs procedure 1rstprocedure 2rstprocedure 3Recursion SpecificationsA procedure or a function call itself directory or in-directory.Every recursive process consist of two parts:A smallest base case that is processed without re
2、cursion.A general method that reduces a particular case to one or more of the smaller cases, thereby making progress toward eventually reducing the problem all the way to the base case.Implementation of recursionEstablishing recursion working stack.Recursion -An exampleCalculate the factorials funct
3、ion of a positive integer n. 1 if n=0 n!= n*(n-1)! if n0Recursion -An example int factoria(int n)/* pre: n is a nonnegative integer Post: Return the value of the factorial of n. */ if(n=0) return 1; else return n*factorial(n-1);analyzing of recursion executionvoid print(int w) int i; if ( w=0) cout1
4、endl; else print(w-1); for(i=1;i=w;+i) coutw“, “1, move (n-1) disks from tower A to tower B first and then move the nth disk from A to C.Convert the problem from n disks Hanoi problem to (n-1) disks Hanoi problem until to 1 disk Hanoi problem. The Tower of HanoiMove(63,1,2,3) /move 63 disks from /to
5、wer 1 to 2 (tower 3 as temporary)Cout “Move disk 64 from tower 1 to tower 3. “endl;Move(63,2,3,1) /move 63 disks from /tower 2 to 3 (tower 1 as temporary)executionKeeping a recursion working stack to store: formal parameters n,x,y,z and return address.Return address is represented using line number.
6、n x y z return address main() int m; coutm; coutm“ disksn”; hanoi(m,A,B,C); void hanoi(int n,char x,char y,char z) /x: start,y:temp,z:finish(1) (2) if(n=1)(3) move(1,x,z);(4) else(5) hanoi(n-1,x,z,y);(6) move(n,x,z);(7) hanoi(n-1,y,x,z);(8) ABC1233 A B C 03 A B C 02 A C B 63 A B C 02 A C B 61 A B C
7、6ABC3 A B C 02 A C B 6main() int m; coutm; coutm“ disksn”; hanoi(m,A,B,C); void hanoi(int n,char x,char y,char z)(1) (2) if(n=1)(3) move(1,x,z);(4) else(5) hanoi(n-1,x,z,y);(6) move(n,x,z);(7) hanoi(n-1,y,x,z);(8) (9) ABC3 A B C 02 A C B 61 C A B 8ABC3 A B C 02 A C B 63 A B C 03 A B C 02 A C B 6 mai
8、n() int m; coutm; coutm“ disksn”; hanoi(m,A,B,C); void hanoi(int n,char x,char y,char z)(1) (2) if(n=1)(3) move(1,x,z);(4) else(5) hanoi(n-1,x,z,y);(6) move(n,x,z);(7) hanoi(n-1,y,x,z);(8) (9) ABC3 A B C 02 B A C 83 A B C 02 B A C 81 B C A 6ABC3 A B C 02 B A C 83 A B C 0main() int m; coutm; coutm“ d
9、isksn”; hanoi(m,A,B,C);void hanoi(int n,char x,char y,char z)(1) (2) if(n=1)(3) move(1,x,z);(4) else(5) hanoi(n-1,x,z,y);(6) move(n,x,z);(7) hanoi(n-1,y,x,z);(8) (9) ABC3 A B C 02 B A C 81 A B C 8ABC3 A B C 02 B A C 83 A B C 0Stack empty3 A B C 02 B A C 8Principles of recursion(1)Find the key step:
10、Begin by asking yourself “How can this problem be divided into parts?”. Be sure to keep your answer simple but generally applicable.Find the stopping ruleThe stopping rule indicates that the problem or a suitable part of it is done.Outline your algorithm.Combine the stopping rule and the key step, u
11、sing an if statement to select between them. You should now be able to write the main program and a recursive function that will describe how to carry the key step through until the stopping rule applies.Principles of recursion(2)Check termination.Verification that the recursion will always terminat
12、e. Start with a general situation and check that, in a finite number of steps, the stopping rule will be satisfied and the recursion terminate.Draw a recursion tree.The key tool for the analysis of recursive algorithms is the recursion tree. The height of the tree is closely related to the amount of
13、 memory that the program will require and the size of the tree reflects the number of times the key step will be done and hence the total time the program will use.When not to use recursionA recursive function can accomplish exactly the same tasks as an iterative function using a stack. E.g.Int fact
14、orial(int n) if(n=0) return 1; Else return n*factorial(n-1);Int factorial(int n) int count, product=1; for(count=1;count=2Int Fibonacci(int n)/* Fibonacci: recursive version */If(n=0) return 0;Else if(n=1) return 1;Else return Fibonacci(n-1)+Fibonacci(n-2)Int Fibonacci(int n)/* Fibonacci: iterative
15、version */ int last_but_one, last_value, current; if(n=0) return 0; else if(n=1) return 1; else last_but_one=0; last_value=1; for(int i =2; I=n; i+) current= last_but_one+ last_value; last_but_one=last_value; last_value=current; Assess of these two algorithmsThe amount of time used by the recursive function to calculate Fn grows exponentially with n.The amount of time used by iterative function is indirect proportion with n.Guidelines and conclusionsConsider the recursion tree:If the tree has a simple form, the iterative version may be better.If it involves duplicate tasks, then da
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年中级银行从业资格《风险管理》押题宝典考试题库附参考答案详解【基础题】
- 2026年企业人力资源管理师之四级人力资源管理师测试卷含答案详解【能力提升】
- 2026年电焊工考证考试押题卷附完整答案详解(易错题)
- 汽车零部件加工手册
- 流水线多岗位技能培训工作手册
- 2025云南基投公司社会招聘12人笔试历年难易错考点试卷带答案解析
- 机床维护与故障排除手册
- 2025中建西部建设股份有限公司招聘笔试历年难易错考点试卷带答案解析
- 2025中国黄金集团建设有限公司矿业分公司招聘28人笔试历年常考点试题专练附带答案详解
- 导游服务规范与旅游安全管理手册
- 2026年4年级下册试卷大全及答案
- 2026年5级起重工试题及答案
- 2026陕西继续教育专业课+答题(3套)试卷及答案
- 2026江苏南京大学档案馆、校史博物馆内勤招聘建设考试参考题库及答案解析
- 2026西安交通大学专职辅导员招聘24人备考题库附答案详解【完整版】
- 海南橡胶行业深度分析报告
- 户外运动协会工作制度
- 煤矿安全隐患排查及整改工作方案
- GB/T 338-2025工业用甲醇
- 2025年中职增材制造(3D打印基础)试题及答案
- 2026年高考全国卷语文考试真题及答案
评论
0/150
提交评论