Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
30 views
17 pages
Daa
Uploaded by
Suman Pandit
AI-enhanced title
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
Download
Save
Save daa For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
30 views
17 pages
Daa
Uploaded by
Suman Pandit
AI-enhanced title
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
Carousel Previous
Carousel Next
Download
Save
Save daa For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save daa For Later
You are on page 1
/ 17
Search
Fullscreen
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 16Now 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 16level 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 16Qn 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 16b) 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 16a4 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(Ib
1 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 16Qs 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 16Q6 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
DAA-Complete Buddha Series Unit-1 To 5
PDF
No ratings yet
DAA-Complete Buddha Series Unit-1 To 5
126 pages
Advanced Algorithm Analysis: Muhammad Nadeem July 15, 2017
PDF
No ratings yet
Advanced Algorithm Analysis: Muhammad Nadeem July 15, 2017
27 pages
1-Divide and Conquer Algorithms
PDF
No ratings yet
1-Divide and Conquer Algorithms
99 pages
Daa Module 2
PDF
No ratings yet
Daa Module 2
40 pages
Week 4
PDF
No ratings yet
Week 4
45 pages
Algorithm:: Unit-I
PDF
No ratings yet
Algorithm:: Unit-I
9 pages
Divide and Conquer Approach Complete
PDF
No ratings yet
Divide and Conquer Approach Complete
63 pages
5-Stages of Algorithm Development & Time Complexity Analysis-05-01-2024
PDF
No ratings yet
5-Stages of Algorithm Development & Time Complexity Analysis-05-01-2024
31 pages
Small 05 Div and Conquer
PDF
No ratings yet
Small 05 Div and Conquer
47 pages
Module 1 Chapter 2
PDF
No ratings yet
Module 1 Chapter 2
73 pages
2.divide and Conquer
PDF
No ratings yet
2.divide and Conquer
62 pages
Week 5
PDF
No ratings yet
Week 5
37 pages
2017 Mid-Daa Solution
PDF
No ratings yet
2017 Mid-Daa Solution
16 pages
Week - 1 - Daa - SM
PDF
No ratings yet
Week - 1 - Daa - SM
28 pages
Recurrence Relations PDF
PDF
100% (1)
Recurrence Relations PDF
16 pages
04 CS316 Algorithms Recursive Algorithms
PDF
No ratings yet
04 CS316 Algorithms Recursive Algorithms
33 pages
Lab 1
PDF
No ratings yet
Lab 1
11 pages
Unit-1 DAA - Notes
PDF
No ratings yet
Unit-1 DAA - Notes
25 pages
CS702. 2011-Sol
PDF
No ratings yet
CS702. 2011-Sol
13 pages
Test 1 Review and Practice Questions
PDF
No ratings yet
Test 1 Review and Practice Questions
7 pages
小考20181026 (第一次期中考)
PDF
No ratings yet
小考20181026 (第一次期中考)
8 pages
Algorithms 2022-2023
PDF
No ratings yet
Algorithms 2022-2023
258 pages
CS-2012 (Daa) - CS End Nov 2023
PDF
No ratings yet
CS-2012 (Daa) - CS End Nov 2023
21 pages
Practice Problems
PDF
No ratings yet
Practice Problems
4 pages
Analysis of Algorithms (SET: 1) Solutions
PDF
No ratings yet
Analysis of Algorithms (SET: 1) Solutions
8 pages
DAA Notes Jntu
PDF
No ratings yet
DAA Notes Jntu
45 pages
3 Recurrence
PDF
No ratings yet
3 Recurrence
89 pages
DAA Unit 1 Notes
PDF
No ratings yet
DAA Unit 1 Notes
34 pages
DAAQB Test1
PDF
No ratings yet
DAAQB Test1
3 pages
Divide and Conquer Algorithm
PDF
No ratings yet
Divide and Conquer Algorithm
58 pages
CS 412 - T2313 - CH 4 - Part1
PDF
No ratings yet
CS 412 - T2313 - CH 4 - Part1
74 pages
DAA - Questions With Answer
PDF
No ratings yet
DAA - Questions With Answer
9 pages
22CII42 CIE1 Scheme
PDF
No ratings yet
22CII42 CIE1 Scheme
3 pages
RecursiveAlgo RunningTime
PDF
No ratings yet
RecursiveAlgo RunningTime
36 pages
AD I Practice Questions
PDF
No ratings yet
AD I Practice Questions
11 pages
Midterm Sol 1 PDF
PDF
No ratings yet
Midterm Sol 1 PDF
6 pages
Daa 20 21
PDF
No ratings yet
Daa 20 21
28 pages
Daa 20 21
PDF
No ratings yet
Daa 20 21
27 pages
Assignment 1 AoA
PDF
No ratings yet
Assignment 1 AoA
14 pages
DAA Unit-1
PDF
No ratings yet
DAA Unit-1
34 pages
Daa Unit-Ii
PDF
No ratings yet
Daa Unit-Ii
24 pages
Lecture 03
PDF
No ratings yet
Lecture 03
37 pages
Abhijeet Daa Solution All Years Previous Year Paper
PDF
No ratings yet
Abhijeet Daa Solution All Years Previous Year Paper
81 pages
SET B DAA CT1 QP-key
PDF
No ratings yet
SET B DAA CT1 QP-key
16 pages
CT1-Answer Key Set A DAA
PDF
No ratings yet
CT1-Answer Key Set A DAA
7 pages
Daa2 Marks
PDF
No ratings yet
Daa2 Marks
22 pages
DAA SET A CT 1 Answer Key
PDF
No ratings yet
DAA SET A CT 1 Answer Key
7 pages
Ch8.1-8.3-RecursiveAlgs (1 Slider Per Page)
PDF
No ratings yet
Ch8.1-8.3-RecursiveAlgs (1 Slider Per Page)
23 pages
03 - D&C Intro - All
PDF
No ratings yet
03 - D&C Intro - All
23 pages
DAA FJ1 - Set B Key
PDF
No ratings yet
DAA FJ1 - Set B Key
9 pages
Notes 1
PDF
No ratings yet
Notes 1
10 pages
WINSEM2024-25 BCSE204L TH VL2024250501496 2024-12-14 Reference-Material-I
PDF
No ratings yet
WINSEM2024-25 BCSE204L TH VL2024250501496 2024-12-14 Reference-Material-I
32 pages
Unit 1
PDF
No ratings yet
Unit 1
140 pages
All Questions
PDF
No ratings yet
All Questions
6 pages
Design and Analysis of Algorithms (Ict 2222)
PDF
No ratings yet
Design and Analysis of Algorithms (Ict 2222)
4 pages
Desgin
PDF
No ratings yet
Desgin
4 pages
8.ada QB BCS401
PDF
No ratings yet
8.ada QB BCS401
19 pages