• edited by
25,623 views
59 59 votes

The running time of the following algorithm

Procedure $A(n)$

If $n \leqslant 2$ return ($1$) else return $(A( \lceil  \sqrt{n}  \rceil))$;

is best described by

  1. $O(n)$
  2. $O(\log n)$
  3. $O(\log \log n)$
  4. $O(1)$

7 Answers

Best answer
79 79 votes

The complexity will be the number of times the recursion happens which is equal to the number of times we can take square root of n recursively, till n becomes $2$.

$T(n) = T\left(\lceil \sqrt n \rceil \right) + 1$

$T(2) = 1$
$T\left(2^2\right) =  T(2) + 1 = 2$
$T\left(2^{2^2}\right) =  T(4) + 1 = 3$
$T\left(2^{2^3}\right) =  T(16) + 1 = 4$

So, $T(n) = \lg\lg n + 1 = O\left(\log \log n\right)$

Answer : Option C

• edited by
45 45 votes
T(n)=T(sqrt(n))+1

We substitute n=2^m. Then T(2^m)=T(2^m/2) +1

we substitute T(2^m)=S(m).

S(m)=S(m/2)+1.

Now applying masters theoram we get S(m)=O(log m). As m=logn.

T(n)=O(log log n)
23 23 votes
Another way to solve this is:

T(n) = T(n^1/2)+c

T(n) = T(n^1/4)+c+c

T(n) = T(n^1/8)+c+c+c

T(n) = T(n^1/16)+c+c+c+c

................

.........

...........

T(n) = T(n^(1/2^k))+k.c

So, n^(1/2^k) = 2

taking log will give us:-      log(n) = 2^k--------------futher taking log --------- k =loglogn

T(n) = T(2)+loglogn . c

so , ans will be C) O(loglogn)
18 18 votes

$Ans:C$


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

Assume $n=2^k$

$T(2^k)=T(2^{k/2})+c$

Assume $T(2^k)=S(k)$

$S(k)=S(k/2)+c$

Apply Master Theorem

$S(k)=\Theta(1.\log\ k)$

Now just do the reverse

$T(2^k)=\Theta(\log\ k)$

Now replace with $k=\log_{2}n$

$T(2^{\log_{2}n})=\Theta(\log(\log_{2}n))$

$T(n)=\Theta(\log(\log_{2}n))$

• edited by
Answer:
Position:
Show:

Related questions

46 46 votes
5 answers 5 answers
20.9k
20.9k views
Kathleen asked Sep 15, 2014
20,883 views
The solution to the recurrence equation $T(2^k) = 3T(2^{k-1})+1, T(1) =1$ is$2^k$$\frac{(3^{k+1}-1)}{2}$$3^{\log_2 k}$$2^{\log_3 k}$
31 31 votes
3 answers 3 answers
10.1k
10.1k views
Kathleen asked Sep 15, 2014
10,149 views
Fill in the blanks in the following template of an algorithm to compute all pairs shortest path lengths in a directed graph $G$ with $n*n$ adjacency matrix $A$. $A[i,j]$ ...
95 95 votes
17 answers 17 answers
35.6k
35.6k views
Kathleen asked Sep 15, 2014
35,557 views
Consider the following algorithm for searching for a given number $x$ in an unsorted array $A[1..n]$ having $n$ distinct values:Choose an $i$ at random from $1..n$If $A[i...
59 59 votes
8 answers 8 answers
24.8k
24.8k views
Kathleen asked Sep 15, 2014
24,777 views
A $B^+$ - tree index is to be built on the Name attribute of the relation STUDENT. Assume that all the student names are of length $8$ bytes, disk blocks are of size $512...