1.0 - Solving Recurrences

1.1 - Recurrences


1.1 - Substitution Method

1.1.1 - Substitution Method - Example 1

Given the recurrence:

T(n)={2if n=22T(n/2)+nif n=2k,for k>1T(n)=\begin{cases}2&\text{if } n=2\\2T(n/2)+n&\text{if }n=2^k, \text{for } k>1\end{cases}

1.1.2 - Substitution Method - Example 2

🌱 We can use this particular technique to prove that a recurrence has a certain asymptotic upper bound, say O(nlgn)O(n \lg n)

Given the recurrence (includes floor):

T(n){1if n=12T(n2)+nif n>1T(n)\begin{cases} 1&\text{if } n=1\\ 2T(\lfloor\frac n2\rfloor) + n&\text{if }n>1 \end{cases}

1.1.3 - Substitution Method - Guessing

Consider the following recurrence:

T(n)=2T(n/2)+17+n T(n)=2T(\lfloor n/2 \rfloor)+17+n

The difference between:

T(n/2)         and         T(n/2)+17 T(\lfloor n/2\rfloor)\ \ \ \ \ \ \ \ \ \text{and}\ \ \ \ \ \ \ \ \ T(\lfloor n/2\rfloor)+17

Since the difference is small, we can guess the same solution.

We can then guess loose upper and lower bounds, then refine:

T(n)=Ω(n)T(n)=O(n2) T(n)=\Omega(n)\\ T(n)=O(n^2)

Try to lower the upper bound and raise the lower bound until convergence to get a sufficiently tight bound

1.1.4 - Substitution Method - Strengthening the Guess

🌱 We can “Strengthen the Guess” by removing a lower-order term.

Consider:

T(n)=T(n/2)+T(n/2)+1 T(n)=T(\lfloor n/2\rfloor) + T(\lceil n/2\rceil) +1

We guess T(n)=O(n)T(n)=O(n)

In our inductive step, we assume that T(n/2)cn/2T(\lfloor n/2 \rfloor)\le c\lfloor n/2\rfloor and T(n/2)cn/2T(\lceil n/2\rceil)\le c\lceil n/2\rceil and attempt to prove T(n)cnT(n)\le cn

T(n)=T(n/2)+T(n/2)+1cn/2+cn/2+1     substitute inductive assumptions=cn+1≰ cn \begin{aligned} T(n)&={\color{pink}T(\lfloor n/2 \rfloor)} + {\color{pink}T(\lceil n/2 \rceil)} +1\\ &\le {\color{pink}c\lfloor n/2 \rfloor} + {\color{pink}c\lceil n/2 \rceil} + 1 \ \ \ \ \ &\text{substitute inductive assumptions}\\ &=cn+1\\ &\not{\le}\ cn \end{aligned}

We could try to prove that T(n)=O(n2)T(n)=O(n^2), but that is too weak. Let’s strengthen the guess by subtracting a lower-order term. Guess that:

c,n0>00T(n)cnb                         where b>0 \exists c, n_0 > 0 \bullet 0\le T(n)\le cn-b\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\text{where }b>0}

Inductive Step Assume for n/2\lfloor n/2 \rfloor and n/2\lceil n/2 \rceil and prove for nn

T(n)=T(n/2)+T(n/2)+1n/2b+cn/2b+1        substitute assumptions=cn2b+1cnbif b1 \begin{aligned} T(n)&=T(\lfloor n/2 \rfloor) + T(\lceil n/2 \rceil) +1\\ &\le \lfloor n/2 \rfloor -b+c\lceil n/2 \rceil-b+1\ \ \ \ \ \ \ \ & \text{substitute assumptions}\\ &=cn-2b+1\\ &\le cn-b&\text{if } b\ge 1 \end{aligned}

Boundary Conditions Now find values for c,n0c, n_0 such that the boundary conditions are satisfied

1.1.5 - Substitution Method - Incorrect Guess & Bugs in Proof

1.1.6 - Change of Variables Technique

Consider

T(n)=2T(n)+lgn T(n)=2T(\lfloor \sqrt n \rfloor)+\lg n

This looks hard to work with. Try the change of varaible technique, where m=lgnm=\lg n (or n=2mn=2^m)

T(2m)=2T(2m/2)+m T(2^m)=2T(2^{m/2})+m

Rename S(m)=T(2m)S(m)=T(2^m)

S(m)=2S(m/2)+m S(m)=2S(m/2)+m

We know from before that the solution is Θ(mlgm)\Theta(m \lg m)

We now change back the variables:

T(n)=T(2m)=S(m)=Θ(mlgm)=Θ(lgnlglgn) \begin{aligned} T(n)&=T(2^m)=S(m)=\Theta(m \lg m)\\ &=\Theta(\lg n \lg \lg n) \end{aligned}

1.2 - Iteration Method

1.2.1 - Iteration Example

Consider the following recurrence:

T(n)={Θ(1)                if n1n+3T(n/4)if n>1 T(n)= \begin{cases} \Theta(1)\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ &\text{if }n\le1\\ n+3T(\lfloor n/4 \rfloor) &\text{if } n > 1 \end{cases}

We begin by taking the recurrence and expanding it out:

T(n)    =n+3T(n/4)                                          if n>1=n+3(n/4+3T(n/16))if n4>1=n+3(n/4+3(n/16+3T(n/64)))if n16>1=n+3n/4+9n/16+27T(n/64)=n+3n/4+32n/42+33T(n/43) \begin{aligned} \color{pink}T(n)\ \ \ \ &\color{pink}=n+3T(\lfloor n/4\rfloor) \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \color{pink}&\text{if } n>1\\ &\color{orange}=n+3(\lfloor n/4 \rfloor + 3T(\lfloor n/16\rfloor)) &\color{orange}\text{if } \lfloor \frac n4 \rfloor > 1\\ &\color{lightgreen}= n+3(\lfloor n/4 \rfloor + 3(\lfloor n/16 \rfloor + 3T(\lfloor n/64\rfloor))) &\color{lightgreen}\text{if } \lfloor \frac {n} {16} \rfloor >1\\ &\color{lightgreen}=n+3\lfloor n/4\rfloor + 9\lfloor n/16\rfloor + 27T(\lfloor n/{64}\rfloor) \\ &\color{lightgreen}= n+3\lfloor n/4\rfloor + 3^2\lfloor n/4^2\rfloor +3^3T(\lfloor n/4^3\rfloor) \end{aligned}

We want to develop a generic form for the expression above, if we were to expand it some arbitrary amount of times, ii.

=n+3n/4+32n/42++3iT(n/4i)      ifn4i1>1 =n+3\lfloor n/4\rfloor +3^2\lfloor n/4^2\rfloor +\cdots+3^iT(\lfloor n/4^i\rfloor)\ \ \ \ \ \ \text{if} \lfloor \frac{n}{4^{i-1}}\rfloor>1

1.2.2 - Iteration Method Notes


1.2.3 - Iteration Example 2

Solve the following recurrence using iteration:

T(n)=1+2T(n1) T(n)=1+2T(n-1)

assuming that T(n)Θ(1)T(n)\in\Theta(1) for n0n\le 0

T(n)=1+2T(n1)                                             for n>0=1+2(1+2T(n2))for n>1=1+2+4T(n2)=3+4T(n2)=3+4(1+2T(n3))for n>2=3+4+8T(n3)=7+8T(n3)=7+8(1+2T(n4))=15+16T(n4)=j=0i12j+2iT(ni)for n>i \begin{aligned} \color{pink}T(n)&\color{pink}=1+2T(n-1) \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ &\color{pink}\text{for }n>0\\ &\color{orange}=1+2(1+2T(n-2)&\color{orange})\text{for } n>1\\ &\color{orange}=1+2+4T(n-2)\\ &\color{orange}=3+4T(n-2)\\ &\color{yellow}=3+4(1+2T(n-3))&\color{yellow}\text{for } n>2\\ &\color{yellow}=3+4+8T(n-3)\\ &\color{yellow}=7+8T(n-3)\\ &\color{lightgreen}=7+8(1+2T(n-4))\\ &\color{lightgreen}=15+16T(n-4)\\ &=\sum^{i-1}_{j=0}2^j +2^i T(n-i)&\text{for } n>i\\ \end{aligned}

1.3 - Master Method

Need to memorise 3 cases for solving some recurrences of the form:

T(n)=aT(n/b)+f(n) T(n)=aT(n/b)+f(n)

where n/bn/b can be n/b\lfloor n/b\rfloor or n/b\lceil n/b\rceil

In each case, we compare nlogban^{\log_b a} with f(n)f(n)

  1. nlogban^{\log_b a} is polynomially larger than f(n)f(n) → Solution is Θ(nlogba)\Theta(n^{\log_b a})
  2. nlogban^{\log_b a} is same tight asymptotic bound as f(n)f(n) → Solution is Θ(nlogbalgn)\Theta(n^{\log_b a} \lg n)
  3. f(n)f(n) is polynomially larger than nlogban^{\log_b a} and f(n)f(n) is regular → Solution is Θ(f(n))\Theta(f(n))

1.3.1 - Polynomially Larger Than

A function f(n)f(n) is polynomially larger than g(n)g(n) when

f(n)Ω(g(n)×nϵ)      for some ϵ>0 f(n)\in\Omega(g(n)\times n^\epsilon) \ \ \ \ \ \ \text{for some }\epsilon>0

Equivalently:

f(n)g(n)Ω(nϵ)     for some ϵ>0 \frac{f(n)}{g(n)}\in\Omega(n^\epsilon)\ \ \ \ \ \text{for some } \epsilon>0

In which f(n)f(n) is polynomially larger than g(n)g(n) by polynomial nϵn^\epsilon

Example:

Is f(n)=n2f(n)=n^2 polynomially larger than g(n)=n3/2g(n)=n^{3/2}?

f(n)g(n)=n2n3/2=n23/2=n1/2\frac{f(n)}{g(n)}=\frac{n^2}{n^{3/2}}=n^{2-3/2}=n^{1/2}

Yes, as n2Ω(n3/2×n1/2)n^2\in \Omega(n^{3/2}\times n^{1/2})

Is f(n)=log2nf(n)=\log_2 n polynomially larger than g(n)=1g(n)=1?

No, as log2n∉Ω(1×nϵ)\log_2 n \not{\in}\Omega(1\times n^\epsilon) for any ϵ>0\epsilon>0

log2n\log_2 n is larger than, but it is not polynomially larger than

1.3.2 - Regularity

Function f(n)f(n) is regular when:

af(n/b)<cf(n)     for some c<1 af(n/b)<cf(n)\ \ \ \ \ \text{for some c<1}

1.3.3 - Formal Definition of Master Method

Given recurrences of the form:

T(n)=aT(nb)+f(n) T(n)=aT(\frac nb)+f(n)

case 1T(n)Θ(nlogba)               if               f(n)O(nlogbaϵ)    for some ϵ>0case 2T(n)Θ(nlogbalgn)        if               f(n)Θ(nlogba)       for some ϵ>0case 3T(n)Θ(f(n))                if               f(n)Ω(nlogba+ϵ)    for some ϵ>1                                           and           af(nb)cf(n)          for some c<1\text{case 1}\\ T(n)\in\Theta(n^{\log_b a})\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \text{if}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ f(n)\in O(n^{\log_b a-\epsilon})\ \ \ \ \text{for some } \epsilon>0\\ \text{case 2}\\ T(n)\in\Theta(n^{\log_b a }\lg n) \ \ \ \ \ \ \ \ \text{if}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ f(n)\in\Theta(n^{\log_b a})\ \ \ \ \ \ \ \text{for some }\epsilon>0\\ \text{case 3}\\ T(n)\in\Theta(f(n))\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \text{if}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ f(n)\in\Omega(n^{\log_b a + \epsilon}) \ \ \ \ \text{for some } \epsilon>1\\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \text{and } \ \ \ \ \ \ \ \ \ \ af(\frac nb) \le cf(n) \ \ \ \ \ \ \ \ \ \ \text{for some } c<1

We can re-define the master method, using the division syntax.

case 1T(n)Θ(nlogba)               if               f(n)nlogbaϵO(nϵ)    for some ϵ>0case 2T(n)Θ(nlogbalgn)        if               f(n)nlogbaΘ(1)          for some ϵ>0case 3T(n)Θ(f(n))                if               f(n)nlogba+ϵΩ(nϵ)      for some ϵ>1                                           and           af(nb)cf(n)      for some ​c<1\text{case 1}\\ T(n)\in\Theta(n^{\log_b a})\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \text{if}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\small\frac{f(n)}{n^{\log_b a-\epsilon}}}\in O(n^{-\epsilon})\ \ \ \ \text{for some } \epsilon>0\\ \text{case 2}\\ T(n)\in\Theta(n^{\log_b a }\lg n) \ \ \ \ \ \ \ \ \text{if}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \frac{f(n)}{n^{\log_b a}}\in\Theta(1)\ \ \ \ \ \ \ \ \ \ \text{for some }\epsilon>0\\ \text{case 3}\\ T(n)\in\Theta(f(n))\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \text{if}\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \frac{f(n)}{n^{\log_b a + \epsilon}}\in\Omega(n^\epsilon)\ \ \ \ \ \ \text{for some } \epsilon>1\\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \text{and } \ \ \ \ \ \ \ \ \ \ af(\frac nb) \le cf(n) \ \ \ \ \ \ \text{for some } ​c<1

1.3.4 - Application of the Master Theorem:

Recurrence: T(n)=2T(n2)+f(n)     where f(n)Θ(n)\color{lightblue} T(n)=2T(\frac n2)+f(n)\ \ \ \ \ \text{where } f(n)\in \Theta(n)

The master method works on recurreneces of the form

T(n)=aT(nb)+f(n) T(n)=aT(\frac nb)+f(n)

    a=2,b=2\ \ \ \ a=2, b=2

f(n)nlogbaΘ(nnlog22)=Θ(nn1)=Θ(1)\frac{f(n)}{n^{\log_b a}}\in\Theta(\frac{n}{n^{\log_2 2}})=\Theta(\frac{n}{n^1})=\Theta(1)

Therefore, the second case applies, and the solution is Θ(nlog22lgn)=Θ(nlgn)\Theta(n^{\log_2 2} \lg n)=\Theta(n \lg n)


Recurrence: T(n)=1T(n2)+f(n)     where f(n)Θ(1)\color{lightblue} T(n)=1T(\frac n2)+f(n)\ \ \ \ \ \text{where } f(n)\in\Theta(1)

The master method works on recurreneces of the form

T(n)=aT(nb)+f(n) T(n)=aT(\frac nb)+f(n)

    a=1,b=2\ \ \ \ a=1, b=2

f(n)nlogbaΘ(1nlog21)=Θ(11)=Θ(1)\frac{f(n)}{n^{\log_b a}}\in \Theta(\frac{1}{n^{\log_2 1}})=\Theta(\frac 11)=\Theta(1)

Therefore, the second case applies, and the solution is Θ(nlog21lgn)=Θ(n0lgn)=Θ(lgn)\Theta(n^{\log_2 1 }\lg n)=\Theta(n^0 \lg n)=\Theta(\lg n)


Recurrence: T(n)=7T(n2)+f(n)      where f(n)Θ(n2)\color{lightblue} T(n)=7T(\frac n2)+f(n)\ \ \ \ \ \ \text{where } f(n)\in \Theta(n^2)

The master method works on recurreneces of the form

T(n)=aT(nb)+f(n) T(n)=aT(\frac nb)+f(n)

    a=7,b=2\ \ \ \ a=7, b=2

f(n)nlogbaΘ(n2nlog27)=Θ(n(log272))\frac {f(n)}{n^{\log_b a}}\in\Theta(\frac{n^2}{n^{\log_2 7}})=\Theta(n^{-(\log_2 7-2)})

Additionally, ϵ=log272>0\epsilon=\log_2 7-2>0

Since 4<7<84\lt7\lt8, we know that log24<log27<log28\log_2 4<\log_2 7<\log_2 8 and therefore, 2<log27<32\lt \log_2 7\lt3

Hence, case 1 applies and T(n)Θ(nlogba)=Θ(nlog27)Θ(n2.81)T(n )\in \Theta(n^{log_b a})=\Theta(n^{log_2 7}) \approx\Theta(n^{2.81})


Recurrence: T(n)=4T(n2)+f(n)     where f(n)Θ(n3)\color{lightblue}T(n)=4T(\frac n2)+f(n)\ \ \ \ \ \text{where } f(n)\in\Theta(n^3)

The master method works on recurrences of the form

T(n)=aT(nb)+f(n) T(n)=aT(\frac nb)+f(n)

    a=4,b=2\ \ \ \ a=4, b=2

f(n)nlogbaΘ(n3nlog24)=Θ(n3n2)=Θ(n)\frac {f(n)}{n^{\log_b a}}\in\Theta(\frac{n^3}{n^{\log_2 4}})=\Theta(\frac{n^3}{n^2})=\Theta(n)

Case 3 of the master method applies, as long as we can satisfy the regularity condition.

af(n/b)cf(n)   for some c<1 af(n/b)\le cf(n) \ \ \ \text{for some } c<1

That is,

af(nb)=4(n2)3=4n38=12n3cn3 af(\frac nb)=4(\frac n2)^3=4\frac{n^3}8=\frac 12n^3\le cn^3

For some choice of c=12<1c=\frac12<1

1.3 - Extended Master Method

Fills in a gap in the Master Theorem

The above case 2 is a special case of this, when k=0k=0