




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 金融行業(yè)辦公自動(dòng)化與效率提升策略
- 足浴店客戶留存策略客戶關(guān)系管理的新篇章
- 跨境電商平臺(tái)的客戶服務(wù)與售后支持體系構(gòu)建
- 金融安全文化的推廣與教育創(chuàng)新
- 音樂(lè)APP的界面視覺(jué)設(shè)計(jì)與聽(tīng)覺(jué)體驗(yàn)結(jié)合
- 遠(yuǎn)程教育興起及行業(yè)前景展望
- 跨文化團(tuán)隊(duì)中的溝通與沖突解決策略研究
- 跨區(qū)域醫(yī)療健康信息平臺(tái)建設(shè)研究
- 高中語(yǔ)文情感美文雪山上的父親
- 通過(guò)智能算法實(shí)現(xiàn)以數(shù)據(jù)為核心的營(yíng)銷閉環(huán)在社區(qū)銀行的實(shí)踐研究
- 酸堿滴定分析與討論實(shí)驗(yàn)報(bào)告
- 2024年邵陽(yáng)職業(yè)技術(shù)學(xué)院?jiǎn)握新殬I(yè)適應(yīng)性測(cè)試題庫(kù)完美版
- 2024年湖南理工職業(yè)技術(shù)學(xué)院?jiǎn)握新殬I(yè)適應(yīng)性測(cè)試題庫(kù)必考題
- 中國(guó)風(fēng)川劇戲曲京劇文化傳統(tǒng)文化國(guó)粹世界戲劇日活動(dòng)策劃完整課件兩篇
- (正式版)JTT 1495-2024 公路水運(yùn)危險(xiǎn)性較大工程安全專項(xiàng)施工方案審查規(guī)程
- 醫(yī)院dip付費(fèi)績(jī)效考核制度
- 20G520-1-2鋼吊車梁(6m-9m)2020年合訂本
- 電梯維護(hù)保養(yǎng)規(guī)則(TSG T5002-2017)
- 義務(wù)教育數(shù)學(xué)課程標(biāo)準(zhǔn)(2022年版)解讀與案例分析
- 植物營(yíng)養(yǎng)學(xué)課件
- 體育概論課外體育活動(dòng)
評(píng)論
0/150
提交評(píng)論