The Master theorem is used for the analysis of Divide-and-Conquer algorithms. I find that at first the Master theorem might seem confusing or ambiguous. So in this article i hope that i can simplify the Theorem as much as possible.

So first, Let’s start with the formula of recurrence relation and we will use Merge sort as our practical example so it wont be only theorem. So the formula is:

T(n) ≤ a T(n/b) + c nᵈ

Let’s try to understand what each term stands for. So “a” stands for the number of recursive calls which in Merge sort we make two recursive calls.

So this means that “a” is equal to 2. Next is the “n” and “b”. “n” is the size of the input , so for example in merge sort, if we want to sort an Array size of 100000 elements, then n = 100000.

For the “b” its on each recurrence call, how do we divide the input? Into how many pieces? Recall that these recursive algorithms are called divide-and-conquer. So in our MergeSort example, each recursive call we divide the input into two. So b = 2. Note that we keep dividing till the input is small enough that the cost of sorting is T(2) = 1. Which means size of Left = 1 and Right = 1 and we just compare and rearrange them ascendingly.

Finally we are left with the “c nᵈ ” expression which is fairly simple. To understand this lets recall what we are trying to do here. We are trying to divide a large problem into smaller ones and then solve the smaller ones in less time and combine them. So after we finish splitting the array and sorting 2 elements , we need to add the small sorted pieces together. Recall in MergeSort where the total time of Merge step is considered Linear. As we just pass through the inputs once and rearrange them as the following code.

As we can see in the previous “MergeStep.py” , our degree of work is linear, so d = 1 and c is the running time for the base case which is equal to 1 also. So we are left with n. The final formula for mergesort is:

T(n) ≤ 2 T(n/2) + n

Observing this formula can be done by something called Recursion Trees. This tree shows the expansion of the divide-and-conquer algorithm and keep track of time spent. So lets construct this tree. First lets consider we have an array of size 16.

If we looked carefully, we are kind of increasing the problem to solve with each level by a factor of 2, however we decrease the size of the problem to solve by halve. If we looked carefully we have a size of 2ʲ problems with each level where j is the number of levels. And the size of input is equal 16/(2ʲ) each level. Finally, the length of tree is log₂(16) + 1 (The root). So we can generalize these numbers to, each level we have aʲ sub problems and each problem of size n/bʲ for log(n) base b levels. These formulas are to help use in the proof of Master Theorem next part.

Hope you find this article useful. Feedback is highly appreciated. See you next part.