SlideShare a Scribd company logo
Time Complexity of Recursive
Algorithm
By Ms. Neelam Thapa
A recurrence relation is a mathematical
expression that defines a sequence in
terms of its previous terms.
General form of a Recurrence Relation:
where f is a function that defines the relationship between
the current term and the previous terms
Example Recurrence Relation
Fibonacci Sequence F(n) = F(n-1) + F(n-2)
Factorial of a number
n
F(n) = n * F(n-1)
Merge Sort T(n) = 2*T(n/2) + O(n)
Tower of Hanoi H(n) = 2*H(n-1) + 1
Binary Search T(n) = T(n/2) + 1
Examples of Recurrence Relations
Recursion & Recurrence Relations
•Recurrence Relation:
Defines T(n) in terms of earlier values
e.g., T(n) = T(n–1) + n.
•Recursion:
Solving a problem by reducing it to smaller
instances of the same problem.
Types of Recurrence Relations
• Linear – depends on previous terms
(e.g., T(n)=T(n–1)+T(n–2)+T(n–3))
• Divide & Conquer – splits input into parts
(e.g., T(n)=3T(n/2)+9n)
• First-Order – depends only on immediately preceding
term
(e.g., T(n)=2T(n–1)^2)
• Higher-Order – depends on multiple previous terms
(e.g., T(n)=2T(n–1)^2 + kT(n–2) + T(n–3))
3 Methods for Solving Recurrences
1. Substitution (Forward & Backward)
2. Recursion Tree
3. Master Theorem
Forward Substitution – Steps
Pick recurrence and base case
Expand: express T(n–1), T(n–2)…
Observe the pattern
Guess the closed form
Prove via induction
Recurrence Problem:
T(n) = T(n-1) + n, n>1
T(n) = 1, n=1
1. Pick Recurrence and the given initial
Condition:
T(n)=T(n-1)+n, n>1 T(n)=1, n=1
2. Put the value from previous recurrence into the next
recurrence:
T(1) = 1T(2) = T(1) + 2 = 1 + 2 = 3T(3) = T(2) + 3 = 1 + 2 + 3 = 6T(4)= T(3) + 4 = 1 + 2 +
3 + 4 = 10
3. Observe and Guess the pattern and the time:
So guessed pattern will be-T(n) = 1 + 2 + 3 .... + n = (n * (n+1))/2Time Complexity will
be O(n2
)
4. Prove that the guessed result is correct using mathematical Induction:
Prove T(1) is true:
T(1) = 1 * (1+1)/2 = 2/2 = 1 and from definition of recurrence we know T(1) = 1.
Hence proved T(1) is true
Assume T(N-1) to be true:
Assume T(N-1) = ((N - 1) * (N-1+1))/2 = (N * (N-1))/2 to be true
Then prove T(N) will be true:T(N) = T(N-1) + N from recurrence definition
Now, T(N-1) = N * (N-1)/2So, T(N) = T(N-1) + N = (N * (N-1))/2 + N = (N * (N-1) +
2N)/2 =N * (N+1)/2And from our guess also T(N)=N(N+1)/2
Hence T(N) is true.
Therefore our guess was correct and time will be O(N2
)
Backward Substitution – Steps
● Write T(n-1), T(n-2), … in terms of T(n)
● Substitute backwards until base case
● Sum up initial condition contributions
Recurrence Problem: T(n) = T(n-1) + n, n>1 T(n) = 1, n=1
1. Take the main recurrence and try to write recurrences of previous terms:
T(n) = T(n-1) + nT(n-1) = T(n-2) + n - 1T(n-2) = T(n-3) + n - 2
3. Again take one more previous recurrence and substitute into main recurrence
put T(n-2) into T(n)So, T(n)=T(n-3)+ n-2 + n-1 + n
2. Take just previous recurrence and substitute into main recurrence
put T(n-2) into T(n)So, T(n)=T(n-3)+ n-2 + n-1 + n
4. Again take one more previous recurrence and substitute into main recurrence
So similarly we can find T(n-3), T(n-4)......and so on and can insert into T(n).
Eventually we will get following: T(n)=T(1) + 2 + 3 + 4 +.........+ n-1 + n
5. After this substitute the the value from initial condition and get the solution
Put T(1)=1, T(n) = 1 +2 +3 + 4 +..............+ n-1 + n = n(n+1)/2.
So Time will be O(N2
)
Limitations of Substitution Method
• Requires guessing the correct form
• No systematic approach for selecting the guess
• May yield approximate rather than tight solutions
• Can be cumbersome for complex recurrences
https://www.geeksforgeeks.org/dsa/how-to-solve-time-c
omplexity-recurrence-relations-using-recursion-tree-me
thod
/
Recursion tree method theory take from here
https://www.geeksforgeeks.org/dsa/advanced-master-t
heorem-for-divide-and-conquer-recurrences
/
For master theorem theory
Time Complexity of Recursive Functions.pptx
Time Complexity of Recursive Functions.pptx
Time Complexity of Recursive Functions.pptx
Time Complexity of Recursive Functions.pptx
Time Complexity of Recursive Functions.pptx
Time Complexity of Recursive Functions.pptx
Time Complexity of Recursive Functions.pptx
Time Complexity of Recursive Functions.pptx
Time Complexity of Recursive Functions.pptx
Time Complexity of Recursive Functions.pptx

More Related Content

PPTX
PCC_CS_404_OUCHITYAPRODHAN_17000124029[1].pptx
PPT
Time complexity
PPT
recurrence relation is explained in this
PDF
Recurrence relation
PDF
Recurrences
PPT
Analysis Of Algorithms Ii
PDF
Recurrence relation solutions
PPTX
recurrence relation on ca1 topic by makaut.pptx
PCC_CS_404_OUCHITYAPRODHAN_17000124029[1].pptx
Time complexity
recurrence relation is explained in this
Recurrence relation
Recurrences
Analysis Of Algorithms Ii
Recurrence relation solutions
recurrence relation on ca1 topic by makaut.pptx

Similar to Time Complexity of Recursive Functions.pptx (20)

PPTX
solving_Recurrence_relations_using_methods1.pptx
PPT
recurrence relations in analysis of algorithm
PDF
3.pdf
PDF
Lecture 5 6_7 - divide and conquer and method of solving recurrences
PPTX
RECURRENCE EQUATIONS & ANALYZING THEM
PPTX
2.pptx
PPTX
solving_recurrence_relations_using_methods.pptx
PPT
04_Recurrences_1.ppt
PPTX
Solving recurrences
PPTX
3. D&C and Recurrence Relation.ppYtxVVVV
PPT
Recurrences
PDF
P1-07-Recurrences.pdf
PDF
8.-DAA-LECTURE-8-RECURRENCES-AND-ITERATION-METHOD.pdf
PPTX
T2311 - Ch 4_Part1.pptx
PPTX
Complexity Analysis of Recursive Function
PPTX
Algorithm Design and Complexity - Course 3
PPTX
recurence solutions
PDF
Solving recurrences
PDF
Solving recurrences
solving_Recurrence_relations_using_methods1.pptx
recurrence relations in analysis of algorithm
3.pdf
Lecture 5 6_7 - divide and conquer and method of solving recurrences
RECURRENCE EQUATIONS & ANALYZING THEM
2.pptx
solving_recurrence_relations_using_methods.pptx
04_Recurrences_1.ppt
Solving recurrences
3. D&C and Recurrence Relation.ppYtxVVVV
Recurrences
P1-07-Recurrences.pdf
8.-DAA-LECTURE-8-RECURRENCES-AND-ITERATION-METHOD.pdf
T2311 - Ch 4_Part1.pptx
Complexity Analysis of Recursive Function
Algorithm Design and Complexity - Course 3
recurence solutions
Solving recurrences
Solving recurrences
Ad

More from neelam108thapa (7)

PPT
Lecture 1 - Overview of Data Structure .ppt
PPTX
VILASINI RADHA DD CHATUSLOKI BAGVATAM (2).pptx
PPTX
Neelam-TE-A-CLASS INCHARGE ORIENTATION.pptx
PPTX
HARE KRISHNA Givinda Hare Murari gopinath.pptx
PPTX
THE BEAUTY OF LORD KRISHNA by vilsiniradha dd.pptx
PPTX
UNSDG Theme & Problem statement pdf.pptx
PPTX
VILASINI RADHA DD CHATUSLOKI BAGVATAM (3).pptx
Lecture 1 - Overview of Data Structure .ppt
VILASINI RADHA DD CHATUSLOKI BAGVATAM (2).pptx
Neelam-TE-A-CLASS INCHARGE ORIENTATION.pptx
HARE KRISHNA Givinda Hare Murari gopinath.pptx
THE BEAUTY OF LORD KRISHNA by vilsiniradha dd.pptx
UNSDG Theme & Problem statement pdf.pptx
VILASINI RADHA DD CHATUSLOKI BAGVATAM (3).pptx
Ad

Recently uploaded (20)

PDF
Insiders guide to clinical Medicine.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Basic Mud Logging Guide for educational purpose
PDF
Computing-Curriculum for Schools in Ghana
PDF
Classroom Observation Tools for Teachers
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
RMMM.pdf make it easy to upload and study
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Pharma ospi slides which help in ospi learning
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Complications of Minimal Access Surgery at WLH
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Sports Quiz easy sports quiz sports quiz
Insiders guide to clinical Medicine.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Basic Mud Logging Guide for educational purpose
Computing-Curriculum for Schools in Ghana
Classroom Observation Tools for Teachers
VCE English Exam - Section C Student Revision Booklet
RMMM.pdf make it easy to upload and study
Microbial disease of the cardiovascular and lymphatic systems
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Pharma ospi slides which help in ospi learning
O7-L3 Supply Chain Operations - ICLT Program
Module 4: Burden of Disease Tutorial Slides S2 2025
Supply Chain Operations Speaking Notes -ICLT Program
Complications of Minimal Access Surgery at WLH
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPH.pptx obstetrics and gynecology in nursing
Anesthesia in Laparoscopic Surgery in India
Renaissance Architecture: A Journey from Faith to Humanism
Sports Quiz easy sports quiz sports quiz

Time Complexity of Recursive Functions.pptx

  • 1. Time Complexity of Recursive Algorithm By Ms. Neelam Thapa
  • 2. A recurrence relation is a mathematical expression that defines a sequence in terms of its previous terms. General form of a Recurrence Relation: where f is a function that defines the relationship between the current term and the previous terms
  • 3. Example Recurrence Relation Fibonacci Sequence F(n) = F(n-1) + F(n-2) Factorial of a number n F(n) = n * F(n-1) Merge Sort T(n) = 2*T(n/2) + O(n) Tower of Hanoi H(n) = 2*H(n-1) + 1 Binary Search T(n) = T(n/2) + 1 Examples of Recurrence Relations
  • 4. Recursion & Recurrence Relations •Recurrence Relation: Defines T(n) in terms of earlier values e.g., T(n) = T(n–1) + n. •Recursion: Solving a problem by reducing it to smaller instances of the same problem.
  • 5. Types of Recurrence Relations • Linear – depends on previous terms (e.g., T(n)=T(n–1)+T(n–2)+T(n–3)) • Divide & Conquer – splits input into parts (e.g., T(n)=3T(n/2)+9n) • First-Order – depends only on immediately preceding term (e.g., T(n)=2T(n–1)^2) • Higher-Order – depends on multiple previous terms (e.g., T(n)=2T(n–1)^2 + kT(n–2) + T(n–3))
  • 6. 3 Methods for Solving Recurrences 1. Substitution (Forward & Backward) 2. Recursion Tree 3. Master Theorem
  • 7. Forward Substitution – Steps Pick recurrence and base case Expand: express T(n–1), T(n–2)… Observe the pattern Guess the closed form Prove via induction
  • 8. Recurrence Problem: T(n) = T(n-1) + n, n>1 T(n) = 1, n=1 1. Pick Recurrence and the given initial Condition: T(n)=T(n-1)+n, n>1 T(n)=1, n=1 2. Put the value from previous recurrence into the next recurrence: T(1) = 1T(2) = T(1) + 2 = 1 + 2 = 3T(3) = T(2) + 3 = 1 + 2 + 3 = 6T(4)= T(3) + 4 = 1 + 2 + 3 + 4 = 10 3. Observe and Guess the pattern and the time: So guessed pattern will be-T(n) = 1 + 2 + 3 .... + n = (n * (n+1))/2Time Complexity will be O(n2 )
  • 9. 4. Prove that the guessed result is correct using mathematical Induction: Prove T(1) is true: T(1) = 1 * (1+1)/2 = 2/2 = 1 and from definition of recurrence we know T(1) = 1. Hence proved T(1) is true Assume T(N-1) to be true: Assume T(N-1) = ((N - 1) * (N-1+1))/2 = (N * (N-1))/2 to be true Then prove T(N) will be true:T(N) = T(N-1) + N from recurrence definition Now, T(N-1) = N * (N-1)/2So, T(N) = T(N-1) + N = (N * (N-1))/2 + N = (N * (N-1) + 2N)/2 =N * (N+1)/2And from our guess also T(N)=N(N+1)/2 Hence T(N) is true. Therefore our guess was correct and time will be O(N2 )
  • 10. Backward Substitution – Steps ● Write T(n-1), T(n-2), … in terms of T(n) ● Substitute backwards until base case ● Sum up initial condition contributions
  • 11. Recurrence Problem: T(n) = T(n-1) + n, n>1 T(n) = 1, n=1 1. Take the main recurrence and try to write recurrences of previous terms: T(n) = T(n-1) + nT(n-1) = T(n-2) + n - 1T(n-2) = T(n-3) + n - 2 3. Again take one more previous recurrence and substitute into main recurrence put T(n-2) into T(n)So, T(n)=T(n-3)+ n-2 + n-1 + n 2. Take just previous recurrence and substitute into main recurrence put T(n-2) into T(n)So, T(n)=T(n-3)+ n-2 + n-1 + n 4. Again take one more previous recurrence and substitute into main recurrence So similarly we can find T(n-3), T(n-4)......and so on and can insert into T(n). Eventually we will get following: T(n)=T(1) + 2 + 3 + 4 +.........+ n-1 + n 5. After this substitute the the value from initial condition and get the solution Put T(1)=1, T(n) = 1 +2 +3 + 4 +..............+ n-1 + n = n(n+1)/2. So Time will be O(N2 )
  • 12. Limitations of Substitution Method • Requires guessing the correct form • No systematic approach for selecting the guess • May yield approximate rather than tight solutions • Can be cumbersome for complex recurrences