## Introduction

This post is to be read in continuation to the Divide and Conquer methodology for e.g. the Merge Sort problem. This post is an extension over the problem of solving recurrences or recurrence equations.

There are several ways of solving recurrences namely Substitution Method, Master Method and Recurrence Tree method. The most confusing one or may I say relatively complex one is the Master Theorem. Here we will discuss the same.

## Master Theorem

##### What does it solve?

Not all the recurrences can be solved using the Master Theorem, but it still solves a large family of recurrences. Here is the classification of recurrences which can be solved using this theorem.

- It solves the recurrences of form T(n) = aT(n/b) + f(n).
**a**should be greater than or equal to 1. Which means that the problem is at least reduced to a smaller sub problem once. At least one recursion is needed**b**should be greater than 1. Which means at every recursion, the size of the problem is reduced to a smaller size. If b is not greater than 1, that means our sub problems are not of smaller size.- f(n) must be positive for relatively larger values of n.

##### Basis of Master Theorem

Let us consider the below tree:

Lets say we have a problem of size **n** to be solved. At each step the problem can be divided into **a** sub problems and each sub problem is of smaller size, where the size is reduced by a factor of **b**.

The above statement in simple words means that a problem of size **n** can be divided into **a** sub problems of relatively smaller sizes **n/b**.

Also, the above diagram shows that at the end when we have divided the problems multiple times, each sub problem would be so small that it can be solved in constant time.

##### Some Deductions

**Now, what can we say about the height of the tree?**

Let us say that **H** is the height of the tree, then **H = log _{b}n**.

Here is a simple deduction, the problem size reduces by a factor **b** at each level. So it would take log_{b}n levels to reduce it to a problem of size 1 and it cannot be divided further.

**What is the number of leaves in the tree?**

The relationship between height of a tree and the number of leaf nodes is as follows:

number of leaves = branching_factor^{height}.

Hence the number of leaves would be **a ^{H}**. Replacing H in this equation, we would get the number of leaves =

**a**.

^{logbn}By the following properties of logarithms we can rearrange the formula.

N^{logkM} = M^{logkN}. Therefore the number of leaves = **n ^{logba}**.

**Finding the work done at each level in the tree**

- Total work done at Level 1 : f(n)
- Total work done at Level 2 : a * f(n/b)
- Total work done at Level 1 : a * a * f(n/b
^{2}) - Total work done at last Level : number of leaves * θ(1). This equals to n
^{logba}

**Note** : In case the above calculations do not make much sense, I would advice you to understand these by redoing it for a simple binary tree.

## Three cases of Master Theorem

With the help of the above deductions, we are in a shape to discuss the three cases of the Master Theorem.

- Now let us assume that the cost of operation is increasing by a significant factor at each level and by the time we reach the leaf level the value of f(n) becomes polynomially smaller than the value n
^{logba}. Then the overall running time will be heavily dominated by the cost of the last level. Hence**T(n) = θ(n**^{logba})

- Let us assume that the cost of the operation on each level is roughly equal. In that case f(n) is roughly equal to n
^{logba}. Hence, the total running time would be f(n) times the total number of levels.

**T(n) = θ(n**where k can be >=0. Where log^{logba}* log_{k+1}n)_{k+1}n would be the height of a tree for k >= 0

- Let us assume that the cost of the operation on each level is decreasing by a significant factor at each level and by the time we reach the leaf level the value of f(n) becomes polynomially larger than the value n
^{logba}. Then the overall running time will be heavily dominated by the cost of the first level. Hence**T(n) = θ(f(n))**

.

This is the simplest way how we can understand the Master Theorem.

## Few Examples of Solving Recurrences – Master Method

Now that we know the three cases of Master Theorem, let us practice one recurrence for each of the three cases.

##### Example for Case 1

Assume the recurrence equation is **T(n) = 4T(n/2) + n**

Let us compare this recurrence with our eligible recurrence for Master Theorem T(n) = aT(n/b) + f(n).

We find that a = 4, b = 2 and f(n) = n

Let us find out n^{logba}, which is the work done at last level, using the above values. It is equal to n^{log24}, which is equal to n^{2}.

Now let us compare the work done at first and last level. Which means comparing f(n) with n^{logba}.

f(n) = n and n^{logba} = n^{2}.

We know that n^{2} is significantly greater than n for larger n. Hence, it is the first case of Master Theorem. Therefore the solution to this recurrence is θ(n^{logba}). So T(n) = θ(n^{2}).

##### Example for Case 2

Assume the recurrence equation is **T(n) = 4T(n/2) + n ^{2}**

Let us compare this recurrence with our eligible recurrence for Master Theorem T(n) = aT(n/b) + f(n).

We find that a = 4, b = 2 and f(n) = n^{2}

Let us find out n^{logba}, which is the work done at last level, using the above values. It is equal to n^{log24}, which is equal to n^{2}.

Now let us compare the work done at first and last level. Which means comparing f(n) with n^{logba}.

f(n) = n^{2} and n^{logba} = n^{2}.

We know that n^{2} is equal to n^{2}. Hence, it is the second case of Master Theorem. Therefore the solution to this recurrence is θ(n^{logba} * log_{k+1}n). So T(n) = θ(n^{2}log_{2}n).

##### Example for Case 3

Assume the recurrence equation is **T(n) = 4T(n/2) + n ^{3}**

Let us compare this recurrence with our eligible recurrence for Master Theorem T(n) = aT(n/b) + f(n).

We find that a = 4, b = 2 and f(n) = n^{3}

Let us find out n^{logba}, which is the work done at last level, using the above values. It is equal to n^{log24}, which is equal to n^{2}.

Now let us compare the work done at first and last level. Which means comparing f(n) with n^{logba}.

f(n) = n^{3} and n^{logba} = n^{2}.

We know that n^{3} is significantly greater than n^{2} for larger n. Hence, it is the third case of Master Theorem. Therefore the solution to this recurrence is θ(f(n)), which is equal to n^{3}.

## Conclusion

A very important point worth noting is that, we need to apply this method only to recurrence which satisfy the necessary conditions. You can try applying it to more complicated recursions. The approach remains same.

Also, an advice of caution is that, few recursions seems to satisfy the prerequisites of the Master theorem in their non normalized form, but when you simplify the equation, it might not fit in this class of recursion.

Don’t forget to **subscribe to TechieMe** to get updates on latest posts.

Pingback: Algorithm and Data structure – myBlog()