1

Possible Duplicate:
Solving or approximating recurrence relations for sequences of numbers

I know that the solution for $T(n) = 2 T(n/2) + O(n)$ is $ T(n) = O(n \log(n))$

But how do you get to that point? I don't understand when it says put t into the equation repeatedly until it drops out...

Any help?

  • @hd1 It may not be easy to do after the fact, but this knowledge does often help you make decisions before creating a first implementation.

    If you've learned your algorithmic theory well, it often doesn't take much extra time to deduce the complexities of different solutions (mostly a few seconds in your head). In a CPU-limited program, this can possibly save you from having to start over and find a whole new solution, after the fact.

    Of course, you also have to consider whether your data is big enough for the abstractions of the Big O notation to be valid.

    –  Nov 23 '12 at 09:26
  • read about master theorem http://en.wikipedia.org/wiki/Master_theorem it is the fastest to calculate these kind of complexity – Alexandros Mouzakidis Nov 23 '12 at 22:17
  • Since this question is completely answered by the one A.Schulz linked, I close this as a duplicate. If you face specific problems applying what you find there, please edit your question accordingly and flag for reopening. – Raphael Nov 23 '12 at 22:42

1 Answers1

4

It means "open" the recursion.

For simplicity - denote $O(n)$ as $c\cdot n$:

$$ \begin{align} T(n) &= 2T(n/2) + cn \\ &= 2(2T(n/4) + cn/2) + cn\\ &= 2 (2 (2T(n/8) + cn/4) + cn/2) + cn\\ & \vdots \end{align}$$

It might give you intuition, but it is NOT a proof. To prove it, you will need mathematical induction or the master theorem.


Proving with induction (assuming O(n) component is n for simplicity):

Claim: T(n) <= n*logn + n

Base:
T(1) = 1 (assumption)
Assumption: the claim is correct for all k < n.
Proof:

T(n) = 2T(n/2) + n = (assumption) <= 2* (n/2 * log(n/2) + n/2) + n
     = n*log(n/2) + 2n = n*(log(n)-log(2)) + 2n = (assuming base 2 for log)
     = n*(log(n) -1 ) + 2n = nlogn -n + 2n = n*logn +n

QED

A.Schulz
  • 12,167
  • 1
  • 40
  • 63
amit
  • 313
  • 1
  • 4
  • How would you prove with master theorem? –  Nov 23 '12 at 09:25
  • @user1846486 just apply the formula. –  Nov 23 '12 at 09:26
  • what is the master theorem formula? –  Nov 23 '12 at 09:28
  • 1
    just google it :D –  Nov 23 '12 at 09:28
  • OK, its something well known then? Thanks –  Nov 23 '12 at 09:29
  • I don't understand why people keep saying this is not a proof. If you do it *carefully,* it is a proof. On the other hand, it is very easy to slip up and make a mistake when you're doing this, especially if there are inequalities in the recursion formula. In this case, there can be some subtle behavior which will give you the wrong answer even when the formulas all look right. This is why you should use induction to prove it. – Peter Shor Nov 23 '12 at 13:54
  • 1
    Let me take it back—I think I just explained why instructors tell you this isn't a proof. It's because it's too hard to teach people how to do this properly in introductory algorithms courses. – Peter Shor Nov 23 '12 at 14:04
  • @PeterShor I'd say $\dots$ can be a proof if and only if every prospective reader will be able to fill them in withou much thought. That is to say, "almost never". (Although it's a different matter in research papers, probably.) – Raphael Nov 23 '12 at 22:43
  • @Raphael: with that criterion, hardly anybody has ever written down any proofs. That can be made into a perfectly good proof, and in my experience teaching intro discrete math classes, most students will understand it better than the induction proof. It's when you try to teach them how to do it for a more complicated recurrence that you understand why presenting the induction proof is better. – Peter Shor Nov 24 '12 at 14:40
  • @PeterShor It's not an either-or. You use the "dot-dot-technique" to make an educated guess, and verify your guess by induction (at least that's one way we teach). I would never have claimed that induction-only is a valid approach because it is not constructive in itself. But dots-only is no proof. Together, they shine. (About criterions for proofs: I am not that pessimistic, but yes: oftentimes what passes for a proof is more a structured idea. The "right" level lies somewhere between there and a proof that can be checked algorithmically, depending on the context.) – Raphael Nov 24 '12 at 15:17
  • @Raphael: that's the way we teach as well. But the ... technique can be made into a perfectly rigorous proof, especially in cases as easy as this one. – Peter Shor Nov 24 '12 at 15:27
  • @PeterShor: I am not sure I know what you mean by that. Care to elaborate over here? – Raphael Nov 24 '12 at 15:29