You are on page 1of 6

Each student will solve each and every question of these assignments.

There are no choices for this subject.


TCS 610 Design and Analysis of Algorithms

Q. What is the complexity of the following piece of


code:-

1. What is the time, space complexity of following


code: 6.

int a = 0, b = 0;
for (i = 0; i < N; i++) { void recursion(int n)
a = a + rand(); {
} if(n == 1) return;
for (j = 0; j < M; j++) { recursion(n-1);
b = b + rand(); print(n);
} recursion(n-1);
}
1. O(N * M) time, O(1) space
2. O(N + M) time, O(N + M) space
3. O(N + M) time, O(1) space 7.

4. O(N * M) time, O(N + M) space


int recursion(int what[], int thisone, int
2.
thatone, int x)
{
int sum = 0, i;
if (thatone >= thisone)
{
for(i=0;i<n;i = i+2)
int something = thisone + (thatone -
{
thisone)/2;
sum += i;
if (what[something] == x)
}
return something;
else if (what[something] > x)
return recursion(what, thisone,
something-1, x);
return recursion(what, something+1,
3
thatone, x);
}
return -1;
int sum = 0, i; }
for(i=0;i<n; i = i*2){
sum += i;
}
// even if it is i*10 or i*100
answer is same asymptotically
8. Solve the following recurrence relation:- T(1) =
1
1. T(n) = T(n-1) + 1
4.
2. T(n) = T(n-1) + n
3. T(n) = T(n/2) + 1
4. T(n) = 2T(n/2) + 1
int sum = 0, i; 5. T(n) = 2T(n-1) + 1
for(i=0;i*i<n;i++) 6. T(n) = 3T(n-1), T(0) = 1
{
7. T(n) = T( n )+1

sum += i;
} 8. T(n) = T( n )+n

5.
int sum = 0, i;
for(i=0;i<n;i++)
int j = 1, i = 0; {
sum += i;
while(i<=n){ }
i = i+j;
j++;
}
1
15. Sort the following functions in the decreasing
10. What is the time complexity of following code:
order of their asymptotic (big-O) complexity:

int a = 0; f1(n) = n^√n , f2(n) = 2^n, f3(n) = (1.000001)^n ,


for (i = 0; i < N; i++) { f4(n) = n^(10)*2^(n/2)

for (j = N; j > i; j--) { (a) f2> f4> f1> f3

a = a + i + j; (b) f2> f4> f3> f1

} (c) f1> f2> f3> f4

} (d) f2> f1> f4> f3

Options:

1. O(N)
16. f(n) = 2^(2n)

2. O(N*log(N))
Which of the following correctly represents the
3. O(N * Sqrt(N))
above function?

4. O(N*N)
(a) O(2^n)

(b) Ω(2^n)

11. What is the time complexity of following code:


(c) Θ(2^n)

int i, j, k = 0; (d) None of these

for (i = n / 2; i <= n; i++) {


for (j = 2; j <= n; j = j * 2) 17. T(n) = 2T(n/2) + n^2. T(n) will be

{ (a) O(n^2)

k = k + n / 2; (b) O(n^(3/2))

} (c) O(n log n)

} (d) None of these

Options:

18.

1. O(n)

2. O(nLogn)

3. O(n^2)
int gcd(int n, int m){
4. O(n^2Logn)

if (n%m ==0) return m;


12. What does it mean when we say that an if (n < m) swap(n, m);
algorithm X is asymptotically more efficient than Y?

Options:
while (m > 0){
1. X will always be a better choice for small n = n%m;
inputs

2. X will always be a better choice for large swap(n, m);


inputs
}
3. Y will always be a better choice for small return n;
inputs
}
4. X will always be a better choice for all
inputs

13. What is the time complexity of following code:


19.
int a = 0, i = N;
while (i > 0) {
a += i; int a = 0, b = 0;
i /= 2; for (i = 0; i < N; i++){
}
Options:
for (j = 0; j < N; j++){
1. O(N)
a = a + j;}
2. O(Sqrt(N))

3. O(N / 2)
}
4. O(log N)
for (k = 0; k < N; k++){

14. Solve the following recurrence relation?


b = b + k;}
T(n) = 7T(n/2) + 3n^2 + 2

(a) O(n^2.8)

(b) O(n^3)

(c) θ(n^2.8)

(d) θ(n^3)

2
Assignment-2

Q1. For this problem, assume that you are using Insertion Sort. Assume that
the fi rst, second, and third smallest elements are next to each other
(somewhere in the input), the fourth, fi fth, and sixth smallest elements are
next to each other, the 7th, 8th, and 9th elements are next to each other, etc.
For example, 80, 70, 90, 40, 50, 60, 30, 10, 20, 120, 110, 100
(The algorithm does not know this and executes without this extra
information.) Assume the
problem size n is a multiple of 3. For each part show your work.
(a) Assume each of the above triples are in reverse order (so that the third
smallest element
comes before the second smallest which comes before the smallest, the sixth
smallest comes
before the fifth smallest which comes before the fourth smallest, etc.). For
example,
90, 80, 70, 60, 50, 40, 30, 20, 10, 120, 110, 100
What is the exact number of comparisons in the best case?
(b) Assume each of the above triples are in order (so that the smallest
element comes before
the second smallest which comes before the third smallest, the fourth
smallest comes
before the fifth smallest which comes before the sixth smallest, etc.). For
example,
70, 80, 90, 40, 50, 60, 10, 20, 30, 100, 110, 120
What is the exact number of comparisons in the worst case?
(c) Assume each of the above triples are in reverse order (as in part (a)), and
n = 6. Calculate
the exact number of comparisons in the average case?

Q2. Slow sort:


i =1
while(i < n ){
if (a[i]>a[i+1]){
Swap (a[i], a[i+1])
i =1
}
else{
i =i+1
}
(a )Compute time complexity of above code(slow sort).

(b). what is the best case


(c). what is worst case.
Assume you start with a sorted list, pick two distinct elements at random, and
inter-
change them. That is your input for Slow sort.
(d) What is the best case?
(e) What is the worst case?

Q.2. Complete the following table:-

Algorithm Best Case Average Case Worst Case

Time Space Time Space Time Space

Bubble Sort

Selection Sort

Insertion Sort

Merge Sort

Quick Sort

Heap Sort

Counting Sort

DFS

BFS

Kruskal’s Algorithm

Q.3. What will happen if quick sort divide the array in 99% and 1% on each
partition call.
T(n) = T(99*n/100) + T(n/100) + n
T(1) = 1
What will be the time complexity? Solve the above equation step by step

Q.4.
PART 1:- Modify insertion sort to apply on linked list.
PART 2:- Can we sort the linked list while inserting a new node in the
list. If Yes,
then write the code for that also.
Q.5. Given two character strings x[1..n] and y[1..m], the edit distance is the
cost of transforming the string x to y using a minimum number of operations
from the set {replace, insert, delete}. Design an effi cient algorithm to fi nd the
minimum edit distance between two given strings.

What if there are specifi c costs associated with each of the operation and you
want to minimize the total cost ? This has direct application to DNA
sequencing problem, i.e. how close they are to each other. 


Q.6. Implement Dijkstra’s Algorithm using binary heap.

Q.7. Write a Program to find complement of a graph.


[ NOTE:- Complement of a graph means adding an edge where there was no
edge and removing the edge if there was one.]
Input:
Output:

Q.8. Write an algorithm to detect cycle in a graph.

Q.9. An element is common, if it occurs more than n/4 times in in a given set
of n elements. Design an O(n) algorithm to find a common element if one
exists. 


Q.10. We are given a sequence of integers in the range [1, n] where each
value occurs at most once. An operation called EXTRACT-MIN, occurs at
arbitrary places in the sequence which detects the minimum element up to
that point in the sequence and deletes it. 

For example
3,7,1,E,9,4,E,... the output is 1,3.

Design an efficient algorithm to handle a sequence of such operations.

Q.11. Given an array of integers (not necessarily positive), fi nd a subarray xi,


xi+1 . . . xj , such that the sum of the numbers in the subarray is maximum
over all possible subarrays.

For example in the array
-3, -1, 4, , 6 , -3, 5, -4, 2,
the subarray that attains the maximum sum is 4,6,-3,5.
Design a linear time algorithm for this problem.

You might also like