Given a recurrence, guess a function and test if the recurrence
matches the function by induction.
If the test fails, you might have to consider a fast-growing or
slow-growing function and test it again.
This is a pretty dangerous method because if a function does not match
a recurrence, it does not necessarily imply that another one
proportional to this one will not work.
Method 4: Master theorem
Suppose $T(n)$ has the following recurrence:
\[ T(n) = \left. \begin{cases} c & \text{if } n < d,\\
aT\left(\frac{n}{b}\right) + f(n) & \text{if } n \ge d. \end{cases}
\right\} \]
where $d \ge 1$ is a fixed natural number and $a \ge 1, c > 0, b > 1$
are real constants, and $f(n)$ is a positive function for $n \ge d$.
Then, the complexity of $T(n)$ is
\[ T(n) \in \left. \begin{cases} \Theta(n^{\Delta}) & \text{if } f(n)
\in \mathcal{O}(n^{\Delta - \epsilon}) \text{ for } \epsilon > 0,\\
\Theta(f(n) \log n) & \text{if } f(n) \in \Theta(n^{\Delta} \log^k
n),\\ \Theta(f(n)) & \begin{array}{@{}l@{}} \text{if } f(n) \in
\Omega(n^{\Delta + \epsilon}) \text{ for } \epsilon > 0 \text{ and }
f(n) > af(n/b). \end{array}\\ \end{cases} \right\} \]
where $\Delta = \log_b a$ is the discriminant of the recurrence.
Justification of master theorem
\[ \begin{aligned} T(n) &= a T \left( \frac{n}{b} \right) + f(n)\\ &= a^2
T \left( \frac{n}{b^2} \right) + af\left(\frac{n}{b}\right) + f(n)\\ &=
a^3 T \left( \frac{n}{b^3} \right) + a^2f\left(\frac{n}{b^2}\right) +
af\left(\frac{n}{b}\right) + f(n)\\ &= a^{\log_b n} T (1) + \sum_{i =
0}^{\log_b n - 1} a^i f\left(\frac{n}{b^i}\right)\\ &= n^{\log_b a} +
\sum_{i = 0}^{\log_b n - 1} a^i f\left(\frac{n}{b^i}\right) \end{aligned}
\]
$f(n)$ is polynomially smaller than $n^{\Delta}$ $\implies T(n) \in
\Theta(n^{\Delta})$
$f(n)$ is asymptotically close to $n^{\Delta}$ $\implies T(n) \in
\Theta(f(n) \log n)$
$f(n)$ is polynomially larger than $n^{\Delta}$ $\implies T(n) \in
\Theta(f(n))$
Problems
Solve $T(n) = T(n/2) + 1$ using the master theorem
$a = 1$
$b = 2$
$f(n) = 1 = n^0$
$n^{\Delta} = n^{\log_b a} = n^{\log_2 1} = n^0$
As $f(n) \in \Theta(n^{\Delta} \log^k n)$, i.e., $n^0 \in \Theta(n^0
\log^0 n)$, we apply Case 2
$T(n) \in \Theta(f(n) \log n) =
\Theta(n^0 \log n) = \Theta(\log n)$
Solve $T(n) = 2T(n/2) + 1$ using the master theorem
$a = 2$
$b = 2$
$f(n) = 1 = n^0$
$n^{\Delta} = n^{\log_b a} = n^{\log_2 2} = n^1$
As $f(n) \in \mathcal{O}(n^{\Delta - \epsilon})$, i.e., $n^0 \in
\mathcal{O}(n^{1 - 1})$, we apply Case 1
$T(n) \in \Theta(n^{\Delta})
= \Theta(n)$
Solve $T(n) = 2T(n/2) + n$ using the master theorem
$a = 2$
$b = 2$
$f(n) = n = n^1$
$n^{\Delta} = n^{\log_b a} = n^{\log_2 2} = n^1$
As $f(n) \in \Theta(n^{\Delta} \log^k n)$, i.e., $n^1 \in \Theta(n^1
\log^0 n)$, we apply Case 2
$T(n) \in \Theta(f(n) \log n) = \Theta(n
\log n)$
Solve $T(n) = 2T(n/2) + n^2$ using the master theorem
$a = 2$
$b = 2$
$f(n) = n^2$
$n^{\Delta} = n^{\log_b a} = n^{\log_2 2} = n^1$
As $f(n) \in \Omega(n^{\Delta + \epsilon})$, i.e., $n^2 \in
\Omega(n^{1 + 1})$, we apply Case 3
$T(n) \in \Theta(f(n)) =
\Theta(n^2)$
Solve the following recurrences using the master theorem
$T(n) = T(\sqrt{n}) + n$
Master theorem cannot be directly applied
Reason: $\sqrt{n}$ cannot be written as $n/b$ for any real constant $b
> 1$
$T(n) = n T(n/2) + n$
Master theorem cannot be directly applied
Reason: $a = n$ is not a constant
$T(n) = T(n-1) + n$
Master theorem cannot be directly applied
Reason: Subproblem $(n-1)$ cannot be written as $n/b$ for any real
constant $b > 1$
$T(n) = T(n/2) + T(n/3) + n$
Master theorem cannot be directly applied
Reason: Cannot have multiple subproblems of different sizes
$T(n) = 2 T(n/(2/3)) + 2^n$
Master theorem cannot be directly applied
Reason: $b < 1$; $b$ should have been larger than $1$
$T(n) = 2 T(n/2) + n \times (1 + \sin n)$
Master theorem cannot be directly applied
Reason: $f(n)$ does not have a clean $\Theta$-class relative to
$n^{\Delta}$
$T(n) = T(n/2) + T(n/2) + T(n/2) - n$
Master theorem cannot be directly applied
Reason: $f(n)$ is a negative function
$T(n) = 2 T(n/2) + g(n)$ where $g(n)$ is $n$ if $n$ even, $1$ if $n$
odd
Master theorem cannot be directly applied
Reason: $g(n)$ does not have a clean $\Theta$-class relative to
$n^{\Delta}$
Problems and Solutions
Array Sum
Problem
Compute the sum of elements of a given array.
Step 1. Subproblem
\[ \begin{aligned}
&\mathrm{Sum}(A[\ell \ldots h]) = \text{Sum of all
elements in subarray } A[\ell \ldots h]\\
&\text{Compute } \mathrm{Sum}(A[1 \ldots n])
\end{aligned} \]