DAA Assignment 2
DAA Assignment 2
int a = 0, i = N;
while (i > 0) {
a += i;
i +=3;
Q2. For the functions, nk and cn,, what is the asymptotic relationship between these
functions?
1. nk is O( cn)
2. Cn is O(nk)
Q3. Write down the output, purpose and perform asymptotic analysis of the following.
int i, j, k = 0;
for (j = 2; j <= n; j = j * 2) {
k = k + n / 2;
}
Algorithm ABC(a,b, temp)
if(temp % b == 0 && temp % a == 0)
return temp;
temp++;
ABC(a,b, temp);
return temp;
Q4. What does it mean when we say that an algorithm X is asymptotically more efficient than Y?
Q5. Perform asymptotic analysis of each of the following loops. The standard function min returns the
minimum of the two arguments.
A. for ( int i = 0; i < n; ++i ) { for ( int j = 0; j < n/2; ++j ) { ++sum; }}
B. for ( int i = 0; i < n; ++i ) { for ( int j = 0; j < std::min( 10, i ); ++j ) { ++sum; }}
∑2k
k =1
Q7. For each function f(n) below, give an asymptotic upper bound using “big-Oh” notation. You should
give the tightest bound possible.
🞑 Against each sort, I want three graphs. One for worst case, one for best case, and one for average
case.
🞑 On the x-axis should be the increasing input size (n), on the y-axis should be the running time.
🞑 To avoid the system dependencies, against each value of n, repeat 10 experiments and use the
average value.
Q9. Solve the following recursive functions using Substitution, Tree and master Method.
• T (n) = 3T (n/3)+ pn
• T (n) = 4T (n/2)+ cn
• T (n) = 7T (n/3)+ n2
• T(n)=2T(n-1)+O(n)
Q10. See the Algorithms of Merge sort and Quick sort in the Book of “Introduction to Algorithms” form
there recursive function and solve them to find total time complexity.