0

I have this algorithm:

S(n)
  if n=1 then return(0)
  else
    S(n/3)
    x <- 0
    while x<= 3n^3 do
       x <- x+3
    S(n/3)

Is 2 * T(n/3) + n^3 the recurrence relation?

Is T(n) = O(n^3) the execution time?

0

2 Answers 2

2

The recurrence expression is correct. The time complexity of the algorithm is O(n^3).

The recurrence stops at T(1).

Running an example for n = 27 helps deriving a general expression:

T(n) = 2*T(n/3)+n^3 =
= 2*(2*T(n/9)+(n/3)^3)+n^3 =
= 2*(2*(2*T(n/27)+(n/9)^3)+(n/3)^3)+n^3 =
= ... =
= 2*(2*2*T(n/27)+2*(n/9)^3+(n/3)^3)+n^3 =
= 2*2*2*T(n/27)+2*2*(n/9)^3+2*(n/3)^3+n^3

From this example we can see that the general expression is given by:

enter image description here

Which is equivalent to:

enter image description here

Which, in turn, can be solved to the following closed form:

enter image description here

The dominating term in this expression is (1/25)*27n^3 (2^(log_3(n)) is O(n), you can think of it as 2^(log(n)*(1/log(3))); dropping the constant 1/log(3) gives 2^log(n) = n), thus, the recurrence is O(n^3).

Sign up to request clarification or add additional context in comments.

Comments

1
2 * T(n/3) + n^3

Yes, I think this is a correct recurrence relation.

Time complexity:

while x<= 3n^3 do
       x <- x+3

This has a Time complexity of O(n^3). Also, at each step, the function calls itself twice with 1/3rd n. So the series shall be

n, n/3, n/9, ...

The total complexity after adding each depth

n^3 + 2/27 * (n^3) + 4/243 * (n^3)...

This series is bounded by k*n^3 where k is a constant.

Proof: if it is considered as a GP with a factor of 1/2, then the sum becomes 2*n^3. Now we can see that at each step, the factor is continuously decreasing and is less than half. Hence the upper bound is less than 2*n^3.

So in my opinion the complexity = O(n^3).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.