SlideShare a Scribd company logo
1 of 19
Recursive Functions ,[object Object],[object Object]
Function Analysis for call  fib(5) fib(5) fib(4) fib(3) fib(3) fib(2) fib(1) fib(0) fib(2) fib(1) fib(0) fib(1) fib(2) fib(1) fib(0) fib(1) public static int  fib( int  n) if  (n == 0 || n == 1) return  n else return  fib(n-1) + fib(n-2) 1 1 1 1 1 0 0 0 1 1 2 1 3 2 5
Recursive Function Calls on the Stack ,[object Object],[object Object],[object Object],[object Object],[object Object]
Anatomy of a Recursive Function ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Finding Recursive Solutions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Steps Leading to Recursive Solutions ,[object Object],[object Object],[object Object],[object Object]
Designing a recursive solution: Writing a String Backward ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Designing a recursive solution: Writing a String Backward ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Designing a recursive solution: Writing a String Backward ,[object Object],[object Object],[object Object],[object Object],[object Object]
Box trace of  writeBackward(“cat”, 3) The initial call is made, and the method begins execution: s = “cat” size = 3 s = “cat” size = 3 s = “cat” size = 2 s = “cat” size = 1 s = “cat” size = 2 s = “cat” size = 3 A A A Outputline:  ta Point A is reached, and the recursive call is made. The new invocation begins execution: Outputline:  t Point A ( writeBackward(s, size-1))  is reached, and the recursive call is made. The new invocation begins execution:
Box trace of  writeBackward(“cat”, 3) Output line:  tac Point A is reached, and the recursive call is made. The new evocation begins execution: s = “cat” size = 3 s = “cat” size = 2 s = “cat” size = 1 s = “cat” size = 0 s = “cat” size = 0 s = “cat” size = 1 s = “cat” size = 2 s = “cat” size = 3 A A A A A A This is the base case, so this invocation completes. Control returns to the calling box, which continues execution:
Box trace of  writeBackward(“cat”, 3) This invocation completes. Control returns to the calling box, which continues execution: s = “cat” size = 3 s = “cat” size = 2 s = “cat” size = 1 s = “cat” size = 0 s = “cat” size = 0 s = “cat” size = 1 s = “cat” size = 2 s = “cat” size = 3 A A A A A A This invocation completes. Control returns to the calling box, which continues execution: This invocation completes. Control returns to the statement following the initial call.
A recursive definition of factorial  n! ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
return 2*fact(1) 2* ? Figure 2.2 fact(3) System.out.println(fact(3)); ? return 3*fact(2) 3*? return 1*fact(0) 1* ? return 1 6 1 1 2
Figure 2.3 A box
Figure 2.4 The beginning of the box trace
Figure 2.5a Box trace of  fact(3)
Figure 2.5b Box trace of  fact(3)
Figure 2.5c Box trace of  fact(3)

More Related Content

What's hot

What's hot (20)

Rsa encryption
Rsa encryptionRsa encryption
Rsa encryption
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Recursion
RecursionRecursion
Recursion
 
40+ examples of user defined methods in java with explanation
40+ examples of user defined methods in java with explanation40+ examples of user defined methods in java with explanation
40+ examples of user defined methods in java with explanation
 
Assignment week0 c++
Assignment  week0 c++Assignment  week0 c++
Assignment week0 c++
 
Recursion
RecursionRecursion
Recursion
 
Recursion for GCE AS Computing
Recursion for GCE AS ComputingRecursion for GCE AS Computing
Recursion for GCE AS Computing
 
User Defined Functions in C Language
User Defined Functions   in  C LanguageUser Defined Functions   in  C Language
User Defined Functions in C Language
 
Mid term
Mid termMid term
Mid term
 
Symmetric ciphers questions and answers
Symmetric ciphers questions and answersSymmetric ciphers questions and answers
Symmetric ciphers questions and answers
 
Fibonacci using matlab
Fibonacci using matlabFibonacci using matlab
Fibonacci using matlab
 
Modern Block Cipher- Modern Symmetric-Key Cipher
Modern Block Cipher- Modern Symmetric-Key CipherModern Block Cipher- Modern Symmetric-Key Cipher
Modern Block Cipher- Modern Symmetric-Key Cipher
 
Recursion(Advanced data structure)
Recursion(Advanced data structure)Recursion(Advanced data structure)
Recursion(Advanced data structure)
 
Vb.net ii
Vb.net iiVb.net ii
Vb.net ii
 
Computer Science Assignment Help
Computer Science Assignment HelpComputer Science Assignment Help
Computer Science Assignment Help
 
Computer Science Programming Assignment Help
Computer Science Programming Assignment HelpComputer Science Programming Assignment Help
Computer Science Programming Assignment Help
 
Chapter 9 divide and conquer handouts with notes
Chapter 9   divide and conquer handouts with notesChapter 9   divide and conquer handouts with notes
Chapter 9 divide and conquer handouts with notes
 
Reconstruction
ReconstructionReconstruction
Reconstruction
 
Paper on Optimized AES Algorithm Core Using FeedBack Architecture
Paper on Optimized AES Algorithm Core Using  FeedBack Architecture Paper on Optimized AES Algorithm Core Using  FeedBack Architecture
Paper on Optimized AES Algorithm Core Using FeedBack Architecture
 
Network security CS6
Network security   CS6Network security   CS6
Network security CS6
 

Viewers also liked

แบบอ้างอิง Osi
แบบอ้างอิง Osiแบบอ้างอิง Osi
แบบอ้างอิง Osi
Pituk Sense
 
Picasa
PicasaPicasa
Picasa
nok_bb
 
Расчет настоящего уровня инфляции
Расчет настоящего уровня инфляцииРасчет настоящего уровня инфляции
Расчет настоящего уровня инфляции
alexeykonkov
 
Inanimate Alice episode 5 by Ian and Henry
Inanimate Alice episode 5 by Ian and Henry Inanimate Alice episode 5 by Ian and Henry
Inanimate Alice episode 5 by Ian and Henry
MrsPrentice
 
De Wijkonderneming Terugblik En Resultatenoverzicht 2010.
De Wijkonderneming Terugblik En Resultatenoverzicht 2010.De Wijkonderneming Terugblik En Resultatenoverzicht 2010.
De Wijkonderneming Terugblik En Resultatenoverzicht 2010.
Mieke Sanden, van der
 
English janlokpal (1)
English janlokpal (1)English janlokpal (1)
English janlokpal (1)
Mb Narayanan
 
ACC 626 - Forensics for IT
ACC 626 - Forensics for ITACC 626 - Forensics for IT
ACC 626 - Forensics for IT
j9lai
 
Angical fest 2011 2º dia
Angical fest 2011 2º diaAngical fest 2011 2º dia
Angical fest 2011 2º dia
angical-piaui
 
Win pcap filtering expression syntax
Win pcap  filtering expression syntaxWin pcap  filtering expression syntax
Win pcap filtering expression syntax
Vota Ppt
 

Viewers also liked (20)

แบบอ้างอิง Osi
แบบอ้างอิง Osiแบบอ้างอิง Osi
แบบอ้างอิง Osi
 
Picasa
PicasaPicasa
Picasa
 
Biz ideas v5
Biz ideas v5Biz ideas v5
Biz ideas v5
 
Расчет настоящего уровня инфляции
Расчет настоящего уровня инфляцииРасчет настоящего уровня инфляции
Расчет настоящего уровня инфляции
 
Inanimate Alice episode 5 by Ian and Henry
Inanimate Alice episode 5 by Ian and Henry Inanimate Alice episode 5 by Ian and Henry
Inanimate Alice episode 5 by Ian and Henry
 
Supporting Apache Brands While Making A Profit - v2.0b
Supporting Apache Brands While Making A Profit - v2.0bSupporting Apache Brands While Making A Profit - v2.0b
Supporting Apache Brands While Making A Profit - v2.0b
 
Islamic Religion
Islamic  ReligionIslamic  Religion
Islamic Religion
 
De Wijkonderneming Terugblik En Resultatenoverzicht 2010.
De Wijkonderneming Terugblik En Resultatenoverzicht 2010.De Wijkonderneming Terugblik En Resultatenoverzicht 2010.
De Wijkonderneming Terugblik En Resultatenoverzicht 2010.
 
Bio outsource hcp
Bio outsource hcpBio outsource hcp
Bio outsource hcp
 
Lugares turisticos del mundo
Lugares turisticos del mundoLugares turisticos del mundo
Lugares turisticos del mundo
 
English janlokpal (1)
English janlokpal (1)English janlokpal (1)
English janlokpal (1)
 
Chapter5
Chapter5Chapter5
Chapter5
 
Gerbang Logika
Gerbang LogikaGerbang Logika
Gerbang Logika
 
Test
TestTest
Test
 
Materi Dasar PHP
Materi Dasar PHPMateri Dasar PHP
Materi Dasar PHP
 
Pagenotes
PagenotesPagenotes
Pagenotes
 
ACC 626 - Forensics for IT
ACC 626 - Forensics for ITACC 626 - Forensics for IT
ACC 626 - Forensics for IT
 
Angical fest 2011 2º dia
Angical fest 2011 2º diaAngical fest 2011 2º dia
Angical fest 2011 2º dia
 
Win pcap filtering expression syntax
Win pcap  filtering expression syntaxWin pcap  filtering expression syntax
Win pcap filtering expression syntax
 
Calendario 2012
Calendario 2012Calendario 2012
Calendario 2012
 

Similar to 5 2

Sample Exam Questions on Python for revision
Sample Exam Questions on Python for revisionSample Exam Questions on Python for revision
Sample Exam Questions on Python for revision
afsheenfaiq2
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
josies1
 
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
Sure interview algorithm-1103
Sure interview algorithm-1103Sure interview algorithm-1103
Sure interview algorithm-1103
Sure Interview
 

Similar to 5 2 (20)

algo_vc_lecture8.ppt
algo_vc_lecture8.pptalgo_vc_lecture8.ppt
algo_vc_lecture8.ppt
 
Sample Exam Questions on Python for revision
Sample Exam Questions on Python for revisionSample Exam Questions on Python for revision
Sample Exam Questions on Python for revision
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
 
Recursion.pdf
Recursion.pdfRecursion.pdf
Recursion.pdf
 
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting class
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)
 
NPTEL QUIZ.docx
NPTEL QUIZ.docxNPTEL QUIZ.docx
NPTEL QUIZ.docx
 
Recursion.ppt
 Recursion.ppt Recursion.ppt
Recursion.ppt
 
Hive function-cheat-sheet
Hive function-cheat-sheetHive function-cheat-sheet
Hive function-cheat-sheet
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Recursion vs. Iteration: Code Efficiency & Structure
Recursion vs. Iteration: Code Efficiency & StructureRecursion vs. Iteration: Code Efficiency & Structure
Recursion vs. Iteration: Code Efficiency & Structure
 
Unit-I Recursion.pptx
Unit-I Recursion.pptxUnit-I Recursion.pptx
Unit-I Recursion.pptx
 
1.2 matlab numerical data
1.2  matlab numerical data1.2  matlab numerical data
1.2 matlab numerical data
 
lecture4-recursion.pptx
lecture4-recursion.pptxlecture4-recursion.pptx
lecture4-recursion.pptx
 
Tower of Hanoi.ppt
Tower of Hanoi.pptTower of Hanoi.ppt
Tower of Hanoi.ppt
 
B61301007 matlab documentation
B61301007 matlab documentationB61301007 matlab documentation
B61301007 matlab documentation
 
Sure interview algorithm-1103
Sure interview algorithm-1103Sure interview algorithm-1103
Sure interview algorithm-1103
 
6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx
 
Recursion - Computer Algorithms
Recursion - Computer AlgorithmsRecursion - Computer Algorithms
Recursion - Computer Algorithms
 

More from FALLEE31188 (20)

Lecture4
Lecture4Lecture4
Lecture4
 
Lecture2
Lecture2Lecture2
Lecture2
 
L16
L16L16
L16
 
L2
L2L2
L2
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Functions
FunctionsFunctions
Functions
 
Field name
Field nameField name
Field name
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
Cis068 08
Cis068 08Cis068 08
Cis068 08
 
Chapter14
Chapter14Chapter14
Chapter14
 
Chapt03
Chapt03Chapt03
Chapt03
 
C++lecture9
C++lecture9C++lecture9
C++lecture9
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
C1320prespost
C1320prespostC1320prespost
C1320prespost
 
Brookshear 06
Brookshear 06Brookshear 06
Brookshear 06
 
Book ppt
Book pptBook ppt
Book ppt
 
Assignment 2
Assignment 2Assignment 2
Assignment 2
 

Recently uploaded

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Krashi Coaching
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
fonyou31
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Recently uploaded (20)

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 

5 2

  • 1.
  • 2. Function Analysis for call fib(5) fib(5) fib(4) fib(3) fib(3) fib(2) fib(1) fib(0) fib(2) fib(1) fib(0) fib(1) fib(2) fib(1) fib(0) fib(1) public static int fib( int n) if (n == 0 || n == 1) return n else return fib(n-1) + fib(n-2) 1 1 1 1 1 0 0 0 1 1 2 1 3 2 5
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10. Box trace of writeBackward(“cat”, 3) The initial call is made, and the method begins execution: s = “cat” size = 3 s = “cat” size = 3 s = “cat” size = 2 s = “cat” size = 1 s = “cat” size = 2 s = “cat” size = 3 A A A Outputline: ta Point A is reached, and the recursive call is made. The new invocation begins execution: Outputline: t Point A ( writeBackward(s, size-1)) is reached, and the recursive call is made. The new invocation begins execution:
  • 11. Box trace of writeBackward(“cat”, 3) Output line: tac Point A is reached, and the recursive call is made. The new evocation begins execution: s = “cat” size = 3 s = “cat” size = 2 s = “cat” size = 1 s = “cat” size = 0 s = “cat” size = 0 s = “cat” size = 1 s = “cat” size = 2 s = “cat” size = 3 A A A A A A This is the base case, so this invocation completes. Control returns to the calling box, which continues execution:
  • 12. Box trace of writeBackward(“cat”, 3) This invocation completes. Control returns to the calling box, which continues execution: s = “cat” size = 3 s = “cat” size = 2 s = “cat” size = 1 s = “cat” size = 0 s = “cat” size = 0 s = “cat” size = 1 s = “cat” size = 2 s = “cat” size = 3 A A A A A A This invocation completes. Control returns to the calling box, which continues execution: This invocation completes. Control returns to the statement following the initial call.
  • 13.
  • 14. return 2*fact(1) 2* ? Figure 2.2 fact(3) System.out.println(fact(3)); ? return 3*fact(2) 3*? return 1*fact(0) 1* ? return 1 6 1 1 2
  • 16. Figure 2.4 The beginning of the box trace
  • 17. Figure 2.5a Box trace of fact(3)
  • 18. Figure 2.5b Box trace of fact(3)
  • 19. Figure 2.5c Box trace of fact(3)