2

It is a Textbook Assignment question in my Sophomore

Tried solving it for 3 days but having a hard time.

The question is:

Find the Time and Space complexity , along with the Value Returned and No of function calls

rec(n)
{
    if( n <= 2 )
        return 1;

    return 2*rec(sqrt(n)) + 2*rec(sqrt(n)) + log(n);
}

I got TC as Θ(Logn), value returned as Θ(log^2(n)), number of function calls as Θ(LogLogn) and space complexity as Θ(LogLogn)

can anyone please help.

what is wrong and what's the right way if I am wrong!

Original problem statement @ https://ibb.co/Z8SvHcf

Fe2O3
  • 6,077
  • 2
  • 4
  • 20

1 Answers1

2

The recurrence relation describing the problem is:

 T(n) = 2T(√n) + logn

Now we define S(n) = T(e^n) And we get:

S(n) = T(e^n) = 2T(√(e^n)) + log(e^n) = 2T(e^(n/2)) + n = 2S(n/2) + n

After applying master theorem we get:

S(n) = Θ(nlogn)

For n > 0 we have

T(n) = S(logn) = Θ(lognloglogn)

You will find a more in-depth discussion here: https://cs.stackexchange.com/questions/96422/how-to-solve-tn-2t%E2%88%9Anlog-n-with-the-master-theorem?newreg=0deef64b56714962a0cd046f7f0ca745 Hope this helps.

Chandler Bong
  • 461
  • 1
  • 3
  • 18
  • Thanks alot :-) can you also please help me about knowing the space complexity , yes i got the time complexity , you explained well . But the Remaining 3 the space complexity , return value and the number of function calls – Isabella Amdahl Sep 11 '22 at 11:16
  • 1
    I guess (I AM NOT SURE) that the space complexity will be O(loglog(n)). Let's suppose that n = 4, then we have 1 function call hence the stack will contain 1 frame which is equal to loglog(4). When n = 16 we have 2 stack frames which is also equal to loglog(16) and the pattern continues.. someone right me if i am wrong..... – Chandler Bong Sep 11 '22 at 12:19
  • regrading the return value, iam not quite sure... – Chandler Bong Sep 11 '22 at 12:33