• edited by
47,089 views
73 73 votes

What is the $\text{time complexity}$ of the following recursive function?

int  DoSomething (int n) {
    if (n <= 2)
        return 1;
    else 
        return (DoSomething (floor (sqrt(n))) + n);
}
  1. $\Theta(n^2)$

  2. $\Theta(n \log_2n)$

  3. $\Theta(\log_2n)$

  4. $\Theta(\log_2\log_2n)$

9 Answers

Best answer
104 104 votes

We are asked the time complexity which will be the number of recursive calls in the function as in each call we perform a constant no. of operations and a recursive call. The recurrence relation for this is (considering constant time "$c$" as $1$)

$T(n) = T(\sqrt n) + 1$

$\qquad= T\left(n^{1/4}\right) + 2 $
$\\\qquad=T\left(n^{1/8}\right) + 3 $

Going like this we will eventually reach $T(3)$ or $T(2)$. For asymptotic case this doesn't matter and we can assume we reach $T(2)$ and in next step reach $T(1)$. So, all we want to know is how many steps it takes to reach $T(1)$ which will be $1+ $no. of steps to reach $T(2)$. 

From the recurrence relation we know that $T(2)$ happens when $n^{\left(\frac{1}{2^k}\right)} = 2$.

Taking $\log $ and equating,

$\frac{1}{2^k} \log n = 1$
$ \\\implies 2^k = \log n$
$ \\\implies k = \log \log n$. 

So, $T(1)$ happens in $\log \log n + 1$ calls, but for asymptotic complexity we can write as $\Theta \left( \log \log n\right)$


Alternatively,

Substituting values

$T(1) = 1$
$T(2) = 1$
$T(3) = T(1) + 1 = 2$
$\vdots$
$T(8) = T(2) + 1 = 2$
$T(9) = T(3) + 1 = 3$
$\vdots$

$T\left(\left({\left({2^2}\right)^2}\right)^2\right) = T\left(\left({2^2}\right)^2\right) + 1$
$ \\\quad= T(2^2)+ 2$
$ \\\quad= T(2) + 3 = 1 + 3 = 4, $
$\\ \log \log n = 3 \text{ as } n = 256$.


$T\left(\left({\left({\left({\left({2^2}\right)^2}\right)^2}\right)^2}\right)^2\right) = 6,$
$\\\quad \log \log n = 5 \text{ as } n = 65536 \times 65536 = 2^{32}$

$T\left(2^{(2^{10})}\right) = T\left(2^{512}\right) + 1 $
$\\\quad= T(2^{256}) + 2 $
$\\\quad= T(2^{128}) + 3$
$\\\quad = T(2^{64}) + 4 $
$\\\quad= T(2^{32}) + 5 $
$\\\quad= T(2^{16}) + 6 $
$\\\quad= T(2^8)+7 $
$\\\quad= T(2^4) + 8 $
$\quad\\= T(2^2) + 9$
$\quad = T(2) + 10 = 11,$
$\quad \log \log n = 10$

So, answer is D.

http://stackoverflow.com/questions/16472012/what-would-cause-an-algorithm-to-have-olog-log-n-complexity

• edited by
47 47 votes

Recursive relation for the DoSomething() is

  T(n) =  T(√n) + C1 if n > 2  

We have ignored the floor() part as it doesn’t matter here if it’s a floor or ceiling.

  Let n = 2^m,  T(n) = T(2^m)
  Let T(2^m) =  S(m)

  From the above two, T(n) = S(m)

  S(m) = S(m/2) + C1  /* This is simply binary search recursion*/
  S(m)  = O(logm)      
          = O(loglogn)  /* Since n = 2^m */
  
  Now, let us go back to the original recursive function T(n) 
  T(n)  = S(m) 
          = O(LogLogn)
30 30 votes

T(n)=T(√n) + c  

Let n= 2^m   hence m=log n
T(2^m) = T(√(2^m))+c
Let T(2^m) = S(m)

Now S(m)=S(m/2)+c
Apply master's theorem.
a=1, b=2, k=0,p=0
a=b^k, p>-1,  
Hence S(m)= theta(log m)
Hence it is theta(log log n)  (Since m=log n)

7 7 votes
The recurrence relation for this is following:

    $T(n)=T(n^{1/2})+n$ ......(1)

   put  $n=n^{1/2}$

  we get,

   $T(n^{1/2})=T(n^{1/4})+n^{1/2}$

  put this in (1), we get,

  $T(n)=T(n^{1/4})+n^{1/2}+n$

similarly,

  $T(n)=T(n^{1/2^{k}})+n^{1/2^{k-1}}+......+n$

This will stop when  $n^{1/2^{k}}=2$

Taking log of base 2 both side,we get

$1/2^{k} logn=log2

logn=2^{k}

log log n=k$

T(n)=O(log log n)
4 4 votes

 =(floor(sqrt(n))) + n

here n is constant so 

T(n)=T$\sqrt{n}$ + 1

T(n) = T(n1/2) +1 EQU-A

put n1/2 in place of n we will get

T(n1/2) = T(n1/4) +1  EQU-1

NOW PUT EQU-1 INTO EQU-A WE WILL GET FOLLOWING

T(n) = T(n1/4) +1 +1 BY DOING MORE THIS BACK SUBSTITUTION WE WILL GET GENERALIZED EQUA.

T(n) = T(n1/(2^i)) + $\sum_{1}^{i+1}1$ EQU-F

n1/(2^i) = 2 taking log both the sided we will get following

logn=2i taking again log both LHS AND RHS 

loglogn=i 

$\sum_{1}^{i+1}1$ = i+1

putting value of i into EQU-F WE WILL GET TIME COMPLEXITY = O(loglogn)

2 2 votes
The program is not going to end when started with n > 2. A suitable base condition is required for the program to stop.

Time complexity for this program should be infinity.
Answer:
Position:
Show:

Related questions

88 88 votes
16 answers 16 answers
57.4k
57.4k views
Arjun asked Jul 6, 2016
57,367 views
Consider the following segment of C-code:int j, n; j = 1; while (j <= n) j = j * 2;The number of comparisons made in the execution of the loop for any $n 0$ is:$\lceil \...
55 55 votes
6 answers 6 answers
23.8k
23.8k views
Kathleen asked Sep 21, 2014
23,805 views
Consider the following C program segment:int IsPrime (n) { int i, n; for (i=2; i<=sqrt(n);i++) if(n%i == 0) {printf("Not Prime \n"); return 0;} return 1; }Let $T(n)$ deno...
86 86 votes
9 answers 9 answers
39.2k
39.2k views
Kathleen asked Sep 21, 2014
39,218 views
In the following C function, let $n \geq m$.int gcd(n,m) { if (n%m == 0) return m; n = n%m; return gcd(m,n); }How many recursive calls are made by this function?$\Theta(\...
93 93 votes
11 answers 11 answers
45.8k
45.8k views
Kathleen asked Sep 21, 2014
45,772 views
An array of $n$ numbers is given, where $n$ is an even number. The maximum as well as the minimum of these $n$ numbers needs to be determined. Which of the following is T...