• edited by
36,184 views
67 67 votes

Consider functions $\textsf{Function_1}$ and $\textsf{Function_2}$ expressed in pseudocode as follows:

Function_1
    while n>1 do
        for i=1 to n do
            x = x + 1;
        end for
        n = ⌊n/2⌋;
    end while
Function_2
   for i = 1 to 100 * n do
       x = x + 1;
   end for

 

Let $f_{1}(n)$ and $f_{2}(n)$ denote the number of times the statement $\textsf{“x = x + 1”}$ is executed in $\textsf{Function_1}$ and $\textsf{Function_2},$ respectively.

Which of the following statements is/are $\text{TRUE}?$

  1. $f_{1}(n) \in \Theta\left(f_{2}(n)\right)$
  2. $f_{1}(n) \in o\left(f_{2}(n)\right)$
  3. $f_{1}(n) \in \omega\left(f_{2}(n)\right)$
  4. $f_{1}(n) \in O(n)$

6 Answers

7 7 votes

Lets analyze Function_1

  1. It has a for loop inside the while which runs for $n$ times
  2. The $n$ is halved every iteration of the while loop

We would sum up the the number of times the inner loop (as its the only block which isn’t const time)

$S(n) = n + \frac{n}{2} + \frac{n}{4} + \frac{n}{8} \cdots + 1$

(We can assume n was a power of 2 WLOG)

To get the number of terms $p$
$1 = n (\frac{1}{2})^{p}$

$p = log_2(n)$
$\frac{1}{n} = (\frac{1}{2})^p$

 

Now if we sum it up.

$S(n) = n \cdot \frac{1-\frac{1}{2}^p}{1-\frac{1}{2}}$
$S(n) = 2n-2$
 

 

Now for function_2

Its a simple for loop which runs for $100n$

Both of the functions belong to $\theta(n)$

Hence A and D are correct

D because we also consider equality in big-O

7 7 votes

 

 

0 0 votes

Function_1 :- The for loop in function_1 will run 'n' times, but the outer while loop will change the value of 'n' to half each time.
Therefore, f1(n) = n + (n/2) + (n/4) + (n/8) + ...  = n/(1-0.5) = 2n

Function_2 :- The for loop in function_2 will run '100n' times. Therefore, f2(n) = 100n

Theta(f2(n)) represents the set of all those functions who is asymptotically equal to f2(n). 
Similarly, small-oh(f2(n)) represents the set of all those functions who is asymptotically smaller than f2(n). And small-omega(f2(n)) represents the set of all those functions who is asymptotically greater than f2(n).

example :- small-oh(n) represents the set of all those functions who is asymptotically smaller than equal to n. 
small-oh(n) = {n, 2n, 3n, log(n), 100, ...} (all these functions are asymptotically smaller than equal to n)

Ans is A,D.

1 flag:
✌ Edit necessary (Hari_raam “small oh means strictly lesser than”)
Answer:
Position:
Show:

Related questions

39 39 votes
5 5 answers
27.4k
27.4k views
admin asked Feb 15, 2023
27,389 views
Let $f$ and $g$ be functions of natural numbers given by $f(n)=n$ and $g(n)=n^{2}.$ Which of the following statements is/are $\text{TRUE}?$$f \in O(g)$$f \in \Omega(g)$$f...
58 58 votes
9 9 answers
17.6k
17.6k views
admin asked Feb 15, 2023
17,567 views
Let $f: A \rightarrow B$ be an onto (or surjective) function, where $A$ and $B$ are nonempty sets. Define an equivalence relation $\sim$ on the set $A$ as\[a_{1} \sim a_{...
36 36 votes
10 10 answers
28.5k
28.5k views
admin asked Feb 15, 2023
28,505 views
Suppose you are asked to design a new reliable byte-stream transport protocol like $\text{TCP}.$ This protocol, named $\textsf{myTCP}$, runs over a $100 \mathrm{~Mbps}$ n...
43 43 votes
6 answers 6 answers
17.1k
17.1k views
admin asked Feb 15, 2023
17,108 views
Let $X$ be a set and $2^{X}$ denote the powerset of $X$.Define a binary operation $\Delta$ on $2^{X}$ as follows:\[A \Delta B=(A-B) \cup(B-A) \text {. }\]Let $H=\left(2^{...