SlideShare a Scribd company logo
Recursion
“A function that either directly or indirectly make a
call to itself.”
Powerful and efficient programming tool.
Example of directly call
method1()
{
System.out.println(“We Are Anonymous.”)
method1();
}
2
Example of Indirect Call
A()
{
System.out.println(“Function ‘A’ Call”);
B();
}
B()
{
System.out.println(“Fuction B Call”);
A();
}
3
Requirements for Recursive Solution
 At least one “small” case that you can solve directly
 A way of breaking a larger problem down into:
 One or more smaller subproblems
 Each of the same kind as the original
 A way of combining subproblem results into an overall
solution to the larger problem
4
Recursive Design Example: Code
Recursive algorithm for finding length of a string:
public static int length (String str) {
if (str == null ||
str.equals(“”))
return 0;
else
return length(str.substring(1)) + 1;
}
5
Recursive Design Example: printChars
Recursive algorithm for printing a string:
public static void printChars
(String str) {
if (str == null ||
str.equals(“”))
return;
else
System.out.println(str.charAt(0));
printChars(str.substring(1));
}
6
Recursive Design Example: printChars2
Recursive algorithm for printing a string?
public static void printChars2
(String str) {
if (str == null ||
str.equals(“”))
return;
else
printChars2(str.substring(1));
System.out.println(str.charAt(0));
}
7
Tracing a Recursive Method
8
length(“ace”)
return 1 + length(“ce”)
return 1 + length(“e”)
return 1 + length(“”)
0
1
2
3
Overall
result
Recursive Definitions: Fibonacci Series
Definition of fibi, for integer i > 0:
fib1 = 1
fib2 = 1
fibn = fibn-1 + fibn-2, for n > 2
9
Fibonacci Series Code
public static int fib (int n) {
if (n <= 2)
return 1;
else
return fib(n-1) + fib(n-2);
}
This is straightforward, but an inefficient recursion ...
10
Efficiency of Recursion: Inefficient Fibonacci
11

More Related Content

What's hot

Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
Algorithm Analyzing
Algorithm AnalyzingAlgorithm Analyzing
Algorithm Analyzing
Haluan Irsad
 
Daa unit 6_efficiency of algorithms
Daa unit 6_efficiency of algorithmsDaa unit 6_efficiency of algorithms
Daa unit 6_efficiency of algorithms
snehajiyani
 
Recursion and looping
Recursion and loopingRecursion and looping
Recursion and looping
xcoolanurag
 
Safety Verification of Deep Neural Networks_.pdf
Safety Verification of Deep Neural Networks_.pdfSafety Verification of Deep Neural Networks_.pdf
Safety Verification of Deep Neural Networks_.pdf
Polytechnique Montréal
 
Iterations and Recursions
Iterations and RecursionsIterations and Recursions
Iterations and Recursions
Abdul Rahman Sherzad
 
IRJET- Bidirectional Graph Search Techniques for Finding Shortest Path in Ima...
IRJET- Bidirectional Graph Search Techniques for Finding Shortest Path in Ima...IRJET- Bidirectional Graph Search Techniques for Finding Shortest Path in Ima...
IRJET- Bidirectional Graph Search Techniques for Finding Shortest Path in Ima...
IRJET Journal
 
Recursion
RecursionRecursion
Recursion
Malainine Zaid
 
01. design & analysis of agorithm intro & complexity analysis
01. design & analysis of agorithm intro & complexity analysis01. design & analysis of agorithm intro & complexity analysis
01. design & analysis of agorithm intro & complexity analysis
Onkar Nath Sharma
 
Breadth first algorithm for solving Image based maze problem
Breadth first algorithm for solving Image based maze problemBreadth first algorithm for solving Image based maze problem
Breadth first algorithm for solving Image based maze problem
Navin Kumar
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSGayathri Gaayu
 
Time andspacecomplexity
Time andspacecomplexityTime andspacecomplexity
Time andspacecomplexity
LAKSHMITHARUN PONNAM
 
Recursion | C++ | DSA
Recursion | C++ | DSARecursion | C++ | DSA
Recursion | C++ | DSA
Sumit Pandey
 
CPSC 125 Ch 2 Sec 4
CPSC 125 Ch 2 Sec 4CPSC 125 Ch 2 Sec 4
CPSC 125 Ch 2 Sec 4
David Wood
 
Unit 7 dynamic programming
Unit 7   dynamic programmingUnit 7   dynamic programming
Unit 7 dynamic programming
Nageswara Rao Thots
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
Dr. Rajdeep Chatterjee
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis tools
Abdullah Al-hazmy
 
Golden Section method
Golden Section methodGolden Section method
Golden Section method
Syed Rubaid Ahmad
 

What's hot (20)

Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Algorithm Analyzing
Algorithm AnalyzingAlgorithm Analyzing
Algorithm Analyzing
 
Daa unit 6_efficiency of algorithms
Daa unit 6_efficiency of algorithmsDaa unit 6_efficiency of algorithms
Daa unit 6_efficiency of algorithms
 
Recursion and looping
Recursion and loopingRecursion and looping
Recursion and looping
 
Safety Verification of Deep Neural Networks_.pdf
Safety Verification of Deep Neural Networks_.pdfSafety Verification of Deep Neural Networks_.pdf
Safety Verification of Deep Neural Networks_.pdf
 
Iterations and Recursions
Iterations and RecursionsIterations and Recursions
Iterations and Recursions
 
IRJET- Bidirectional Graph Search Techniques for Finding Shortest Path in Ima...
IRJET- Bidirectional Graph Search Techniques for Finding Shortest Path in Ima...IRJET- Bidirectional Graph Search Techniques for Finding Shortest Path in Ima...
IRJET- Bidirectional Graph Search Techniques for Finding Shortest Path in Ima...
 
Recursion
RecursionRecursion
Recursion
 
01. design & analysis of agorithm intro & complexity analysis
01. design & analysis of agorithm intro & complexity analysis01. design & analysis of agorithm intro & complexity analysis
01. design & analysis of agorithm intro & complexity analysis
 
Breadth first algorithm for solving Image based maze problem
Breadth first algorithm for solving Image based maze problemBreadth first algorithm for solving Image based maze problem
Breadth first algorithm for solving Image based maze problem
 
Analysis of Algorithm
Analysis of AlgorithmAnalysis of Algorithm
Analysis of Algorithm
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
Time andspacecomplexity
Time andspacecomplexityTime andspacecomplexity
Time andspacecomplexity
 
Recursion | C++ | DSA
Recursion | C++ | DSARecursion | C++ | DSA
Recursion | C++ | DSA
 
CPSC 125 Ch 2 Sec 4
CPSC 125 Ch 2 Sec 4CPSC 125 Ch 2 Sec 4
CPSC 125 Ch 2 Sec 4
 
Unit 7 dynamic programming
Unit 7   dynamic programmingUnit 7   dynamic programming
Unit 7 dynamic programming
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
 
poster
posterposter
poster
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis tools
 
Golden Section method
Golden Section methodGolden Section method
Golden Section method
 

Similar to Recursion

20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction
Intro C# Book
 
Computer-programming-User-defined-function-1.pptx
Computer-programming-User-defined-function-1.pptxComputer-programming-User-defined-function-1.pptx
Computer-programming-User-defined-function-1.pptx
JohnRehldeGracia
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
Arockia Abins
 
09. Java Methods
09. Java Methods09. Java Methods
09. Java Methods
Intro C# Book
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
MKalpanaDevi
 
Major Java 8 features
Major Java 8 featuresMajor Java 8 features
Major Java 8 features
Sanjoy Kumar Roy
 
Vb.net iii
Vb.net iiiVb.net iii
Vb.net iii
argusacademy
 
C#.net
C#.netC#.net
C#.net
vnboghani
 
Vb.net ii
Vb.net iiVb.net ii
Vb.net ii
argusacademy
 
Question 1 briefly respond to all the following questions. make
Question 1 briefly respond to all the following questions. make Question 1 briefly respond to all the following questions. make
Question 1 briefly respond to all the following questions. make
YASHU40
 
Functions in c
Functions in cFunctions in c
Functions in c
KavithaMuralidharan2
 
.net progrmming part3
.net progrmming part3.net progrmming part3
.net progrmming part3
Dr.M.Karthika parthasarathy
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
MuhammadTalha436
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
Saranya saran
 
Recursion.ppt
 Recursion.ppt Recursion.ppt
Recursion.ppt
TalhaHussain58
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
Maýur Chourasiya
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
Svetlin Nakov
 
c++ referesher 1.pdf
c++ referesher 1.pdfc++ referesher 1.pdf
c++ referesher 1.pdf
AnkurSingh656748
 

Similar to Recursion (20)

20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction
 
Computer-programming-User-defined-function-1.pptx
Computer-programming-User-defined-function-1.pptxComputer-programming-User-defined-function-1.pptx
Computer-programming-User-defined-function-1.pptx
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
 
09. Java Methods
09. Java Methods09. Java Methods
09. Java Methods
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Major Java 8 features
Major Java 8 featuresMajor Java 8 features
Major Java 8 features
 
7
77
7
 
Vb.net iii
Vb.net iiiVb.net iii
Vb.net iii
 
C#.net
C#.netC#.net
C#.net
 
Vb.net ii
Vb.net iiVb.net ii
Vb.net ii
 
Chap14
Chap14Chap14
Chap14
 
Question 1 briefly respond to all the following questions. make
Question 1 briefly respond to all the following questions. make Question 1 briefly respond to all the following questions. make
Question 1 briefly respond to all the following questions. make
 
Functions in c
Functions in cFunctions in c
Functions in c
 
.net progrmming part3
.net progrmming part3.net progrmming part3
.net progrmming part3
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Recursion.ppt
 Recursion.ppt Recursion.ppt
Recursion.ppt
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
 
c++ referesher 1.pdf
c++ referesher 1.pdfc++ referesher 1.pdf
c++ referesher 1.pdf
 

More from Muhammad Umar Farooq

Linear network
Linear networkLinear network
Linear network
Muhammad Umar Farooq
 
Digital vs analogue
Digital vs analogueDigital vs analogue
Digital vs analogue
Muhammad Umar Farooq
 
Project planning and scheduling
Project planning and schedulingProject planning and scheduling
Project planning and scheduling
Muhammad Umar Farooq
 
It training
It trainingIt training
Cyber crime ethics and un ethics
Cyber crime ethics and un ethicsCyber crime ethics and un ethics
Cyber crime ethics and un ethics
Muhammad Umar Farooq
 
It usages &amp; role
It usages &amp;  roleIt usages &amp;  role
It usages &amp; role
Muhammad Umar Farooq
 
Future prediction-ds
Future prediction-dsFuture prediction-ds
Future prediction-ds
Muhammad Umar Farooq
 
Computer virus
Computer virusComputer virus
Computer virus
Muhammad Umar Farooq
 
Singular and non singular matrix
Singular and non singular matrixSingular and non singular matrix
Singular and non singular matrix
Muhammad Umar Farooq
 
Assembly language (addition and subtraction)
Assembly language (addition and subtraction)Assembly language (addition and subtraction)
Assembly language (addition and subtraction)
Muhammad Umar Farooq
 
Ring
RingRing
Power
PowerPower
Principal ideal
Principal idealPrincipal ideal
Principal ideal
Muhammad Umar Farooq
 
Quotient ring
Quotient ringQuotient ring
Quotient ring
Muhammad Umar Farooq
 
What is communication
What is communicationWhat is communication
What is communication
Muhammad Umar Farooq
 
Ring homomorphism
Ring homomorphismRing homomorphism
Ring homomorphism
Muhammad Umar Farooq
 
Leaner algebra presentation (ring)
Leaner algebra presentation (ring)Leaner algebra presentation (ring)
Leaner algebra presentation (ring)
Muhammad Umar Farooq
 

More from Muhammad Umar Farooq (17)

Linear network
Linear networkLinear network
Linear network
 
Digital vs analogue
Digital vs analogueDigital vs analogue
Digital vs analogue
 
Project planning and scheduling
Project planning and schedulingProject planning and scheduling
Project planning and scheduling
 
It training
It trainingIt training
It training
 
Cyber crime ethics and un ethics
Cyber crime ethics and un ethicsCyber crime ethics and un ethics
Cyber crime ethics and un ethics
 
It usages &amp; role
It usages &amp;  roleIt usages &amp;  role
It usages &amp; role
 
Future prediction-ds
Future prediction-dsFuture prediction-ds
Future prediction-ds
 
Computer virus
Computer virusComputer virus
Computer virus
 
Singular and non singular matrix
Singular and non singular matrixSingular and non singular matrix
Singular and non singular matrix
 
Assembly language (addition and subtraction)
Assembly language (addition and subtraction)Assembly language (addition and subtraction)
Assembly language (addition and subtraction)
 
Ring
RingRing
Ring
 
Power
PowerPower
Power
 
Principal ideal
Principal idealPrincipal ideal
Principal ideal
 
Quotient ring
Quotient ringQuotient ring
Quotient ring
 
What is communication
What is communicationWhat is communication
What is communication
 
Ring homomorphism
Ring homomorphismRing homomorphism
Ring homomorphism
 
Leaner algebra presentation (ring)
Leaner algebra presentation (ring)Leaner algebra presentation (ring)
Leaner algebra presentation (ring)
 

Recently uploaded

Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 

Recently uploaded (20)

Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 

Recursion

  • 1.
  • 2. Recursion “A function that either directly or indirectly make a call to itself.” Powerful and efficient programming tool. Example of directly call method1() { System.out.println(“We Are Anonymous.”) method1(); } 2
  • 3. Example of Indirect Call A() { System.out.println(“Function ‘A’ Call”); B(); } B() { System.out.println(“Fuction B Call”); A(); } 3
  • 4. Requirements for Recursive Solution  At least one “small” case that you can solve directly  A way of breaking a larger problem down into:  One or more smaller subproblems  Each of the same kind as the original  A way of combining subproblem results into an overall solution to the larger problem 4
  • 5. Recursive Design Example: Code Recursive algorithm for finding length of a string: public static int length (String str) { if (str == null || str.equals(“”)) return 0; else return length(str.substring(1)) + 1; } 5
  • 6. Recursive Design Example: printChars Recursive algorithm for printing a string: public static void printChars (String str) { if (str == null || str.equals(“”)) return; else System.out.println(str.charAt(0)); printChars(str.substring(1)); } 6
  • 7. Recursive Design Example: printChars2 Recursive algorithm for printing a string? public static void printChars2 (String str) { if (str == null || str.equals(“”)) return; else printChars2(str.substring(1)); System.out.println(str.charAt(0)); } 7
  • 8. Tracing a Recursive Method 8 length(“ace”) return 1 + length(“ce”) return 1 + length(“e”) return 1 + length(“”) 0 1 2 3 Overall result
  • 9. Recursive Definitions: Fibonacci Series Definition of fibi, for integer i > 0: fib1 = 1 fib2 = 1 fibn = fibn-1 + fibn-2, for n > 2 9
  • 10. Fibonacci Series Code public static int fib (int n) { if (n <= 2) return 1; else return fib(n-1) + fib(n-2); } This is straightforward, but an inefficient recursion ... 10
  • 11. Efficiency of Recursion: Inefficient Fibonacci 11