Skip to content
Analyze Recurrences

Analyze Recurrences

The Iteration Method

Steps:

  1. Expand the recurrence a few times.
  2. Write the expanded form as a summation.
  3. Simplify the summation.
  4. Substitute the base condition to get the result.

Example 1

Given that:

T(n)=c+T(n/2) T(n) = c + T(n/2)

Firstly, expand it.

T(n)=c+T(n/2)=c+c+T(n/4)=c+c+c+T(n/8)=…=kc+T(n/2k) \begin{align*} T(n) &= c + T(n/2) \\ &= c + c + T(n/4) \\ &= c + c + c + T(n/8) \\ &= \dots \\ &= kc + T(n/2^k) \end{align*}

Then, assume that:

n=2k⟹k=log⁡n n = 2^k \Longrightarrow k = \log{n}

Finally, substitute it to the original expression:

T(n)=kc+T(n/2k)=clog⁡n+T(n/2log⁡n)=clog⁡n+T(1)=O(log⁡n) \begin{align*} T(n) &= kc + T(n/2^k) \\ &= c\log{n} + T(n/2^{\log{n}}) \\ &= c\log{n} + T(1) \\ &= O(\log{n}) \end{align*}

Example 2

Given that:

T(n)={1n=1T(n−1)+log⁡nn>1 T(n) = \begin{cases} 1 &n=1 \\ T(n-1) + \log{n} &n>1 \end{cases}

Then

T(n)=T(n−1)+log⁡n=T(n−2)+log⁡n+log⁡n−1=…=T(n−k)+∑i=1nlog⁡i=T(n−k)+log⁡(n!) \begin{align*} T(n) &= T(n-1) + \log{n} \\ &= T(n-2) + \log{n} + \log{n-1} \\ &= \dots \\ &= T(n-k) + \sum_{i=1}^{n}{\log{i}} \\ &= T(n-k) + \log{(n!)} \\ \end{align*}

Since when n=1n=1, T(n)=1T(n) = 1, so we assume that k=n−1k = n - 1 finally.

Then

T(n)=T(1)+log⁡(n!)=1+log⁡(n!) T(n) = T(1) + \log{(n!)} = 1 + \log{(n!)}

Use Stirling’s formula,

log⁡n=nlog⁡n−nlog⁡e+O(log⁡n)T(n)=1+log⁡(n!)T(n)=1+nlog⁡n−nlog⁡e+O(log⁡n)T(n)=O(nlog⁡n) \begin{align*} \log{n} &= n\log{n} - n\log{e} + O(\log{n}) \\ T(n) &= 1 + \log{(n!)} \\ T(n) &= 1 + n\log{n} - n\log{e} + O(\log{n}) \\ T(n) &= O(n\log{n}) \end{align*}

The Substitution/Induction Method

Steps:

  1. Guess the result form.
  2. Prove by induction.

Example

Given that

T(n)=2T(n2)+n T(n) = 2T(\frac{n}{2}) + n

Firstly, we guess that T(n)∈O(nlog⁡n)T(n) \in O(n\log{n})

So we need to prove that T(n)≤c⋅nlog⁡nT(n) \le c \cdot n\log{n}

Assume that

T(n2)≤c⋅n2log⁡n2 T(\frac{n}{2}) \le c \cdot \frac{n}{2}\log{\frac{n}{2}}

Then the inductive steps are:

T(n)=2T(n2)+n≤2(c⋅n2log⁡n2)+n=cnlog⁡n2+n=cn(log⁡n−1)+n=cnlog⁡n−(c−1)n≤cnlog⁡n∀c≥1∴T(n)∈O(nlog⁡n) \begin{align*} T(n) &= 2T(\frac{n}{2}) + n \\ &\le 2(c \cdot \frac{n}{2}\log{\frac{n}{2}}) + n \\ &= cn\log{\frac{n}{2}} + n \\ &= cn(\log{n} - 1) + n \\ &= cn\log{n} - (c - 1)n \\ &\le cn\log{n} \quad \forall c \ge 1 \\ \therefore T(n) &\in O(n\log{n}) \end{align*}

Recursion Tree Method

Draw the recursion tree and calculate the cost of each level which start at 00.

Master Method

Suppose that we have:

T(n)=aT(nb)+f(n)a≥1,b>1 T(n) = aT(\frac{n}{b}) + f(n) \quad a \ge 1, b \gt 1
  • nn is the size of the current problem.
  • aa is the number of sub problems of the recursion.
  • nb\frac{n}{b} is the size of each sub problem.
  • f(n)f(n) is the cost of each level recursion.

Then we have three theorems:

  1. If f(n)=O(nlog⁡ba−ϵ)f(n) = O(n^{\log_b{a} - \epsilon}), then T(n)=Θ(nlog⁡ba)T(n) = \Theta(n^{\log_b{a}})
  2. If f(n)=Θ(nlog⁡ba)f(n) = \Theta(n^{\log_b{a}}), then T(n)=Θ(nlog⁡balog⁡n)T(n) = \Theta(n^{\log_b{a}}\log{n})
  3. If f(n)=Ω(nlog⁡ba+ϵ)f(n) = \Omega(n^{\log_b{a} + \epsilon}), and if af(nb)≤cf(n)af(\frac{n}{b}) \le cf(n) for some c<1c \lt 1, and all sufficiently large nn, then T(n)=Θ(f(n))T(n)=\Theta(f(n)).

Example 1

Given that

T(n)=2T(n2)+n T(n) = 2T(\frac{n}{2}) + n

Then

a=2,b=2,f(n)=nnlog⁡ba=nnlog⁡ba=f(n)∴T(n)=Θ(nlog⁡balog⁡n)=Θ(nlog⁡(n)) \begin{align*} a = 2, b &= 2, f(n) = n \\ n^{\log_b{a}} &= n \\ n^{\log_b{a}} &= f(n) \\ \therefore T(n) &= \Theta(n^{\log_b{a}}\log{n}) \\ &= \Theta(n\log(n)) \end{align*}

Example 2

Given that

T(n)=9T(n3)+n T(n) = 9T(\frac{n}{3}) + n

Then

a=9,b=3,f(n)=nnlog⁡ba=n2nlog⁡ba≥f(n)=n∴f(n)=O(nlog⁡ba−ϵ)=O(n2−ϵ)∴T(n)=Θ(n2) \begin{align*} a = 9, b &=3, f(n) = n \\ n^{\log_b{a}} &= n^2 \\ n^{\log_b{a}} &\ge f(n) = n \\ \therefore f(n) &= O(n^{\log_b{a} - \epsilon}) \\ &= O(n^{2 - \epsilon}) \\ \therefore T(n) &= \Theta(n^2) \end{align*}

Example 3

Given that

T(n)=3T(n4)+nlog⁡n T(n) = 3T(\frac{n}{4}) + n\log{n}

Then

a=3,b=4,f(n)=nlog⁡nnlogba=nlog43<n<f(n) \begin{align*} a = 3, b &= 4, f(n) = n\log{n} \\ n^{log_b{a}} &= n^{log_4{3}} \lt n \lt f(n) \\ \end{align*}

Before applying case 3, we need to check the regularity condition, which means that af(nb)≤cf(n)af(\frac{n}{b}) \le cf(n) where c<1c \lt 1 should be true.

af(nb)=3f(n4)=34nlog⁡n4cf(n)=cnlog⁡n∴34nlog⁡n4≤cnlog⁡n \begin{align*} af(\frac{n}{b}) &= 3f(\frac{n}{4}) \\ &= \frac{3}{4}n\log{\frac{n}{4}} \\ cf(n) &= cn\log{n} \\ \therefore \frac{3}{4}n\log{\frac{n}{4}} &\le cn\log{n} \\ \end{align*}

When c=34c = \frac{3}{4}, the expression is true. So the condition check is successfully.

f(n)=Ω(nlog⁡ba+ϵ)=Ω(nlog⁡43+ϵ)∴T(n)=Θ(f(n))=Θ(nlog⁡n) \begin{align*} f(n) &= \Omega(n^{\log_b{a} + \epsilon}) = \Omega(n^{\log_{4}{3} + \epsilon}) \\ \therefore T(n) &= \Theta(f(n)) \\ &= \Theta(n\log{n}) \end{align*}
Last updated on