5

I'm currently in a course, and I for the life of me cannot figure out what my professor is doing. I could really use a working example, and I was hoping someone here might oblige.

Suppose we have this recurrence:

$$ T(n) = 2T(n/2) + \log n$$

How would we figure out running time for this, and specifically, how would we PROVE the upper and lower bounds (ideally as tight as possible)?

I tried working through my homework with the help of a few YouTube videos, but I don't seem to be following it. In giving an explanation, please try and be careful to explain why something is a certain time, as I haven't really gotten the hang of intuiting that yet.

I have some working knowledge of induction and calculus, but it seems like I'm missing a big chunk of context somewhere and I really need to get up to speed. Thank you for any assistance.

Yuval Filmus
  • 280,205
  • 27
  • 317
  • 514

2 Answers2

6

First of all, a recurrence is not necessarily about the running time of anything. So you don't figure out "the running time", you solve the recurrence.

Second, your recurrence only possibly makes sense for powers of 2, and even then, it needs a base case. I'll assume that $T(1) = C$.

You can use the master theorem to solve your recurrence, but you can also do it directly. First you expand it (for simplicity, I'm assuming the base of the logarithm is 2): $$ \begin{align*} T(2^k) &= k + 2T(2^{k-1}) \\ &= k + 2(k-1) + 4T(2^{k-2}) \\ &= k + 2(k-1) + 4(k-2) + \cdots + 2^{k-1}(k-(k-1)) + 2^k T(1) \\ &= (1+2+\cdots+2^{k-1})+(1+2+\cdots+2^{k-2})+\cdots+(1) + 2^k C \\ &= (2^k-1)+(2^{k-1}-1)+\cdots+(2-1)+(1-1) + 2^kC \\ &= (2^k+\cdots+1)-(k+1) + 2^kC \\ &= 2^{k+1}-k-2 + 2^kC. \end{align*} $$ If you want to be very pedantic, you can prove this by induction. In terms of $n=2^k$, this reads $$ T(n) = (2+C)n-\log n - 2 \sim (2+C) n. $$ In particular, $T(n) = \Theta(n)$. You could also get this conclusion from case 1 of the master theorem.

Yuval Filmus
  • 280,205
  • 27
  • 317
  • 514
1

It's probably easier solving this using the Master Theorem.

T ( n ) = 2 T ( n / 2 )

log ⁡ n


enter image description here

So in this case A=2, B=2, and D=0 because f(n) = log n and n^0 * log n = log n

We of course assume the base case is a constant such that T(1) = C

So we can easily see that the answer for this is T ( n ) = Θ ( n ) , since A is greater than B to the power of D.

We can see that is true just by plugging in the values

= A > B^D

= 2 > 2^0

= 2 > 1 (which is always true)

This guy on YouTube has a very nice video explaining how to solve recurrences using the Master theorem, I will leave the link here.

tempmail
  • 21
  • 1