SlideShare a Scribd company logo
1 of 12
Recursion
o Recursion is a principle closely related to mathematical
induction.
o In a recursive definition, an object is defined in terms of
itself.
o We can recursively define sequences, functions and sets.
Recursion
o RECURSION
The process of defining an object in terms of smaller versions of
itself is called recursion.
o A recursive definition has two parts:
1. BASE:
An initial simple definition which cannot be expressed in terms of
smaller versions of itself.
2. RECURSION:
The part of definition which can be expressed in terms of smaller
versions of itself.
Recursion
o BASE:
1 is an odd positive integer.
o RECURSION:
If k is an odd positive integer, then k + 2 is an odd
positive integer.
Now, 1 is an odd positive integer by the definition base.
With k = 1, 1 + 2 = 3, so 3 is an odd positive integer.
With k = 3, 3 + 2 = 5, so 5 is an odd positive integer
and so, 7, 9, 11, … are odd positive integers.
o The main idea is to “reduce” a problem into smaller problems.
Recursion
f(0) = 3
f(n + 1) = 2f(n) + 3
o Find f(1), f(2), f(3) and f(4)
 From the recursive definition it follows that
f(1) = 2 f(0) + 3 = 2(3) + 3 = 6 + 3 = 9
f(2) = 2 f(1) + 3 = 2(9) + 3 = 18 + 3 = 21
f(3) = 2 f(2) + 3 = 2(21) + 3 = 42 + 3 = 45
f(4) = 2 f(3) + 3 = 2(45) + 3 = 90 + 3 = 93
THE FACTORIAL OF A POSITIVE INTEGER: Example
o For each positive integer n, the factorial of n denoted as n! is defined
to be the product of all the integers from 1 to n:
n! = n.(n - 1).(n - 2) . . .3 . 2 . 1
• Zero factorial is defined to be 1
0! = 1
EXAMPLE: 0! = 1 1! = 1
2! = 2.1 = 2 3! = 3.2.1 = 6
4! = 4.3.2.1 = 24 5! = 5.4.3.2.1 = 120
! = 6.5.4.3.2.1= 720 7! = 7.6.5.4.3.2.1= 5040
REMARK:
5! = 5 .4.3 . 2 . 1 = 5 .(4 . 3 . 2 .1) = 5 . 4!
In general, n! = n(n-1)! for each positive integer n.
THE FACTORIAL OF A POSITIVE INTEGER: Example
oThus, the recursive definition of
factorial function F(n) is:
1. F(0) = 1
2. F(n) = n F(n-1)
The Fibonacci numbers
of(0) = 0, f(1) = 1
of(n) = f(n – 1) + f(n - 2)
f(0) = 0
f(1) = 1
f(2) = f(1) + f(0) = 1 + 0 = 1
f(3) = f(2) + f(1) = 1 + 1 = 2
f(4) = f(3) + f(2) = 2 + 1 = 3
f(5) = f(4) + f(3) = 3 + 2 = 5
f(6) = f(5) + f(4) = 5 + 3 = 8
RECURSIVELY DEFINED FUNCTIONS
o A function is said to be recursively defined if the function
refers to itself such that
1. There are certain arguments, called base values, for which
the function does not refer to itself.
2. Each time the function does refer to itself, the argument
of the function must be closer to a base value.
RECURSIVELY DEFINED FUNCTIONS
procedure fibo(n: nonnegative integer)
if n = 0 then fibo(0) := 0
else if n = 1 then fibo(1) := 1
else fibo(n) := fibo(n – 1) + fibo(n – 2)
end
f(4)
f(3)
f(2)
f(1) f(0)
f(1)
f(2)
f(1) f(0)
USE OF RECURSION
o At first recursion may seem hard or impossible, may be
magical at best. However, recursion often provides elegant,
short algorithmic solutions to many problems in computer
science and mathematics.
– Examples where recursion is often used
• math functions
• number sequences
• data structure definitions
• data structure manipulations
• language definitions
USE OF RECURSION
o For every recursive algorithm, there is an
equivalent iterative algorithm.
o However, iterative algorithms are usually more
efficient in their use of space and time.

More Related Content

What's hot (20)

Binary Search
Binary SearchBinary Search
Binary Search
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 
Data types in python
Data types in pythonData types in python
Data types in python
 
Data structure ppt
Data structure pptData structure ppt
Data structure ppt
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Looping statement in python
Looping statement in pythonLooping statement in python
Looping statement in python
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Function in c
Function in cFunction in c
Function in c
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
 
Sorting
SortingSorting
Sorting
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data Structures
 
List in Python
List in PythonList in Python
List in Python
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 

Similar to Recursive Definitions Explained

Similar to Recursive Definitions Explained (20)

Recursive Definitions in Discrete Mathmatcs.pptx
Recursive Definitions in Discrete Mathmatcs.pptxRecursive Definitions in Discrete Mathmatcs.pptx
Recursive Definitions in Discrete Mathmatcs.pptx
 
Recursion DM
Recursion DMRecursion DM
Recursion DM
 
CMSC 56 | Lecture 12: Recursive Definition & Algorithms, and Program Correctness
CMSC 56 | Lecture 12: Recursive Definition & Algorithms, and Program CorrectnessCMSC 56 | Lecture 12: Recursive Definition & Algorithms, and Program Correctness
CMSC 56 | Lecture 12: Recursive Definition & Algorithms, and Program Correctness
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
CRYPTOGRAPHY AND NUMBER THEORY, he ha huli
CRYPTOGRAPHY AND NUMBER THEORY, he ha huliCRYPTOGRAPHY AND NUMBER THEORY, he ha huli
CRYPTOGRAPHY AND NUMBER THEORY, he ha huli
 
L16
L16L16
L16
 
Per4 induction
Per4 inductionPer4 induction
Per4 induction
 
CMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of FunctionsCMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of Functions
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 
Dr hasany 2467_16649_1_lec-2-zabist
Dr hasany 2467_16649_1_lec-2-zabistDr hasany 2467_16649_1_lec-2-zabist
Dr hasany 2467_16649_1_lec-2-zabist
 
Algebra 2 Section 4-4
Algebra 2 Section 4-4Algebra 2 Section 4-4
Algebra 2 Section 4-4
 
Binomial Theorem
Binomial TheoremBinomial Theorem
Binomial Theorem
 
Evaluating definite integrals
Evaluating definite integralsEvaluating definite integrals
Evaluating definite integrals
 
Master method
Master method Master method
Master method
 
Binomial theorem
Binomial theorem Binomial theorem
Binomial theorem
 
maths ppt.pdf
maths ppt.pdfmaths ppt.pdf
maths ppt.pdf
 
maths ppt.pdf
maths ppt.pdfmaths ppt.pdf
maths ppt.pdf
 
Proof Techniques
Proof TechniquesProof Techniques
Proof Techniques
 
Module 2 exponential functions
Module 2   exponential functionsModule 2   exponential functions
Module 2 exponential functions
 
Sequence and Series
Sequence and SeriesSequence and Series
Sequence and Series
 

More from Abdur Rehman

Financial accounting
Financial accountingFinancial accounting
Financial accountingAbdur Rehman
 
Valid & invalid arguments
Valid & invalid argumentsValid & invalid arguments
Valid & invalid argumentsAbdur Rehman
 
Proving existential statements
Proving existential statementsProving existential statements
Proving existential statementsAbdur Rehman
 
Method of direct proof
Method of direct proofMethod of direct proof
Method of direct proofAbdur Rehman
 
Proofs by contraposition
Proofs by contrapositionProofs by contraposition
Proofs by contrapositionAbdur Rehman
 
Converse, contrapositive, inverse
Converse, contrapositive, inverseConverse, contrapositive, inverse
Converse, contrapositive, inverseAbdur Rehman
 
Constructing circuits for boolean expressions(gate)
Constructing circuits for boolean expressions(gate)Constructing circuits for boolean expressions(gate)
Constructing circuits for boolean expressions(gate)Abdur Rehman
 
Application of bases
Application of basesApplication of bases
Application of basesAbdur Rehman
 
Intro to disceret structure
Intro to disceret structureIntro to disceret structure
Intro to disceret structureAbdur Rehman
 
logic, preposition etc
logic, preposition etclogic, preposition etc
logic, preposition etcAbdur Rehman
 

More from Abdur Rehman (18)

Financial accounting
Financial accountingFinancial accounting
Financial accounting
 
Dscrete structure
Dscrete  structureDscrete  structure
Dscrete structure
 
Valid & invalid arguments
Valid & invalid argumentsValid & invalid arguments
Valid & invalid arguments
 
Sets
SetsSets
Sets
 
Sequences
SequencesSequences
Sequences
 
Queue
QueueQueue
Queue
 
Quantification
QuantificationQuantification
Quantification
 
Proving existential statements
Proving existential statementsProving existential statements
Proving existential statements
 
Method of direct proof
Method of direct proofMethod of direct proof
Method of direct proof
 
Proofs by contraposition
Proofs by contrapositionProofs by contraposition
Proofs by contraposition
 
Laws in disceret
Laws in disceretLaws in disceret
Laws in disceret
 
Converse, contrapositive, inverse
Converse, contrapositive, inverseConverse, contrapositive, inverse
Converse, contrapositive, inverse
 
Constructing circuits for boolean expressions(gate)
Constructing circuits for boolean expressions(gate)Constructing circuits for boolean expressions(gate)
Constructing circuits for boolean expressions(gate)
 
Application of bases
Application of basesApplication of bases
Application of bases
 
Truth table
Truth tableTruth table
Truth table
 
Intro to disceret structure
Intro to disceret structureIntro to disceret structure
Intro to disceret structure
 
Dst lec3
Dst lec3Dst lec3
Dst lec3
 
logic, preposition etc
logic, preposition etclogic, preposition etc
logic, preposition etc
 

Recently uploaded

THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 

Recursive Definitions Explained

  • 1.
  • 2. Recursion o Recursion is a principle closely related to mathematical induction. o In a recursive definition, an object is defined in terms of itself. o We can recursively define sequences, functions and sets.
  • 3. Recursion o RECURSION The process of defining an object in terms of smaller versions of itself is called recursion. o A recursive definition has two parts: 1. BASE: An initial simple definition which cannot be expressed in terms of smaller versions of itself. 2. RECURSION: The part of definition which can be expressed in terms of smaller versions of itself.
  • 4. Recursion o BASE: 1 is an odd positive integer. o RECURSION: If k is an odd positive integer, then k + 2 is an odd positive integer. Now, 1 is an odd positive integer by the definition base. With k = 1, 1 + 2 = 3, so 3 is an odd positive integer. With k = 3, 3 + 2 = 5, so 5 is an odd positive integer and so, 7, 9, 11, … are odd positive integers. o The main idea is to “reduce” a problem into smaller problems.
  • 5. Recursion f(0) = 3 f(n + 1) = 2f(n) + 3 o Find f(1), f(2), f(3) and f(4)  From the recursive definition it follows that f(1) = 2 f(0) + 3 = 2(3) + 3 = 6 + 3 = 9 f(2) = 2 f(1) + 3 = 2(9) + 3 = 18 + 3 = 21 f(3) = 2 f(2) + 3 = 2(21) + 3 = 42 + 3 = 45 f(4) = 2 f(3) + 3 = 2(45) + 3 = 90 + 3 = 93
  • 6. THE FACTORIAL OF A POSITIVE INTEGER: Example o For each positive integer n, the factorial of n denoted as n! is defined to be the product of all the integers from 1 to n: n! = n.(n - 1).(n - 2) . . .3 . 2 . 1 • Zero factorial is defined to be 1 0! = 1 EXAMPLE: 0! = 1 1! = 1 2! = 2.1 = 2 3! = 3.2.1 = 6 4! = 4.3.2.1 = 24 5! = 5.4.3.2.1 = 120 ! = 6.5.4.3.2.1= 720 7! = 7.6.5.4.3.2.1= 5040 REMARK: 5! = 5 .4.3 . 2 . 1 = 5 .(4 . 3 . 2 .1) = 5 . 4! In general, n! = n(n-1)! for each positive integer n.
  • 7. THE FACTORIAL OF A POSITIVE INTEGER: Example oThus, the recursive definition of factorial function F(n) is: 1. F(0) = 1 2. F(n) = n F(n-1)
  • 8. The Fibonacci numbers of(0) = 0, f(1) = 1 of(n) = f(n – 1) + f(n - 2) f(0) = 0 f(1) = 1 f(2) = f(1) + f(0) = 1 + 0 = 1 f(3) = f(2) + f(1) = 1 + 1 = 2 f(4) = f(3) + f(2) = 2 + 1 = 3 f(5) = f(4) + f(3) = 3 + 2 = 5 f(6) = f(5) + f(4) = 5 + 3 = 8
  • 9. RECURSIVELY DEFINED FUNCTIONS o A function is said to be recursively defined if the function refers to itself such that 1. There are certain arguments, called base values, for which the function does not refer to itself. 2. Each time the function does refer to itself, the argument of the function must be closer to a base value.
  • 10. RECURSIVELY DEFINED FUNCTIONS procedure fibo(n: nonnegative integer) if n = 0 then fibo(0) := 0 else if n = 1 then fibo(1) := 1 else fibo(n) := fibo(n – 1) + fibo(n – 2) end f(4) f(3) f(2) f(1) f(0) f(1) f(2) f(1) f(0)
  • 11. USE OF RECURSION o At first recursion may seem hard or impossible, may be magical at best. However, recursion often provides elegant, short algorithmic solutions to many problems in computer science and mathematics. – Examples where recursion is often used • math functions • number sequences • data structure definitions • data structure manipulations • language definitions
  • 12. USE OF RECURSION o For every recursive algorithm, there is an equivalent iterative algorithm. o However, iterative algorithms are usually more efficient in their use of space and time.