4

Problem

I've seen plenty of discussion regarding solving recurrences like this with the master theorem, but I need to solve it via back substitution. Was doing pretty good until I got stuck in the summation part.

Assumptions are that $n$ is a power of $3$ so $n = 3^k$ and hence $k = \log_3(n)$. Also $T(1) = 1$.

After substituting $3$ functions to recognize the pattern I found this general form for the recurrence:

Formula 1

And then I followed with:

Formula 2

However this is the part where I got stuck:

Formula 3

I have absolutely no idea of how to resolve that sum, I tried applying logarithm properties to get a difference instead of division and then make two separated and simplier sums, but I still couldn't find or figure out if I can use any summation identities.

MikeKatz45
  • 167
  • 11
  • 1
    Note that $$\sum_{i=0}^{k-1}\log_3\left(\frac n{3^i}\right)=\sum_{i=0}^{k-1}(\log_3 n-i)=k\log_3n-\sum_{i=0}^{k-1}i$$ hence $$\sum_{i=0}^{k-1}\log_3\left(\frac n{3^i}\right)=k\log_3n-\frac{k(k-1)}2$$ – Did Jan 26 '19 at 19:12
  • Why are you able to take log3(n) out of the summation without writing it in sigma notation again? Where does the k multiplying that come from? D: – MikeKatz45 Jan 26 '19 at 19:24
  • 1
    Because $\log_3n$ does not depend on $i$. And because there are $k$ terms in the sum. For every number $c$ independent of $i$, $$\sum_{i=0}^{k-1}c=c,k$$ – Did Jan 26 '19 at 19:27
  • that means log3(n) could be treated as a constant? or does "constant" in this case just means not dependant on the sum index? – MikeKatz45 Jan 26 '19 at 19:35
  • Either one you prefer. – Did Jan 26 '19 at 19:37
  • also I just noticed it is log2(n/3^i) and not log3 ): – MikeKatz45 Jan 26 '19 at 19:58
  • Trivial changes, similar result. – Did Jan 26 '19 at 20:32

2 Answers2

2

I will redo the work from begginning also using $\lg x=\log_2 x$ as binary logarithm.

First I will iterate the first term, then later combine it with the sum which the second term gave:$$T(n)=3T\left(\frac{n}{3}\right)+n\lg n$$ $$3T\left(\frac{n}{3}\right)=3\left(3T\left(\frac{n}{3^2}\right)+\frac{n}{3}\lg\frac{n}{3}\right)=3^2T\left(\frac{n}{3^2}\right)+n\lg\frac{n}{3}$$ $$3^2T\left(\frac{n}{3^2}\right)=3^2\left(3T\left(\frac{n}{3^3}\right)+\frac{n}{3^2}\lg\frac{n}{3^2}\right)=3^3T\left(\frac{n}{3^3}\right)+n\lg\frac{n}{3^2}$$ $$\dots$$ $$3^{k-1}T\left(\frac{n}{3^{k-1}}\right)=3^kT\left(\frac{n}{3^k}\right)+n\lg\frac{n}{3^{k-1}}$$ The recursion ends when we hit $T(1)$, thus we can take $k$ from there:
$$T\left(\frac{n}{3^k}\right)=T(1)\Rightarrow n=3^k\Rightarrow k=\log_3n$$ $$\Rightarrow T(n)=3^k \cdot \underbrace{T(1)}_{=1}+n\lg n +n\lg\frac{n}{3}+\dots+n\lg\frac{n}{3^{k-1}}$$ $$=\underbrace{3^{\log_3 n}}_{=n} \cdot 1 +\sum_{i=0}^{k-1}n \lg\frac{n}{3^{i}}=n+n\sum_{i=0}^{k-1}\left(\lg n-\lg3^{i}\right),\quad \quad a^{\log_a b}=b^{\log_a a}$$ $$=n+n\lg n \sum_{i=0}^{k-1}1-n\sum_{i=0}^{k-1}i \cdot \lg 3, \quad \quad \lg a^b = b\lg a$$ $$=n+n\lg n \cdot k -n\lg 3 \cdot \frac{(k-1)k}{2}$$ $$=n+n\lg n \cdot \frac{\lg n}{\lg 3}-\frac{\lg 3}{2}n\left(\frac{\lg n}{\lg 3}-1\right)\frac{\lg n}{\lg 3},\quad \quad k=\log_3 n =\frac{\lg n}{\lg 3}$$ $$=n+\frac{1}{\lg 3} n\lg^2 n -\frac12 \frac{1}{\lg 3}n\lg^2 n+\frac12 n\lg n$$ $$\boxed{T(n)=n+\frac1{2\log_2 3} n\log_2^2 n +\frac12 n\log_2 n}$$

Zacky
  • 27,674
  • 1
    I tried to follow from where I got stuck, but I got a slightly different result. Could you check my edit at the end of my question and check if I missed something? I think you missed multiplying by lg3 in one of the denominators, but it could be my mistake too – MikeKatz45 Jan 27 '19 at 03:20
  • @MikeKatz45 Yes, I forgot to multiply there by the red factor: $$\frac{\lg 3}{2}n\left(\frac{\lg n}{\lg 3}-1\right)\frac{\lg n}{\color{red}{\lg 3}}$$ Also note that you have a minus sign when you mutiply by $1$, thus the last term will have a plus. – Zacky Jan 27 '19 at 09:54
  • 1
    Excellent thank you very much, it took me a while to follow your proceedure, but I understand everything now (: – MikeKatz45 Jan 27 '19 at 18:21
0

Considering from $T(n)-3T(\frac n3)=n\log_2 n$

$$ \mathcal{T}(\log_3 n)-3\mathcal{T}(\log_3(\frac n3))=n\log_2 n $$

or

$$ \mathcal{T}(\log_3 n)-3\mathcal{T}(\log_3 n -1))=n\log_2 n $$

then making $z = \log_3 n$ we have the linear difference equation

$$ \mathcal{T}(z)-3\mathcal{T}(z -1)=3^z\log_2 3^z = z 3^z\log_2 3 $$

The solution can be represented as

$$ \mathcal{T} = \mathcal{T}_h+ \mathcal{T}_p $$

such that

$$ \mathcal{T}_h(z)-3\mathcal{T}_h(z -1)=0\\ \mathcal{T}_p(z)-3\mathcal{T}_p(z -1)=z 3^z\log_2 3 $$

but

$$ \mathcal{T}_h(z) = C 3^{z-1} $$

now substituting $\mathcal{T}_p(z) = C(z) 3^{z-1}$ into the particular equation (variation of constants as with DE's) we obtain

$$ C(z)-C(z-1) = z^2 3^{2-z}\log_2 3 $$

and then

$$ C(z) = \frac 12 3^{3-z}\left(3^z-z-\frac{z^2}{3}-1\right)\log_2 3 $$

and finally

$$ \mathcal{T} = C 3^{z-1}+\left( \frac 12 3^{3-z}\left(3^z-z-\frac{z^2}{3}-1\right)\log_2 3\right)3^{z-1} $$

etc.

Cesareo
  • 33,252