×

Close

- Design And Analysis Of Algorithm - DAA
- 2015
- PYQ Solution
**Kalinga Institute of Industrial Technology - KIIT**- Computer Science Engineering
- B.Tech
**1741 Views**- 19 Offline Downloads
- Uploaded 9 months ago

Semester: V (Regular) Sub & Code: DAA, CS-3001 Branch (s): CSE & IT AUTUMN MID SEMESTER EXAMINATION-2015 Design & Analysis of algorithm Full Marks: 25 [ CS-3001] Time: 2 Hours Answer any four 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 Q1 Answer the following questions: a) Consider the following C function. int fun(int n) { int i, j, p=0; for(i = 1; i < n; ++i) for(j = n; j > 1; j = j/2) ++p; return p; } What is most closely approximates value returned by the function fun? Scheme: Correct answer : 2 Mark Wrong answer, but explanation approaches to answer : Step Marking Answer: (n-1)log2n => n log2 n The outer loop i body will execute (n-1) times. For each outer i iteration the inner j loop will execute k=logn times ( 2k<=n) means k times the value of p will be incremented by 1. b) Rank the following functions by order of growth in increasing sequence? log n, log √n, √n, n log √n, n!, 2n Scheme: Arrangement of given functions with correct sequence (only answer) : 2 Mark Answer: The functions by order of growth in increasing sequence log √n, log n, √n, n log √n, 2n, n! (2 x 5)

c) Can the master method be applied to solve the following recurrence? T(n) = 4T(n/2) + n2 log n Justify your answer. Scheme: Correct answer with justification: 2 Mark Wrong answer, but explanation approaches to answer : Step Marking Answer: Master theorem can not be applied to solve the given recurrence. Explanation Solving by master theorem Step-1: Guess for which case of master theorem Given a=4, b=2, f(n)= n2 log n nlogba= nlog24=n2 Comparing nlogba with f(n), we found f(n) is asymptotically faster growing function. So we guess the solution may exists in case-3. Step-2: Conformation test for case-3 If case-3, then f(n)=(nlogba+ϵ) f(n) >=cnlogba+ϵ) n2 log n >=cn2+ϵ n2 log n >=cn2+ϵ As no value of ϵ >0 exists that makes the above inequality valid, so master theorem can not be applied to solve the given recurrence. d) Write the merge sort procedure (only) which divides the array into two parts such that first part contains elements twice of second part. Also derive the time complexity of that merge sort. Scheme: Writing the algorithm with partitioning index q as mentioned below : 1 Mark Time complexity derivation: 1 Mark Answer: Let p and r are the lower and upper index of the array. q is the partitioning point that divides the array into two parts such that first part contains elements twice of second part. First Part Second Part p q r So, q-p+1=2(r-q+1)=> q=(2r+p-1)/3 MERGE-SORT(A,p,r) { q ← (2r+p-1)/3 MERGE-SORT(A,p,q) MERGE-SORT(A,q+1,r) MERGE(A,p,q,r) }

The Recurrence relation of the above algorithm is T(n) = T(n/3) + T(2n/3) + O(n) Solving the unbalanced partitioning we will get the solution as T(n) = nlog3/2n e) Match the following Algorithms and its recurrences Bubble-sort T(n)=T(n/2)+ Ө(1) Quick-sort T(n)=T(n-1)+ Ө(n) Merge-sort T(n)=T(k) + T(n-k)+Ө(n) Binary-Search T(n)=2T(n/2)+ Ө(n) Scheme: Each correct matching : 0.5 Mark Answer: Bubble-sort Quick-sort Merge-sort Binary-Search Q2 a) T(n)=T(n-1)+ Ө(n) T(n)=T(k) + T(n-k)+Ө(n) T(n)=2T(n/2)+ Ө(n) T(n)=T(n/2)+ Ө(1) What is the significance of asymptotic notations? Define different asymptotic notations used in algorithm analysis. Scheme: Significance of asymptotic notation : 0.5 Mark Correct Definition of asymptotic notations => 2 Marks Answer: We often want to know a quantity approximately, instead of exactly, in order to compare one algorithm with another, we introduce some terminology that enables us to make meaningful (but inexactness) statements about the time and space complexity of an algorithm, they are known as asymptotic notations. Significance of Asymptotic Notations o Asymptotic Notations are used to describe the running time of an algorithm in a meaningful way, is defined in terms of functions whose domains are the set of natural numbers. o These notations refer to how the problem scales as the problem gets larger. o The asymptotic run time of an algorithm gives a simple and machine independent, characterization of its complexity. o The notations works well to compare algorithm efficiencies because we want to say that the growth of effort of a given algorithm approximates the shape of a standard function. (2.5)

Definitions of different asymptotic notations used in algorithm analysis There are five different notations commonly used in algorithm analysis. They are i) O - Notation (Big-Oh Notation) : Asymptotic upper bound ii) - Notation (Big-Omega Notation) : Asymptotic lower bound iii) Θ - Notation (Theta Notation) : Asymptotic tight bound iv) o – Notation (Little-oh Notation) : Upper bound that is not asymptotically tight v) – Notation (Little-omega Notation): Lower bound that is not asymptotically tight i) O - Notation (Big-Oh Notation It represents the upper bound of the resources required to solve a problem.(worst case running time) Definition: Formally it is defined as For any two functions f(n) and g(n), which are non-negative for all n ≥ 0, f(n) is said to be g(n), f(n) = O(g(n)), if there exists two positive constants c and n0 such that 0 ≤ f(n) ≤ c g(n) for all n ≥ n0 Less formally, this means that for all sufficiently big n, the running time of the algorithm is less than g(n) multiplied by some constant. For all values n to the right of n0, the value of the function f(n) is on or below g(n). ii) - Notation (Big-Omega Notation) Definition: For any two functions f(n) and g(n), which are nonnegative for all n ≥ 0, f(n) is said to be g(n), f(n) = (g(n)), if there exists two positive constants c and n0 such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0

## Leave your Comments