You are on page 1of 3

# Design and Analysis of Algorithm

ASSIGNMENT-1

1. Rank the following Functions

7n
3
+3n
4n
2

n
12log(n)/log(n)
1/n
2
+18n
5

n
8
621909
e
n log log

2
n 3

6nlogn
n!

2. f(n)=7n+8 and g(n)=n. Is f(n)O(g(n)) ?

3. Consider method cal below, the call cal(5) evaluates to 15.
public int cal(int n){
if (n == 0) return 0;
return n + cal(n-1);
}
Write the recurrence relation
Using big-Oh what is the running time of the call cal(n)? Justify your answer.
Using big-Oh what is the value returned by the call cal(n) (note: complexity of
value returned, not running time: use big-Oh)
Using big-Oh what is the running time of the call cal(cal(n)) based on your
answers to the previous Two questions. Justify.
Using big-Oh what is the value returned by the call cal(cal(n)) (again, based on
previous answers, justify).

4. What is the value returned by the call ada(802) where the method ada is below.
Justify your answer.

public static int ada(int n){
int sum = 0;
for(int k=1; k <= n; k = k * 2) {
sum += k;
}
return sum;
}
What is the runtime complexity of the call ada(N)? Justify your answer.
What is the runtime complexity of the call ada(N*N*N*N)? Justify your answer.

5. Write an algorithm to count the number of common elements in two one
dimensional arrays A{1…m} and B{1..n}. What is the number of comparisions
made by your algorithm.

6. Calculate Frequency count for time and space for the following
a) ifstream filein;
filein.open("datafile.dat", ios::in);
int a[max];
int j, k=0,h;
filein >> j;
while (filein) {
a[k] = j;
k++;
filein >> j;
}
filein.close( );

b) int b,c,d;
for (int x=0; x<5; x++)
for (int y=0; y<4; y++) {
b = x * y;
cout << b;
}

7. Solve the recurrence
a) T (n) =2T (
 
n ) + logn

8. Fibonacci series is defined as follows:
f(0) = 0
f(1) = 1
f(n) = f(n-1) + f(n-2)

Find an iterative algorithm and a recursive one for computing element number n
in Fibonacci series, Fibonacci(n).
Analyze the running-time of each algorithm.

9. A list x
1
, x
2
, x
3
, …, x
n
is said to be cyclic sorted if the smallest number in the list
is x
i
for some unknown i, and the list x
i
, x
1  i
,x
n
,x
1
,x
2
,….x
1  i
is sorted. As an
example 4, 5,1,2,3 is a cyclic sorted. Given a cyclic sorted list of n numbers,
design and analyze an algorithm for finding a particular numbers in the list.

10. Explain and verify the analysis of Quick sort in worst and best case.

11. Fibonacci series is defined as follows:
f(0) = 0
f(1) = 1
f(n) = f(n-1) + f(n-2)

Find an iterative algorithm and a recursive one for computing element number n
in Fibonacci series, Fibonacci(n).
Analyze the running-time of each algorithm.

12. Explain significance of Huffman codes. Also explain Prefix codes. Draw code
tree according to Huffman for the following file with symbol's frequencies are as
follows: (A 24), (B 12), (C 10), (D 8), (E 8) .Write code, code length, and total length
for the tree you drawn.

13. Give the proof of Master theorem

14. Solve