0% found this document useful (0 votes)
38 views7 pages

DAA SET A CT 1 Answer Key

This document outlines the structure and content of a test for the course 'Design and Analysis of Algorithms' at SRM Institute of Science and Technology for the academic year 2022-23. It includes a course articulation matrix, various questions categorized into parts A, B, and C, covering topics such as algorithm complexity, sorting algorithms, and recurrence relations. The test is designed to assess students' understanding of algorithm design and analysis concepts.

Uploaded by

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

DAA SET A CT 1 Answer Key

This document outlines the structure and content of a test for the course 'Design and Analysis of Algorithms' at SRM Institute of Science and Technology for the academic year 2022-23. It includes a course articulation matrix, various questions categorized into parts A, B, and C, covering topics such as algorithm complexity, sorting algorithms, and recurrence relations. The test is designed to assess students' understanding of algorithm design and analysis concepts.

Uploaded by

vatsalbbk2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

SRM Institute of Science and Technology

SET A
College of Engineering and Technology
School of Computing
SRM Nagar, Kattankulathur – 603203, Chengalpattu District, Tamil Nadu
Academic Year: 2022-23 (EVEN)

Test: CLA-T1 Date:


Course Code & Title: 21CSC204J Design and Analysis of Algorithms Duration: 1 hour 40 min
Year & Sem: II Year / IV Sem Max. Marks: 50

Course Articulation Matrix: (to be placed)


Course PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO1 PO11 PO12 Program Specific Outcomes
Outcome 0
PSO-1 PSO-2 PSO-3

CO1 2 1 2 1 - - - - 3 - 3 3 1 -
CO2 2 1 2 1 - - - - 3 - 3 3 1 -
CO3 2 1 2 1 - - - - 3 - 3 3 1 -
2 1 2 1 - - - - 3 - 3 3 1 -
CO4

CO5 2 1 2 1 - - - - 3 - 3 3 1 -

Part - A
(1 x 10 = 10 Marks)
Instructions: Answer all
Q. Question Marks BL CO PO PI
No Code
1 Assume that f(n) and g(n) are asymptotically positive, 1 1 1 1- 1.1,
4,10,1
then which of the following relation holds good? 2
1.3,
a. f(n)=O(g(n)) and g(n) = O(h(n)) then f(n) 2.4,
=O(h(n) 12.2.
b. f(n)=Ω(g(n)) and g(n) = O(h(n)) then f(n) =Ω(h(n) 2
c. f(n)=Ω(g(n)) and g(n) = ϴ(h(n)) then f(n) =Ω(h(n)
d. f(n)=O(g(n)) and g(n) = ϴ(h(n)) then f(n) =Ω(h(n)

2 1 1 1 1- 1.1,
4,10,1
2
1.3,
2.4,
12.2.
2

a. only I
b. only II
c. I or III or IV but not II
d. II or III or IV but not I
3 1 1 1 1- 1.1,
4,10,1
2
1.3,
2.4,
12.2.
2

a. log n
b. log n * n
c. log n + 1
d. n

4 The Worst case occur in linear search algorithm when 1 1 1 1- 1.1,


4,10,1
_________ 2
1.3,
a. Item is somewhere in the middle of the array 2.4,
b. Item is not in the array at all 12.2.
c.Item is the last element in the array 2
d. Item is the last element in the array or is not
there at all
5 The worst case complexity for insertion sort is 1 1 1 1- 1.1,
4,10,1
_________ 2
1.3,
a) O(n) 2.4,
b) O(log n) 12.2.
c) O(n^2) 2
d) O(n log n)
6 If for an algorithm time complexity is given by 1 1 2 1- 1.1,
4,10,1
O(log2 100) then upper bounded complexity will be 2
1.3,
___________ 2.4,
a) constant 12.2.
b) polynomial 2
c) linear
d) none of the mentioned
7 What is the result of the recurrences? Let the 1 1 2 1- 1.1,
4,10,1
recurrence be given by T(n)=aT(n/b)+f(n) and 2
1.3,
f(n)=nc, when f(n) is O(nlogba-e) for some constant e> 2.4,
0, using Master’s theorem 12.2.
2
a) T(n) = 𝑶(𝒏𝐥𝐨𝐠𝒃 𝒂 )
b) T(n) = O(nc log n)
c) T(n) = O(f(n))
d) T(n) = O(n2)
8 What is the worst case time complexity of a quick 1 1 2 1- 1.1,
4,10,1
sort algorithm? 2
1.3,
a) O(N) 2.4,
b) O(N log N) 12.2.
c) O(N^2) 2
d) O(log N)
9 What is the optimal time required for solving the 1 1 2 1- 1.1,
4,10,1
closest pair problem using divide and conquer 2
1.3,
approach? 2.4,
a) O(N) 12.2.
b) O(log N) 2
c) O(N log N)
d) O(N^2)
10 What is the runtime efficiency of using brute force 1 1 2 1- 1.1,
4,10,1
technique for the closest pair problem? 2
1.3,
a) O(N) 2.4,
b) O(N log N) 12.2.
c) O(N2) 2
d) O(N3 log N)
Part – B
(5 x 4 = 20 Marks)
Instructions: Answer All the Questions
11 Let t(n)=9n2. Prove that this is of the order of O(n2) 5 2 1 1- 1.1,
4,10,1
Answer: 2
1.3,
It can be observed from the given relation that, 9n2 2.4,
<=c* n2. This holds good for all values of c greater 12.2.
than 9. Also, assuming c=9, the given relation holds 2
good. This is true n>= n0, where n0 =1. Therefore t(n)
ϵ O(n2)
12 Perform the complexity analysis of the following 2.5 2 1 1- 1.1,
4,10,1
algorithm segment. +2.5 2
1.3,
a) Algorithm Sample() 2.4,
Begin 12.2.
loop i=1 to n 2
loop j=1 to m
A(i,j)=0
End loop
End loop
End

Answer :
Ans: O( n X m)
Here, the outer loop runs from 1 to n
In the inner loop runs from 1 to m

So, time complexity will be O(nm).

b) def fun(N):
for i in range(1,N+1)
j=N
while j>0:
j/=2

Answer: O( N x log (N))


Here, the outer loop runs from 1 to N
In the inner while loop, the j gets divided by 2 every
time.
So the inner loop will run for log2(j) = log2(N)
times.
As the inner loop runs for N times, our time
complexity will be N x log(N).

13 Compare and contrast Merge sort and Quick sort 5 2 2 1- 1.1,


4,10,1
Similarities: 2
1.3,
Divide and conquer: Both algorithms divide the input 2.4,
list recursively into smaller sub-lists, sort the sub-lists,
and then merge them back together in the correct 12.2.
order. 2
Average time complexity: Both have an average time
complexity of O(n log n), making them efficient for
large datasets.
Differences:

Methodology:

Merge Sort: Divides the list exactly in half and merges


the sorted halves. Requires extra space for temporary
storage.
Quick Sort: Chooses a pivot element and partitions the
list into elements less than and greater than the pivot.
More flexible partition ratios possible. Requires no
extra space.
Complexity:

Worst case time complexity:


Merge Sort: O(n log n) (guaranteed)
Quick Sort: O(n^2) (possible in certain cases where
pivot selection is poor)
14 Solve using substitution method 5 2 2 1- 1.1,
4,10,1
T(n)=T(n-1)+3; t(1)=4. 2
1.3,
Answer: 2.4,
T(n-1)= (T(n-2)+3)+3 12.2.
T(n-2) =((T(n-3)+3)+3)+3 2
On generalizing,
T(i)= T(n-i) +i*3
When i=n-1, the resulting equation would be,
T(i)=T(1)+(n-1)*3
=4+3n -3
=3n +1
The solution of this recurrence equation is 3n+1
Part – C
(2 x 10 = 20 Marks)
Instructions: Answer All the Questions
15. Solve the following the recurrence relations using 10 3 1 1- 1.1,
4,10,1
A recursive tree method: 2
1.3,
a. T(n)= T(n/3) +T(2n/3)+n ; T(1)=1. 2.4,
b. T(n) = 8T(n/2) +1; T(1)=1. 12.2.
2
OR
15. Prove the following relations: 10 3 1 1- 1.1,
4,10,1
B a. T(n)=3n3+2n2+3; g(n) =n3; T(n) is in O(g(n)) 2
1.3,
Answer : 2.4,
12.2.
2

b. nϵ O(n2)
Answer: The given statement implies that
n <=c* n2. This is true n>= n0, where n0 =0 and
c>0. Therefore n ϵ O(n2)
16. Design an algorithm to arrange given integers in an 10 3 2 1- 1.1,
4,10,1
A array with running time complexity in O(nlogn) and 2
1.3,
prove the same. 2.4,
Answer: mergesort( array) 12.2.
If len(array) > 1 Then 2
# This is the point where the array is divided into
two subarrays
halfArray = len(array) / 2

FirstHalf = array[:halfArray]
# The first half of the data set

SecondHalf = array[halfArray:]
# The second half of the data set

# Sort the two halves


mergeSort(FirstHalf)
mergeSort(SecondHalf)
merge(FirstHalf, SecondHalf)

k=0

Merge:
merge(FirstHalf, SecondHalf)
# Begin swapping values
While i < len(FirstHalf) and j < len(SecondHalf)
If FirstHalf[i] < SecondHalf[j] Then
array[k] = FirstHalf[i]
i += 1
Else
array[k] = SecondHalf[j]
j += 1
k += 1
EndIf
EndWhile
EndIf
OR
16. Justify the statement with an illustrative example 10 3 2 1- 1.1,
4,10,1
B “selection of pivot in Quick sort algorithm affects the 2
1.3,
running time” 2.4,
Answer: 12.2.
The selection of the pivot in the Quick Sort algorithm 2
indeed has a significant impact on its running time.
Quick Sort works by partitioning an array around a
chosen pivot element, such that all elements smaller
than the pivot are placed before it, and all elements
greater than the pivot are placed after it. Then, the
algorithm recursively sorts the subarrays created by
the partitioning process.

Let's illustrate this with an example:


Consider an array of integers: [5, 9, 3, 7, 2, 8, 6].

Suppose we choose the first element, 5, as the pivot.


In this case, after the partitioning step, the array might
become [3, 2, 5, 9, 7, 8, 6], with the pivot 5 ending up
in its sorted position.

Now, if the pivot is chosen poorly (for example,


always picking the first element in the array), and the
array is already sorted or nearly sorted, Quick Sort
degrades into its worst-case time complexity of
O(n^2). This happens because the partitioning step
doesn't effectively split the array into two roughly
equal halves, leading to one subarray significantly
larger than the other, causing inefficient recursive
calls.
Example 2:
Consider an unsorted array: [8, 3, 1, 4, 2, 7, 6, 5]

Choose a bad pivot: Let's pick the largest element,


which is 8 in this case.
Partition the array: All elements are less than 8,
resulting in: [1, 2, 3, 4, 5, 6, 7] | 8
Recursively sort both sub-arrays: We only have one
element on the left (8) and a sub-array of size n-1 on
the right. This creates a highly unbalanced situation
where one sub-array remains unsorted in each
recursive call.
This scenario continues, leading to n-1 comparisons
for each element in the worst case, resulting in a time
complexity of O(n^2).

However, if the pivot is chosen effectively, such as


selecting the median or a randomly chosen element,
Quick Sort achieves its average-case time complexity
of O(n log n). This is because a good pivot choice
ensures that the array is split into approximately equal
halves, leading to balanced partitions and efficient
sorting.

You might also like