The Master Theorem

Learn the master theorem of the divide-and-conquer algorithm.

Divide-and-conquer algorithm

A typical divide-and-conquer algorithm solves a problem of size nn as follows:

Divide: Break the problem into aa problems of size at most n/bn/b and solve them recursively. We assume that a1a ≥ 1 and b>1b > 1.

Conquer: Combine the answers found by recursive calls to an answer for the initial problem.

Therefore, the algorithm makes aa recursive calls to problems of size n/bn/b.

Note: bb does not necessarily divide nn, so instead of n/bn/b, we should usually write n/b⌈n/b⌉. Still, we write n/bn/b; it simplifies the notation and does not break the analysis.

Assume that everything that is done outside of recursive calls takes time O(nd)O(n^d) where dd is a non-negative constant. This includes preparing the recursive calls and combining the results.

Since this is a recursive algorithm, we also need to specify conditions under which a problem is solved directly rather than recursively (with no such base case, a recursion would never stop). This usually happens when nn becomes small. For simplicity, we assume that it is n=1n = 1 and that the running time of the algorithm is equal to 11 in this case.

Therefore, by denoting the running time of the algorithm on problems of size nn by T(n)T (n), we get the following recurrence relation on T(n)T(n):

T(n)={1if   n=1,aT(nb)+O(nd)if   n>1.T(n) = \begin{cases} 1 & \text{if }~~ n = 1, \\ aT(\frac{n}{b})+O(n^d) & \text{if }~~ n > 1. \end{cases}

Below, we show that we can find out the growth rate of T(n)T (n) by looking at parameters aa, bb, and dd. Before doing this, we illustrate how this recurrence relation captures some well-known algorithms.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.