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)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Discrete Mathematics | Types of Recurrence Relations - Set 2
- Practice Set for Recurrence Relations
- Mathematics | Introduction and types of Relations
- Mathematics | Closure of Relations and Equivalence Relations
- Number of possible Equivalence Relations on a finite set
- Discrete Mathematics | Representing Relations
- Minimum relations satisfying First Normal Form (1NF)
- Mathematics | Representations of Matrices and Graphs in Relations
- Finding the candidate keys for Sub relations using Functional Dependencies
- Stop and Wait protocol, its problems and solutions
- Previous Year Gate CSE/IT papers with solutions
- Types of Routing
- Cryptography and its Types
- Types of Network Topology
- Types of Schedules in DBMS
- Types of Cache Misses
- Types of Transmission Media
- Types of Operating Systems
- Types of Three-address codes
- Types of Attributes in ER Model
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 firstname.lastname@example.org. 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.