SlideShare a Scribd company logo
2
Most read
7
Most read
15
Most read
‘COMPLEXITY ANALYSIS OF RECURSIVE FUNCTION’
PRESENTED BY: PRASHI JAIN
ROLL NO. : MCA/25022/18
RECURSIVE ALGORITHM
A recursive function is a function that is defined in terms of itself. Similarly, an algorithm is said to
be recursive if the same algorithm is invoked in the body.
Direct recursion occurs when an algorithm calls itself .
Indirect recursion occurs when a function calls another function , eventually resulting in the original
method being called again.
A
A
A
B
PROPERTIES
• A recursive function can go infinite like a loop. To avoid infinite running of
recursive function, there are two properties that a recursive function must have −
Base criteria − There must be at least one base criteria or condition, such that, when this
condition is met the function stops calling itself recursively.
 Progressive approach − The recursive calls should progress in such a way that each time
a recursive call is made it comes closer to the base criteria.
ANALYSIS OF RECURSION
• One may argue why to use recursion, as the same task can be done with
iteration. The first reason is, recursion makes a program more readable and
because of latest enhanced CPU systems, recursion is more efficient than
iterations.
• There are mainly two ways for analysis of any algorithm :-
1- Time Complexity
2- Space Complexity
TIME COMPLEXITY
Time complexity is the amount of time it takes to run an algorithm.
In case of iterations, we take number of iterations to count the time complexity.
Likewise, in case of recursion, assuming everything is constant, we try to figure out
the number of times a recursive call is being made.
A call made to a function is Ο(1), hence the (n) number of times a recursive call is
made makes the recursive function Ο(n).
SPACE COMPLEXITY:
Space complexity is counted as what amount of extra space is required for a module to
execute.
 In case of iterations, the compiler hardly requires any extra space. The compiler
keeps updating the values of variables used in the iterations.
 In case of recursion, the system needs to store activation record each time a recursive
call is made. Hence, it is considered that space complexity of recursive function may
go higher than that of a function with iteration.
FINDING COMPLEXITY USING TFC
• We can find complexity of iteration algorithm using frequency count.
For example:
For(i=1;i<n;i++) n times
{
For(j=1 ; j<n ;j++) (n-1)n times
{
Statement; n2 times
}
}
The total frequency count for the above algorithm is 4n2 -1. Then the complexity of algorithm is O(n2).
WHY RECURRENCE RELATION ?
• When we use tfc (total frequency count) method for analysis of any algorithm then it is useful
for many algorithms . But Many algorithms such as Tower of Hanoi, selection sort , binary
search , Fibonacci and many more , where we use a recursive then finding the complexity of
that using tfc is not possible , because you can not exactly find out how many times a statement
will execute in a recursive function.
• For solving the above problem we use recurrence relation to find out the complexity of any
recursive algorithm.
WHAT IS A RECURRENCE RELATION?
When analysing a recursive function for its step count ( running time), we often obtain a recursive
formula. These recursive formulas are referred to as recurrence relations which are solved by repeated
substitutions method.
A recurrence relation, T(n), is a recursive function of an integer variable n.
Example:
1 if n=0
T(n)=
1+T(n-1) for n>o
The portion of the definition that does not contain T is called the base case of the recurrence relation.
The portion that contains T is called the recurrent or recursive case.
STEPS FOR FINDING COMPLEXITY OF RECURSIVE FUNCTION
 forming a recurrence relation
 solving the recurrence relation
FORMING A RECURRENCE RELATION
Example
function factorial(n)
1-2. if (n=1) then factorial=1;
Or else
3. Factorial =n*factorial(n-1)
And end factorial
The base case is reached when n = = 0. The method performs one comparison. Thus, the number of
operations when n = = 0, T(0), is some constant a.
When n > 0, the method performs two basic operations and then calls itself, using ONE recursive call, with
a parameter n – 1.
• Therefore the recurrence relation is:
T(0) = a for some constant a
T(n) = b + T(n – 1) for some constant b
SOLVING RECURRENCE RELATIONS
• Steps:
 Expand the recurrence relation.
 Express the expansion as a summation by plugging the recurrence back into itself
until you see a pattern.
 Evaluate the summation
FOR EXAMPLE: TOWER OF HANOI PROBLEM
Complexity Analysis of Recursive Function
ALGORITHM : TOWER OF HANOI
TOH(n ,x , y , z)
{
If(n>=1)
{
//put (n-1) disk to z by using y
TOH((n-1), x ,z , y)
// move larger disk to right place
Move : x- -> y
//put (n-1) disk to right place
TOH((n-1), z ,y ,x)
}
x y z
SOLUTION:
• The recurrence relation for the running time of the method hanoi is:
T(n) = a if n = 1
T(n) = 2T(n - 1) + b if n > 1
Expanding:
T(1) = a (1)
T(n) = 2T(n – 1) + b if n > 1 (2)
= 2[2T(n – 2) + b] + b = 22 T(n – 2) + 2b + b by substituting T(n – 1) in (2)
= 22 [2T(n – 3) + b] + 2b + b = 23 T(n – 3) + 22b + 2b + b by substituting T(n-2) in (2)
= 23 [2T(n – 4) + b] + 22b + 2b + b = 24 T(n – 4) + 23 b + 22b + 21b + 20b by substituting
T(n – 3) in (2)
= ……
= 2k T(n – k) + b[2k- 1 + 2k– 2 + . . . 21 + 20]
The base case is reached when n – k = 1  k = n – 1, we then have:
Therefore the complexity of Tower of Hanoi is O(2n).
REFERENCE:
Book:
Data structure and algorithms (GAV PAI)
Websites:
www.tutorialspoint.com
THANK YOU

More Related Content

PPTX
Bruteforce algorithm
PPTX
Graph data structure and algorithms
PPT
Divide and Conquer
PPTX
File system structure
PPT
PDF
Red black tree
PDF
Rabin karp string matcher
PDF
Searching and Sorting Techniques in Data Structure
Bruteforce algorithm
Graph data structure and algorithms
Divide and Conquer
File system structure
Red black tree
Rabin karp string matcher
Searching and Sorting Techniques in Data Structure

What's hot (20)

PPT
Transactions in dbms
PPT
Lecture 1 - Lexical Analysis.ppt
PPTX
Insertion Sorting
PPTX
Hashing
PPTX
Protection Domain and Access Matrix Model -Operating System
PPT
Dinive conquer algorithm
PPT
Hash tables
PPTX
Huffman coding
DOCX
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
PPT
Binary search tree(bst)
PPSX
Functional dependency
PPTX
daa-unit-3-greedy method
PPTX
Top down and botttom up Parsing
PPTX
6-Practice Problems - LL(1) parser-16-05-2023.pptx
PDF
UNIT-V.pdf daa unit material 5 th unit ppt
PPTX
Regular expressions
PPSX
Data Structure (Queue)
PPTX
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
PPTX
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
PPT
Disk structure
Transactions in dbms
Lecture 1 - Lexical Analysis.ppt
Insertion Sorting
Hashing
Protection Domain and Access Matrix Model -Operating System
Dinive conquer algorithm
Hash tables
Huffman coding
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
Binary search tree(bst)
Functional dependency
daa-unit-3-greedy method
Top down and botttom up Parsing
6-Practice Problems - LL(1) parser-16-05-2023.pptx
UNIT-V.pdf daa unit material 5 th unit ppt
Regular expressions
Data Structure (Queue)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Disk structure
Ad

Similar to Complexity Analysis of Recursive Function (20)

PDF
lec 03wweweweweweweeweweweewewewewee.pdf
PDF
Lecture 5 6_7 - divide and conquer and method of solving recurrences
PPT
algo_vc_lecture8.ppt
PPTX
PCC_CS_404_OUCHITYAPRODHAN_17000124029[1].pptx
PPTX
Algorithm Design and Complexity - Course 3
PPTX
Recursive Algorithm Detailed Explanation
PPTX
3. D&C and Recurrence Relation.ppYtxVVVV
PPTX
Chapter 3 - Methods of Algorithm Analysis.pptx
PDF
Copy of y16 02-2119divide-and-conquer
PPTX
Time Complexity of Recursive Functions.pptx
PDF
3.pdf
PPT
Analysis Of Algorithms Ii
PPTX
Recursion in Data Structure
PPTX
2.pptx
PPT
CS8451 - Design and Analysis of Algorithms
PPT
recurrence relation is explained in this
PDF
Recurrence relation
PPTX
Analysis of Algorithms, recurrence relation, solving recurrences
PPTX
designs and analysis of algorithmss.pptx
lec 03wweweweweweweeweweweewewewewee.pdf
Lecture 5 6_7 - divide and conquer and method of solving recurrences
algo_vc_lecture8.ppt
PCC_CS_404_OUCHITYAPRODHAN_17000124029[1].pptx
Algorithm Design and Complexity - Course 3
Recursive Algorithm Detailed Explanation
3. D&C and Recurrence Relation.ppYtxVVVV
Chapter 3 - Methods of Algorithm Analysis.pptx
Copy of y16 02-2119divide-and-conquer
Time Complexity of Recursive Functions.pptx
3.pdf
Analysis Of Algorithms Ii
Recursion in Data Structure
2.pptx
CS8451 - Design and Analysis of Algorithms
recurrence relation is explained in this
Recurrence relation
Analysis of Algorithms, recurrence relation, solving recurrences
designs and analysis of algorithmss.pptx
Ad

More from Meghaj Mallick (20)

PPT
24 partial-orderings
PPTX
PORTFOLIO BY USING HTML & CSS
PPTX
Introduction to Software Testing
PPTX
Introduction to System Programming
PPTX
MACRO ASSEBLER
PPTX
Icons, Image & Multimedia
PPTX
Project Tracking & SPC
PPTX
Peephole Optimization
PPTX
Routing in MANET
PPTX
Macro assembler
PPTX
Architecture and security in Vanet PPT
PPTX
Design Model & User Interface Design in Software Engineering
PPTX
Text Mining of Twitter in Data Mining
PPTX
DFS & BFS in Computer Algorithm
PPTX
Software Development Method
PPTX
Secant method in Numerical & Statistical Method
PPTX
Motivation in Organization
PPTX
Communication Skill
PPT
Partial-Orderings in Discrete Mathematics
PPTX
Hashing In Data Structure
24 partial-orderings
PORTFOLIO BY USING HTML & CSS
Introduction to Software Testing
Introduction to System Programming
MACRO ASSEBLER
Icons, Image & Multimedia
Project Tracking & SPC
Peephole Optimization
Routing in MANET
Macro assembler
Architecture and security in Vanet PPT
Design Model & User Interface Design in Software Engineering
Text Mining of Twitter in Data Mining
DFS & BFS in Computer Algorithm
Software Development Method
Secant method in Numerical & Statistical Method
Motivation in Organization
Communication Skill
Partial-Orderings in Discrete Mathematics
Hashing In Data Structure

Recently uploaded (20)

PDF
Instagram's Product Secrets Unveiled with this PPT
PPTX
What is Clause, definition and structure
PPTX
Presentation for DGJV QMS (PQP)_12.03.2025.pptx
PPTX
Understanding-Communication-Berlos-S-M-C-R-Model.pptx
PPTX
Caption Text about Social Media Post in Internet
PPTX
Cohort Study_PPT.group presentation_pdf.pptx
PDF
Swiggy’s Playbook: UX, Logistics & Monetization
PPTX
Project and change Managment: short video sequences for IBA
PDF
Parts of Speech Prepositions Presentation in Colorful Cute Style_20250724_230...
PPTX
Non-Verbal-Communication .mh.pdf_110245_compressed.pptx
PPTX
2025-08-03 Joseph 01 (shared slides).pptx
PPTX
Relationship Management Presentation In Banking.pptx
PPTX
Emphasizing It's Not The End 08 06 2025.pptx
PPTX
IBA DISTRICT PIR PRESENTATION.POWERPOINT
PPTX
Phrase, structure, use, definition in sentence
PPTX
Presentation of Project of Enterprenuership topic- "Green Gaurdian"
PPTX
Called To More (Final I Think) 08 03 2025.pptx
PPTX
INTERNATIONAL LABOUR ORAGNISATION PPT ON SOCIAL SCIENCE
PPTX
business communication final draftt.pptx
PPTX
Role and Responsibilities of Bangladesh Coast Guard Base, Mongla Challenges
Instagram's Product Secrets Unveiled with this PPT
What is Clause, definition and structure
Presentation for DGJV QMS (PQP)_12.03.2025.pptx
Understanding-Communication-Berlos-S-M-C-R-Model.pptx
Caption Text about Social Media Post in Internet
Cohort Study_PPT.group presentation_pdf.pptx
Swiggy’s Playbook: UX, Logistics & Monetization
Project and change Managment: short video sequences for IBA
Parts of Speech Prepositions Presentation in Colorful Cute Style_20250724_230...
Non-Verbal-Communication .mh.pdf_110245_compressed.pptx
2025-08-03 Joseph 01 (shared slides).pptx
Relationship Management Presentation In Banking.pptx
Emphasizing It's Not The End 08 06 2025.pptx
IBA DISTRICT PIR PRESENTATION.POWERPOINT
Phrase, structure, use, definition in sentence
Presentation of Project of Enterprenuership topic- "Green Gaurdian"
Called To More (Final I Think) 08 03 2025.pptx
INTERNATIONAL LABOUR ORAGNISATION PPT ON SOCIAL SCIENCE
business communication final draftt.pptx
Role and Responsibilities of Bangladesh Coast Guard Base, Mongla Challenges

Complexity Analysis of Recursive Function

  • 1. ‘COMPLEXITY ANALYSIS OF RECURSIVE FUNCTION’ PRESENTED BY: PRASHI JAIN ROLL NO. : MCA/25022/18
  • 2. RECURSIVE ALGORITHM A recursive function is a function that is defined in terms of itself. Similarly, an algorithm is said to be recursive if the same algorithm is invoked in the body. Direct recursion occurs when an algorithm calls itself . Indirect recursion occurs when a function calls another function , eventually resulting in the original method being called again. A A A B
  • 3. PROPERTIES • A recursive function can go infinite like a loop. To avoid infinite running of recursive function, there are two properties that a recursive function must have − Base criteria − There must be at least one base criteria or condition, such that, when this condition is met the function stops calling itself recursively.  Progressive approach − The recursive calls should progress in such a way that each time a recursive call is made it comes closer to the base criteria.
  • 4. ANALYSIS OF RECURSION • One may argue why to use recursion, as the same task can be done with iteration. The first reason is, recursion makes a program more readable and because of latest enhanced CPU systems, recursion is more efficient than iterations. • There are mainly two ways for analysis of any algorithm :- 1- Time Complexity 2- Space Complexity
  • 5. TIME COMPLEXITY Time complexity is the amount of time it takes to run an algorithm. In case of iterations, we take number of iterations to count the time complexity. Likewise, in case of recursion, assuming everything is constant, we try to figure out the number of times a recursive call is being made. A call made to a function is Ο(1), hence the (n) number of times a recursive call is made makes the recursive function Ο(n).
  • 6. SPACE COMPLEXITY: Space complexity is counted as what amount of extra space is required for a module to execute.  In case of iterations, the compiler hardly requires any extra space. The compiler keeps updating the values of variables used in the iterations.  In case of recursion, the system needs to store activation record each time a recursive call is made. Hence, it is considered that space complexity of recursive function may go higher than that of a function with iteration.
  • 7. FINDING COMPLEXITY USING TFC • We can find complexity of iteration algorithm using frequency count. For example: For(i=1;i<n;i++) n times { For(j=1 ; j<n ;j++) (n-1)n times { Statement; n2 times } } The total frequency count for the above algorithm is 4n2 -1. Then the complexity of algorithm is O(n2).
  • 8. WHY RECURRENCE RELATION ? • When we use tfc (total frequency count) method for analysis of any algorithm then it is useful for many algorithms . But Many algorithms such as Tower of Hanoi, selection sort , binary search , Fibonacci and many more , where we use a recursive then finding the complexity of that using tfc is not possible , because you can not exactly find out how many times a statement will execute in a recursive function. • For solving the above problem we use recurrence relation to find out the complexity of any recursive algorithm.
  • 9. WHAT IS A RECURRENCE RELATION? When analysing a recursive function for its step count ( running time), we often obtain a recursive formula. These recursive formulas are referred to as recurrence relations which are solved by repeated substitutions method. A recurrence relation, T(n), is a recursive function of an integer variable n. Example: 1 if n=0 T(n)= 1+T(n-1) for n>o The portion of the definition that does not contain T is called the base case of the recurrence relation. The portion that contains T is called the recurrent or recursive case.
  • 10. STEPS FOR FINDING COMPLEXITY OF RECURSIVE FUNCTION  forming a recurrence relation  solving the recurrence relation
  • 11. FORMING A RECURRENCE RELATION Example function factorial(n) 1-2. if (n=1) then factorial=1; Or else 3. Factorial =n*factorial(n-1) And end factorial The base case is reached when n = = 0. The method performs one comparison. Thus, the number of operations when n = = 0, T(0), is some constant a. When n > 0, the method performs two basic operations and then calls itself, using ONE recursive call, with a parameter n – 1. • Therefore the recurrence relation is: T(0) = a for some constant a T(n) = b + T(n – 1) for some constant b
  • 12. SOLVING RECURRENCE RELATIONS • Steps:  Expand the recurrence relation.  Express the expansion as a summation by plugging the recurrence back into itself until you see a pattern.  Evaluate the summation
  • 13. FOR EXAMPLE: TOWER OF HANOI PROBLEM
  • 15. ALGORITHM : TOWER OF HANOI TOH(n ,x , y , z) { If(n>=1) { //put (n-1) disk to z by using y TOH((n-1), x ,z , y) // move larger disk to right place Move : x- -> y //put (n-1) disk to right place TOH((n-1), z ,y ,x) } x y z
  • 16. SOLUTION: • The recurrence relation for the running time of the method hanoi is: T(n) = a if n = 1 T(n) = 2T(n - 1) + b if n > 1
  • 17. Expanding: T(1) = a (1) T(n) = 2T(n – 1) + b if n > 1 (2) = 2[2T(n – 2) + b] + b = 22 T(n – 2) + 2b + b by substituting T(n – 1) in (2) = 22 [2T(n – 3) + b] + 2b + b = 23 T(n – 3) + 22b + 2b + b by substituting T(n-2) in (2) = 23 [2T(n – 4) + b] + 22b + 2b + b = 24 T(n – 4) + 23 b + 22b + 21b + 20b by substituting T(n – 3) in (2) = …… = 2k T(n – k) + b[2k- 1 + 2k– 2 + . . . 21 + 20] The base case is reached when n – k = 1  k = n – 1, we then have: Therefore the complexity of Tower of Hanoi is O(2n).
  • 18. REFERENCE: Book: Data structure and algorithms (GAV PAI) Websites: www.tutorialspoint.com