2

Solve $T(n)= T(\frac n 2) + 2 T(\frac n 4) + n, T(1)=1$

Each vertex in the tree has 3 children, we have up to the $\log _4 n $ level a complete tree, that has $3 ^{\log_4 n}$ vertices on that level. The last level is $\log_2 n$ and it has $3 ^{\log_2 n}$ vertices. In each level we have $n$ "work", so we get that:

$$n3^{\log_4 n}= n \sum_{i=0}^{\log_4 n}3^i \le T(n) \le n \sum_{i=0}^{\log_2 n}3^i \le T(n)=n \frac {1-3^{\log_2 n}}{-2}= n3^{\log_2 n}$$

Now both $3^{\log_4 n}, 3^{\log_2 n}$ are very similar to $3^{\log_3 n}$, so does it simply follow that $T(n) = \Theta (n\cdot n) = \Theta (n^2)$ ? Is this considred a legal move?

Edit: found my error, I should summed (amount of levels) * (work in each level) instead of (amount of leaves in each level)*(work in each level)

shinzou
  • 3,981

1 Answers1

1

Suppose we start by solving the following recurrence: $$T(n) = T(\lfloor n/2 \rfloor) + 2T(\lfloor n/4 \rfloor) + n$$ where $T(1) = 1$ and $T(0) = 0.$

Now let $$n = \sum_{k=0}^{\lfloor \log_2 n \rfloor} d_k 2^k$$ be the binary representation of $n.$ We unroll the recursion to obtain an exact formula for $n\ge 1$ $$T(n) = \sum_{j=0}^{\lfloor \log_2 n \rfloor} [z^j] \frac{1}{1-z-2z^2} \sum_{k=j}^{\lfloor \log_2 n \rfloor} d_k 2^{k-j}.$$

Studying the generating function we use partial fractions by residues on the simple poles to get

$$ \frac{1}{1-z-2z^2} = \frac{1}{z+1}\frac{1}{-1-4(-1)} + \frac{1}{z-1/2}\frac{1}{-1-4(1/2)} \\ = \frac{1}{3} \frac{1}{z+1} -\frac{1}{3} \frac{1}{z-1/2} = \frac{1}{3} \frac{1}{z+1} + \frac{2}{3} \frac{1}{1-2z}.$$

This yields $$ [z^j] \frac{1}{1-z-2z^2} = \frac{1}{3} (-1)^j + \frac{2}{3} 2^j.$$

The new exact formula becomes

$$T(n) = \frac{1}{3}\sum_{j=0}^{\lfloor \log_2 n \rfloor} (-1)^j \sum_{k=j}^{\lfloor \log_2 n \rfloor} d_k 2^{k-j} + \frac{2}{3}\sum_{j=0}^{\lfloor \log_2 n \rfloor} 2^j \sum_{k=j}^{\lfloor \log_2 n \rfloor} d_k 2^{k-j}.$$

We now compute lower and upper bounds which are actually attained and cannot be improved upon. For the lower bound consider a one digit followed by a string of zeroes, to give

$$T(n) \ge \frac{1}{3}\sum_{j=0}^{\lfloor \log_2 n \rfloor} (-1)^j 2^{\lfloor \log_2 n \rfloor-j} + \frac{2}{3}\sum_{j=0}^{\lfloor \log_2 n \rfloor} 2^j 2^{\lfloor \log_2 n \rfloor-j} \\ = \frac{1}{3} 2^{\lfloor \log_2 n \rfloor} \frac{1-(-1/2)^{\lfloor \log_2 n \rfloor+1}}{1-(-1/2)} + \frac{2}{3} (\lfloor \log_2 n \rfloor+1) 2^{\lfloor \log_2 n \rfloor} \\ = \frac{2}{9} \left(2^{\lfloor \log_2 n \rfloor} + \frac{1}{2} (-1)^{\lfloor \log_2 n \rfloor}\right) + \frac{2}{3} (\lfloor \log_2 n \rfloor+1) 2^{\lfloor \log_2 n \rfloor}.$$

We see that the dominant term here is $$\frac{2}{3} \lfloor \log_2 n \rfloor 2^{\lfloor \log_2 n \rfloor}.$$

For an upper bound consider a string of one digits to get

$$T(n) \le \frac{1}{3}\sum_{j=0}^{\lfloor \log_2 n \rfloor} (-1)^j \left(2^{\lfloor \log_2 n \rfloor - j + 1} - 1\right) + \frac{2}{3}\sum_{j=0}^{\lfloor \log_2 n \rfloor} 2^j \left(2^{\lfloor \log_2 n \rfloor - j + 1} -1 \right).$$

Recognizing the terms we already computed in the lower bound this becomes

$$T(n) \le \frac{4}{9} \left(2^{\lfloor \log_2 n \rfloor} + \frac{1}{2} (-1)^{\lfloor \log_2 n \rfloor}\right) + \frac{4}{3} (\lfloor \log_2 n \rfloor+1) 2^{\lfloor \log_2 n \rfloor} \\ - \frac{1}{6} \left(1+(-1)^{\lfloor \log_2 n \rfloor}\right) - \frac{2}{3} \left(2^{\lfloor \log_2 n \rfloor+1}-1\right).$$

Collecting like terms finally yields $$T(n) \le \frac{4}{9} 2^{\lfloor \log_2 n \rfloor} + \frac{1}{18} (-1)^{\lfloor \log_2 n \rfloor} + \frac{1}{2} + \frac{4}{3} \lfloor \log_2 n \rfloor 2^{\lfloor \log_2 n \rfloor}.$$

We see that the dominant term here is $$\frac{4}{3} \lfloor \log_2 n \rfloor 2^{\lfloor \log_2 n \rfloor}.$$

Joining the upper and the lower bound we get for the asymptotics of this recurrence that it is

$$T(n)\in\Theta \left(\lfloor \log_2 n \rfloor 2^{\lfloor \log_2 n \rfloor}\right) = \Theta\left(\log_2 n \times 2^{\ \log_2 n}\right) = \Theta(n \log n),$$

which, let it be said, could also have been obtained by inspection or from the Master theorem.

Remark. Note that on the lower bound the floor on the logarithm is exact and we get $$\frac{2}{3} \log_2 n 2^{\log_2 n} = \frac{2}{3} n \log_2 n.$$

On the upper bound the floor on the logarithm is off by about one and we get $$\frac{4}{3} (\log_2 n - 1) 2^{\log_2 n - 1} = \frac{2}{3} n \log_2 n - \frac{2}{3} n$$

which permits us to say that the dominant asymptotic is $$\frac{2}{3} n \log_2 n.$$

This formula is very stable numerically which isn't always the case with approximations to these Master theorem type recurrences.

A closely related recurrence where the asymptotics are of a different type is at this MSE link.

Marko Riedel
  • 61,317