0% found this document useful (0 votes)
30 views17 pages

Daa

Uploaded by

Suman Pandit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
30 views17 pages

Daa

Uploaded by

Suman Pandit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 17
Semester: V (Regular) Sub & Code: DAA, C5-3001, Branch (6): ¢SE &1T SS AUTUMN MID SEMESTER EXAMINATION-2016 Design & Analysis of Algorithm [.cs-3001] Full Marks: 25, Time: 2 Hours Answer any five questions including question No.1 which is compulsory. The figures in the margin indicate full marks. Candidates are required to give their answers in their own words as far as practicable and all parts of a question should be answered at one place only. DAA MID-SEM SOLUTION & EVALUATION SCHEME QI Answer the following questions: (xs) a) Consider the following C function. int fun(int n) { if (n<=2) return 1; else return fun(\in); } Represent this function by recurrence and find out the time complexity. Answer: Scheme The recurrence is T(n)=T(n)+1 | Each answer 0.5, No explanation Time Complexity = @(log log n) required T(n)=T(vn) +1 a) Solving this recurrence by change in variable Let m= log n => n= 28 = n!2 => 22 Substituting the value of n in recurrence (1), the recurrence becomes TQ") =T2™) +1 ~(2) let Ten) = S(m) => T(2") = S(m) => T(2™2) = S(m/2) Now substituting this value in equation (2), the recurrence becomes S(m) = S(mm/2) + 1 ~ 3) Recurrence (3) is similar to master theorem, applying master theorem, a=I, b=2, fm)=1 mi'=1, As per master theorem. Comparing m'®*s* and f(m), we found both are same, so it comes under case-2. As per case-2 of master theorem, the solution of the recurrence is S(m) = ©(m"*%Alog m) = @(m!"%*log m) = O(log m) Page 1of 16 Now substitute the value of n back to this eq. (3) ‘T(n) = ©(log m) = O(log log n) b) Consider the following pairs of functions f(n), g(n). Which pair the functions are such, that f(n) is O(g(n)) and g(n) is not O(f(n)) ? A.f(n)=n*, g(n) = nlog(n?) B. f(n)=log(n) g(n) = 10 logn C.f@)=1, g(n) =log n D. f(a)=n? , g(n) = 10n logn Answer: heme Option: C Only answer 1 mark Explanation As per the definition of Big-O, In f(n)=n’, g(n) = n"log(n’) => n* log(n) <10 logn is true and the reverse is also true In f(a)=1, g(n) = log n =>1 n<10n logn is false, but the reverse is true. ©) Which of the following sorting algorithms in its typical implementation gives best performance when applied on an array which is sorted, or almost sorted (at most two elements are misplaced). A. Quick Sort B. Heap Sort C. Merge Sort —_D. Insertion Sort Answer: Scheme Option: D Either answer | mark If sorted is assumed in proper order Option: B and D If sorted is assumed in reverse order Explanatio1 Sorted in proper order (almost) * Quick sort performs worse case time complexity (O(n*)), Merge and Heap performs its best case time complexity (O(n log n)) and Insertion sort performs its best case time complexity that is O(n). Hence Insertion sort will give the best solution sorted in reverse order (almost) © Quick sort and insertion sort performs worse case time complexity (O(n?)), Merge and Heap performs its best case time complexity (O(n log n)) and Hence Merge and Heap sort will give the best solution 4) A priority queue is implemented as a Max-Heap. Initially, it has 5 elements, The Page 2 of 16 level order traversal of heap is as 11, 9, 6, 4, 3. Two new elements 2 and 8 and inserted in the heap in that order. Find out the level order traversal of the heap after the insertion of the elements? Scheme: Only answer : 1 Mark Answer: The level order traversal of the heap after the insertion of the elements 2 and 8 is: 11, 9,8,4,3,2,6 - i | i | @ @ | i © @6® | | | | Insert 2, eg @ 2 ®@ S @® SOS O® Insert 8 © @ ) What is the difference between Divide & Conquer and Greedy algorithm? Schem © Difference — 1 Mark Page 3 of 16 Qn a) © The di * In this approach, the problem is broken into several sub problem that are similar to the original problem but smaller in size, the sub problems are solved recursively, and then these solutions are combined to create a solutions to the original problem, ide and conquer paradigm involves 3 steps at each level of the recursion, Divide the problem into a number of sub problems. Conquer the sub problems by solving them recursively. If the sub problem sizes are small enough, however, just solve the sub problems in a straight forward manner. Combine the solutions to the problems into the solution for the original problem Greedy Algorithm «A “greedy algorithm” sometimes works well for opti * A greedy algorithm works in phases. At each phase: * An optimization problem is one in which you want to find, not just ¢ solution, but the best solution. ization problems i, You take the best you can get right now, without regard for future consequences. ii, You hope that by choosing a /ocal optimum at each step, you will end up at a global optimum. What is purpose of algorithm analysis? Find out the complexity of the given funetion S77) 2* Answer: Scher Purpose of Algorithm Analysis Purpose of algorithm analysis — In computer science, the analysis of — mark algorithms is the determination of Time complexity - 1.5 mark the amount of resources (such as time and storage) necessary to execute them, The purpose of analyzing an algorithm is to discover its characteristics in order to evaluate its suitability for various applications or compare it with other algorithms for the same application. Moreover, the analysis of an algorithm can help us understand it better, and can suggest informed improvements. Algorithms tend to become shorter, simpler, and more elegant during the analysis process. Page 4 of 16 b) Solve the following recurrence. (2.5) T(n) = Vn T0/n) + Assume that T(n) is constant for sufficiently small n. Scheme: * Correct answer with necessary steps : 2.5 Marks * Incorrect answer with some valid steps — step marking (0.5 to 2) Answer: T(n) aT (fm) +n Dividing by n, we get T(n) n Putting n=2", we can get TQ”) T(a™/?) 7 = ga A Let, T(2") S(m) = (2) = oe Now the original recurrence becomes S(m)=S(an/2)+1 This looks like master theorem, soling we can get S(m) = @(log m) Now returning from S to T with n= => m=log n, we have S(m) = @(log m) => 7 = @(loglog n)=> T(a)= @(n log log n) Solve the following recurrence. Qs) T(n) = 3 T(n/4) +n? Assume that T(n) is constant for sufficiently small n. Scheme: * Correct answer with proper explanation —2.5 marks Answer: Tin) = 3 T(/4) +n? This recurrence looks like master theorem form. So a=3, b=4, f(n)= n? n= nbs Sn, Comparing nb s* and f{n) we found f(n) is asymptotically larger than n®,!, So we guess the solution is case-3 of master theorem. Testing of case-3 is done as follows An) =O IPE y=) =O 975") => n?Be N97 =>True for e=1, €=0.25 Now af(n/b)3 x n°/16 True Page 5 of 16 a4 b) So as per case-3, the solution of the recurrence is T(n) = @(f(n)) = O(n") Write a function in C language to arrange the numbers stored in an array as (2.5) follows: odd numbers followed by even numbers, in O(n) time. The prototype of the function is given as void ARRANGE-ARRAY (int [], int); Scheme: * Correct algorithm solved in O(n) time , uses the given prototype — 2.5 marks * Correct algorithm solved in O(n) time , uses different prototype — 2 marks * Correct algorithm solved in other than O(n) time ~ 1.5 mark Answer: void ARRANGE-ARRAY (int af], int n) { int Ib=0, ub=n-1; while(Ib1 elements, we break down the running time as follows: Divide: The divide step just computes the middle of the sub array, whichn takes constant time. Thus D(n) = ©(1) Conquer: When two sub problems are recursively solved, each of size n/2, it contributes 2T(n/2) to the running time Combine: The MERGE procedure on an n-element subarray takes time @(n), and so C(n)= O(n) Now the recurrence for the worst case running time T(n) of merge sort O(n) ifn=1 rr, T(n/2)+ O(n) ifn>l Solving this recurrence by master theorem, we can get the solution as T(a)=O(n log n) Given 12 activities, A=< a1, a, finish time (fi) are given as follow: i[i [2 [3 [4 [5 [6 [7 [8 .ay0,a11,a12 > along with their start time (s;) and (2.5) f [6 [25 [96 [89 | 84 | 62 [17 [70 Use an efficient method that computes a schedule activities on that stage. Scheme: ‘* Description of efficient method such as GREEDY-ACTIVITY-SELECTOR in. largest number of Page 8 of 16 Qs a) terms of language or algorithm or ¢-function — 1 mark * Getting the come Answer: * It uses the following GREEDY-ACTIVITY-SELECTOR computes a schedule with largest number of activities. * It assumes that the input activities are ordered by monotonically increasing finishing time. * Itcollects selected activities into a set A and retums this set when it is done. Write the algorithm for MAX-HEAPIFY(A,i). Illustrate the operation of MAX- solution schedule with proper steps -1.5 marks method ‘GREEDY-ACTIVITY-SELECTORG, f) { netength|s] Aw {ai} ket for m2 ton ‘ iffs(m)=f00) { © ACAU fan} kom + t Return A i a % fi___[ Selection (Yes/No) al 16 17 Vv a2 7 25 x al2 26 57 Nv a6 49 62 x a8 44 70 x all 27 79 x aS 27 84 x ad 44 84 x al 44 86 x ad 83 89 v ald 58 94 x a3 37 96 x The solution schedule is HEAPIFY(A,3) on the array A={27, 17, 3, 16, 13, 10, 1, 5, 7, 12, 4, 8, 9, 0} Scheme: Page 9 of 16 that @5) ‘* Proper illustration through diagrams ~ 2.5 marks Answer: Applying MAX-HEAPIFY at i=3 (7) @ Qa Qo © @@ 0000 b) Write the Insertion Sort algorithm. Analyze the best case and worst case time (2.5) complexity of this algorithm. Answer: Line Insertion Sort Algorithm Cost Times No. 1 INSERTION-SORT(A) 0 2 { 0 3 forje2 to length[A] el a 4 { 0 5 keyAli] 2 nl 6 ‘MMinsert A[j] into the sorted sequence A[1.je1] 0 7 iej-l 3 n-l 8 while(i>0 and Afi]>key) cf Yu 9 { 0 10 Ali+1J—Ali] 5 Sonn Page 10 of 16 Q6 u iit 6 Sun 12 } 0 13 Ali+1}ckey 7 nel 4 } 0 1S} 0 To compute T(n), the running time of INSERTION-SORT on an input of n values, we sum the products of the cost and times column, obtaining ‘Teaymetn + e2a-1) + Bat) + ALM, tH + SLE, (HL) + e6YE CH= 1) +74) Best case Analysi * Best case occurs if the array is already sorted. © For each j=2 to n, we find that A[i] vfi+l]/w[it1]. M is the knapsack size and x[1:n] is the solution vector.*/ { fori — Itondo x{i] <0 1 Initialize x UeM fori Itondo { if (w[i] > U) then break; xfi] — 1.0 UA (U-wiil) } if (

You might also like