Master theorem

5 The Master theorem

Analyzing the time complexity of algorithms, especially recursive ones, is more often than not a non-trivial task. For a recursive algorithm, its time complexity can be written as a recurrence formula, which is generally not easy, sometimes even impossible to solve with a closed formula. In some cases, though, we can find asymptotic bounds of the solution, despite not being able to necessarily find the precise solution to the recurrence. One large class of such cases is the class of divide-and-conquer algorithms, i.e. algorithms that recursively split the problem into smaller, similarly-sized subproblems. The Master theorem puts asymptotic bounds on divide-and-conquer recurrences.

Theorem 119 Master theorem for divide-and-conquer recurrences
#

Let \(T f : \mathbb {N} \rightarrow \mathbb {N}\) be functions such that the recurrence

\[ T(n) = a T(n/b) + f(n) \]

holds for some \(a {\gt} 0\) and \(b {\gt} 1\). Let also \(f(n) \in \Theta (n^d)\) for some \(d \ge 1\). Then the following holds:

  1. if \(a {\lt} b^d\), then \(T(n) \in \Theta (n^d)\),

  2. if \(a = b^d\), then \(T(n) \in \Theta (n^d \log _b{n})\) and

  3. if \(a {\gt} b^d\), then \(T(n) \in \Theta (n^{\log _b{a}})\).

In the case where \(f\) is bounded above by \(n^d\) only above or only below, \(T\) is also bounded only above or only below by the respective function.

We prove this theorem by proving all of its cases separately.

Definition 120
#

Let \(T f : \mathbb {N} \rightarrow \mathbb {N}\) be functions such that \(T\) is monotone and the recurrence

\[ T(n) \leq a T(\lceil n/b \rceil ) + f(n) \]

holds for all \(n \ge n\_ 0\) for some \(a {\gt} 0\), \(n\_ 0 {\gt} 1\) and \(b {\gt} n\_ 0\). Let also \(f(n) \in O(n^d)\) for some \(d \ge 1\). The above recurrence is an upper master recurrence with parameters \((a, n\_ 0, b, d)\).

Definition 121
#

Let \(T f : \mathbb {N} \rightarrow \mathbb {N}\) be functions such that \(T\) is monotone and the recurrence

\[ T(n) \geq a T(\lfloor n/b \rfloor ) + f(n) \]

holds for all \(n \ge n\_ 0\) for some \(a {\gt} 0\), \(n\_ 0 {\gt} 1\) and \(b {\gt} n\_ 0\). Let also \(f(n) \in \Omega (n^d)\) for some \(d \ge 1\). The above recurrence is a lower master recurrence with parameters \((a, n\_ 0, b, d)\).

Lemma 122

Let \(T\) and \(f\) form a lower master recurrence with parameters \((a, n\_ 0, b, d)\). Then \(T(n) \in O(n^{\log _b{a}} + \sum _{k=0}^{\lfloor \log _b{n} \rfloor } (\frac{a}{b^d})^k n^d)\).

Proof

To eliminate ceilings in the recurrence, we substitute \(T(n)\) with \(S(n) = T(n+b)\). Since \(T\) is monotone, \(T(n) \leq S(n) = T(n+b)\) holds for all \(n\). Therefore, an upper bound on \(S\) is also an upper bound on \(T\). We must first show that \(S\) follows the recurrence of \(T\) without ceilings. By the assumption that \(b\) is a natural number such that \(b {\gt} 1\), we have the inequality

\begin{align*} \lceil \frac{n+b}{b} \rceil & \leq \frac{n+b}{b} + 1 \\ & = \frac{n}{b} + 2 \\ & \leq \frac{n}{b} + b. \end{align*}

Therefore, by monotonicity of \(T\) we have

\begin{align*} S(n) & = T(n+b) \\ & \leq a T(\frac{n}{b} + b) + f(n+b) \\ & = a S(\frac{n}{b}) + f(n+b), \end{align*}

which captures the wanted recurrence. Integer division here is defined as the floor of real-number division. By substituting the inequality into itself repeatedly, we get

\begin{align} \label{eq:s_rec} S(n) & \leq a S(\frac{n}{b}) + f(n + b) \\ & \leq a S(\frac{n}{b}) + C n^d \\ & \leq a^2 S(\frac{n}{b^2}) + C n^d + C \frac{a}{b^d} n^d \\ & \leq a^3 S(\frac{n}{b^3}) + C (1 + \frac{a}{b^d} + (\frac{a}{b^d})^2) n^d \\ & \leq \dots \\ & \leq a^k S(\frac{n}{b^k}) + C \sum _{i=0}^k (\frac{a}{b^d})^i n^d \\ \end{align}

Let \(N\) be the integer such that for all \(n \geq N \geq n\_ 0\), the inequality \(f(n) \leq C n^d\) holds. Such \(N\) exists because \(f(n) \in O(n^d)\). We set \(k = \lfloor \log _b{\frac{n}{N}} \rfloor \). This choice of \(k\) allows the inequality \(n \geq N * b^k\) to hold for large enough \(n\).

Consider both sum parts on the right side of the Equation ??. For the first part, we notice that \(n^{\log _b{a}} = a^{\log _b{n}}\). Expand \(k\) in the exponent and apply monotonicity:

\begin{align*} a^k & = a^{\lfloor \log _b{\frac{n}{N}} \rfloor } \\ & \leq a^{\log _b{\frac{n}{N}}} \\ & \leq a^{\log _b{n}} \end{align*}

This implies \(a^k \in O(n^{\log _b{a}})\). We also need \(S(\frac{n}{b^k})\) to be bounded by a constant. We show that \(S(\frac{n}{b^k}) \leq S(N*b)\). Since \(S\) is monotone, this is equivalent to showing \(\frac{n}{b^k} \leq N*b\). We rewrite the left side as

\begin{align*} \frac{n}{b^k} & = \frac{n}{b^{\lfloor \log _b{\frac{n}{N}} \rfloor }} \\ & = \frac{n}{b^{\lfloor \log _b{n} - \log _b{N} \rfloor }} \\ & = \frac{b^{\log _b{n}}}{b^{\lfloor \log _b{n} - \log _b{N} \rfloor }} \\ & = b^{\log _b{n} - \lfloor \log _b{n} - \log _b{N} \rfloor } \end{align*}

Then, rewrite the right side as \(N*b = b^{\log _b{N} + 1}\). With both sides written as exponents of \(b\), we only need to prove

\[ \log _b{n} - \lfloor \log _b{n} - \log _b{N} \rfloor \leq \log _b{N} + 1. \]

By swapping terms, we get

\[ \log _b{n} - \log _b{N} \leq \lfloor \log _b{n} - \log _b{N} \rfloor + 1, \]

which holds for all real numbers.

For the second part, the inequality \(k \leq \lfloor \log _b{n} \rfloor \) holds by monotonicity of logarithms. As geometric sums are monotone in the exponent, we get \(\sum _{i=0}^k (\frac{a}{b^d})^i n^d \in O(\sum _{i=0}^{\lfloor \log _b{n} \rfloor } (\frac{a}{b^d})^i n^d)\).

Theorem 123

Let \(T\) and \(f\) form a lower master recurrence with parameters \((a, n\_ 0, b, d)\), where \(a {\lt} b^d\). Then \(T(n) \in O(n^d)\).

Proof

First, we apply Lemma 122. Since \(a {\lt} b^d\), we have \(\frac{a}{b^d} {\lt} 1\). By basic properties of geometric sums, we get

\begin{align*} T(n) & \leq \sum _{i=0}^{\lfloor \log _b{n} \rfloor } (\frac{a}{b^d})^i n^d \\ & \leq \frac{1}{1 - \frac{a}{b^d}} n^d, \end{align*}

which proves the upper bound.

Theorem 124

Let \(T\) and \(f\) form an upper master recurrence with parameters \((a, n\_ 0, b, d)\), where \(a = b^d\). Then \(T(n) \in O(n^d \log _b{n})\).

Proof

After applying Lemma 122, we note that \(\log _b{a} = d\) and then the proof boils down to showing that the geometric sum is bounded by \(\log _b{n}\). Since \(\frac{a}{b^d} = 1\), the geometric sum equals \(\lfloor \log _b{n} \rfloor \), which is obviously bounded by \(\log _b{n}\).

Theorem 125

Let \(T\) and \(f\) form an upper master recurrence with parameters \((a, n\_ 0, b, d)\), where \(a {\gt} b^d\). Then \(T(n) \in O(n^{\log _b{a}})\).

Proof

After applying Lemma 122, the left side of the sum if trivially bounded by \(n^{\log _b{a}}\). We are left with the right summand, which we transform using an inequality involving the geometric sum:

\begin{align*} \sum _{k=0}^{\lfloor \log _b{n} \rfloor } (\frac{a}{b^d})^k n^d & \leq \frac{1}{\frac{a}{b^d} - 1} ((\frac{a}{b^d})^{\lfloor \log _b{n} \rfloor } - 1) n^d \\ & \leq \frac{1}{\frac{a}{b^d} - 1} ((\frac{a}{b^d})^{\log _b{n}} - 1) n^d \\ & = \frac{1}{\frac{a}{b^d} - 1} (b^{\log _b{\frac{a}{b^d}}})^{\log _b{n}} n^d \\ & = \frac{1}{\frac{a}{b^d} - 1} (b^{\log _b{n}})^{\log _b{\frac{a}{b^d}}} n^d \\ & = \frac{1}{\frac{a}{b^d} - 1} n^{\log _b{a} - d} n^d \\ & = \frac{1}{\frac{a}{b^d} - 1} \frac{n^{\log _b{a}}}{n^d} n^d \\ & = \frac{1}{\frac{a}{b^d} - 1} n^{\log _b{a}} \\ \end{align*}

Since \(a {\gt} b^d\), \(\frac{a}{b^d} {\gt} 1\) holds, so \(\frac{1}{\frac{a}{b^d} - 1} {\gt} 0\), which proves boundedness by \(n^{\log _b{a}}\).

Lemma 126

Let \(T\) and \(f\) form an upper master recurrence with parameters \((a, n\_ 0, b, d)\). Then \(T(n) \in \Omega (\sum _{k=0}^{\lfloor \log _b{n} \rfloor } (\frac{a}{b^d})^k n^d)\).

Proof

We consider the recurrence formula with ceilings replaced by floors. If the resulting inequality holds, then so does the master recurrence, so it suffices to prove the lower bound for this inequality.

By substituting the inequality into itself repeatedly, we get

\begin{align} \label{eq:t_rec} T(n) & \leq a T(\frac{n}{b}) + f(n + b) \\ & \leq a T(\frac{n}{b}) + C n^d \\ & \leq a^2 T(\frac{n}{b^2}) + C n^d + C \frac{a}{b^d} n^d \\ & \leq a^3 T(\frac{n}{b^3}) + C (1 + \frac{a}{b^d} + (\frac{a}{b^d})^2) n^d \\ & \leq \dots \\ & \leq a^k T(\frac{n}{b^k}) + C \sum _{i=0}^k (\frac{a}{b^d})^i n^d \\ \end{align}

We set \(k = \lfloor \log _b{n} \rfloor \). This choice of \(k\) allows the inequality \(n \geq b^k\) to hold for large enough \(n\). Here \(C\) is a positive constant such that \(f(n) \geq C n^d\) for all \(n \geq n\_ 0\). Such a constant exists, because \(f(n) \in \Omega (n^d)\) implies \(f(n) \geq C\_ 0 n^d\) for some \(C\_ 0 {\gt} 0\) for all \(n \geq N\) for some \(N\). For \(n\_ 0 \leq n \leq N\), The argument is as follows. The set of natural numbers between \(n\_ 0\) and \(N\) is finite, so the image of \(f\) on this set has a maximal element \(M\). We then have \(f(n) \geq \frac{M}{N^d} n^d\).

The second summand in the right side of Equation ?? is trivially bounded above by \(\sum _{k=0}^{\lfloor \log _b{n} \rfloor } (\frac{a}{b^d})^k n^d\). This is sufficient to prove the upper bound od \(T(n)\) as the left summand is non-negative.

Lemma 127

Let \(T\) and \(f\) form a lower master recurrence with parameters \((a, n\_ 0, b, d)\). Then \(T(n) \in \Omega (n^d)\).

Proof

Since \(T(n) \geq f(n)\) for all \(n \ge n\_ 0\), the lower bound follows directly from \(f(n) \in \Omega (n^d)\).

Theorem 128

Let \(T\) and \(f\) form a lower master recurrence with parameters \((a, n\_ 0, b, d)\) where \(a = b^d\). Then \(T(n) \in \Omega (n^d \log _b{n})\).

Proof

By Lemma 126, it suffices to show that \(\sum _{i=0}^{\lfloor \log _b{n} \rfloor } (\frac{a}{b^d})^i \in \Omega (\log _b{n})\). Applying equality \(a = b^d\), the sum simplifies to

\begin{align*} \sum _{i=0}^{\lfloor \log _b{n} \rfloor } (\frac{a}{b^d})^i & = \sum _{i=0}^{\lfloor \log _b{n} \rfloor } 1^i & = \lfloor \log _b{n} \rfloor . \end{align*}
Theorem 129

Let \(T\) and \(f\) form an upper master recurrence with parameters \((a, n\_ 0, b, d)\) where \(a {\gt} b^d\). Then \(T(n) \in \Omega (n^{\log _b{a}})\).

Proof

By Lemma 126, we need to show that \(\sum _{i=0}^{\lfloor \log _b{n} \rfloor } (\frac{a}{b^d})^i n^d \in \Omega (n^{\log _b{a}})\). By \(a {\gt} b^d\), we have

\begin{align*} \sum _{i=0}^{\lfloor \log _b{n} \rfloor } (\frac{a}{b^d})^i n^d & \geq (a b^{-d} - 1)^{-1} ((a b^{-d})^{\lfloor \log _b{n} \rfloor } - 1) n^d \\ & \geq 2^{-1} (a b^{-d} - 1)^{-1} (a b^{-d})^{\lfloor \log _b{n} \rfloor } n^d \\ & \geq 2^{-1} (a b^{-d} - 1)^{-1} (a b^{-d})^{\log _b{n} - 1} n^d \\ & \geq 2^{-1} a^{-1} b^d (a b^{-d} - 1)^{-1} a^{\log _b{n}} (b^{\log _b{n} - 1})^{-d} n^d \\ & \geq 2^{-1} a^{-1} b^d (a b^{-d} - 1)^{-1} n^{\log _b{a}} n^{-d} n^d \\ & \geq 2^{-1} a^{-1} b^d (a b^{-d} - 1)^{-1} n^{\log _b{a}}, \end{align*}

which proves the bound.

Let \(T\) and \(f\) form an upper and lower master recurrence with parameters \((a, n\_ 0, b, d)\) where \(a {\lt} b^d\). Then \(T(n) \in \Theta (n^d)\).

Proof

By Theorem 79, it suffices to show lower and upper bounds for \(T\), which we already proved in Theorem 123 and Lemma 127.

Let \(T\) and \(f\) form an upper and lower master recurrence with parameters \((a, n\_ 0, b, d)\) where \(a = b^d\). Then \(T(n) \in \Theta (n^d \log _b{a})\).

Proof

By Theorem 79, it suffices to show lower and upper bounds for \(T\), which we already proved in Theorems 124 and 128.

Let \(T\) and \(f\) form an upper and lower master recurrence with parameters \((a, n\_ 0, b, d)\) where \(a {\gt} b^d\). Then \(T(n) \in \Theta (n^{\log _b{n}})\).

Proof

By Theorem 79, it suffices to show lower and upper bounds for \(T\), which we already proved in Theorems 125 and 129.