The Detailed Guide to Master Method to Find the Time Complexity of Any Recursive Algorithm | by Rashida Nasrin Sucky | Towards Data Science

Recursion is one of the very essential parts of programming. Many popular algorithms are dome in recursion. So, it has a lot of importance. If recursion is important, the analysis of the time complexity of a recursive algorithm is also important. In this article, I will explain a widely used method for calculating the time complexity of a recursion. That is the Master method.
One thing to remember here is, the master method is a method to solve a recurrence. But before that, a recurrence expression needs to be drawn from the algorithm.
I will start by showing how to draw a recurrence expression from a basic and well-known algorithm. And then will explain how to solve different recurrence expression using the master method with examples.
How to draw a recurrence expression from an algorithm with an exampleThe formula of the master methodSolving the recurrence using the master methodHow to draw upper and lower bounds in the cases where the master method does not work directly.
How to draw a recurrence expression from an algorithm with an example
The formula of the master method
Solving the recurrence using the master method
How to draw upper and lower bounds in the cases where the master method does not work directly.
Let’s dive in!
For this demonstration, I chose to use a merge sort algorithm. Here is the algorithm, if you need a refresher. Here is the merge sort algorithm:
def merge(p, q, A):    n = len(p)    r = len(q)    i, j, k =0    while i < n and j < r:        if p[i] < q[j]:            A[k] = p[i]            i += 1            k += 1        else:            A[k] = q[j]            j += 1            k += 1    while i < n:        A[k] = p[i]        i += 1        k += 1    while j < r:        A[k] = q[j]        j += 1        k += 1    return Adef mergeSort(a):    while len(a) > 1:        n = len(a)        mid = int(n//2)        p = a[:mid]        q = a[mid:]        mergeSort(p)        mergeSort(q)        merge(p, q, a)        return a
I am not explaining how the merge sort algorithm works because this article is actually about solving recursion. I am assuming you already know this algorithm.
Here is how to derive the recursion expression from the merge sort algorithm:
Let, n is the length of the input array or list, and T(n) is the running time. If the problem is small enough, say, a constant then the running time will be theta(1).
Now I will introduce a few important terms:
Let, ‘a’ be the number of subproblems that come out of the division. In merge sort, you divide the original array into two parts each time. so, ‘a’ is 2.
“1/b * n” is the size of each subproblem ‘a’.
In the merge sort problem, 1/2 *n is the size of each subproblem ‘a’. So, both ‘a’ and ‘b’ are 2.
The standard form for a master method is:
For merge sort algorithm, it has to be:
Why a and b both are 2, that was already explained. But the second term is O(n) because the ‘merge’ method in the code has a linear running time. If you notice, the running time of the ‘merge’ method is O(n).
The master method is very straightforward. They call it a cookbook method. Just follow the recipe. It’s done!
There are three solutions for the standard form of the master method:
Case 1:
Case 2:
Case 3:
Let’s solve the recurrence of merge sort:
This was the recurrence of the merge sort algorithm:
Comparing with the standard form of the Master method mentioned earlier, here, a = 2, b = 2, and d = 1.
So, Case 1 is the solution for the merge sort algorithm. So, the time complexity of the merge sort algorithm is O(nlog(n)).
Here I have two more examples of the Master method. Please have a look at this recurrence below:
In this recurrence, a = 3, b=2 and d=2. If you calculate:
That means case 2 applies here.
Let’s solve the last example for the master method. Here is another recurrence:
In this recurrence, a = 16, b = 4 and d=1. Please notice:
So, Case 3 applies here. The time complexity for this recurrence is:
I hope, you know how to use the master method now.
But still, it is possible to derive an upper bound and lower bound
Here is an example,
This recurrence does not fall into any of the three cases directly. Please try it yourself.
But we can find an upper and lower bound using the Master theorem. If we just avoid the ‘logn’ term, clearly the left-hand side becomes greater than or equal to the right-hand side:
Using this formula we can find the lower bound. So, let’s solve this.
Here, a = 6, b = 2 and d = 2.
So, it falls under case 3. The lower bound of the time complexity will be:
On the other hand, instead of the ‘logn’ term, if we add an e term in the power of n,
Where, e > 0. Because, if the power of n grows that will grow faster than the ‘logn’ term. By solving this recurrence, we can get the upper bound.
Here, a = 6, b = 2 and d = 2+e. For large e,
So, it falls in case 2. The upper bound of this recurrence should be:
This is how to find the upper bound and lower bound of a recurrence using the master method.
For example, let’s look at this recurrence:
Here, a = 6, d = 2, and b is unknown. If it has to fall in case 1,
Plugging in the values,
Or,
As per case 1, the solution is:
In the same way, if it has to fall in case 2 category of the master method, we need to have,
As per case 2, the solution is:
If it has to fall in case 3, it has to be,
The solution is for case 3 is:
If you notice, we have a lower bound and an upper bound, even if we do not have b. Analyzing these three solutions,
The lower bound is:
And the upper bound is:
I hope this article was helpful in learning the Master method clearly. Though the master method has some limitations. It does not directly work on each and every recurrence. But still, it can be used to derive an upper bound and a lower bound. It can provide an upper and lower even if there is a parameter missing or variable. I hope you will be able to use it to find the time complexity of some cool recursive algorithm.
Feel free to follow me on Twitter and like my Facebook page.