23CS312 DAA Answer Key
23CS312 DAA Answer Key
g.
No
Question Paper
Code:
Fifth / Seventh Semester
23CS312 – DESIGN AND ANALYSIS OF ALGORITHM
Computer Science and Engineering
(Regulation 2023)
ANSWER KEY
Time: Three hours Maximum:
100 marks
Answer ALL Questions
PART – A (10 x 2 = 20 Marks)
Mark CO Bloo
s m’s
Q.NoQuestions
Lev
el
Explain the concept of Time Complexity and Space
complexity analysis in the context of heap sort?
2 CO1 K1
Time complexity measures the number of operations an al-
1. gorithm performs, while space complexity refers to the
amount of memory it uses. Heap Sort has a time complex-
ity of O(n log n) due to heap construction (O(n)) and ex-
traction (O(log n) per element). Its space complexity is
O(1) as it sorts in place, using minimal extra memory.
Define Recurrent relation.
A recurrence relation is an equation that defines a sequence
based on previous terms. It expresses each term as a 2 CO1 K1
function of its predecessors, commonly used to analyze
recursive algorithms.
Page 1 of 7
another node by an edge. For example, in a graph with edge 2 CO2 K1
(A, B), nodes A and B are adjacent.
What is a single source shortest path problem?
Page 2 of 7
problem's goal.
Algorithm:
Code:
Time Complexity:
Space Complexity:
(OR)
(b) Explain the steps followed for Rabin-Karp-
algorithm for Pattern searching with an example
and discuss its Time complexity. 13 CO1 K2
The Rabin-Karp algorithm is a string searching method
that uses hashing to find a pattern in a text. It calculates
the hash value of the pattern and compares it with the
hash values of substrings in the text.
Steps:
1. Hash Calculation: Compute the hash for the pat-
tern and the first substring of the text with the same
length.
2. Sliding Window: Slide the window over the text,
updating the hash for each new substring.
3. Compare Hashes: If the hashes match, check the
actual substring for an exact match.
4. Return Matches: If a match is found, return the in-
dex; otherwise, continue.
Example:
Page 4 of 7
Time Complexity:
Best Case: O(n) if no matches are found.
Worst Case: O(n * m) with many hash collisions.
Average Case: O(n + m) with minimal collisions.
Steps:
1. Start from the source node and enqueue it.
2. Dequeue a node from the front of the queue.
3. Visit all its unvisited neighbors, enqueue them.
4. Repeat the process until the queue is empty.
Example:
Time Complexity:
O(V + E), where V is the number of vertices and E is
the number of edges. Every vertex and edge is pro-
cessed once.
Space Complexity:
O(V), as the algorithm stores all vertices in the
queue in the worst case.
Time Complexity:
O(E * V) where E is the number of edges and V is
the number of vertices. In each DFS, all edges may
be checked.
Space Complexity:
O(V + E) for storing the graph and matchings.
4o mini
13.(a)
13 CO3 K3
(OR)
(b)
13 CO3 K3
14.(a)
13 CO4 K2
(b)
13 CO4 K2
15.(a)
13 CO5 K2
(OR)
Page 6 of 7
(b)
13 CO5 K2
PART – C (1 x 15 = 15 Marks)
Mark CO Bloo
s m’s
Q.No Questions
Leve
l
****************
Page 7 of 7