Solving a recurrence T(n) = 2T(n/2) + n^4
Asked Answered
P

4

5

I am studying using the MIT Courseware and the CLRS book Introduction to Algorithms.

I am currently trying to solve the recurrence (from page 107)

T(n) = 2T(n/2) + n4

If I make a recurrence tree, I get:

Level 0: n4

Level 1 2(n/2)4

Level 2 4(n/4)4

Level 3 8(n/8)4

The tree has lg(n) levels. Therefore I think that the recurrence should be

T(n) = Θ(n4 lg n)

But, If I use the master theorem, I get that

T(n) = Θ(n4)

Clearly both of these can't be right. Which one is correct? And where did I go wrong with my reasoning?

Plimsoll answered 5/1, 2011 at 4:50 Comment(0)
M
6

The second one looks correct. Notice that your recurrence tree looks like

n4 + 2(n/2)4 + 4(n/4)4 + ... + 2i (n / 2i)4

But 2(n/2)4 ≠ n4, because (n/2)4 = n4 / 16, and so 2(n/2)4 = n4/8. In fact, if you work out the math, you get that the work being done at level i is given by

n4 / (2-3i)

So we get (1 + 1/8 + 1/64 + 1/512 + ... ) n4, which can be shown to be less than 2n4. So your function is Θ(n4).

Monaxial answered 5/1, 2011 at 4:57 Comment(1)
ah yes, I see my mistake. Nice explanation on why it is less than 2n^4. ThanksPlimsoll
A
2

With recursion it is Θ(n^4)

T(n) = 2*T(n/2) + n^4 
T(n) = 2( 2*T(n/4) + (n/2)^4) + n^4 = 4*T(n/4) + 2*(n/2)^4 + n^4
T(n) = 4(2*T(n/8) + (n/4)^4) + 2*(n/2)^4 + n^4 = 8*T(n/8) + 4*(n/4)^4 + 2(n/2)^4 + n^4

T(n) = 8*T(n/8) + n^4*(1 + 1/(2^3) + 1/(2^6))
...

T(n) = 2^k*T(n/(2^k)) + n^4*(1+ 1/(2^3) + 1/(2^6) + 1/(2^9)...+ 1/((2^(k-1))^3)

We know T(1) = 1

n = 2^k so k = log2(n) Then

T(n) = n*T(1) + n^4*( 1 - (1/(2^3))^k)/(1-1/8)

T(n) = n + (8/7)*n^4*(1 - n^(-3))

T(n) = n + (8/7)*(n^4 - n)

T(n) = (8/7)*n^4 - (1/7)*n


Θ(T(n)) = Θ((8/7)*n^4 - (1/7)*n)
Θ(T(n)) = Θ(n^4)

it is Θ(n^4)

Antiserum answered 5/1, 2011 at 5:38 Comment(0)
T
1

You can use the master theorem here directly.

This equation fits in case 1 of master theorem where log (a) base b < f( n)

a : Number of recurrence b : Number of subparts

log a base b = log 2 base 2 = 1 < n^4

Therefore by masters theorem, T(n) = theta(f(n)) = theta(n^4)

Teaching answered 7/10, 2015 at 2:0 Comment(0)
J
0

By Master Theorem

a=2, b=2, f(n)=n^4

First step - Calculate n^(log a to base b) => n(log 2 to base 2) = n*1 = n Second Step - Is f(n)> First step result => n^4> n => YES This means use Case 3 of Master Theorem.

Third step - Check Regularity Condition

a. f(n/b) <= c. f(n) where c>1 
a(n/b) . log(n/b) <= c. f(n)
2.(n/2) . log(n/2) <= c. n^4
n.log(n/2) <= c.n^4

Yes, Regularity condition is met, so our solution must be.

T(n) =theta (f(n)) = theta(n^4)
Jarvis answered 31/1, 2020 at 2:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.