SlideShare ist ein Scribd-Unternehmen logo
1 von 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.

Weitere ähnliche Inhalte

Was ist angesagt?

Call by value
Call by valueCall by value
Call by value
Dharani G
 
Linear differential equation with constant coefficient
Linear differential equation with constant coefficientLinear differential equation with constant coefficient
Linear differential equation with constant coefficient
Sanjay Singh
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
Nikhil Sharma
 

Was ist angesagt? (20)

Array data structure
Array data structureArray data structure
Array data structure
 
Recursion
RecursionRecursion
Recursion
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Set in discrete mathematics
Set in discrete mathematicsSet in discrete mathematics
Set in discrete mathematics
 
Stack
StackStack
Stack
 
Selection sorting
Selection sortingSelection sorting
Selection sorting
 
Binary search
Binary searchBinary search
Binary search
 
Rolles theorem
Rolles theoremRolles theorem
Rolles theorem
 
Call by value
Call by valueCall by value
Call by value
 
Linear differential equation with constant coefficient
Linear differential equation with constant coefficientLinear differential equation with constant coefficient
Linear differential equation with constant coefficient
 
python Function
python Function python Function
python Function
 
Sequences and Series (Mathematics)
Sequences and Series (Mathematics) Sequences and Series (Mathematics)
Sequences and Series (Mathematics)
 
Big o notation
Big o notationBig o notation
Big o notation
 
Data types in python
Data types in pythonData types in python
Data types in python
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
 
Introduction to Recursion (Python)
Introduction to Recursion (Python)Introduction to Recursion (Python)
Introduction to Recursion (Python)
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 

Ähnlich wie Recursion

Binomial Theorem
Binomial TheoremBinomial Theorem
Binomial Theorem
itutor
 

Ähnlich wie Recursion (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
 

Mehr von 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
 

Kürzlich hochgeladen

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Kürzlich hochgeladen (20)

How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 

Recursion

  • 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.