DR. B.C.
ROY ENGINEERING COLLEGE, DURGAPUR
Name of the Exam – CA1
Title :- Analysis of Recursive Algorithms through Recurrence Relations: Master’s Theorem
Paper name:-Design and Analysis of Algorithms
Paper Code :– PCC-CS 404
Department :– CSE(DS)
Semester :- 4th
Academic Year:- 2024-2025
Name:-Ankur Mondal Assigned Professor for the paper:- Dr. Saibal Majumder
University roll no. :- 12030523017 Designation :- Assistant Professor, CSE(DS)
Introduction to Recursive algorithm
A recursive algorithm is a method of solving a problem by breaking it down into smaller, more manageable subproblems that
are similar in nature to the original problem. Each subproblem is solved using the same algorithm, and the results are combined to
solve the larger problem.
•Key Idea: The algorithm calls itself with a smaller input, progressing toward a base case (the simplest instance of the problem that
can be solved directly).
•Base Case: The condition that stops the recursion (prevents infinite loops).
•Recursive Case: The part where the algorithm calls itself with a smaller or simpler input.
Example - Factorial of a number
The factorial of a number n (denoted as n!) is the product of all positive integers less than or equal to n
Recursive Definition:
1 If n = 0 (base case)
n! = ቊ
𝑛× 𝑛−1 ! If n > 0 (recursive case)
5! = 5×4! = 5×4×3! = ⋯ = 120.
What Are Recurrence Relations?
A recurrence relation is a mathematical expression that defines a sequence in terms of its previous terms. In the
context of algorithmic analysis, it is often used to model the time complexity of recursive algorithms.
General form of a Recurrence Relation: an = f(an−1,an−2,....,an−k)
where f is a function that defines the relationship between the current term and the previous terms
Example:
• Factorial: T(n)=T(n−1)+O(1)
• Merge Sort: T(n)=2T(n/2)+O(n)
Types of Recurrence Relation:
1. Linear Recurrence Relation
2. Divide and conquer recurrences
3. Substitution recurrences
4. Homogeneous recurrences
5. Non-Homogeneous recurrences
Ways to Solve Recurrence Relations:
• Substitute the input size into the recurrence relation to obtain a sequence of terms.
• Identify a pattern in the sequence of terms, if any, and simplify the recurrence relation to obtain a closed-form expression for
the number of operations performed by the algorithm.
• Determine the order of growth of the closed-form expression by using techniques such as the Master Theorem, or by finding
the dominant term and ignoring lower-order terms.
• Use the order of growth to determine the asymptotic upper bound on the running time of the algorithm, which can be
expressed in terms of big O notation.
Introduction to Master’s Theorem
Master’s theorem is one of the many methods that are applied to calculate time complexities of algorithms. In
analysis, time complexities are calculated to find out the best optimal logic of an algorithm. Master’s theorem is
applied on recurrence relations.
Master’s theorem can only be applied on decreasing and dividing recurring functions. If the relation is not
decreasing or dividing, master’s theorem must not be applied.
Consider a relation of type −
T(n) = a T(n/b) + f(n) ------------- where a ≥ 1, b > 1, f(n) = Θ(nk logab), is asymptotically positive
Here,
n − size of the problem
a − number of sub-problems in the recursion
n-b − size of the sub problems based on the assumption that all sub-problems are of the same size.
f(n) − represents the cost of work done outside the recursion → Θ(nk), where k ≥ 0.
If the recurrence relation is in the above given form, then there are three cases in the master
theorem to determine the asymptotic notations.
We assume two values forever:
• logba
• The power of n, ie. k
Case 1: logba > k, then T(n) = Θ (nlogba)
Case 2: logba = k
case (i), if p>-1, then
T(n) = Θ(nk log p+1 n)
case (ii), if p = -1, then
T(n) = Θ(nklog log n)
case (iii), if p < -1, then
T(n) = Θ(nk)
Case 3: logba < k, then
case (i) if p≥ 0 then
T(n) = Θ(nklogpn)
case (ii) if p<0 then
T(n) = Θ(nk)
How Master’s Theorem works:
The master method is mainly derived from the recurrence tree method. If we draw the recurrence tree of T(n) = aT(n/b)
+ f(n), we can see that the work done at the root is f(n), and work done at all leaves is Θ(nc) where c is Logba. And the
height of the recurrence tree is Logbn
In the recurrence tree method, we calculate the total work done. If the work done at leaves is polynomially more, then
leaves are the dominant part, and our result becomes the work done at leaves (Case 1). If work done at leaves and root is
asymptotically the same, then our result becomes height multiplied by work done at any level (Case 2). If work done at
the root is asymptotically more, then our result becomes work done at the root (Case 3).
Examples:
1. Consider the recurrence relation for Matrix Multiplication
T(n) = 8T(n/2) + n2
In this problem, a = 8, b = 2 and f(n) = Θ(nk log p n) = n2 , giving us k = 2 and p = 0.
log2 8 = 3
logba > k, Hence, case 1 must be applied for this equation.
To calculate, T(n) = Θ (nlogb a ) = nlog28 = n3
Therefore, T(n) = Θ(n3) is the tight bound for this equation.
2. Consider a recurrence relation given as T(n) = 4T(n/2) + n2
In this problem, a = 4, b = 2 and f(n) = Θ(nk log pn) = n2, giving us k = 2 and p = 0.
Logba = log24 = 2 = k and p > -1 Hence, case 2(i) must be applied for this equation.
To calculate, T(n) = Θ (nklogp+1 n) = Θ(n2 log n) is the tight bound for this equation.
Limitations of Master’s Theorem:
Master’s Theorem is a powerful tool for analyzing the time complexity of recursive algorithms, particularly those that
follow a divide-and-conquer pattern. However, it has some limitations that make it unsuitable for certain types of
recurrence relations.
•Non-Divide-and-Conquer Recurrences: Master’s Theorem only works for divide-and-conquer problems. It fails for
recurrences that do not split into equal subproblems, such as T(n)=T(n−1)+O(1)
•Variable Coefficients & Non-Standard Forms: If the recurrence changes dynamically (e.g., T(n)=2nT(n/2)+O(n),
Master’s Theorem cannot be applied.
•Fibonacci Recurrence Issue:
1. Fibonacci recurrence: T(n)=T(n−1)+T(n−2) does not fit into Master’s Theorem.
2. The problem size reduces unevenly (n−1 and n−2), breaking the required structure.
Alternative Approaches:
For such cases, use Recursion Tree Method, Substitution Method, or Characteristic Equations to determine complexity.
THANK YOU!