3

I have been trying to solve the following recurrence relation

$$T(n)=2T(\frac{n}{2}) + nlgn$$ by using substitution method.

I started to compute $T(1)$ ,$T(4)$,$T(8)$,$T(16)$ to guess a solution as below:

$$T(1)=1$$ $$T(2)=2+2lg2=4$$ $$T(4)=8+8lg2=16$$ $$T(8)=32+24lg2=56$$ $$T(16)=112+64lg2=176$$

My guess is that $T(n)$ should be something like $nlg^2n$ but I couldn't get a correct term.

Can anyone improve my guess?

5 Answers5

3

At each step, we replace $n$ with $n/2$ and multiply the equation by $2$: \begin{align*} T(n) - 2T(\tfrac{n}{2}) &= n\lg n \\ 2T(\tfrac{n}{2}) - 2^2T(\tfrac{n}{2^2}) &= n\lg \tfrac{n}{2} \\ 2^2T(\tfrac{n}{2^2}) - 2^3T(\tfrac{n}{2^3}) &= n\lg \tfrac{n}{2^2} \\ 2^3T(\tfrac{n}{2^3}) - 2^4T(\tfrac{n}{2^4}) &= n\lg \tfrac{n}{2^3} \\ &~~\vdots \\ 2^{\lg n - 1}T(2) - 2^{\lg n}T(1) &= n\lg2 \\ \end{align*} Summing the $\lg n$ equations together, we note that the LHS telescopes, yielding: \begin{align*} T(n) - 2^{\lg n}T(1) &= n\sum_{k=0}^{\lg n - 1} \lg \tfrac{n}{2^k} \\ T(n) - n \cdot 1 &= n\sum_{k=0}^{\lg n - 1} (\lg n - k) \\ T(n) - n &= n\sum_{k=0}^{\lg n - 1}\lg n - n\sum_{k=0}^{\lg n - 1} k \\ T(n) - n &= n(\lg n)(\lg n) - n\frac{(\lg n)(\lg n - 1)}{2} \\ T(n) &= \frac{1}{2}n\lg^2 n + \frac{1}{2}n \lg n + n \end{align*} Thus, we conclude that $T(n) = \Theta(n \lg^2 n)$, which you suspected.

Adriano
  • 41,576
  • Appreciate your answer. I was just unable to do these steps – Bledi Boss Oct 25 '14 at 19:46
  • you mention $T(n)$ to be $$\frac{1}{2}n\lg^2 n - \frac{1}{2}n \lg n + n$$ but plugging in values for $T(2)$, $T(4)$ and so on would produce different values from what I found. – Bledi Boss Oct 25 '14 at 19:56
  • My mistake. I switched the negative sign by accident. They now agree so that $T(2) = 4$, $T(4) = 16$, and so on. Note that from the given recurrence: $$ T(4) = 2T(2) + 4 \lg 4 = 2(4) + 4(2) = 8 + 8 = 16 $$ – Adriano Oct 25 '14 at 20:05
1

Take $n = 2^kp$ where $p$ is odd, then $$T(2^{k+1}p) = 2T(2^kp) + 2^{k}p\log(2^kp) $$

So $$\dfrac{T(2^{k+1}p)}{2^{k+1}} = \dfrac{T(2^kp)}{2^k} + \dfrac{pk\log(2)+p\log p }{2}$$

Then we have $$\dfrac{T(2^{k+1}p)}{2^{k+1}} = T(p) + \dfrac{p\log2}{2}(\sum_{j=0}^k j) + \frac{p(k+1)\log p}{2}$$

$$T(2^{k+1}p) = 2^{k+1}T(p) + 2^k p \log2\dfrac{(k+1)k}{2} + p(k+1)(\log p)2^k$$

If you plug $p=1$, this can be simplified a bit

  • Would this solution bring me a formula for $T(n)$? – Bledi Boss Oct 25 '14 at 18:56
  • Your recurrence relation only defines a sequence $T(2^kp)$ for each $p$, and there is no relation between sequence with different $p$. But of course if you take $p=1$, you get a formula for $T(2^k)$ – Petite Etincelle Oct 25 '14 at 19:02
  • From what you wrote, taking $p=1$ and $T(1)=1$: $$ T(2^k2) = 2^{k+1} + 2^k(k+1)\frac{k}{2} $$ $$T(2n)=2^k2+n(lgn+1)\frac{lgn}{2}$$ $$T(2n)=2n+n(lgn+1)\frac{lgn}{2}$$ is that correct? – Bledi Boss Oct 25 '14 at 19:13
  • @BlediBoss take $p=1$, we only get a formula of $T(2n)$ for $n$ in form of $2^k$, but it's not for all $n$. For example I can define $T(2n+1) = e^{2n+1}$, of course it grows much faster than $n$ and $\log n$ – Petite Etincelle Oct 25 '14 at 19:21
  • @martycohen showed something different from your solution. I am just confused. – Bledi Boss Oct 25 '14 at 19:27
  • @BlediBoss Marty missed the $2$ before $T(2^{m-1})$ – Petite Etincelle Oct 25 '14 at 20:15
1

There is another closely related recurrence that admits an exact solution. Suppose we have $T(0)=0$ and $T(1)=1$ and for $n\ge 2$ $$T(n) = 2 T(\lfloor n/2 \rfloor) + n \lfloor \log_2 n \rfloor.$$

Furthermore let the base two representation of $n$ be $$n = \sum_{k=0}^{\lfloor \log_2 n \rfloor} d_k 2^k.$$

Then we can unroll the recurrence to obtain the following exact formula for $n\ge 2$ $$T(n) = 2^{\lfloor \log_2 n \rfloor} + \sum_{j=0}^{\lfloor \log_2 n \rfloor -1} 2^j \times (\lfloor \log_2 n \rfloor - j) \times \sum_{k=j}^{\lfloor \log_2 n \rfloor} d_k 2^{k-j} \\ = 2^{\lfloor \log_2 n \rfloor} + \sum_{j=0}^{\lfloor \log_2 n \rfloor -1} (\lfloor \log_2 n \rfloor - j) \times \sum_{k=j}^{\lfloor \log_2 n \rfloor} d_k 2^k.$$

Note that this formula produces matching values for the data at $n=2^j$ posted by the OP in the question, namely the sequence $$1, 4, 16, 56, 176, 512, 1408, 3712\ldots$$

Now to get an upper bound consider a string of one digits to obtain $$T(n) \le 2^{\lfloor \log_2 n \rfloor} + \sum_{j=0}^{\lfloor \log_2 n \rfloor -1} (\lfloor \log_2 n \rfloor - j) \times \sum_{k=j}^{\lfloor \log_2 n \rfloor} 2^k.$$ This simplifies to $$\lfloor \log_2 n \rfloor^2 2^{\lfloor \log_2 n \rfloor} + \lfloor \log_2 n \rfloor 2^{\lfloor \log_2 n \rfloor} - 2^{\lfloor \log_2 n \rfloor} + \lfloor \log_2 n \rfloor + 2.$$

This bound is actually attained and cannot be improved upon, just like the lower bound, which occurs with a one digit followed by zeroes to give $$T(n) \ge 2^{\lfloor \log_2 n \rfloor} + \sum_{j=0}^{\lfloor \log_2 n \rfloor -1} (\lfloor \log_2 n \rfloor - j) \times 2^{\lfloor \log_2 n \rfloor}.$$

This simplifies to $$\frac{1}{2} \lfloor \log_2 n \rfloor^2 2^{\lfloor \log_2 n \rfloor} + \frac{1}{2} \lfloor \log_2 n \rfloor 2^{\lfloor \log_2 n \rfloor} + 2^{\lfloor \log_2 n \rfloor}.$$

Joining the dominant terms of the upper and the lower bound we obtain the asymptotics $$\lfloor \log_2 n \rfloor^2 \times 2^{\lfloor \log_2 n \rfloor} \in \Theta\left((\log_2 n)^2 \times 2^{\log_2 n}\right) = \Theta\left((\log n)^2 \times n\right).$$

The above would seem to be in agreement with what the Master theorem would produce.

There are some additional calculations using this method at this MSE link.

Marko Riedel
  • 61,317
0

In $T(n)=2T(\frac{n}{2}) + n\ lgn$ put $n=2^m$. This becomes $T(2^m) =2T(2^{m-1})+m 2^m $.

(Note: I fixed an error so the following has been completely rewritten.)

Let $S(m) =T(2^m) $. Then $S(m) =2S(m-1)+m2^m $. Dividing by $2^m$, $2^{-m}S(m) =2^{-m-1}S(m-1)+m $. Letting $U(m) =2^{-m}S(m) $, $U(m) =U(m-1)+m $ or $U(m)-U(m-1) =m $. Summing, $U(m) =m(m-1)/2 \approx m^2/2 $.

Working back, $S(m) =2^mU(m) \approx 2^mm^2/2 $. Finally, $T(n) =S(lg\ n) \approx n (lg\ n)^2/2 $.

marty cohen
  • 107,799
  • In your answer you are implying that there is no $lg^2n$ formula for $T(n)$, right? That mean my guess was wrong. But the first answer approved that there'd be some $lg^2n$ term. – Bledi Boss Oct 25 '14 at 19:21
  • You are right. I found my error and fixed it. Now I get the $lg^2n$ term. – marty cohen Oct 26 '14 at 02:23
0

with Mathematica we get this here $\left\{\left\{a(n)\to 2 c_1 n-n \log \left(n^{\frac{1}{2} \left(-\frac{\log (n)}{\log (2)}-1\right)}\right)\right\}\right\}$