# Advanced master theorem for divide and conquer recurrences

Master Theorem is used to determine running time of algorithms (divide and conquer algorithms) in terms of asymptotic notations.

Consider a problem that be solved using recursion.

function f(input x size n)if(n > k) solve x directly and returnelsedivide x intoasubproblems 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) + 2
^{n}

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 > b
^{k}, then T(n) = θ(n^{logba}) - if a = b
^{k}, then

(a) if p > -1, then T(n) = θ(n^{logba}log^{p+1}n)

(b) if p = -1, then T(n) = θ(n^{logba}loglogn)

(c) if p < -1, then T(n) = θ(n^{logba}) - if a < b
^{k}, then

(a) if p >= 0, then T(n) = θ(n^{k}log^{p}n)

(b) if p < 0, then T(n) = θ(n^{k})

**Time Complexity Analysis –**

**Example-1: Binary Search –**T(n) = T(n/2) + O(1)

a = 1, b = 2, k = 0 and p = 0

b^{k}= 1. So, a = b^{k}and p > -1 [Case 2.(a)]

T(n) = θ(n^{logba}log^{p+1}n)

T(n) = θ(logn)**Example-2: Merge Sort –**T(n) = 2T(n/2) + O(n)

a = 2, b = 2, k = 1, p = 0

b^{k}= 1. So, a = b^{k}and p > -1 [Case 2.(a)]

T(n) = θ(n^{logba}log^{p+1}n)

T(n) = θ(nlogn)**Example-3:**T(n) = 3T(n/2) + n^{2}

a = 3, b = 2, k = 2, p = 0

b^{k}= 4. So, a < b^{k}and p = 0 [Case 3.(a)]

T(n) = θ(n^{k}log^{p}n)

T(n) = θ(n^{2})**Example-4:**T(n) = 3T(n/2) + log^{2}n

a = 3, b = 2, k = 0, p = 2

b^{k}= 1. So, a > b^{k}[Case 1]

T(n) = θ(n^{logba})

T(n) = θ(n^{log23})**Example-5:**T(n) = 2T(n/2) + nlog^{2}n

a = 2, b = 2, k = 1, p = 2

b^{k}= 1. So, a = b^{k}[Case 2.(a)]

T(n) = θ(n^{logba}log^{p+1}n )

T(n) = θ(n^{log22}log^{3}n)

T(n) = θ(nlog^{3}n)**Example-6:**T(n) = 2^{n}T(n/2) + n^{n}

This recurrence can’t be solved using above method since function is not of form T(n) = aT(n/b) + θ(n^{k}log^{p}n)

**GATE Practice questions –**

## Recommended Posts:

- Master Theorem For Subtract and Conquer Recurrences
- Regularity condition in the master theorem.
- GATE CS 2016 Sec 5 – Divide and Conquer
- Dynamic Programming vs Divide-and-Conquer
- Divide and Conquer Algorithm | Introduction
- Maximum Subarray Sum using Divide and Conquer algorithm
- Divide and Conquer | Set 5 (Strassen's Matrix Multiplication)
- Tiling Problem using Divide and Conquer algorithm
- Convex Hull using Divide and Conquer Algorithm
- The Skyline Problem using Divide and Conquer algorithm
- Closest Pair of Points using Divide and Conquer algorithm
- Merge K sorted arrays | Set 3 ( Using Divide and Conquer Approach )
- Longest Common Prefix using Divide and Conquer Algorithm
- Generate a random permutation of elements from range [L, R] (Divide and Conquer)
- Merge K sorted arrays of different sizes | ( Divide and Conquer Approach )

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.