The Master Theorem is a tool used to solve recurrence relations that arise in the analysis of divide-and-conquer algorithms. The Master Theorem provides a systematic way of solving recurrence relations of the form:
T(n) = aT(n/b) + f(n)
- where a, b, and f(n) are positive functions and n is the size of the problem. The Master Theorem provides conditions for the solution of the recurrence to be in the form of O(n^k) for some constant k, and it gives a formula for determining the value of k.
- The advanced version of the Master Theorem provides a more general form of the theorem that can handle recurrence relations that are more complex than the basic form. The advanced version of the Master Theorem can handle recurrences with multiple terms and more complex functions.
- It is important to note that the Master Theorem is not applicable to all recurrence relations, and it may not always provide an exact solution to a given recurrence. However, it is a useful tool for analyzing the time complexity of divide-and-conquer algorithms and provides a good starting point for solving more complex recurrences.
Master Theorem is used to determine running time of algorithms (divide and conquer algorithms) in terms of asymptotic notations.
Consider a problem that is solved using recursion.
function f(input x size n) if(n < k) solve x directly and return else divide x into a subproblems of size n/b call f recursively to solve each subproblem Combine the results of all sub-problems
The above algorithm divides the problem into a subproblems, each of size n/b and solve them recursively to compute the problem and the extra work done for problem is given by f(n), i.e., the time to create the subproblems and combine their results in the above procedure.
So, according to master theorem the runtime of the above algorithm can be expressed as:
T(n) = aT(n/b) + f(n)
where n = size of the problem
a = number of subproblems in the recursion and a >= 1
n/b = size of each subproblem
f(n) = cost of work done outside the recursive calls like dividing into subproblems and cost of combining them to get the solution.
Not all recurrence relations can be solved with the use of the master theorem i.e. if
- T(n) is not monotone, ex: T(n) = sin n
- f(n) is not a polynomial, ex: T(n) = 2T(n/2) + 2n
This theorem is an advance version of master theorem that can be used to determine running time of divide and conquer algorithms if the recurrence is of the following form :-
where n = size of the problem
a = number of subproblems in the recursion and a >= 1
n/b = size of each subproblem
b > 1, k >= 0 and p is a real number.
Then,
- if a > bk, then T(n) = θ(nlogba)
- if a = bk, then
(a) if p > -1, then T(n) = θ(nlogba logp+1n)
(b) if p = -1, then T(n) = θ(nlogba loglogn)
(c) if p < -1, then T(n) = θ(nlogba)
- if a < bk, then
(a) if p >= 0, then T(n) = θ(nk logpn)
(b) if p < 0, then T(n) = θ(nk)
Time Complexity Analysis –
- Example-1: Binary Search – T(n) = T(n/2) + O(1)
a = 1, b = 2, k = 0 and p = 0
bk = 1. So, a = bk and p > -1 [Case 2.(a)]
T(n) = θ(nlogba logp+1n)
T(n) = θ(logn) - Example-2: Merge Sort – T(n) = 2T(n/2) + O(n)
a = 2, b = 2, k = 1, p = 0
bk = 2. So, a = bk and p > -1 [Case 2.(a)]
T(n) = θ(nlogba logp+1n)
T(n) = θ(nlogn) - Example-3: T(n) = 3T(n/2) + n2
a = 3, b = 2, k = 2, p = 0
bk = 4. So, a < bk and p = 0 [Case 3.(a)]
T(n) = θ(nk logpn)
T(n) = θ(n2) - Example-4: T(n) = 3T(n/2) + log2n
a = 3, b = 2, k = 0, p = 2
bk = 1. So, a > bk [Case 1]
T(n) = θ(nlogba )
T(n) = θ(nlog23) - Example-5: T(n) = 2T(n/2) + nlog2n
a = 2, b = 2, k = 1, p = 2
bk = 2. So, a = bk [Case 2.(a)]
T(n) = θ(nlogbalogp+1n )
T(n) = θ(nlog22log3n)
T(n) = θ(nlog3n) - Example-6: T(n) = 2nT(n/2) + nn
This recurrence can’t be solved using above method since function is not of form T(n) = aT(n/b) + θ(nk logpn)
GATE Practice questions –
Here are some important points to keep in mind regarding the Master Theorem:
- Divide-and-conquer recurrences: The Master Theorem is specifically designed to solve recurrence relations that arise in the analysis of divide-and-conquer algorithms.
- Form of the recurrence: The Master Theorem applies to recurrence relations of the form T(n) = aT(n/b) + f(n), where a, b, and f(n) are positive functions and n is the size of the problem.
- Time complexity: The Master Theorem provides conditions for the solution of the recurrence to be in the form of O(n^k) for some constant k, and it gives a formula for determining the value of k.
- Advanced version: The advanced version of the Master Theorem provides a more general form of the theorem that can handle recurrence relations that are more complex than the basic form.
- Limitations: The Master Theorem is not applicable to all recurrence relations, and it may not always provide an exact solution to a given recurrence.
- Useful tool: Despite its limitations, the Master Theorem is a useful tool for analyzing the time complexity of divide-and-conquer algorithms and provides a good starting point for solving more complex recurrences.
- Supplemented with other techniques: In some cases, the Master Theorem may need to be supplemented with other techniques, such as the substitution method or the iteration method, to completely solve a given recurrence relation.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!