Analysis of Algorithms (SET: 2) : Q.1 What Is Time Complexity of Fun ?
Analysis of Algorithms (SET: 2) : Q.1 What Is Time Complexity of Fun ?
(SET: 2)
int fun(int n)
{
int count=0;
for (int i= n; i> 0; i/=2)
for(int j=0; j< i; j++)
count+= 1;
return count;
}
int fun(int n)
{
int count=0;
for(int i=0; i<n; i++)
for(int j=I; j>0; j--)
count= count+1;
return count;
}
Q.3 The recurrence relation capturing the optimal time of the Tower of Hanoi problem with n
discs is—
1
Q.4 Let w(n) and A(n) denote respectively, the worst case and average case running time of an
algorithm executed on an input of size n. which of the following is ALWAYS TRUE?
(GATE CS 2012)
Q.6 Which of the given options provides the increasing order of asymptotic complexity of
functions f1, f2, f3 and f4?
f1(n) = 2^n
f2(n) = n^(3/2)
f3(n) = nLogn
f4(n) = n^(Logn)
Q.7 Consider the following program fragment for reversing the digits in a given integer to obtain
a new integer. Let n = D1D2…Dm
int n, rev;
rev = 0;
while (n > 0)
{
rev = rev*10 + n%10;
n = n/10;
}
The loop invariant condition at the end of the ith iteration is: (GATE CS 2004)
(a) n = D1D2….Dm-i and rev = DmDm-1…Dm-i+1
(b) n = Dm-i+1…Dm-1Dm and rev = Dm-1….D2D1
(c) n!= rev
(d) n = D1D2….Dm and rev = DmDm-1…D2D1
2
Q.8 What is the time complexity of the below function?
Q.9 In a competition, four different functions are observed. All the functions use a single for
loop and within the for loop, same set of statements are executed. Consider the following for
loops:
If n is the size of input (positive), which function is most efficient (if the task to be performed is
not an issue)?
(a) A (b) B
(c) C (d) D
Q.10 What does it mean when we say that an algorithm X is asymptotically more efficient than
Y?
Q.11 What is the time complexity of Floyd–Warshall algorithm to calculate all pair shortest path
in a graph with n vertices?
3
Q.12 Consider the following functions:
f(n) = 2^n
g(n) = n!
h(n) = n^logn
Which of the following statements about the asymptotic behavior of f(n), g(n), and h(n) is true?
(a) f(n) = O(g(n)); g(n) = O(h(n)) (b) f(n) = Ω (g(n)); g(n) = O(h(n))
(c) g(n) = O(f(n)); h(n) = O(f(n)) (d) h(n) = O(f(n)); g(n) = Ω (f(n))
Q.13 The minimum number of comparisons required to find the minimum and the maximum of
100 numbers is _________________.
int gcd(n,m)
{
if (n%m ==0) return m;
n = n%m;
return gcd(m, n);
}
f(n) = 3n√x
g(n) = 2√x log2 n
h(n) = n!
4
Q.16 Consider the following three claims:
Q.18 Consider the following two functions. What are time complexities of the functions?
int fun1(int n)
{
if (n <= 1) return n;
return 2*fun1(n-1);
}
int fun2(int n)
{
if (n <= 1) return n;
return fun2(n-1) + fun2(n-1);
}
(a) O(2^n) for both fun1() and fun2() (b) O(n) for fun1() and O(2^n) for fun2()
(c) O(2^n) for fun1() and O(n) for fun2() (d) O(n) for both fun1() and fun2()
5
Q.19 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:
Base of Log is 2 in all options.
(a) CEIL(logn) + 1 (b) n
(c) CEIL(logn) (d) FLOOR(logn) + 1
Q.20 Consider the following C-program fragment in which i, j and n are integer variables.
for (i = n, j = 0; i >0; i /= 2, j += i);
Let val(j) denote the value stored in the variable j after termination of the for loop. Which one of
the following is true?
(a) val(j) = theta(log n) (b) val(j) = theta(sqrt(n))
(c) val(j) = theta(n) (d) val(j) = theta(nlog n)