0

How do I solve the recurrence relation $T(n)=n+T(n/2)+T(n/4)+\cdots+T(n/2^k)$, for constant $k$?

I am told that the answer does not depend on $k$.

David Richerby
  • 81,689
  • 26
  • 141
  • 235

1 Answers1

2

Suppose that the base case ensures that $T(n) \geq 0$. Clearly $T(n) \geq n$. On the other hand, guessing that $T(n) \leq Cn$, an inductive proof would work as long as $$ n + C(n/2 + n/4 + \cdots + n/2^k) \leq Cn. $$ Now the left-hand side is at most $n(1 + C(1-1/2^k))$, so we would need $C$ to satisfy $$ 1 + (1-1/2^k)C \leq C. $$ In other words, $1 \leq C/2^k$, and so we can choose $C \geq 2^k$. Concluding, we have shown that $T(n) = \Theta(n)$. While this is true for every finite $k$, the hidden constant depends on $k$ and on the initial conditions.


The case in which $k$ is unbounded – let's say goes all the way to $\lfloor \log_2 n \rfloor$ – is more interesting. In this case we guess a solution of the form $T(n) \approx nf(n)$. The function $f(n)$ has to satisfy the recurrence $$ f(n) = 1 + \frac{f(n/2)}{2} + \frac{f(n/4)}{4} + \cdots. $$ A reasonable guess is $f(n) = \log_b n$, under which the right-hand side reads $$ 1 + \frac{\log_b n - \log_b 2}{2} + \frac{\log_b n - 2\log_b 2}{4} + \cdots = \log_b n - \kappa + o(1), $$ where the constant $\kappa$ equals $$ \kappa = 1 - \sum_{m \geq 1} \frac{m}{2^m} \log_b 2 = 1 - 2\log_b 2. $$ Choosing $b=4$, we see that $T(n) \approx n \log_4 n$, and certainly $T(n) = \Theta(n\log n)$.

Yuval Filmus
  • 276,994
  • 27
  • 311
  • 503