views:

550

answers:

3

Consider this example :

T(n) = T(7n/8) + 2n

I assumed T(1) = 0

and tried to solve it in the following way

T(n) = T(7n/8) + 2n
     = T(49n/64) + 2.(7n/8) + 2n
     = T(343n/512) + 2.(7n/8).(7n/8)+ 2.(7n/8) + 2n 
     = T(1) + 2n ( (7n/8)^i + ..... + 1)               

but I could not come to any conclusion about this. I am confused about what should I do in the next step.

A: 

T(n) = T(7n/8) + 2n = 2n * (1 + 7/8 + (7/8)^2 + ... (7/8)^Z) + T(1) where Z = ?

The only trick is finding Z. I bet a log will help. Sorry it is late, and I am not thinking straight, but ... you should not need to add multiple 2n.

Edit: Z is how many time you need to multiply n by 7/8 until you get 1.

So, n * 7^Z / 8^Z = 1

(7/8)^Z = 1/n

(8/7)^Z = n

You want to solve for Z.

Hamish Grubijan
I tried doing this (7/8)^Z = 1/n and took log as you said whose base is 7/8 which gave meZ = log base 7/8 (1/n) and substituted this in the geometric series summation formula as T(n) = 2n * 8(1-(1/n)) which gives O(n^2) ... not sure whether it is correct.
Tasbeer
Logartihms are not necessary to solve this problem; I can't think of an approach where they naturally come up.
Jason
As the other poster said, ust let it run to infinity ...
Hamish Grubijan
A: 

What you got there in the last line is a geometric series and there is a formula to simplify such a sum.

sth
+4  A: 

Your approach is sound, but you'll see what to do if you rewrite it slightly differently:

T(n) = T((7/8)^1 * n) + 2 * (7/8)^0 * n
     = T((7/8)^2 * n) + 2 * (7/8)^1 * n + 2 * (7/8)^0 * n
     = T((7/8)^3 * n) + 2 * (7/8)^2 * n + 2 * (7/8)^1 * n + 2 * (7/8)^0 * n
     .
     .
     .
     = T((7/8)^k * n) + 2 * n * sum j = 0 to k-1 (7/8)^j

Now, let k tend to infinity and see what happens. It would help if you're familiar with geometric series.

Jason
If I take k -> infinity then on the right part I will get 8 as S(inf) = a/1-r = 1/(1-7/8) = 8 which results in an order of O(n) . Again I am not sure whether it is correct :(
Tasbeer
That's exactly right! The sum is 8. Now what happens to `(7/8)^k` as `k` tends to infinity?
Jason
mmm that would be infinity
Tasbeer
What does it look like if you compute `(7/8)^0`, `(7/8)^1`, `(7/8)^2`, `(7/8)^3`, ... using a calculator? Compute as many terms as you need until you understand what it is happening.
Jason
oh right it approaches towards 0 ... so we can assume that when k approaches infinity T((7/8)^k*n) = T(0) = 0 ??
Tasbeer
That's right! It approaches zero. So now you have told me that `T((7/8)^k * n)` approaches `T(0)` (and no, you can not assume that `T(0) = 0`) and you have told me that the sum is 8. So can you put this all together and deduce what `T(n)` is?
Jason
order of O(n) ?
Tasbeer
Right now don't worry about asymptotic analysis; can you put the above together to find a closed-form expression of `T(n)`?
Jason
T(n) = T(0) + 16 n -> is it correct ?
Tasbeer
Yes! And this is clearly `O(n)` as you stated before. Very nice work.
Jason
Thanks a lot :D !
Tasbeer