Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
25 views

DSA Algorithms

Uploaded by

rajulal13
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
25 views

DSA Algorithms

Uploaded by

rajulal13
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 18
GATE SOLVED PAPER - CS ALGORITHMS Randomized quicksort is an extension of quicksort where the pivot is chosen randomly. What is the worst case complexity of sorting m numbers using randomized quicksort ? (A) O(n) (B) O(a logn) (C) 0G») (D) O(mt) Consider any array representation of an n element binary heap where the elements are stored ftom index 1 to index of the array. For the element stored at index i of the array (i dir») (©) dru) < ar) (D) None of the above Vow many undirected graphs (not necessarily connected) ean be constructed cut ofa given set = {V),PqioonsVr} Of vertices ? (ay 2D ae On @) What is the minimum aumber of stacks of size n required to implement a queue to sizen ? (A) One (@) Two (C) Three (D) Four The solution to the recurrence equation 7(2*) = 37 (2) +1,7(1) = 1 is (aye @ er) (cat (py 24 The minimum number of colours required to ealour the vertices of a eycle with » nodes in such a way that no two adjacent nodes have same colout is 2 3 4 (©) n~25]+2 In the worst case, the number of comparisons needed to search a single linked list offength n for a given clement is (A) logn (B) 5 (© bos =1 (Dyn Maximum number of edges in a n-node undirected graph without self loops is 2 nn—1) wr (ey (n= Di) (m1 wy The number of leaf nodes in a rooted tree of m nodes, with each node having 0 or 3 children is 2 n= oy w & 2 nt Qn-+1 oa” Ore Consider the following algorithm for scarching fora given number x in an unsorted arcay A L..u] having m distinct values (1) Choose an f uniformly at random from (1...) (2) If A[’]=<« then stop ese Goro 1; Assuming that x is present 4 , What isthe expected number of comparisons made by the algorithm before it terminates ? (Ayn (B)n-1 ®) an M5 The running time of the following algorithm Procedure A (n) Ifn return (1) else return (4 (Jv )); Isbest described by (A) 0) (B) (logn) (©) O(logtogn) () 0) A weight-balanced tree is a binary tree in which for each node, the number of nodes in the let sub tree is at least half and at most twice the number of nodes in the right sub tree. The maximum possible height (number of nodes on the path from the root to the furthest leaf) of such a tree on m nodes is best described by which of the following ? (A) login (B) login © loxsn (D) gyn To evaluate an expression without any embedded function calls (A) One stack is enough (B) Two stack are needed (©) As many stacks as the height of the expression tree are needed (D) A Turning machine is needed in the general case Consider the following three claims 1. (1 +H)" = O(n"), where & and m are constants, 2.2! = 02) 3.27"'= 00) ‘Which of these claims are correct? (A) Land 2 () Land3 (C)2 and 3 (D) 1,2 and 3 Consider the following graph © LS my Among the following sequences 1. abeght 2. abfehg 3. abihige 4. agfhibe Which are depth first traversals of the abave graph? (A) 1,2 and 4 only, (B) 1 and 4 only (C) 2,3 ond 4 only (D) 1, 3 and 4 only The usual O(n) implementation of Insertion Sort to sort ab array uses linear search to identify the position where an element is to be inserted into the already sorted part of the array. If, instead, we use binary search to identify the position, ‘the worst case running time will (A) remain (0°) (B) bevome © (7 (logn)?) (©) become O(n logn) (D) become O(n) In a heap with » elements with the smallest element at the root, the 7 smallest clement ban be found in time (A) O(n logn) (2) Gin) (©) O(log) () (1) Common Data For Q. 20 & 21 Solve the problems and choose the correct answers, In a permutation q...d, of n distinct integers, an inversion is a pair (a)a)) such, that £ a, Tall permutation are equally likely, what is the expected number of inversions in a randomly chosen permutation of 1....0? (A) nn 1)/2 B) nad (awe (D) 2nflogsn] What would be the worst case time complexity of the insertion Sort algorithm, if the inputs are restricted to permutations of 1... with at most n inversions? A) 00?) (B) OGrlogn) (© 00" @ 0 The cube root of a natural number m is defined as the largest natural number m such that m? 0 (©) O(logn)') for some constant k > 0, but not O(loglogn)") for any constant m>0 (D) OCloglogn)') for some constant k > 0.5, but not OCloglog n)") Let G =(V',B) be an undirected graph with a sub-graph G; = (V),), Weight are assigned to edges of G as follows Oife cE, wie) = 1 otherwise A single-source shortest path algorithm is executed on the weighted graph (/, 1) with an arbitrary vertex v of Vi as the source. Which of the following can always be inferred from the path costs computed? (A) The number of edges in the shortest paths from » to all vertices of G (B) G. is connected (©) F, forms a clique in G (D) G, is a tree What is the weight ofa mininum spanning tree of the following graph? (ay 29 (B) 31 (©) 38 (py at The following are the starting and ending times ofeetivities 4 ,B,C,D,E,F,G and H respectively in chronological order;"s,ha.dd.ae.thdgecthgheHere, x, denotes the starting time and x, denotes the ending time of activity X. 1 need to schedule the activities in a set of rooms available to us, An activity can be scheduled in a room only if the room is reserved for the activity for its entire duration, What is the minimum number of rooms required? a3 (B)4 (5 M6 Let G = (V,F) bea direction graph with » vertices. A path from 1, to v, in G is sequence of vertices (v,¥1...¥) such that (vv. = E for all k in j through J-1. A simple path is a path in which no vertex appears more than once. Let A bean» 1 array initialized at follow Lif (jk) EE ADA -{ Uae Oerenre Consider the following algorithm fori=lton fori=lton fork=1ton ALi, k]=max (A(3,k] (A341) +i, k))¢ Which of the fallowing statements is necessarily true for all j and & after terminal of the above algorithm” (A) AG ALES (8) If.4 [J] Sn — 1, then G hes @ Haniltenian eyele (C) Ifthere exists @ path trom / to 4,4 [/,4] contains the longest path lens from Jiok (D) If there exists a path from j to k, every simple path from j to k contain most 4 [j,k] edges Level order traversal of a rooted tree can be done by stating from the root and performing: (A) preorder traversal (B) inorder traversal (C) depth first search (D) breadth first search Given the following. input(4322,1334,1471,9679,1989,6171,6173,4199) and the hash function * mod 10, which of the fallowing statements are true? 1, 9679,1989,4199 hash to the same value 2. 1471,6171 hash Co the same value 3. All clement hashes to a different value (A) Lonly (B)2 only (©) 1 end 2 only (D)3 and 4 only The tightest lower bound on the number of comparisons, in the worst case, for comparision-based sorting is ofthe order of Wn @r (C) nlogn (0) nlog'n Consider the label sequences obtained by the following pairs of traversals on a labeled binary tree. Which of these pairs identify a tree uniquely? 1. preorder and postorderr 2. inorderr and postorder 3. preorder and inorder 4, evel order and postorder (A) 1 only (8) 2 and3 (C)3 only (D) 4 only Two matrices Mj and M; are to be stored in arrays A and B respectively. Each array can be stored either in row-major or column-major order in contiguous memory locations, The time complexity of an algorithm to compute MxM: will be (A) best if A is in row-major, and B is in column-major order (B) best if both are in row-major order (C) best if both are in column-major order (D) independent of the storage scheme ‘Suppose each set is represented as a linked list with elements in arbitrary order. Which of the operations among union, intersection, membership, cardinality will be the slowest? (A) union only (B) intersection, membership (©) membership, cardinality (D) union, intersection Suppose we run Dijkstra’s single source shortest-path algorithm en the following edge-weighted directed graph with vertex P as as the source. In what order do the nodes get included into the set of vertices off which the shortest path distances are finalized’? (A) P,Q,R,S7.U (B) P,Q,R,U,S,T (©) P.O,R,U,T,S (D) P.0,T.RUS Let 4[I,...] be an array storing a bit (1 or 0) at each location, and fl) is a function winose time complexity is 6(m). Consider the following program fragment written in a C like language: counter for (islsicnsith) if (A[i]==1)countert+ else(£|counter); counter=0;} The complexity ofthis program fragment is A) 20°) (B) M(rtogn) and OUP) (©) am (D) om) The time complexity of the following C fimnction is (assume n > 0) int recursive (int n) cl a) return (1); elec Af (ns return (recureive (n-1)+recuraive(n=1) ; (ow) (8) O(togn) (©) 0m’) (woe) The recurrence equation T)=1 T(m) = 20 (nN) +n,n 52 evaluates to (Ayz'an-2 (B22 (€)2""'-20-2 (D) 20 A program takes as input a balanced binary search tree with n leaf medes and computes the value of a function g(x) for each node x. If the cost of computing _g(x) is min (number of leafnodes in lear-subtree of x, number of leaf-nodes in right-subtres of x) then the worst case time complexity of the program is (A) 0m) (8) O(mlegn) (©) 0(ny" (0) 00") A undirected graph G has m nodes. Its adjacency matrix is given by by ann x7 square matrix who. 1. diagonal clements are O', and jiagonal elements are 1s, Which one of the following is TRUE? (A) Graph @ has no minimum spanning tree (MST) (B) Graph G has a unique MST of cost in-1 (C) Graph @ has multiple distinet MST"s, each of cost #1 (D) Graph G has multiple spanning trees of different costs 2. non The time complexity of computing the transitive closure ofa binary relation on a sot of x elements is known to be (A) 0m) (B) O(n logn) (cyowy (D) Om") A priority-Queue is implemented as 2 Max-Heap, Initially, it has 5 elements, The level-order traversal of the heap is given below: 10, 8.5, 3.2 Two new elements ‘1’ and “7” are inserted in the heap in that order. The le 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 (©) 10, 8, 7,1,2,3,5 (D) 10, 8, 7,3, 2,1, 5 How many distinct binary search trees can be created out of 4 distinct keys? as (B) 14 (ou () 35 Ina complete keary, every interaal node has exactly & children. The number of eaves in such a tree with m internal nodes is Ark (B) (1H k+1 (©) n(b-1) +1 () m1) Suppose T(n) = 27 (n/ 2) +n,7(0) = T() = Which one of the following is FALSE? (A) 7 (2) = OC?) (B) T(%) = O(nogn) (OT) = 90") (D) T(n) = O(nlogn) ee esters erred ree eect tre cate ecb Perera souree-chectes path algerithm can be implmonted using the binary heap data stricture with time complexity? (Ay ogy |? (B) O(/E|) +) P| log |) (©) 00 {1031} (D) OW\EHH Vos] V1) Suppose there are log n sorted lists ofn/ log elements each. The time complexity ofproducing a sorted list ofall these elements is: (Hint: Use a heap data structure) (A) O(n Ioglogny (B) @(nlogn) (©) Qnlogn) (D) 90°) Common Data For Q. 46 & 47 Solve the problems and choose the correct answers, Consider the following C-finetion: double foo (int n){ int 4; double cum i£ (n==0) return 1.0) else { sum-0 0; for (i=0; O24 += 9; Let Val (/) denote the value stored in the variable f after termination ofthe for oop. Which one of the following is true? (A) yall) = (log) (B) val) = en) (©) vali) = en) (D) val()) = e(nlogn) ‘An clement in an array X’ is called a leader ifit is grater than all clements to the right oft in X, The best algorithm to find all leaders in an array (A) Solves it in linear time using a left to right pass of the array (B) Solves in linear time using a right to leit pass (© Solves itis using divide and conquer in time O(log) (B) Solves it in time 9(0°) Consider the following graph Which one of the following cannot be the sequence of edges added, in that order, toa minimum spanning tree using Kruskal’s algorithm? A) 4B fb— Dd = ah,d 0) (B) =.= P)(0= 00-0) (© @—Pla—b){d— od ede) (D) (d= fa ~b)(b= Aid = ed =e) Let T be a depth first search tree in @ undirected graph G Vertices w and v ate leaves of this tree 7. The degrees of both and y in G are at least 2. Which one ofthe following statements is true? (A) There must exist a vertex w adjacent to both u and v in G (B) There must exist a vertex w whose removal disconnects u and vy in G (©) There must be exist a cycle in G containing w and v (D) There must exist a cycle in G containing uv and all its neighbours in G A set X can be represented by an array x[n].as follows, ca if 1eX 0 otherwise Consider the following algorithm in which x,yand z are boolean arrays of size n algorithm z22(x[hy (204 int i: for (i = 0,9 < mi ++i) 21) = Gl A~ylD Vx aye) } The set Z computed by the algorithm is (ay (xuy) @y(xny) (Cy =Y)NWy =x) (@) Yu Consider the following is true? T(n) = 27 (fenD+ LTD =1 Which one of the following is true? (A) Tn) = A(loglogn) (B) Tn) = Plog) (©)T() =n) (P) Te) = Hn) The median of n elements can be found in O(n) time. Which one of the following is correct about the complexity of quick sort, in which remains is selected as pivot? (A) 6) (B) G(r log) (©) aq”) (D) a") Given two arrays of numbers djonnndy and Dynwnby where each number is 0 oF 1, the fastest algorithm to find the largest span (i,j) such that eb ct bmn Hay bb Ber} onens +-By OF Feport that there is no such span, (A) Takes O(3') and 912") time if hashing is permitted (B) Takes O(n) and W ("4 time in the key comparison model (C) Takes ©(n) time and space (D) Takes O(s'n) time only if the sum of the 2n elements is an even number Consider the following code written in a pass-by reference language like FORTAN and these statements about the code, Subroutine swap (ix, iy) it-ix Ll: 12: end Abst call swap (ia,ib+5) print*,ia,ib end Sl: The complier will generate code to allocate a temporary nameless cell, initialize it to 13, and pass the address of the cell to swap $2: On execution the cade will generate a runtime error on line I.1 83: On execution the code will generate a runtime error on line 12 S4: The program will print 13 and 8 ‘So: The program will print 13 and-2 Exactly the following set of statement (s) is correct (A) SI ands? (B) Si and S4 © $3 (D) St amd ss The height of binary tree is the maximum number of edges in any root to leat path, The maximum aumber of nodes is a binary tree of height fis, (a? (B) 2-1 (C211 (Db) 2" The maximum aumber of binary trees that can be formed with three unlabeled nodes is Con (B) 5 «4 (D3 Which of the following sorting algorithms has the lowest worst-case complexity? (A) Merge sort (B) Bubble sort (© Quick sort (D) Selection sort The inorder and preorder traversal ofa binary tree are dbeajeg andabdecy, respectively The postorder traversal of the binary tree is (A) dedggen (B) edbyfew (C) edbfgew (D) defybea Consider the hash table of size seven, with starting index zero, and a has function (Gr +4) and 7. Assuming the has tabie is initially empty, which of the following is the contents of the table when the sequence 1,3,8,10 is inserted into the table using closed hashing? Note that-denotes an empty location in the table (B) 18,10, In an unweighted, undirected connected graph, thee shortest path from a node 5 to every other node is computed most efficiently, in terms of time complexity, by (A) Dijkstra’s algorithm starting from S,(B) Warshall’s algorithm (C) performing a DFS starting ftom $ (D) preforming a BFS starting fom S A complete n-ary tree is a tree in which cach node has m children or no children Let I be the number of internal nodes and L be the number of leaves in a complete n-ary tree. If L= 41, and J=10, what is the value of n (a3 (B) 4 ws (6 In the following C function, let n 2 (D) ¥ is 2.468 10 12 14 16 18 20] and 2

You might also like