SlideShare uma empresa Scribd logo
1 de 14
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…

Mais conteúdo relacionado

Mais procurados

Lecture 2 predicates quantifiers and rules of inference
Lecture 2 predicates quantifiers and rules of inferenceLecture 2 predicates quantifiers and rules of inference
Lecture 2 predicates quantifiers and rules of inference
asimnawaz54
 
Mathematical induction
Mathematical inductionMathematical induction
Mathematical induction
Sman Abbasi
 
Mathematical Induction
Mathematical InductionMathematical Induction
Mathematical Induction
Edelyn Cagas
 
Number theory
Number theory Number theory
Number theory
tes31
 

Mais procurados (20)

Predicates and Quantifiers
Predicates and Quantifiers Predicates and Quantifiers
Predicates and Quantifiers
 
Chapter 2: Relations
Chapter 2: RelationsChapter 2: Relations
Chapter 2: Relations
 
Set in discrete mathematics
Set in discrete mathematicsSet in discrete mathematics
Set in discrete mathematics
 
Lecture 2 predicates quantifiers and rules of inference
Lecture 2 predicates quantifiers and rules of inferenceLecture 2 predicates quantifiers and rules of inference
Lecture 2 predicates quantifiers and rules of inference
 
Pigeon hole principle
Pigeon hole principlePigeon hole principle
Pigeon hole principle
 
Modular arithmetic
Modular arithmeticModular arithmetic
Modular arithmetic
 
Mathematical induction
Mathematical inductionMathematical induction
Mathematical induction
 
CMSC 56 | Lecture 2: Propositional Equivalences
CMSC 56 | Lecture 2: Propositional EquivalencesCMSC 56 | Lecture 2: Propositional Equivalences
CMSC 56 | Lecture 2: Propositional Equivalences
 
Mathematical induction
Mathematical inductionMathematical induction
Mathematical induction
 
Rules of inference
Rules of inferenceRules of inference
Rules of inference
 
Mathematical Induction
Mathematical InductionMathematical Induction
Mathematical Induction
 
Modular arithmetic
Modular arithmeticModular arithmetic
Modular arithmetic
 
Number theory
Number theoryNumber theory
Number theory
 
Ch 2 lattice & boolean algebra
Ch 2 lattice & boolean algebraCh 2 lattice & boolean algebra
Ch 2 lattice & boolean algebra
 
Partial-Orderings in Discrete Mathematics
 Partial-Orderings in Discrete Mathematics Partial-Orderings in Discrete Mathematics
Partial-Orderings in Discrete Mathematics
 
CMSC 56 | Lecture 6: Sets & Set Operations
CMSC 56 | Lecture 6: Sets & Set OperationsCMSC 56 | Lecture 6: Sets & Set Operations
CMSC 56 | Lecture 6: Sets & Set Operations
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
CMSC 56 | Lecture 5: Proofs Methods and Strategy
CMSC 56 | Lecture 5: Proofs Methods and StrategyCMSC 56 | Lecture 5: Proofs Methods and Strategy
CMSC 56 | Lecture 5: Proofs Methods and Strategy
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Number theory
Number theory Number theory
Number theory
 

Semelhante a Recursion DM

SURF 2012 Final Report(1)
SURF 2012 Final Report(1)SURF 2012 Final Report(1)
SURF 2012 Final Report(1)
Eric Zhang
 
02-Basic Structures .ppt
02-Basic Structures .ppt02-Basic Structures .ppt
02-Basic Structures .ppt
Acct4
 

Semelhante a 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
 

Mais de 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
 

Último

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
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
 
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
 

Último (20)

Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
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
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
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
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
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)
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
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
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 

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