You are on page 1of 30
eke Study Course 2018 Computer Science & IT Objective Practice Sets Sl. Topic Page No. Asymptotic Analysis of Algorithms 1 2 6. 6 MADE EASY MADEEASY Mot Stucents Only For g iI Cr Q.1 Which of the following standard algorithms is not a greedy algorithm? {a) Selection sort (©) Kruskal’s algorithm (©) Beliman ford shortest path algorit (@) Dijkstra’s shortest path algorithm Q.2 Let ir) = A(n) and g(n) = 2 (77). Then Kn) + g(r) is (@) ar) (6) &n) (©) 2(r" (d) On) Q.3 Consider the following function Void fit n) int fk m f< 100;i+4) for (= 0: jn; jo) { for (k= 0: k 0 (b) Orr) (2) None of these puter Science & IT Asymptotic Analysis of Algorithms Q.5 Consider the following functions, n, log n, Jf, logilog n), (log a)? Vilogn, nlogn Identity the functions in increasing order of growth a fe) ® a logn.n logtiog n), (log (log 12, Ja b) log(log n), log n, (log ni. Vm. Villogn a. nlogn gn 7108 oan ©) logttog n), log n, (log m2, Ya, — - aiflogn . n, nlogn. (@) None of these .6 Compute the running time ofthe folowing Ccode. Assume that n> 1 and time complexity as a function of while (x> 0) ly while (v> 0) um + 1; =x- 10, @ An {b) O{nlog n) © ar (9) Or? og n) Q.7 Consider the following code Sum for (i= 1ton) for (/= 10 2n) ‘sum G3 MADE ERSY Find the time complexity of above code in terms ofa, (a) On) (0) O(nlog r) (©) OF) (©) OnFlogn) Q8 Find which ofthe folowing is not correct? () Dwi om) {b) rPlogn = @{n?) (©) 100n? + 2n? = an?) (8) i= Om Q.9._ Determine the asymptotic running time of f(r) for the following C program, Int Rint) ( a return, ) void gtint m) { int y, for (y= m; y> 0; Wi = 2): ) (a) (b) er?) © 2 (6) @(nlog n) Q.10 Consider the following functions: 1. nl 2. ais constant, a> 0 3. 9 4. nf, kis a constant, k> 0 5 ef Choose the correct statement which ranks all functions by order of growth (a) 2<4<5<1<3 (0) 4<2<3<5<1 () 4<5<2<1<3 (0 4<2<5<1<3 wurw.madeeasy.in Postal Study Course BIE| (mane easy Algorithm Q.11 Consider the following Cfunction find (int n) (tne 2) then os {sum =0; for (i= 1; #54; i++) (3) ina (8) lorie ttsemm tse) 1 Assume that the division operation takes constant ime and “sum” is global variable, What is the time complexity of “find (n)" ? (@ er) (0) ©(ePtogn) © er) (@) None of these Q.12 Match the following groups. Group-t (n> 0) Group-11 A 3nt+4nssniogn 1. Ot) B. n+logm+logiogn 2, Ologn) C. W+n+nlognslogn 3. Or) D. 10+ 1000+ 100000 4. O{nlogn) 5. O(rF) Codes: A B CD @ 5 3 4 2 ® 5 4 3 1 @ 5 3 41 @ 5 4 3 2 Q.13 Consider the following two functions. An) = 08, << 10,000 otherwise An) = mi10<< 100 1? + 5n, otherwise Which ofthe following is/are true? 1. fins O17!) 2. g(n)is O(n*) 3. OlAnissame as Og(r)) 4. gnjis On?) {a) Sand4 (b) 1,2and3 (o) 1,2and4 (d) None of these Objective Practice Sets | Computer Science & IT eens) GIMADE EASY Q.14 Look at the following algorithm Let complexity of y is Oflog,n). Then the int n complexity of x will be int A[100]; (@) OF) void x() (0) Q(rFlog,r) (ints (©) Arlog,) for (i= nl2;i>= 158 (2) A) Hi) . " while (n> 1)( Q.15 Let fin) = Q(n), gn) = O(n) and hin) = O(n). Then Wb {fir)- aim) + Her} } (a) Qn) (b) Qn) id int void int i) (c) &(”) ‘ (@) None of these } EEEEIIEN Asymptotic Analysis of Algorithms oO 203 © 4 9 & © 6 7 | & H % & MO) ts). 12 Cr. 185 (3) ia (cy, 181) GEER Asymptotic Analysis of Algorithms 1. (©) 4.) Boliman ford shortest path algorithms example A of dynamic programming. EE(2) = 7rd 2. (0) aa : Given a Lb [i tete enti times] {0) = ate) ie. fn) = a(n) {n)canbe anything butatleast(n)notless than () Given gn) = Qi?) ie gn) & or) x(n) can be anything but atleast (n’) not less than (n?) fin) + gn) = O(n) + (78) = 0%) Here we can not comment about upper bound. 5. (b) log(log n) < logn< (log ny?< Ya < Viilogn < ate ‘| hn raf - ee 3(S)]-o00 logn ° So option (b) is correct, (EY ovjective Practice sets wurnmadeeasy.in MADE ERSY 6. (b) Inner white loop executes logs times for every x. nan BB 1x loggn tines U "24 ‘Be! outerloop: x = mn=10,0=20,..4 n 3) = om «Time complexity = O(nlogyn) ‘So option (b) is correct. 7. (c) Assume that each simple statement executes in constant time. = Ste tet+..+ 2n-i+ tptimes) ‘So option (c)is correct. 8. (b) (log nS k.? will not satisty for any constant k. ++ Option (¢) is net correct. 9. (b) Time complexty of g(m)is : (log) [Note: (n) returns the value 2"] 40. 9) = 92") = glm) =» am) O(log m) = Ollog 2’ am) en) Recurrence relation for f) = Mn-1)+ ote) f(n=1) + O(n) = O(n?) (d) he ae ele nl- 1 and T)=1 Find the time complexity of T(r @ OV) (0) O(7# log n) ©) OV) (@) O(r8 fog n) Q3 Let Tn) = [r{log (n*)—log n)+log nn + log n Find complexity of Tn) (@) O{nlog n) (b) Ov?) (©) O(e? log r) () (7) Q.4 Match List-I (Divide Conquer) with List-I! (Recurrence Relation (Worst-Case) and select the correct answer using the codes given below the ists List-1 List A. Binarysearch 4. Tn) =21{nl2)+C B. Mergesort 2. T{n)= Mn) +1 C. Quick sort 3. Tn) = Tint) +n-1 D. Max Min 4. Tin) = 2712) + kn Codes: A Bc OD (stone 2 2 3 41 (ie aa @2 4 3 1 QS Let Zn} = TinlS) + TI7n/10) + c.n, where cis constant. Find running time of Tin) ee ee Recurrence Relations as a7 as as (a) &n) (©) a) (©) o(ntog n) (@) None of these Let T,, be the recurrence relation is defined as follows: nef,2 Tatn Find the value of 7? @n (o) : © = Counting combinations can be also solved using divide and conquer approach ny _(n-1)_ (n—1 (0-63) Consider the following function to compute things chosen rat a time. junction X(n, 7) { if(r=O0r n=) then retun 1; else return (X(n= 1, r= 1) + Xin- 4, Nh ) Find the worst case time complexity of xin. 0? (a) On) {o) Ofr?) © a2 {d) None ofthese a fn time complexi Let T( ar(2)- 00h. Find plexity of Tiny? (@) @(vn) (©) @(vhlogn) (©) @n) (d) @{nlogn) Let 1(n) = Tn~ 1) + Yn. Then Tn) is fa) @(1) (b) O(log n) (©) (log log n) (a) &) GaMabE EASY Q.10 Which of the following can be solved using master reorem? ) Toy =27( 2) 2 2 «) roy 27(3) 2 9 19-21(2) toa (9 Te=1(S)st00 (d) None of these Q.11 Consider the following recurrance relation Tn) =7Tnl2) + ar? ‘that willbe the solution of above recurrence? (@) OF) (6) or) (©) O(log) —_(d) O4eF*") Q.12 What is the time complexity of the following £) rf are recurence relation T(k)=7(*)+r(*) +42? reo=7(S}+7(§)-« (@) Ow (©) Olkiog A) (bo) ave) (©) Oi log &) EERIE Recurrence Relations VO 28) oe 0) 4c). | 10. () 11. @) 12 ©) 183 @ 14 PEDDIE Recurrence relations Postal Study Course EXXE)| Algorithm | Q.13 Which of the following represent correct recurrence for worst case of Binary search with n element. Tinl2) + O(n) Tin) + O(1) 2T(ni2) + O(n) Tinla) + O(1) Q.14 The recurrence relation TM) =2 Tin) = 37 (nlé) +n, has the solution then T(n) equals to @ a (b) O(log n) (©) (re) (8) none of these Q.15 In Tower of Hanoi there are three towers: left, right and middle. There are n discs in lefl tower ina particular sequence (ascending order), we have to transfer these discs into right tower having same sequence using middle tower (consider that disc with smaller sequence no. is always atop of large sequence no. ie. 1's above 2bbut2 above 1 is not alowed). I Towers of Hanoi is implemented with recursive function then total discs moves to move 9 discs from left to right are @6o7 9&8 © % & @ 1. (c) 2. (c) Tey = Ton) +1 Using Master Theorem Putn = 27 m=logn Tr) = ar(2)="0) may = TWve")e1 io ype = 8 rey”) = (ey )+1 Plog n : uy 208 Sm) Ami2)+4 If a > f{n)then Using Master's Theorem Te = sty Sim) = logm (2 m=logn) aad > > Th = OF) Re) Olegleg So option (c) is correct. worwmadeeasy.in G3MADE EAsy Objective Practice Sets | Computer Science & IT 3. (c) Inflog (r°} log n) + lag nl n+ log n 7 [n(cot)-ean}r+tean 5 = [nlog r? + log nn + log n = 1? 2logn+nlog n+logn = 21? log n+ nlog n+ log n= O(r? og n) So option (c)is correct 4 @ Binary Search: Tn) = Tin2) + 1[Avg, Worst] Merge Sort: Tn) = 27(n!2) + kn{Avg, Best, Worst] Quick Sort: Tn) = T(n-1) + n—1 [Worst Case] Max Min: T(r) = 27(ni2) + C[Best, Avg, Worst] 5. (a) Tin) = TinlS) + Ti7n/10) +0 ler gy - (3) n =n) 6. (c) Te) = Ra-iy+n By Repettve Subsiution Te) = [No -2)+n-1+0 [(n-2)+Tn-3] + (9-1) +0 = O414+2+.04n _. zB So option (c)is correct. 7. (0) Th) = Tn-1)4TIn- ted aMn-1)vd = 2T0-)+d¥2 {Solving by Substitution = n= 1] ne = 2-TD+ dye! (EY objective Practice sets ee Ga MrAoE EAsy yn (t9) = Med} ee ee = (c+ d)2""-d= 2") 8. (0) rd fr) = 100Vn neB = Off) Tr) = @(vnlogn) ‘So option (b) is correct 9. (b) Re) = Tint “) = [to-2 . di = Tina) +o 2) (3) (n-1) 1) 1 + Ollog n) + Qflog n) = Otlog n) 10. (b) Tr) = 2.1%(nl2)+ login O(n") for some e > 0 Tir) = @n!%4) = (71°?) = O(n) Tn) = en) So option (b) can be solved using master theorem a m= eft wuwmadeeasy.in [2MADE EASY EET 2075] Algorithm | Try case 1: <= #0(n"*) for some e>0, an) BOR Re Poe ee eae because i #0tr") my = e[tededen 24 a fee Trycase2: F200), because 2 =o(n) = RO) =O) n 13. (d) Try case 3: Ain") for some e>0, In Binary search we first compare the given ° element with middle element ofthe array. I the because —* = o(n) element matches with middle element, then = return middle element index. Otherwise, go to Allcases failed using master theorem for option let half of array of right half of array. Similarly option (c)is also not applicable. {@), similarly option (¢) PP me 11. (6) Let en T(r) = 3T(nia) en Ta) = m(3)+an® ) (nv) using Masters method = sr Roney 4 4 a=7,0=2, fr) = ark orl Vena af - =97(S ene Ho) (S)+oe2e8 ae = aT (nfat}4n+24 4 So Un) Ot) ‘ i 48 tteapatyana Maly fl sont ea! =F Tn sents D4 Te) = n° Un) = n° 009) Sala ene tet gs ; 114 = 81 = HT ()+n1+—4+54-G4. Ted = ee") drove ede dete] 12. (b) k —~ Pte (y =o -24nx| 4) “| 204m é a 2 (2) aim : : ’ 16. (611) Kye jee & oe £ Sy Soe Trorzarorcratontoroverthan ptm Te) = 2Mn-t) +4 = Number of moves wok Ho kB ing Ge 2 SE Zz a Zz a a wwwmadeeasy.in GamaAne Easy Objective Practice Sets a4 a2 as a4 as 3 ro) Suppose there are 4 sorted lists of [ elements each. Ifwe merge these lists into a single sorted list of n elements, how many key comparisons are needed in the worst case using an elticient algorithm? 6 z (@ Gn-3 (b) Zn-3 8 9 (@ Sn-3 (@) 50-3 The best case of quick sort helps Vivek to sorta particular data set of size ‘n’ is 2048 msec. Gaurav also tried the same algorithm on similar data sot and it took him 324 msec in worst case 10 sort a file of size 18. The file size of Vivek is, Consider the following sorting algorithms A. Mergesort —B. Quick sort C. Heapson —_D.. Insertion sort Find the correct pair such that their best, average and worst case time complexities are same. (Assume that all elements are distinct) @ AB () AC () AD (d) None of these What is time complexity of matrix multiplication (trassen's) using divide and conquer approach? (a) O(ri08) (b) O{ri998) (o) O(r37) (¢) None of these A listhasn-strings where each string s of length 1. Using divide and conquer approach, what is the worst case running time (tightest upper ound) tofind the first string? Note: First string is a string which appears first when all strings are sorted into the lexicagraphical order. reece Divide and Conquer as az (a) OF) (} Oniog n) (©) On) (@) Orelog n) Consider the following sorting algorithm, Sorting (A, low, high) 1 if (low f (lowe ( igh) return, high) it (Allow > Afhigh)) ‘Swap (Allow, Afhigh)); return; Sorting (4, low, t); Sorting (A thigh) Sorting (A, low, f); t What is the running time of Sorting(A, 1, n) function. @) en") (©) en°7) (b} @(n??) (2) @(n°") Which of the following is/are true? 81: A sorting algorithm is in place if the relative order of common element is maintained after sorting $2 : Asorting algorithm is stable if it requires very litle additional space besides the Initial array holding the elements that are tobe sorted, (@) Only St is tue (©) Only S2is tue (6) Both $1 and $2 are true (6) Both $1 and S2 are false G2MBDE ERSS PRES 2075) Algorithm Q8 For insertion sort on 3 elements consider the (@) Only 1and 4 (©) Only 1, 2and 3 following decision tree (©) Only3.and5 (0) Only 1,3 and 4 ere eee Q.11 Consider an array consisting of the following elements in unsorted order (placed randomly), but 60 as first element 60, 80, 15, 95, 7, 12, 35, 90, 55 Quick sort partion algorithm is applied by choosing first element as pivot element. How many total number of arrangements of array integersis possible preserving the effect of frst Each internal node i labelled by indices of array pass of partition algorithm. elements from their original positions, Each leafs labelled by the permutations of orders that the algorithm determines. Fill in the blanks for I, 1,11, 1V (2) ,<1,3,2>, (8:2), <3,2 1 — - (©) <1,8,2>, <8, 1,2>, (2:3), <3,2, 1 Files [fi [fe [#3 [4 (6) <3,1,25, <3,2, 1, (9:2), <1,3,2> Number of records [20 [27] 22 [23 (6) <1,3,2>, 8,2, 1, (2:9), <3, 1.25 Q.12 Find the number of comparisons that will be needed in worst case to merge the following sorted files into a single sorted file by merging together two files ata time? Q.13 Merging 4 sorted files having 200, 80, 128, 26. Q.9 Give the result of partitioning the keys. records wil take 0 (-) time? eae ae Q.14 You want to check whether a given set of items after the 1 pass of quick sort, Choose the last is sorted. Which of the following sorting methods elements as pivot element (R). Also. for will be the most efficientifitis alreacy in sorted duplicates, adopt the convention that both order? pointers stop (@ Bubble sort (b) Selection sort () EHIOCOIERRUSSVTS (©) Insertion sont (d) Merge sort (b) EHISCOIERRUSOVTS () EHIOCOUESRTSSVTR Q.15 Amachine needs a minimum of 100 se0 0 sor (OEHIOOCIERR US are (000 names by quick sort. The minimum time needed to sort 100 names will be approximately .10 Which of the following cases is definite to give (@ 50.2sec (0) 67sec O{nlog 1) time complexity always for quick sort (©) 72.7 sec (@) 11.2800 to sort the array AIO... for any set of values of Q.16 Ifoneuses straight two-way merge sort algorithm tosortthe following elements in ascending order 20, 47, 15, 8.9, 4, 40, 30, 12, 17 array A Case 1: Choosing middle element as pivot. Case 2: Choosing pivot element as (29)" element initially followed by (2")" element, followed by then the order of these elements after the 2nd. (22) element of array A and so on, pass of the algorithm is Case 3: Choosing median element as pivot (@) 8,9, 18, 20,47, 4, 12, 17, 13, 40 Case 4: Choosing the pivot element randomly (b) 8, 15, 20, 47, 4,9, 30, 40, 12, 17 from the array A, (©) 15,20, 47, 4,8, 9, 12, 30, 40, 17 Case 5: Choosing pivot such that the array is (6) 4,8,9, 15, 20, 47, 12, 17, 90, 40 partioned into almost two equal subarrays. Wimaan (Smabe ensy Objective Practice Sets | Computer Science & IT Eee) FIMAGE EASY Q.17 Which of the following is the average no. of key comparisons done by sequential search in the successful case? (@) (n+ 1/2 (b) 2 (©) (nt) (@) 20 Q.18 Given an array A of elements: 80, 70, 55, 90, 71, 72, 73, 77, 52, 50 What will be the autout aft merge sort? pass of 3 way EEEEIIEN oivide ana conquer 1 © 3 & 4 © 5 | 6 &) 14. ©) 15. (b) 16. (b) 17, @ 18 le and Conquer [ (0-1) Comparisons — TO , jo 7 Total comparison: (@) 70, 80, 85, 90, 71, 72, 73, 77, 80, 52 (©) 85, 70, 80, 71, 72, 90, 52, 73, 50, 77 (©) 50, 52, 55, 70, 71, 72, 73, 77, 80, 90 (@) 85, 70, 80, 71, 72, 90, 52, 73, 7, 50 Q.19 Given an array of n elements, each of which is at most k positions from its target postion, if merge sort is used to sort such an an array whats the time complexity? (@ O(nteg n) (0) nlogk (©) nxk (a 7 @ & & % @ 10 © 824 = op? [n= 18] 24 = 6 18? ost Substitute c, = tinequation (1) oy mlog n = 2048 [o,=1] nlogn = 2088 Put n= 2k 2K = 2048 (2) The value of K = Bwhich satisfies equation (2) Vivek’s file size = 2K = 2° = 256 (b) Merge sort and Heap sort have O(n log n) time. complexity. (c) Toy 71(8) +k? Using Master Theorem [n°%” > kr?] 2. (258) Best case of quick sort is C{nlog n) andittakes Ta) = O(n") 2048 msec. te ee (1) To find the first string, algorithm is similar to Worst case of quick sort is O{r?) and it takes Maxtin algorithm using divide and conquer ae To) = 2T{nl2)+ fn) [EE objective Practice sets GgmaDE EF Deo O93 MADE EASY {O(n) Is to find the first string from any two subproblems} Tin) = Olnlog n) 6. (b) Te) = Qifn 1ifn 3.Tan’s) + 2:23 ere) [Using Master theorem] 7. () Stable Sorting : A sorting algorithm is stable i the relative order of common element is maintained after sorting, In-place sorting : A sorting algorithm is inplace ifit requires very litle additional space besides the inital array holding the elements that are to be sorted, For Ex. Quick sorts not stable sorting butit isin piace sorting 8. (b) EPE Ai) AB) AB) jaa combate. A 10A2) itesy then remain as it is and compare y and z i. Ale} 0 A[3) Else compare xto zie. [1] to Al3] ‘Similarly apply the insertion sorting operation. 9. (a) THIS COURSE 15 OVE@= {pot THIS COURSE IS OVER= f i stop 'as youfound stop jas you found at) pivot all pivot it jthen swap a{i] and pivot € r FAIS COURSE IS OVER (ici) i i wuwmadeeasy.in Postal Study Course EXIF] 10. W. 12, (made easy Algorithm | 73 ° s EHP COURSEISAVTR

) i EHIOCOIERRUSSVTS © Only case 3 and § ensures O(n log 7) time complexity always. Case 1: Choosing middle element doesn't mean that pivots location isin middle, after the partition algorithm. In worst case pivot may go either fist cor last location (O(r?). Similarly Case 2, Case 4 Goes not guarantee O(nlog n). But cases 3 and 5 ate similar and makes sure that pivot always goes to middle location, after the partition algorithm, (720) ‘We have to choose first element as pivot. Here 60 js given as frst elemont. Attar first pass, the pivot element goes to its exact location. Here 60 goes to 6" place. Allthe elements less than 60 go toleftof 60 and all the elements greater than 60 go to right of 60. Alter 13 pass s 18 7 12 38 69 80 9 95 os “SS s 3 = 51x31 = 720 possible arrangements. (169) Given fil 20.21, 22, 23) 41-1 1, 22.23) 45-1 40 comparisons 44comparisons 85 comparisons 40444485 = 169 Objecive Pcie Sts 13. 14, 15, 16. GF obiective Practice sets | Computer Science & IT (400) ‘Two sorted file of size mand n take Om + 1) time for merging so total time = 200 + 50 + 125 +25 = 400 (c) Insertion sort in best case = O(n) land bubble sort = O(r?); selection sor merge sort = O{n log n) (b) In the best case quick sort algorithm makes ‘nog (n) comparisons. So 1000 x log (1000) = 9000 comparisons, which takes 100 sec. To sort 100 names a minimum of 100 (log 100) = 600 comparisons are needed, This takes 100 x 600/900 = 6.7 sec. (b) Pass as we GD GD ED oe S a w i A 1207 Hence (b) s the correct option Ered 2078) 17, 18, 19, QgMabE ERsy 3S mMApdeE EAsy (a) In linear or sequential search the maximum no. of comparisons are (n) for n elements hence the ‘no. of comparisons when the elemento be found the middle of the array n+ 12 if nis odd] = 12 [if nis even] in general average cast Hence (a)/s the correct option, @) 3 way merge sort will combine 3 subarrays at a time, After division elements becomes, ae f 7055 wT 2 13 77 82 50 fei ded tea d 5) 70/60) [71|72| 00) (s2|73|77] [50 (b) Divide the elements into nik groups of size k and sort each piece of Ok log 4) time. This preserves the property that no element is more than k element out of position. Now merge each block of k elements with the block to its lft. wiwmadecasy.in u Tray Q.1 Assume that letters p, 9, r, 8, tand u have probabilities respectively. What is the average length of the message using Huftman's coding? Q.2 Consider the following graph STEEL Greedy Techniques message A, B, C, D, E.Theleft and right branch of the tree is interpreted as 1 and 0 receptively. The distance of the external node is counted as bit sequence in decimal from the root node. Then, what will be expected decode time if message transmitted is DBACABEED? = On ‘a Oc e “D (@) 9 units (©) 3t units (©) 45units (@) 68units Find which of he folowing edge isnever usedto 8 Which of the folowing is tue about Huffman ‘compute the shortest path from single source(M) etman cading mey become k 7 to every other vertex using dijkstra’s algorithm. (@)_Hutfman coding may become lossy in some ea Gen 1 en como coestn (©) (A, 8) (SY) luffman coding le Is prefix of any ather code Q.3 Find the running time of dikstra’s algorithm on (6) Huffman codes may not be optimal lossless complete graph of r-vertces. codes In some cases. @ an (b) OU?) (@ Allotthe above (©) OrPiogn) ——_d) Or) Q.7 Find the maximum possible value for fractional Q4 Consider the following instance of knapsack Knapsak problem? Problem. tem [ato elale h 7 Wem [ [2 | [fm | Weight [3 |S] 2| 1] 12/1) 9|o]4| 1 Pott [ts [wa] 9 |e [7 vaue [7b w0 3 [3 [ae] vol val ts |e [Weal 2 [sts tate Knapsack capacity (Wi) = 20. The maximum weight of 12 is allowed in the QB Given a set A=(A,,A,.... Aj of a acivities knapsack. Find the value of maximum profit with the optimal solution of the fractional knapsack problem, (@) 31 (0) 402 (© 485 (2) None of these Q.5 Consider the following decode tree (Called Huffman codes) corresponding to codes for with start and finish time (Sif), 1 A, MtoD:M>A, 94,9830 Mto$: MA, 9A, 9S Mto E: MA, >A, > S30 E, ao Mio Y: MA, 34, 989036, 3¥ : (S, ¥) Edge not used in any shortest path. Ws P = 485 MADE ERSe wr madeecsyin (EY objective Practice sets [Sa MADE EASY AaO B+10 = 2 C3110 = 6 D+1110 = 14 Est = 15 expected decode time Sadi frequency of message m, istance from root to message ™, the given sequence is DBACABEED e Sige, = 20+2+ 14+ 18) +6 =65unis 6. (b) It is never lossy and it is always optimal. The codes assigned to input characters are prefix coda ie. the codes are assigned in such a way that code assigned to one character's not prefix of code assigned to any other character. wuwmadeeasy.in Peet eatin) 2018) 7, MADE EASY Algorithm | (46) Select all of items a, 0, ¢, j and select 1/3 of item g. 1 Beini2et+ gx Total profit=7 +3 +26 +4 #1/3 x 18 = 46 (c) Sort A by finish time 20 Total weig For Do WH SiS) S= SUIAi) Jet Then = 0(nlog n) cca eS a4 a2 as a4 as ‘The number of spanning trees of an undirected complete graph with 7 nodes is _ Number of undirected graphs (not necessarily connected) can be constructed given set 3, 4) of 4 vertices, are Consider the following statements L_ For every weighted graph and any two vertices $ and , Bellman-ford algorithm starting at swil always returia shortest path tot IL At the termination of the Beliman-ford algorithm, even i graph has negative weight cycle, a correct shortest path is found for a Vertex for which shortest path is well-defined. Which of the above statements are true? (@) onlyt (0) only tt (©) bothiandit —(¢) None of these Suppose that minimum spanning tree of the following edge weighted graph contains the edges with weights x, yand z What is the maximum value of x + y+ 2? Suppose that you are running Dikstra’s algorithm, con the edge-weighted digraph below, starting from vertex A. a6 az era Graph Based Algorithms The table gives ‘Distance’ and ‘Parent’ entry of ‘each vertex after vertex E has been deleted from the priority quoue and relaxed, Vertex Distance A 0 | 8 2 | c rm | o 23 e | oa ro | 7 Ge | se | fives een E What could be the possible values of x and y? (a) x=11, 7 (b) x=8, 12 © r=6y=10 (r= 10ya14 Consider the following graph Which ofthe following is NOT a depth frst search traversal of the above graph? (@) ACFHEBDG —(b) ACFHGEBD (©) ABDHFCGE — (d) ABDEHFCG Consider the following graph: as as G3MACDE EASY Postal Study Course BI}E| Algorithm | 1 ‘Which doesnot represent depth fistsearch (DFS) Q.11 Consider the following graph G. sequence for the above graph? (@) 12475386 (b) 12685437 (o) 12453867 (@) 12458637 Which of the following statements is true? (@) Adding a constantto every edge weightina directed graph can change the set of edges that belongs to shortest path tree. Assume unique weights. ‘Adding a constant to every edge weight in an undirected graph can change the set of ‘edges that belongs to the minimum spanning tree (© Rerunning Dijkstra’s algorithm on a graph V times will result in the correct shortest paths tree, even if there are negative edges (but no negative cycles). (@) None of these ©) Which of the following statements is invalid? {@) Agraph where all edge weights are distinct can have more than one shortest path between two vertices, (©) Mutiplying all edge weights by a positive umber might change the cost of minimum, spanning tree. (©) Both (a) and (b) (6) Neither (a) nor (b) Q.10 Assume that G be a graph with (v, 6) where vis the set of vertices and eis the set of edges in G. ‘Assume that E,, be the cost of edges of shortest path from Mo S, which is computed by Dilkstra’s algorithm. The graph Gis given below. Let Ena, is the cost of all edges which show shortest path from Mto every other vertices. What is the value of E,4,— Eg? www.madeeasy.in az a3 famaoe Enss Modified DFS on G applied as follows: © Starting vertex is ‘p’ ‘+ Vertex is visited based on alphabetic order. + Vertices are visited in order p, qr. t ¥ + ttworks same as DFS except the visiting order restriction What is the number of back edges during the above DFS traversal on G? (@) 2 3 4 @s Find the number of strong components in the following graph. Strong component is the maximum subgraph whichis strongly connected. Union of all strong components should contain all vertices of given graph @1 @ 3 (0) 2 (2) None of these Consider the following statements: Sj: DFS of directed graph always produces the same number of edges in the traversal irrespective of starting vertex. Sz It we remove all of back edges found while DFS traversal on directed graph, the resutant graph is acyciic Find which of the following is tre. (@) Both S, and S, are valid (0) Only, vali (©) Only S, Valia (@) Neither 8, nor S, is vai Objeaive rece St ) | Computer Science & IT ei 20is|] GIMADE EASY Q.14 Consider tne following graph. ‘Assume that node ‘S' is the starting vertex for prim’s algorithm. Which of the following can be the correct order of edges in which they are added to construct the minimal spanning tree? @)4,2,1,6,.8 — (0)4,1,2,6,8 (©) 4,2.4,7, 10 (d) None ofthese Q.1 Choose the invalid statement from the following? (@) Aggraph can have more than one shortest path between two vertices. (©) Agraph where all edge weights are distinct can have more than one shortest path between two vertices. (6) Multiplying all edge weights by a positive ‘number might change the graph's minimum spanning tree, (2) Both (b) and (c) Q.16 Assume a graph Ghas negative weight edges: Which of the following statement is correct when the dikstra’s algorithm is applied on G? (a) Always produces correct resuits (b) Always produces incorrect results (¢) It always terminates but may produce incorrect results (0) None of the above Q.17 Consider the following graph G. What is minimum distance from Sto D? @s (©) 16 @s9 (@) 15 (BF ovjective Practice Sets .18 Consider the following graph G. cp Pertorm a DFS on G starting at vertex A and selection of adjacent vertex in DFS decided by the lexicographical order. In graph G, Band D are adjacent to A. Fist it selects 8, because B comes first in lexicographicalorder Find number of cross edges when the given DFS is performed on? @1 (2 3 @4 2.19 Let the weight of minimum spanning tree of given graph Gas per Kruskal’s algorithm is, and as per prim algorithm is w,then (2) Wie Hp (0) We2 Hp (C) We= Wp (@) Any of the above is possible depends on graph structure Q.20 Which ofthe following are the articulation points @ {8) el awa Ne (a) Onlyc (0) O.C.F () BCF (0) None of these Q.21 Given the graph (starting with A) (SMADE EASY Ee 2076] Algorithm | What is the cost of minimum cost spanning tree. ‘Assume prim’s algorithm and kruskal’s algorithm (a) 195 (b) 150 are executed on the above graph to find the (0) 145 (¢) 140 minimum spanning tree. For a particular edge (2) whichis included in minimum spanning tree .22 Consider the folowing graph G. and the position of an edge in minimum spanning trevis denotedby &,,. Where 15, <8 (where position defines the order in which edges are included in the MST). Then whats the maximum, vau0o (@ns~ Cot Modified BFS traversal on G applied as follows 25 Consider the undirected weighted araoh in Fig © Assume starting vertex is ‘a The minimum cost spanning tree for this graph ‘+ Atany evel (breadth), vertices are visited in has the cost, alphabetic order. ‘© In-above graph G, alter visiting vertex ‘a’, the order of vertices visited in G are c,d and. ‘+ Itworks same as BFS except the restriction on order of vertices visited. Which of the following is not across edge during given BFS traversal on G? (a) {d,c) (b) {, b} (©) {8,0} id) fe.) Q.23 Consider the following graph: (@) 18 2 (c) 24 Oz Q.26 Consider the following graph. Omen) Whats the depth rst search (OFS) sequence forthe above graph? ere nee een Assume node Si the starting vertex for prim’s O25 2467 42156387 algorthm, Wich of the folowing can be the correct order of edges in which they are added ‘9 construct the minimal spanning ee? (@) 4.2,1,6,8 ©) 41.268 © 4217.10 (d) None of these Q.24 Consider the weighted undirected graph below womacacsin (MADE Ese objective Pracice sets | Computer Science & IT Q.27 Consider the following graph G 2 Ze = a bes a ae Find the number of minimum cost spanning trees Using kruskal® algorithm or prim’s algorithm, @3 (5 @7 ws [EEIIEN craph based algorithms 3. (b) 5. (b) 6. (d) 7. () 8. 14. (a) 15. (c) 16 (c) 17. @ 18. 23. (a) 25. (b) 26. (a) 27. (b) 28. GESEEEII creph Based algorithms 1. (16807) Number of spanning trees for an undirected complete graph with nodes n= 2 2. (64) ‘As number of undirected graph is = 2(°° 3. (b) I: Ifthe graph contains @ negative weight eycle then no shortest path exist. Ii: If shortest path is well defined then it contains at most V-1 edges. Running V-1 iterations of Bellman ford will find the path 4. (25) xsi ys6 758 (GJ objective Practice sets & Postal Study Course EUAE| MADE EASY fameoe easy Q.28 The depth first search algorithm has been implemented using the stack data structure. One possible order of visiting the node of the fol graphis ving (2) ADBCFE (©) BACDFE () ACEBED (@ BCEFAD @ 2 © 1. © 12 © 13 © (>) 19. © 20. ) 21. (© 22 () @ 5. (b) 15 23 7 13 23 1 36 y+ Gnen>E| =~ 13 23 7205 Compare above table with given table xett = 19 x=8 ye7 2xett y+7 2 8+11 >= yz 6. (d) ABDE HFGHis not OFS traversal wurwmadeeasy.in G3 MADE EASY 9, DFS safe sequence o@——®. @ 4.103 not folw DFS s0 nat DFS sequence because have togod to 7 fest then 7103, 10. o@ 11. DFS tle seqvoce 12, 8. (a) ‘Adding a constant to every edge weight in a directed graph can change the setot edges that belongs to shortest path tree. Assume unique weights. G2 MASE Enay wwwmadeeasy.in Postal Study Course BIRD) Algorithm | (b) (Graph can have more than one shortest path between two vertex where all edge weights are distinct. cos=6 oe (ii) Multiplying all edge weight by a positive number should not effect cost of minimum spanning tree. So option (b) is invalid (81) Minimum spanning tree by using Dijkstra’s is Eng = 042414346474 948410412 = 58 Ego t+S4127 Value of Eng, Exp = 58-7 = 51 ©) Apply the given DFS Tree edges are: (p,q) (4.1%. (¥ 8h (8. Back edges are: (a, (tv) (8, ah (2 ©) ‘There are three strong components 1. GA 2 ETS 3c Option (c)is correct Objective Practice Sets 13. 14, 15, 16. 17. 18, is} | Computer Science & IT (e) &: @—-O©——-© Staring wih Avil gst2 e068 Staring wih Burl get ony edo Staring with Cul getno edge DFS on drecied grephmay rcv same numberof edges 4 Removingallbackedges makes th gph asanyle 505, valid catement Option (c) is correct (a) re ) © Starting vertex is $ Selecting of edges in MST: 4, 2, 1, 6,8 “- Option (a) is correct. ©) Mutipying all edge weights by a positive number does not change the graph’s minimum spanning tree. Option (c) is not valid (ce) Dikstra’s algorithm always terminates after [E| relaxations and IVI + EI priority queue operations, but may produce incorrect result. So, option (c) is correct. Minimum distance =3 +1+3+1=8 (b) = 2Cross edges in G, 1 Forward edge in G, 1 Back edge in G . # cross edges = 2 Objective Practice Sets Postal Study Course PLEE] 19. 20, a4 22, (emace ensu GaMApDE EASY (c) There will be unique minimum cost for any minimum spanning tree in a graph irrespective of algorithm applied. i. more than one mirimum Cost spanning trees are possible but their cost willbe same, (4) In a connected graph, a vertex v is said be an articulation point it by removing that vertex together with its edges the graph become disconnected. In the given graph there is no articulation point, at 5 +104 19+ 20425 +30+40= 145 () Given BFS traversal on Gis JN oe Tree edges are:la, o}, (a, ch, {a €). 10, 6}. (0. Cross edges are:{d, c).(e. b), (2. ff, b)itie| = [d, blisnota cross edge. wwnmadeeasyin MADE EASY ERAS RM 2075] Algorithm | 24, (2) Kruskal’s algorithm: AE, AG, AB, CE, Fl, FH, CD, CF Pris algorithm: AE, AG, AB, CE, CD, CF, Fl, FH max|p rns ~Cp stars] = 15-71 =2 25. (b) Using the Kruskal's algorithm 0@ @ @——® ) O—@ \ ” @ E © All above trees MST with cost: 21 + 22 + 22+ 23423=111 28. (0) Minimum cost is 20, 26. (a) BCEFA Dis conect depth frst search order. Starting vertex is S Selecting of edges in MST : 4, 2, 1, 6,8 wwewmedeeasyin pMaDE EASY Objective Practice Sets 6 rr) a4 a2 as a4 Consider the following problems. 1. Longest common subsequence 2. Optimal Binary search tree 3. 0/1 knapsack problem 4. Matrix chain muttiplication Which of the above problems can be solved using dynamic programming? (@) tand2only — (b) 2and 3only (0) 1,3and 4 only (d) 1,2,3and 4 Consider the following knapsack problem with capacity W= 8 tem | Profit | Weight i 13 i Ie 8 5 aaa 3 Te 3 4 Which of the following itemis not selected in the optimal solution of 0/1 knapsack problem? (@) fonly (b) L,only (©) fyonly (d) None of these Consider the following strings x and y. How many longest common subsequences are possible from x and y? Given two sequences A and B. Let MA, B) denote the number of tines that A appears as subsequence of Bie. sequence ab appears 4 times as a subsequence of aebabdb. Let A, denotes the fist /characters of string Aand Afi] denote the # character. Which of the following will computes the recurrence relation C(4, 8)? enue Dynamic Programing as as a7 ni=0) it> 0nd (@) CA, B.A # BY) OA, 8.) + OA, 8, if Al = Bb (©) O14... 8):i1 All = BL OA, B..) + OA By (©) CA, 8.) it All = BU OA, 8.1) + 1A, 8, if All # Bb (@) None ofthe above ): iF Al = BU] Consider the folowing matrices with cimensions Ayie4x6 A,66x8 A,6Bx4 Ayi64x Which ofthe folowing mutipication order gives optimal solution? () (AADA)A, (0) (A, (AAA, (©) A(AAIAD (A, AMAA) The dlference between maximum possible profit for 0/1 Knapsack and fractional Knapsack problem with capacity (9) a[e[7 Tp] s0| © 3 [26|19[ 1617 tem [@ [>] e Woon! 3/5] 2 Proft [7 [10] 3 Consider the following adjacency matrix ‘D' for represented the directed graph with dist (costs) between every pair vertces, AgC 032 ito 2 410 ° ous G3 MADE EASY if warshall’s algorithm is applied on ‘D’ to compute the shortest distances then following resultant matrix is obtained. ABC afo 3 2 o=8]1 02 cl2 10) If the edge from B to C in the given directed graph is removed then how many shortest distances will be changedin 0"? Q.8_ Whatis the running time of efficient algorithm for the finding the shortest path between two vertices ina directed graph? Assume that all edges are having equal weights. (Vis set of vertices and E is sot of Edges) @ lvl (0) [vlog let (©) Olv+lEl) —(@) Noneofthese Q.9. Match the following groups Group-1 ‘A. Divide and conquer B. Greedy approach ._Dynamic programming D. Back tracking Group-2 1. Longest common subsequence 2. Huffman codes 3. Quick sort 4. Chess game Codes: A BC D @1 23 4 3 1 2 4 @©3 21 4 (6) None of these QMO Let X= and) =0) 28) C4, 8. a Bi wn lovers ae ot ath 1A, 8.) + CA) HAL BL homes cached 5.) (2) (A, AjAy A,reauites (4x 6x8 +4%8x 444 x45) = 400 multiplications (0) (A, (A, AD)A, requires (6 x 8x 4+4x6x 444x 4x5) = 368 mutiplications wwmmadeeasy.in MADE ERS! (©) A,(A,Ay) A) tequires (6 x8x4+6%x 4x 5 + 4x6 x 5) = 482 muttiplications. © (AANA, A) requires (4x 6x8+8%x 4x 5 44x8x5) = 512 multiplications, 6. (3) Fractional Knapsack problem: Select all of tern'a‘d,'e','/ and V3otitem’s’ 8. Total weight = 341412414 1/3x9=20 Total profit = 7+3+26+441/3 x 1 0/1 Knapsack problem: Select all of item j, d, a, eand c. Totalwoight = 2414143412 Total profit Difference = 19 7434+26+4+3=43 Total profit (using fractional knapsack ~ Using 0/1 Knapsack) = 46-4323 ajo 3 Bly cls sooo Onno ” aBgc Afo 3 2 _ 7 = plio2 cl2 10 Atter removal of (B, C) edge, the graph is: 13. (emace easy www.madeeasy.in Postal Study Course BEEE| 10. Algorithm | 2: ABC ABC Afo 3 2 ajo 31 =D,= 8|1 0 «|0,=8|1 0 3 clz 10 cl2 1 oJ = Blo Cshortest path distances are changed. (c) Using BFS by treating all edges as unweighted, ittakes O(VIsIE!) time (o) Divide and conquer: Merge sort, Quick sort, Binary search, etc Greedy approach: Hufiman codes, Dikstra’s, Kruskal's Algorithm, etc Dynamic programming: Longest common subsequence, All pairs shortest Paths, etc. Backtracking: Chess game, 8-queens, Checkers, etc. () Time complexity of LCS = O(mnn) LCS computed as following and race back ater. fo oorj=0 ati d= fe tf tter ro |maxctii-thott) « Waeyi)>0 (b) bobais one possible LCS of Xand ¥ LCS can be computed using dynamic programming, (b) Mi ih inf, +. option (b) is correct. (a) Finding closest pairs can be solved in O(n logn} time using divide and conquer approach, Te) = 272) + O(n) = Ofriog n) Objective Price Soe 14. (b) | Computer Science & IT Algorithm: Divide and Conquer: Step 1: Divide the sot intotwo equal sized parts by the line 1, and recursively comoute the distance ineach part [o, = closestoair (lethal); 4, = Closestpair (right haf});] and returning the points in each set in sorted order by y-coordinate, Merge Procedure: [0(1)] Step 2: Let ‘a be the minimal of two minimal cistances. min (a, 02) [O(n] Step 3: Eliminate points that lie farther than ‘of apart from [0{n)] Step 4: Merge the two sorted lists into cone sorted list. [0(n)] Step 5: Scan the remaining points in the yrorder and compute the distances of each point to its Sneighbors. [0(1)] Step 6: If any of these distances is less than ‘of then update ‘¢. 06) % XQ) # multiplications = 102, which is minimum compared to other options, X_.Xq = # Mutiptcations = 42 XqXs = # Mutiplcations = 20 XXX) = # Mutipications = 30 6.0% Xa) 5x (XeXohx2 = # Multptioations = 10 = 102 Mutiplcations (GE objective Practice sets G3 MADE ERS ESE 20s) GJ MADE EAsy 15. (c) X = gatet23 Ye aget321 Qt, G12, G18, att, at2, af3, got, ge2, go3, act, a€2, 263 Length of LCS = 3 Number of LCS's with length 3 are 12 16. (b) No greedy algorithm exists for 0-1 knapsack problem. 0-1 knapsack problem exhibit optimal substructure property and it has only dynamic programming algorithm. 18. (b) Initialize: TilLmax = 0; Upto_max = 0) Loop for each element of the array (@) Upto_max = Upto_max +ali]; (©) IM{upto_max < 0) Upto_max (¢) (Til_max < Upto_max) TilLmax = Upto_max; return Til_max [ttakes O(n) ime complexity and O(n) space complexity in the form of table. wwwmadeeasy.in

You might also like