You are on page 1of 18
2020- WOFKDOOK @| Detailed Explanations of a Try Yourself Questions Computer Science & IT Algorithm, Data Structure & Programming hi Asymptotic Analysis of Algorithms and Recurrence Relations Detalled Explanation 9 of Try Yourself Questions Deed (a) Ar) = 2p), af) = O("), h(n) = @(n) Then [f(n)- g(r] + hin) fr) = 20) @. f(r) should be anything greater than or equal to‘ lets take n, a(n) = Ofr) 8. g(n) should be less than or equal to ‘r’ lets take 7. Hr) = on, . h(n) should be So f(r) g(n)] + hin) [n-nj+n = On? + On=2(0) nly comment about lower bound. Upper bound depend an the g(n) value ie. né, n?, rf (b) find (int n) { Here if (<2) then return else { sum = 0: for iS4; i+4) find(n/2); > O(log n) for (i= 1; isn" 1; i++) > OlV?) sum = sum +1 } wwwmadeeasypublications.org MADE ERSY © Copyright ) MADE EASY Workbook 3 Since first for loop run 4log n times for which second for loop run n? times for every value of (4 log n) So total time complexity = O(4log nx r?) = O(r? log 1). E Bey The increasing order of given five fuctions are: f,< << fy < fy O(n loglog n) int funt (int n) { int, jk, pq = 0; for(i=t;fem ++i) First loop { p=0; for (j= mi j> 1: f= 12) I Second loop +4: for (k= 1: k< pi k= K2) Third loop Ho return ) First for loop will run mtime since value of fis incremented by 1 every time. Second for loop wil run log atime for every first loop. Third loop will run loglag nntime for every second loop. So, value of qis n (loglog n) and time complexity is also O(n logiog n) (© copyright MADE ERSY wowmadeeasypublcations.org Divide and Conquer @ Detalled Explanation eet Try Yourself Questions Prod (b) max-heapify (int AL ], int n, int i) { int P, m Pei while (2? < n) for checking whether left child is present if (QP + 1$n&&Al2P + 1] > Al2P}) for checking if ight childis present or not and finding between left and right child which is greater. n= PH else m= 2°: whichever is greater, swap that child with its parent if(AIP] < Alm) ‘Swap (A[P], A[m]} Pam: } else return , www.madeeasypublications org MADE EASY © Copyright ) MADE EASY Workbook 5 Pome O(n) The length of array Ais n which stores the integers. We need two additional arrays B[0...k] and C[0...K}. Initialize B and C with 0. It requires O(k). For each element of A increment B[A[i]]. It will take O(n) time BLj] contain the number of elements of A having value j Do C[1] = BL1] and for each element fof array C do Cli] = Bl] + [/- 1] twill take O(4) for getting answer compute C[b] - C[a] + Bla The preprocessing time takes = O(n) Poe rny O(n logk) List Lista ist List [Seed] [Sorted] [Sonod] [Sentea] nikelements nikelements nik elements rik elements ‘Sorted list of r-elements (i) Remove the smallest element from each list and build min heap with k-elements = O(K) (ii) Extract the minimum elements trom this heap that will be the next smallest in the resulted list => O(logh). (iii) Remove the elements from original list where we have extracted next smallest element and insert into the heap = O(logk). Repeat step? and step3 u all elements are in the resulted list, = O() + [O(logk) + Oogk)] + O(n) = O{nlogk) Pere O{mk + mlog (m/k)) Insertion sorttakes O(A?) time per k-element list in worst case. Therefore sorting nik ists of k-element each, take O(Knik) = O(nk) time in worst case. = MADE EASY worimadeeasypublications org 6 Computer Science & iT © Algorithm, Data Structure & Programming MADE EASY (c) Insortion-sort (A) { for j 2 to length (A) key ALj] i= fl while (7> 0&8 ALi] > key) Alt 1] ALi] } Ali+ 1] < key; ( wwrwmadeeasypublications org MADE ERSY © Copyright | Greedy Technique and Dynamic Programming Detailed Explana of Try Yourself Questions (7) Kruskal's algorithm: AE, AG, AB, CE, FI, FH, CD, CF Prim’s algorithm: CF, CE, EA, AG, AB, Fl, FH, CD (since we have to find maximum difference so start with edge CF) max|(¢q eins “(€q Mranaro] = 18-11 =7 Penna [o(m)} Kruskal's algorit (Sorting => O(m log m) (ii) Union =» O(nlog n) (iii) Find = O(mlog n) > Running time = O(mlog m) Now edges are already sorted Running time = O(m) rod (lvl) Let ¢ be an edge of G but not in T () Run DFS on Tufe} (ii) Find eycle (iii) Trace back edges and find edge o’ thus has maximum weight, (i) Remove o' from T u (0} to got MST In TU{e) =» Number of eges = Number of vertices Running time of DFS = o(IVI+IEI)=o(IVI) © copyright MADE EASY wonwmadeeasypublications org 8 Computer Science & iT © Algorithm, Data Structure & Programming MADE EASY Poesy Gis the connected graph with n- 1 edges =» G don't have any cycle. Therefore statement 1 and 3 implies 2. (1500) (AAAAO das CADAAD AAgAsAd — ‘Gox2 ‘aoa Git wu wo x3) “oro ons) 8 ‘u0%8) 50 10) 150 The minimum number of multiplication required using basic matrix multiplication method will be 1500. a ( wwrwmadeeasypublications org MADE ERSY © Copyright | Linked List, Hashing, Stack, Queue and Array Detalled Explanation of Try Yourself Questions Implementation of stack using single linklist Inserting sequence: 1, 2, 3, 4,5, 6 Ingertion take 0(1) time Link list representation: oi7] 9BO-2D-G14 6. Insertion takes 0(1) time Deletion in stack (Pop) Remove top element every time so 0(1) Deletion in linklist Remove 1** node every time with making second node to head LIRLIFLIF IH Head ‘enqueue operation takes O(1) time dequeue operation takes O(n) time [visits last node] © copyright MADE EASY wonwmadeeasypublications org 10 | Computer Science &T © Algorithm, Data Structure & Programming MADE EASY Pome ) PUSH (5, P, Q, Tx) “Gon printf (‘stack overflow") exit (1) else T+ st (ox He ') indicate the last location of the array is already filled. So overfiow occur. (a) Number of push operations = n(insert) + m(delete) So, n+ mx but there are maximum 2n insert operations so n+ mSx<2n Number of pop operations = +m But there are 2m delete operations which are less than no. of pop operations, hence: am =0(n) Time complexity = 0{n)x O(7) = OtrF) Dies [0(1)] main() { inti=3: switch (i) (default: print(‘zer0") Case 1 :printi(“one") break Case 2 : print("tw0") break Case 3 : print(‘thres") break } Since i = 3 so switch (3) will go to case 3 and run the program only one time, So time complexity = 0(1) © copyright MADE EASY wonwmadeeasypublications org 16 | Computer Science &T © Algorithm, Data Structure & Programming MADE EASY Pome 4. Const int*P. declare P as pointer to const integer. 2. int* const P; declare P as constant pointer to integer Piers (i) Char (*(*x ODO; declare x as a function returning pointer to array of pointer to function returning char. (i) Char (“Cx13))() [8]. doclare x as array 3 of pointer to function returning pointer to array 5 of chat. (iti) Void (*btint, void (*#)(int))) (int) Syntac error (iv) Void (*ptefint (9121, int(*(void)); Syntax error Por (b) Char\o (0) Printf(% $", a) = Null = 0 So condition false So answer is else part string is not empty. Pome (a) Since variable d of integer type is static so memory is allocated to it compile time only and same memory 's used every lime. Therefore, every time old value of d (which is update in previous iteration) is used. So, outputis 312213444, www.madeeasypublications org MADE ERSY © Copyright | MADE EASY Workbook | 47 (d) 31] globaly initialize 1. 43) ye 1 2 x2 xea +2 (here ais taken from global variable) Print); = 4 3, Printi(a) = 4 since dyanmic scoping is used, So, take value of inner variable ‘a So answer will be 4, 4 (c) Take random value of Xand Yi.e., X= 5 and Y= 3. Option ( (res+ a)” = (res * x)” (1x5) = (1x 5)8 After iteration 1 res = 5:4 5 Y=3 (198+ a)’ = (res * XP =(5 x 5)*= (5 x5)? 15625 # 625 So, case fail. Option (b) cannot be answer, Option (4): XY = (res a)? 58 = (1x5)8= 125 = 125, After iteration 1 res x 125 # 625 So, case fall Option (d) cannot be answer = MADE EASY worimadeeasypublications org 1g | Computer Science &T © Algorithm, Data Structure & Programming MADE EASY Publieatone XY = essa? BP = 1x5%= 125 = 125 Option ( After iteration 1 tes = 5, a=5;b=2,X=5,¥=3 XY = res + a= 59-5 x S¢= 125 = 125 After iteration 2 tes = 25;a=5;b=1;X=5;Y=3 XY = res + abm52= 25 x5" 125 = 125 So, all cases are passes, So option (c) will the answer. Deed fa, 8) sa function contain 2 parameter one contain starting address of array and second parameter tel number of elementin the array. 2 Every time ‘n’ value compare with 1 when itis less than equal to 1 return 0 and stop the program otherwise continue with recursive function call 1. fla,5) *P = a; P pointed to same address pointed by ‘a n=; nvalue greater than 1 So, max (P+ 1, 8-1), 3-8): or max (f(P + 1, 4) ~ 2} 2. P+1,4) “P= P+ 1; is pointed to next element of array Le., 5. n= 4; nvalue greater than 1 So, max (max(f(P+ 1, 4-1), 5-2), -2) or max (max(((P + 1)3), 3), -2) 3. AP +1,3) “P= P +1; P is pointed to next element of array Le., 2. n= 3; nvalue greater than 1 So, max(max(max(f(P+ 1, 3-1), 2~6), 3) -2) or max(max(max((P + 1, 2),~4), 8) -2) 4, AP +1, 2): “P= P +1; Ps pointed to next element of array Le., 6. n= 2; nvalue greater than 1 So, max(max(max(max((P+1,2~1), 2).-4), 8),-2) or max(max(max(max((P+1, 1), 2),-4),3),-2) 5. AP +4, 1); *P = P+1; Pis pointed to next element of array Le., 4 n= 1; nvalue equal to 1 0, return 0. So max(max(max(max(0, 2),~4), 3), -2) max(max(max(2, -4), 3),-2) max(max(2, 3),-2) max(3, -2)= 8 So the value printed by given code is 8. ajsps]2]e 14 Publications —EEEE | wonmadeeasypublcationsorg MADE ERSY © Copyright |

You might also like