SlideShare a Scribd company logo
Discrete Mathematics
Lecture 10
Rubya Afrin
Lecturer
Northern University of Business and Technology
2
Recursion
Recursive Definitions
Recursion is a principle closely related to mathematical induction.
In a recursive definition, an object is defined in terms of itself.
We can recursively define sequences, functions and sets.
3
Recursively Defined Sequences
Example:
The sequence {an} of powers of 2 is given by
an = 2n for n = 0, 1, 2, … .
The same sequence can also be defined recursively:
a0 = 1
an+1 = 2an for n = 0, 1, 2, …
Obviously, induction and recursion are similar principles.
4
Recursively Defined Functions
We can use the following method to define a function with the natural
numbers as its domain:
• Specify the value of the function at zero.
• Give a rule for finding its value at any integer
from its values at smaller integers.
Such a definition is called recursive or inductive definition.
5
Recursively Defined Functions
Example:
f(0) = 3
f(n + 1) = 2f(n) + 3
f(0) = 3
f(1) = 2f(0) + 3 = 23 + 3 = 9
f(2) = 2f(1) + 3 = 29 + 3 = 21
f(3) = 2f(2) + 3 = 221 + 3 = 45
f(4) = 2f(3) + 3 = 245 + 3 = 93
6
Recursively Defined Functions
How can we recursively define the factorial function f(n) = n! ?
f(0) = 1
f(n + 1) = (n + 1)f(n)
f(0) = 1
f(1) = 1f(0) = 11 = 1
f(2) = 2f(1) = 21 = 2
f(3) = 3f(2) = 32 = 6
f(4) = 4f(3) = 46 = 24
7
Recursively Defined Functions
A famous example: The Fibonacci numbers
f(0) = 0, f(1) = 1
f(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
8
Recursive Algorithms
An algorithm is called recursive if it solves a problem by reducing it to an
instance of the same problem with smaller input.
Example I: Recursive Euclidean Algorithm
procedure gcd(a, b: nonnegative integers with a < b)
if a = 0 then gcd(a, b) := b
else gcd(a, b) := gcd(b mod a, a)
9
Recursive Algorithms
Example II: Recursive Fibonacci Algorithm
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)
10
Recursive Algorithms
Recursive Fibonacci Evaluation:
11
f(4)
f(3)
f(2)
f(1) f(0)
f(1)
f(2)
f(1) f(0)
Recursive Algorithms
procedure iterative_fibo(n: nonnegative integer)
if n = 0 then y := 0
else
begin
 x := 0
 y := 1
 for i := 1 to n-1
 begin
 z := x + y
 x : = y
 y := z
 end
end {y is the n-th Fibonacci number}
12
Recursive Algorithms
For every recursive algorithm, there is an equivalent iterative algorithm.
Recursive algorithms are often shorter, more elegant, and easier to
understand than their iterative counterparts.
However, iterative algorithms are usually more efficient in their use of
space and time.
13
Thank you…

More Related Content

What's hot

Proof By Contradictions
Proof By ContradictionsProof By Contradictions
Proof By Contradictions
GC University Fsd
 
Lesson 5: Continuity (slides)
Lesson 5: Continuity (slides)Lesson 5: Continuity (slides)
Lesson 5: Continuity (slides)
Matthew Leingang
 
Math induction principle (slides)
Math induction principle (slides)Math induction principle (slides)
Math induction principle (slides)
IIUM
 
Pigeonhole Principle
Pigeonhole PrinciplePigeonhole Principle
Pigeonhole Principlenielsoli
 
Predicates and quantifiers
Predicates and quantifiersPredicates and quantifiers
Predicates and quantifiers
Istiak Ahmed
 
CMSC 56 | Lecture 16: Equivalence of Relations & Partial Ordering
CMSC 56 | Lecture 16: Equivalence of Relations & Partial OrderingCMSC 56 | Lecture 16: Equivalence of Relations & Partial Ordering
CMSC 56 | Lecture 16: Equivalence of Relations & Partial Ordering
allyn joy calcaben
 
Mathematical induction and divisibility rules
Mathematical induction and divisibility rulesMathematical induction and divisibility rules
Mathematical induction and divisibility rules
Dawood Faheem Abbasi
 
Mathematical induction
Mathematical inductionMathematical induction
Mathematical induction
rey castro
 
Sequences and Series (Mathematics)
Sequences and Series (Mathematics) Sequences and Series (Mathematics)
Sequences and Series (Mathematics)
Dhrumil Maniar
 
Sets, functions and groups
Sets, functions and groupsSets, functions and groups
Sets, functions and groups
Muhammad Adnan Ejaz
 
Poset in Relations(Discrete Mathematics)
Poset in Relations(Discrete Mathematics)Poset in Relations(Discrete Mathematics)
Poset in Relations(Discrete Mathematics)
Rachana Pathak
 
Proofs by contraposition
Proofs by contrapositionProofs by contraposition
Proofs by contraposition
Abdur Rehman
 
Unit 1 introduction to proofs
Unit 1  introduction to proofsUnit 1  introduction to proofs
Unit 1 introduction to proofs
raksharao
 
Recurrence relations
Recurrence relationsRecurrence relations
Recurrence relations
IIUM
 
Generating function
Generating functionGenerating function
Logical equivalence, laws of logic
Logical equivalence, laws of logicLogical equivalence, laws of logic
Logical equivalence, laws of logic
Lakshmi R
 
5.4 mathematical induction
5.4 mathematical induction5.4 mathematical induction
5.4 mathematical inductionmath260
 
Strings in python
Strings in pythonStrings in python
Strings in python
Prabhakaran V M
 
Taylor series in 1 and 2 variable
Taylor series in 1 and 2 variableTaylor series in 1 and 2 variable
Taylor series in 1 and 2 variable
gidc engineering college
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
Amrinder Arora
 

What's hot (20)

Proof By Contradictions
Proof By ContradictionsProof By Contradictions
Proof By Contradictions
 
Lesson 5: Continuity (slides)
Lesson 5: Continuity (slides)Lesson 5: Continuity (slides)
Lesson 5: Continuity (slides)
 
Math induction principle (slides)
Math induction principle (slides)Math induction principle (slides)
Math induction principle (slides)
 
Pigeonhole Principle
Pigeonhole PrinciplePigeonhole Principle
Pigeonhole Principle
 
Predicates and quantifiers
Predicates and quantifiersPredicates and quantifiers
Predicates and quantifiers
 
CMSC 56 | Lecture 16: Equivalence of Relations & Partial Ordering
CMSC 56 | Lecture 16: Equivalence of Relations & Partial OrderingCMSC 56 | Lecture 16: Equivalence of Relations & Partial Ordering
CMSC 56 | Lecture 16: Equivalence of Relations & Partial Ordering
 
Mathematical induction and divisibility rules
Mathematical induction and divisibility rulesMathematical induction and divisibility rules
Mathematical induction and divisibility rules
 
Mathematical induction
Mathematical inductionMathematical induction
Mathematical induction
 
Sequences and Series (Mathematics)
Sequences and Series (Mathematics) Sequences and Series (Mathematics)
Sequences and Series (Mathematics)
 
Sets, functions and groups
Sets, functions and groupsSets, functions and groups
Sets, functions and groups
 
Poset in Relations(Discrete Mathematics)
Poset in Relations(Discrete Mathematics)Poset in Relations(Discrete Mathematics)
Poset in Relations(Discrete Mathematics)
 
Proofs by contraposition
Proofs by contrapositionProofs by contraposition
Proofs by contraposition
 
Unit 1 introduction to proofs
Unit 1  introduction to proofsUnit 1  introduction to proofs
Unit 1 introduction to proofs
 
Recurrence relations
Recurrence relationsRecurrence relations
Recurrence relations
 
Generating function
Generating functionGenerating function
Generating function
 
Logical equivalence, laws of logic
Logical equivalence, laws of logicLogical equivalence, laws of logic
Logical equivalence, laws of logic
 
5.4 mathematical induction
5.4 mathematical induction5.4 mathematical induction
5.4 mathematical induction
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
Taylor series in 1 and 2 variable
Taylor series in 1 and 2 variableTaylor series in 1 and 2 variable
Taylor series in 1 and 2 variable
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 

Similar to Recursion DM

Recursive Definitions in Discrete Mathmatcs.pptx
Recursive Definitions in Discrete Mathmatcs.pptxRecursive Definitions in Discrete Mathmatcs.pptx
Recursive Definitions in Discrete Mathmatcs.pptx
gbikorno
 
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
allyn joy calcaben
 
Recursion
RecursionRecursion
Recursion
Abdur Rehman
 
ch3.ppt
ch3.pptch3.ppt
5.5 Zeros of Polynomial Functions
5.5 Zeros of Polynomial Functions5.5 Zeros of Polynomial Functions
5.5 Zeros of Polynomial Functions
smiller5
 
02 Notes Divide and Conquer
02 Notes Divide and Conquer02 Notes Divide and Conquer
02 Notes Divide and Conquer
Andres Mendez-Vazquez
 
Applications of Differentiation
Applications of DifferentiationApplications of Differentiation
Applications of Differentiation
Joey Valdriz
 
Recursion.pdf
Recursion.pdfRecursion.pdf
Recursion.pdf
Flavia Tembo Kambale
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
jayavignesh86
 
Function
Function Function
Algebra 2 Section 4-4
Algebra 2 Section 4-4Algebra 2 Section 4-4
Algebra 2 Section 4-4
Jimbo Lamb
 
Research Inventy : International Journal of Engineering and Science
Research Inventy : International Journal of Engineering and ScienceResearch Inventy : International Journal of Engineering and Science
Research Inventy : International Journal of Engineering and Science
inventy
 
SURF 2012 Final Report(1)
SURF 2012 Final Report(1)SURF 2012 Final Report(1)
SURF 2012 Final Report(1)Eric Zhang
 
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
harshmacduacin
 
02-Basic Structures .ppt
02-Basic Structures .ppt02-Basic Structures .ppt
02-Basic Structures .ppt
Acct4
 
Machine Learning
Machine LearningMachine Learning
Machine Learning
Ashwin P N
 
10 - Recursive.pptx
10 - Recursive.pptx10 - Recursive.pptx
10 - Recursive.pptx
bernadusseno
 
Note introductions of functions
Note introductions of functionsNote introductions of functions
Note introductions of functions
SMK Tengku Intan Zaharah
 

Similar to Recursion DM (20)

Recursive Definitions in Discrete Mathmatcs.pptx
Recursive Definitions in Discrete Mathmatcs.pptxRecursive Definitions in Discrete Mathmatcs.pptx
Recursive Definitions in Discrete Mathmatcs.pptx
 
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
 
Recursion
RecursionRecursion
Recursion
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
5.5 Zeros of Polynomial Functions
5.5 Zeros of Polynomial Functions5.5 Zeros of Polynomial Functions
5.5 Zeros of Polynomial Functions
 
02 Notes Divide and Conquer
02 Notes Divide and Conquer02 Notes Divide and Conquer
02 Notes Divide and Conquer
 
Applications of Differentiation
Applications of DifferentiationApplications of Differentiation
Applications of Differentiation
 
Recursion.pdf
Recursion.pdfRecursion.pdf
Recursion.pdf
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
 
Function
Function Function
Function
 
Algebra 2 Section 4-4
Algebra 2 Section 4-4Algebra 2 Section 4-4
Algebra 2 Section 4-4
 
L16
L16L16
L16
 
Research Inventy : International Journal of Engineering and Science
Research Inventy : International Journal of Engineering and ScienceResearch Inventy : International Journal of Engineering and Science
Research Inventy : International Journal of Engineering and Science
 
Number theory
Number theoryNumber theory
Number theory
 
SURF 2012 Final Report(1)
SURF 2012 Final Report(1)SURF 2012 Final Report(1)
SURF 2012 Final Report(1)
 
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
 
02-Basic Structures .ppt
02-Basic Structures .ppt02-Basic Structures .ppt
02-Basic Structures .ppt
 
Machine Learning
Machine LearningMachine Learning
Machine Learning
 
10 - Recursive.pptx
10 - Recursive.pptx10 - Recursive.pptx
10 - Recursive.pptx
 
Note introductions of functions
Note introductions of functionsNote introductions of functions
Note introductions of functions
 

More from Rokonuzzaman Rony

Course outline for c programming
Course outline for c  programming Course outline for c  programming
Course outline for c programming
Rokonuzzaman Rony
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type Conversions
Rokonuzzaman Rony
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
Rokonuzzaman Rony
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
Rokonuzzaman Rony
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
Rokonuzzaman Rony
 
Humanitarian task and its importance
Humanitarian task and its importanceHumanitarian task and its importance
Humanitarian task and its importance
Rokonuzzaman Rony
 
Pointers
 Pointers Pointers
Loops
LoopsLoops
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
Rokonuzzaman Rony
 
Array
ArrayArray
Constants, Variables, and Data Types
Constants, Variables, and Data TypesConstants, Variables, and Data Types
Constants, Variables, and Data Types
Rokonuzzaman Rony
 
C Programming language
C Programming languageC Programming language
C Programming language
Rokonuzzaman Rony
 
User defined functions
User defined functionsUser defined functions
User defined functions
Rokonuzzaman Rony
 

More from Rokonuzzaman Rony (20)

Course outline for c programming
Course outline for c  programming Course outline for c  programming
Course outline for c programming
 
Pointer
PointerPointer
Pointer
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type Conversions
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
 
Humanitarian task and its importance
Humanitarian task and its importanceHumanitarian task and its importance
Humanitarian task and its importance
 
Structure
StructureStructure
Structure
 
Pointers
 Pointers Pointers
Pointers
 
Loops
LoopsLoops
Loops
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
Array
ArrayArray
Array
 
Constants, Variables, and Data Types
Constants, Variables, and Data TypesConstants, Variables, and Data Types
Constants, Variables, and Data Types
 
C Programming language
C Programming languageC Programming language
C Programming language
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
Numerical Method 2
Numerical Method 2Numerical Method 2
Numerical Method 2
 
Numerical Method
Numerical Method Numerical Method
Numerical Method
 
Data structures
Data structuresData structures
Data structures
 
Data structures
Data structures Data structures
Data structures
 

Recently uploaded

The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
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
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
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
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
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
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 

Recently uploaded (20)

The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
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
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
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
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
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...
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 

Recursion DM

  • 1. Discrete Mathematics Lecture 10 Rubya Afrin Lecturer Northern University of Business and Technology
  • 3. Recursive Definitions Recursion is a principle closely related to mathematical induction. In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions and sets. 3
  • 4. Recursively Defined Sequences Example: The sequence {an} of powers of 2 is given by an = 2n for n = 0, 1, 2, … . The same sequence can also be defined recursively: a0 = 1 an+1 = 2an for n = 0, 1, 2, … Obviously, induction and recursion are similar principles. 4
  • 5. Recursively Defined Functions We can use the following method to define a function with the natural numbers as its domain: • Specify the value of the function at zero. • Give a rule for finding its value at any integer from its values at smaller integers. Such a definition is called recursive or inductive definition. 5
  • 6. Recursively Defined Functions Example: f(0) = 3 f(n + 1) = 2f(n) + 3 f(0) = 3 f(1) = 2f(0) + 3 = 23 + 3 = 9 f(2) = 2f(1) + 3 = 29 + 3 = 21 f(3) = 2f(2) + 3 = 221 + 3 = 45 f(4) = 2f(3) + 3 = 245 + 3 = 93 6
  • 7. Recursively Defined Functions How can we recursively define the factorial function f(n) = n! ? f(0) = 1 f(n + 1) = (n + 1)f(n) f(0) = 1 f(1) = 1f(0) = 11 = 1 f(2) = 2f(1) = 21 = 2 f(3) = 3f(2) = 32 = 6 f(4) = 4f(3) = 46 = 24 7
  • 8. Recursively Defined Functions A famous example: The Fibonacci numbers f(0) = 0, f(1) = 1 f(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 8
  • 9. Recursive Algorithms An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input. Example I: Recursive Euclidean Algorithm procedure gcd(a, b: nonnegative integers with a < b) if a = 0 then gcd(a, b) := b else gcd(a, b) := gcd(b mod a, a) 9
  • 10. Recursive Algorithms Example II: Recursive Fibonacci Algorithm 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) 10
  • 11. Recursive Algorithms Recursive Fibonacci Evaluation: 11 f(4) f(3) f(2) f(1) f(0) f(1) f(2) f(1) f(0)
  • 12. Recursive Algorithms procedure iterative_fibo(n: nonnegative integer) if n = 0 then y := 0 else begin  x := 0  y := 1  for i := 1 to n-1  begin  z := x + y  x : = y  y := z  end end {y is the n-th Fibonacci number} 12
  • 13. Recursive Algorithms For every recursive algorithm, there is an equivalent iterative algorithm. Recursive algorithms are often shorter, more elegant, and easier to understand than their iterative counterparts. However, iterative algorithms are usually more efficient in their use of space and time. 13