of 35

• date post

10-Nov-2015
• Category

## Documents

• view

226

2

Embed Size (px)

description

gate previous

### Transcript of DAA Privious Gate-Final

CHAPTER -1ALGORITHM ANALYSIS & ASYMPTOTIC NOTATIONS1. Solve the recurrence equations (GATE-1987)T (n) = T (n-1) +nT (1) =1Ans: T(n)=n(n+1)/2

2. What is the generating function G (z) for the sequence of Fibonacci numbers? (GATE-1987)3. .Express T (n) in terms of the harmonic number Hn = 1/i, n 1, where T (n) (GATE-1990)Satisfies the recurrence relation,T (n) = (n+1)/2 T (n-1) +1, for n 2 and T (1) =1What is the asymptotic behavior of T (n) as a function of n? integers and a if and only if a divides b, satisfy the distributive property. 4. 0 (n), where O (n) stands for order n is:(GATE-1993)(a) O (n) (b) O (n2) (c) o (m3) (d) O (3n2)(e) O (1.5 n3)Ans:B5. Consider the following two functions:(GATE-1994)n3 for 0 = n < 10,000 n for 0 = n=100G1(n) =n2 for n= 10,000 , g2(n) = n3 for n > 100Which of the following is true?(A) g1(n) is 0 (g2(n) )(B) g1 (n) is 0 (g2(n3) )(C) g2(n) is 0 (g1(n) )(C) g2 (n) is 0 (n) Ans:A6. Which of the following is false ?(GATE-1996)(a) 100 n log n = O( n log n/100)(b) log n =0 (log log n)(c) If 0< x< y then nx =O (ny) (d) 2n O(nk)Ans:B7. The concatenation of two lists is to be performed on O(1) time . Which of the following implementations of a list should be used?(GATE-1997)(a) Singly linked list (b) Doubly linked list(c) Circular doubly linked list (d) Array implementation of list Ans:C8. Consider the following piece of C code fragment that removes duplicates from an ordered list of integers.(GATE-1997)struct node {int val ;struct node *next;};typedef struct node Node;Node * remove-duplicates (Node *head, int *j){Node *t1 ,*t2;*j=0;if (t1 != NULL) t2 = t1 next;else return head;*j = 1;if (t2==NULL) return head;while (t2 != NULL){if (t1.val != t2.val) (S1){(*j)++; t1 next = t2; t1=t2 (S2)}t2 = t2 next;}t1 neXt = NULL;return head;}Assume the list contains n elements (n-2) in the following questions.(a) How many times is the comparison in statements S1 made?Ans: n(n-1)/2(b) What is the minimum and the maximum number of times statements marked S2 get executed?Ans: Min=0 Max=n-1(c) What is the significance of the value in the integer pointed to by j when the function completes?Ans: It points to last element in the list9. The maximum gate delay for any output to appear in an array multiplier for multiplying two n bit number is ( GATE-1999)(A) O (n2)(B) O (n) (C) O (log n) (D) O (1)Ans:A10. Consider the following algorithms. Assume procedure A and procedure B takes O (1) and O (1/n) unit of time respectively. Derive the time complexity of the algorithm in O- notation.(GATE-1999)algorithm what (n) beginif n = 1 then call Aelse beginwhat (n-1);call B (n)endend.

An: T(n)=O(n)11. Consider the following functions h(n)=n!Which of the following is true?(a) h(n) is O (f(n) )(b) h (n) is O (g(n))(c) g (n) is not O (f(n)) (d)f(n) is O (g(n))Ans:D12. Let f(n) =n2 log n and g(n) = n(lg n)10 be two positive functions of n. Which of the following statement is correct?(a) f(n) = O(g(n) and g(n) O (f(n))(b) g(n) = O(f(n)) and f(n) O (g(n))(c) f(n) O(g(n) and g(n) O (f(n))(d) f(n) = O(g(n)) and g(n) = O(f(n))Ans:B13. In the worst case , the number of comparisons needed to search a singly linked list of length n for a given elements is (a) log n(b) n/2(c) Log 2 n-1(d) nAns:D14. Consider the following algorithm for searching for a given number x in an unsorted array A[1..n] having n distinct values:1. Chose an i uniformly at random from 1..n; 2. If A[i] = x then Stop else Goto 1;Assuming that x is present A, What is the expected number of comparisons made by the algorithm before it terminates?a) n(b) n-1(c) 2n(d) n/2Ans:A15. The running time of the following algorithm Procedure A (n)If n< =2 return (1) else return (A ( n));Is best described by(a) O(n) (b) O (log n)(c) O (log log n) (d) O (1)Ans:C16. Consider an array multiplier for multiplying two n bit numbers. If each gate in the circuit has a unit delay, the total delay of the multiplier is (a) (1)(b) (log n)(c) (n) (d) (n2)Ans:D17. Consider the following three claimsI. (n+k)m=(nm) where k and m are constantsII. 2n+1 =O (2n)III. 22n = 0(2n)Which of these claims are correct?(a) I and II(B) I and III(C) II and III(D) I,II ,and IIIAns:A18. The cube root of a natural number n is defined as the largest natural number m such that m3 n. The complexity of computing the cube root of n (n is represented in binary notation) isA. O (n) but not O (n0.5)B. O (n0.5) but not O ((log n)k ) for any constant k > 0C. O ((log n)k ) for some constant k > 0 , but not O ((log log n )m) for any constant m > 0D. O ((log log n )k) for some constant k > 0.5 , but not O (( log log n )0.5)Ans:C19. What does the following algorithm approximate? (Assume m > 1, > 0).X=m;Y=1;while (x - y > ){ x = (x+y) / 2; y = m/x;}print (x);

(a) log m(b) m2(c) m1/2(d) m1/3Ans:C20. Let A[1,..,n] be an array storing a bit (1 or 0) at each location, and f(m) is a function whose time complexity is O (m). Consider the following program fragment written in a C like language :counter = 0; for (i=1; i< = n; i++){ if (A [i] == 1) counter++; else { f (counter); counter=0;} }The complexity of this program fragment is(A) (n2)(B) (n log n ) and O (n2)(C) (n)(D) O (n)Ans:C21. The time complexity of the following C function is (assume n > 0)int recursive (int n) {if (n == 1) return (1);else return (recursive (n-1) + recursive (n-1));}(a) O (n) (b) O (n log n)(c) O (n2)(d) O (2n)Ans:D22. The recurrence equationT (1) =1T (n) = 2T (n-1) +n, n2Evaluates to (A) 2n+1 n - 1(B) 2n - n(C) 2n+1 - 2n - 2(D) 2n + nAns:A23. The time complexity of computing the transitive closure of a binary relation on a set of n elements is known to be:(a) O(n) (b) O (n log n)(c) O [n3/2](d) O (n3)Ans:D24. A Priority-Queue is implemented as a Max-Heap. Initially , it has 5 elements. The level-order traversal of the heap is given below:10, 8, 5, 3, 2Two new elements 1 and 7 are inserted in the heap in that order. The level order traversal of the heap after the insertion of the elements is:(a) 10, 8, 7, 5, 3, 2, 1(b) 10, 8, 7, 2, 3, 1, 5(c) 10, 8, 7, 1, 2, 3, 5(d) 10, 8, 7, 3, 2, 1, 5Ans:D25. (i)The space complexity of the above function is :(a) O (1) (b) O (n) (c) O (n!) (d) O (nn)Ans:B (ii) Suppose we modify the above function foo() and store the values of foo(i), 0 i n, as and when they are computed. With this modification, the time complexity for function foo ( ) is significantly reduced. The space complexity of the modified function would be :(a) O (1) (b) O (n) (c) O(n2) (d) O (n!)Ans:B26. 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 ) = (log n) (B) val (j) =( n) (C) val (j) = (n) (D) val (j) = (n log n) Ans:C27. Consider the following segment of C-code:int j, n:j = 1; while (j 0 is :(A) log 2 n + 1(B) n(C) log 2 n(D) log 2 n + 1Ans:D28. What is the time complexity of the following recursive function :int DoSomething (int n) { if (n 2) nodes named v1, v2,vn. Two nodes vi,vj are connected if and only if 0 < | i-j | 2. Each edge (vi,vj) is assigned a weight i+j. A sample graph with n = 4 is shown below

26. What will be the cost of the minimum spanning tree (MST) of such a graph with n nodes?(A) 1/12(11 n2 - 5n)(B) n2 - n+1(C) 6n-11(D) 2n+1Ans:B27. The length of path from v5 to v6 in the MST of previous question with n=10 is(A) 11(B) 25(C) 31(D) 41Ans:C

CHAPTER IVDYNAMIC PROGRAMMING1. Obtain the optimal binary search tree with equal probabilities for the identifier set (a1,a2,a3) = (if, stop, while)2. A binary search tree is generated by inserting in order the following integers:50,15,62,5,20,58,91,3,8,37,60,24The number of nodes in the left sub tree and right sub tree of the root respectively is (a) ( 4,7) (b) (7,4)(c) (8,3) (d) (3,8)Ans:B3. Which one of the following algorithm design techniques is used in finding all pairs of shortest distances in a graph?(a) Dynamic programming (b) Backtracking (c) Greedy (d) Divide and ConquerAns:A4. Fill in the blanks in the following template of an algorithm to compute all pairs shortest path length in a directed graph G with n* n adjacency matrix A. A[i,j] equals if there is an edge in G from i to j , and 0 other wise .Your aim in filling in the blanks is to ensure that the algorithm is correct.INITIALIZATION: For i= 1 n{For j =1 n{ if A[i,j]=0 then P[i,j]= ____else P[i,j]=____;}

ALGORITHM: For i=1 n{For i=1 n{For k=1 n{ P[___,___]=min{___,___};}}](a) Copy the complete line containing the blanks in the Initialization step and fill in the blanks.(b) Copy the complete line containing the blanks in the Algorithm step and fill in the blanks.(c) Fill in the blank: The running time of the Algorithm is O(____).Statement for Linked Answer Questions: 05 & 06The subset-sum problem is defined as follows. Given a set of n positive integers,S = {a1,a2,a3.,an } and positive integers W ,is there a subset of S whose elements sum to W? A dynamic program for solving this problem uses a 2- dimensional Boolean array, X, with n rows and W+1 columns X[i,j], 1in,0jW, is TRUE if and only if there is a subset of {a1,a2,a3,..,an } whose elements sum to j.5. Which of the following is valid for 2 I n and ai j W?a) X [i,j]=X[i-1,j] X[i,j-ai]b) X [i,j]=X[i-1,j] X[i-1, j-a1]c) X [i,j]=X[i-1,j] ^ X[i,j-a1]d) X [i,j]=X[i-1,j] X[i-1,j-a1]Ans:B6. Which entry of the array X , if TRUE, implies that there is a subset whose elements sum to W? (A) X [1,w](B) X[n,0](C) X [n,w](D) X [n-1,n]Ans:CStatement for Linked Answer Questions:07 & 08Consider the following C program that attempts to locate an element x in an array Y [] using binary search.