In this article, we will see how we can solve different types of recurrence relations using different approaches. Before understanding this article, you should have idea about recurrence relations and different method to solve them (See : Worst, Average and Best Cases, Asymptotic Notations, Analysis of Loops).
Type 1: Divide and conquer recurrence relations –
Following are some of the examples of recurrence relations based on divide and conquer.
T(n) = 2T(n/2) + cn T(n) = 2T(n/2) + √n
These types of recurrence relations can be easily solved using Master Method.
For recurrence relation T(n) = 2T(n/2) + cn, the values of a = 2, b = 2 and k =1. Here logb(a) = log2(2) = 1 = k. Therefore, the complexity will be Θ(nlog2(n)).
Similarly for recurrence relation T(n) = 2T(n/2) + √n, the values of a = 2, b = 2 and k =1/2. Here logb(a) = log2(2) = 1 > k. Therefore, the complexity will be Θ(n).
Type 2: Linear recurrence relations –
Following are some of the examples of recurrence relations based on linear recurrence relation.
T(n) = T(n-1) + n for n>0 and T(0) = 1
These types of recurrence relations can be easily solved using substitution method.
T(n) = T(n-1) + n = T(n-2) + (n-1) + n = T(n-k) + (n-(k-1))….. (n-1) + n
Substituting k = n, we get
T(n) = T(0) + 1 + 2+….. +n = n(n+1)/2 = O(n^2)
Type 3: Value substitution before solving –
Sometimes, recurrence relations can’t be directly solved using techniques like substitution, recurrence tree or master method. Therefore, we need to convert the recurrence relation into appropriate form before solving. For example,
T(n) = T(√n) + 1
To solve this type of recurrence, substitute n = 2^m as:
T(2^m) = T(2^m /2) + 1 Let T(2^m) = S(m), S(m) = S(m/2) + 1
Solving by master method, we get
S(m) = Θ(logm) As n = 2^m or m = log2(n), T(n) = T(2^m) = S(m) = Θ(logm) = Θ(loglogn)
Let us discuss some questions based on the approaches discussed.
Que – 1. What is the time complexity of Tower of Hanoi problem?
(A) T(n) = O(sqrt(n))
(D) T(n) = O(n^2)
(C) T(n) = O(2^n)
Solution: For Tower of Hanoi, T(n) = 2T(n-1) + c for n>1 and T(1) = 1. Solving this,
T(n) = 2T(n-1) + c = 2(2T(n-2)+ c) + c = 2^2*T(n-2) + (c + 2c) = 2^k*T(n-k) + (c + 2c + .. kc) Substituting k = (n-1), we get T(n) = 2^(n-1)*T(1) + (c + 2c + (n-1)c) = O(2^n)
Que – 2. Consider the following recurrence:
T(n) = 2 * T(ceil (sqrt(n) ) ) + 1, T(1) = 1
Which one of the following is true?
(A) T(n) = (loglogn)
(B) T(n) = (logn)
(C) T(n) = (sqrt(n))
(D) T(n) = (n)
Solution: To solve this type of recurrence, substitute n = 2^m as:
T(2^m) = 2T(2^m /2) + 1 Let T(2^m) = S(m), S(m) = 2S(m/2) + 1 Solving by master method, we get S(m) = Θ(m) As n = 2^m or m = log2n, T(n) = T(2^m) = S(m) = Θ(m) = Θ(logn)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Discrete Mathematics | Types of Recurrence Relations - Set 2
- Practice Set for Recurrence Relations
- Mathematics | Closure of Relations and Equivalence Relations
- Mathematics | Introduction and types of Relations
- Mathematics | Representations of Matrices and Graphs in Relations
- Number of possible Equivalence Relations on a finite set
- Discrete Mathematics | Representing Relations
- Minimum relations satisfying First Normal Form (1NF)
- Finding the candidate keys for Sub relations using Functional Dependencies
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Different Types of RAM (Random Access Memory )
- Probability of getting two consecutive heads after choosing a random coin among two different types of coins
- Stop and Wait protocol, its problems and solutions
- Previous Year Gate CSE/IT papers with solutions
- Split a given array into K subarrays minimizing the difference between their maximum and minimum
- Types of Keys in Relational Model (Candidate, Super, Primary, Alternate and Foreign)
- Types of area networks - LAN, MAN and WAN
- Threads and its types in Operating System
- Types of DNS Attacks and Tactics for Security
- Cryptography and its Types
This article is contributed by Sonal Tuteja. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.