Professional Documents
Culture Documents
What do above functions do? Analyse above code to find running time complexity of f(m,n) and g(m,n).
d i) Why code tuning techniques (CTT) are required? Can CTT be used to improve time complexity of [02]
an algorithm? Justify your answer.
ii) Optimize following C code snippet, as much as possible: [02]
for (i=0;i<n;i++)
for (j=0;j<n;j++)
if(i%2)
{
x[i]+= 4*j + 5*i;
y[i]+= 7 + 4*j;
}
Q2 a The frequency of a number in an array is the number of times it appears in the array. Describe an [07]
algorithm that finds the most frequent number in an array of n numbers. If there are multiple numbers
with highest frequency then list them all. Analyze the complexity of your algorithm.
Page 1 of 3
OR
b An array contains n elements, out of which only log(n) are unique. Design an algorithm and suitable [07]
data structure to sort the array in O(nloglog(n)) time complexity and O(logn) space complexity.
c Two sorted arrays A and B have n elements each. Design an efficient algorithm to find out median, if [07]
elements of A and B are taken together.
OR
d For each of the following sorting algorithms, write their best and worst case scenarios: [07]
i) Quick sort
ii) Merge sort
iii) Insertion sort
iv) Bubble sort
v) Selection sort
vi) Heap sort
vii) Counting sort
Q3 a Suppose we wish to multiply 5 matrices A, B, C, D and E, to compute AxBxCxDxE. Following are the [07]
dimensions of the matrices:
A : 30 x 35
B : 35 x 15
C : 15 x 5
D : 5 x 20
E : 20 x 25
Write optimal substructure for matrix chain multiplication. Also, find optimal order of multiplying these
matrices, so that minimum number of scalar operations are performed.
OR
b Write optimal substructure to find longest common subsequence (LCS). Find LCS of following two [07]
DNA sequenences: ACCGGTCGAGTGCGC and CCGTTGCTCTGTAAA
c i. The four queens problem is the problem of placing four queens on an 4×4 chessboard such that [03]
none of them attack one another (no two are in the same row, column, or diagonal). Solve this
problem step by step using backtracking.
ii. Using Dijkstra’s algorithm, find shortest path in following graph, taking v1 as source vertex.
[04]
OR
d Consider following pairs:(a,45), (b,13), (c,12), (d,16), (e,9), (f,5). Each pair contains a character and [07]
corresponding frequency (in 1000’s) in a file.
i. Construct Huffman tree and find codes for these characters.
Page 2 of 3
ii. How much percentage space is saved by Huffman encoding, assuming ASCII codes were used
before compression?
iii. Encode following string using above Huffman codes: faecdb.
iv. Decode following encoded string using above Huffman codes: 10011011011100000
Q4 a Legendary Indian mathematician, Srinivasa Ramanujan worked on a problem to find number of ways a [07]
number can be written as sum of other smaller numbers, lets call it Srinivasa Ramanujan Number
(SRN).
e.g. SRN(4) = 4, as 4 can be written as sum of smaller numbers in following 4 ways:
4=1+3
=2+2
=2+1+1
=1+1+1+1
The sequence starts with 1, 1, 2, 4, 8, 16, 23, 28, 38, 49, ...
Write an efficient algorithm to compute an where 1015 ≥ n
c Design an efficient algorithm to find sum of digits of all numbers in range 1 to n (both inclusive). Where [07]
1<=n<=10^15. For example, if n =11, then the sum is 48, as 1+2+3+4+5+6+7+8+9+(1+0)+(1+1) = 48
OR
d A graph is called Eulerian if it is possible to draw the given graph without lifting pencil from the paper [07]
and without tracing any of the edges more than once”. More formally the graph has Euler path. A Euler
path is a path that traverses all the edges in the graph, and it traverses each edge once (it can traverse a
vertex more than once). Design an algorithm that tests if a given unweighted, connected and undirected
graph is Eulerian or not.
Page 3 of 3