views:

115

answers:

2

how do i solve for running time in the towers of Hanoi problem. I get a recurrence realation like t(n) = 2t(n-1) + 1. After drawing the recursion tree i get at every step values like 1+2+4+8... the height of the tree will be lg(n). how do i calculate the sum of the series? when do i stop?

+2  A: 

Did you checked http://en.wikipedia.org/wiki/Tower_of_Hanoi? You have everything in there.

Parkyprg
really? you think I don't know how to use the internet
@user265260: You never specified. Don't get snarky at answers given because your question wasn't well-stated.
Daenyth
@user265260: apparently you hadn't looked at the wikipedia article, because the answer to your question is in there.
LarsH
+6  A: 

What you get at each level of the recursion tree is a power of 2. Hence, the sum is: 2^0 + 2^1 + 2^2 + ... + 2^{n-1}.

That's a geometric sum: http://en.wikipedia.org/wiki/Geometric_progression

Let S(n) = 1 + 2 + 4 + ... + 2^{n-1}. Then: S(n) - 2*S(n) = 1 - 2^n

And finally: S(n) = 2^n - 1.

Sheldon L. Cooper
shouldn't it be till 2^n. I could easily be wrong.
You can find that out by trying with small cases. For `n = 1` you get `2^0`. For `n = 2`, `2^0 + 2^1`. So the highest exponent is `n-1`, not `n`.
Sheldon L. Cooper
@user265260: Mr. Cooper has the correct sum. Think of it this way: what is the largest value an unsigned integer with n bits can represent? That is different than the number of values that n bits can represent since 0 is one of those values.
andand
i think i am wrong in stating that height is `lg(n)`, it should be `n`. Could someone tell me which is right