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

23CS312 DAA Answer Key

Uploaded by

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

23CS312 DAA Answer Key

Uploaded by

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

Re

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.

A recurrence relation is typically written as:

T(n) = a * T(n/b) + f(n)


2.
Where:

 T(n) is the problem size,


 a is the number of subproblems,
 n/b is the subproblem size,
 f(n) is the cost outside the recursion (e.g., partition-
ing).

3. What is the adjacent node?


An adjacent node in a graph is a node directly connected to

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?

4. The single-source shortest path problem involves finding the 2 CO2 K1


shortest paths from a given source node to all other nodes in
a weighted graph. Algorithms like Dijkstra’s and Bellman-
Ford solve this problem.
Differentiate PROS and CONS of greedy Algorithm.

Pros of Greedy Algorithms


2 CO3 K2
1. Simplicity: Easy to implement with straightforward lo-
gic.
2. Efficiency: Fast with lower time complexity, often O(n)
or O(n log n).
3. Optimal for Specific Problems: Works well for prob-
lems like Kruskal's, Prim’s, and Huffman coding.
5.
Cons of Greedy Algorithms
1. Local Optima Trap: May lead to suboptimal solutions
in some cases.
2. Limited Applicability: Not suitable for all problem
types, like NP-complete problems.
3. No Backtracking: Doesn’t revise choices, which can
miss better solutions.

State the general principle of Greedy algorithm.


A greedy algorithm follows the principle of selecting the
6. best possible option at each step without considering 2 CO3 K2
future consequences, aiming to achieve the overall optimal
solution by solving subproblems optimally.
Define the Hamiltonian cycle.
A Hamiltonian cycle in a graph is a cycle that visits each
vertex exactly once and returns to the starting vertex. It is a 2 CO4 K1
7. closed loop where every vertex is included exactly once,
without repetition, except for the starting and ending vertex.
Finding a Hamiltonian cycle is a classic problem in graph
theory, and it is NP-complete.
8. Define Feasible solution and optimal solution.

 Feasible Solution: A feasible solution is one that satisfies 2 CO4 K1


all the constraints of a given problem, ensuring that it is a
valid solution within the problem's limits, but not necessarily
the best solution.

 Optimal Solution: An optimal solution is the best pos-


sible solution among all feasible solutions, typically minimiz-
ing or maximizing the objective function, depending on the

Page 2 of 7
problem's goal.

What are the Tractable and Intractable problem?

 Tractable Problem: A problem is considered tractable if 2 CO5 K1


it can be solved efficiently, usually in polynomial time (e.g.,
O(n), O(n^2)). These problems are feasible to solve even
for large inputs. Examples include sorting and searching al-
gorithms.
9.
 Intractable Problem: A problem is considered intract-
able if it cannot be solved efficiently, often requiring expo-
nential time (e.g., O(2^n)). These problems are impractical
to solve for large inputs. Examples include NP-complete
problems like the Traveling Salesman Problem and Hamilto-
nian Cycle.

How to quick sort algorithm use random pivoting?


10. In Quick Sort with random pivoting, a pivot is randomly
selected from the array, reducing the chance of worst-case 2 CO6 K2
performance and improving average efficiency (O(n log n)).
PART – B (5 x 13 = 65 Marks)
Mark CO Bloo
s m’s
Q.No Questions
Leve
l
11. With a program explain insertion sort and analysis
the space complexity of insertion sort.
(a)
13 CO1 K2
Insertion Sort is a simple sorting algorithm that builds
the sorted list one element at a time. It takes each
element from the unsorted portion of the array and places
it in the correct position in the sorted portion.

Algorithm:

 Iterates through the array from the second element,


and for each element, finds its correct position in the
sorted part of the array by shifting larger elements
to the right.
 The element is then inserted at the correct position.

Code:

void insertionSort(int arr[], int n) {


Page 3 of 7
for (int i = 1; i < n; i++) {
int key = arr[i], j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}

Time Complexity:

 Best Case: O(n) (already sorted array)


 Average/Worst Case: O(n²) (random/reversed array)

Space Complexity:

 O(1) because it sorts the array in place, requiring


only a constant amount of extra space for the key
and j variables.

(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:

Pattern: "abc", Text: "ababcababc".


Hash the pattern "abc", compare it with each substring
("aba", "bab", etc.) and find a match at indices 3 and 6.

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.

Explain Breath first algorithm and with its time and


space complexity.
13 CO2 K2
BFS is an algorithm used to traverse or search a graph (or
tree) level by level, starting from a source node. It ex-
plores all neighbors at the present depth before moving
on to nodes at the next level.

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:

12.(a) For a graph with nodes A, B, C, D:

 Start at A: Visit B and C (enqueue them).


 Then visit B, D (enqueue D), and so on.

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.

BFS is optimal for finding the shortest path in an un-


weighted graph.

(b) Explain the algorithm for maximum bipartite


matching graph.
13 CO2 K2
The Maximum Bipartite Matching problem involves
finding the largest possible matching in a bipartite graph,
where the graph is divided into two disjoint sets of ver-
tices, and edges only exist between vertices from different
Page 5 of 7
sets.

Algorithm (using DFS):


1. Initialize: Create two arrays, matchU[] and
matchV[], to store the matched vertices for each set.
2. DFS for Augmenting Path: Start a DFS from an
unmatched vertex in set U:
o If an unmatched vertex in set V is found, or if
an already matched vertex can find an altern-
ate path, augment the matching.
3. Recursion: If DFS finds an augmenting path, update
the matching and continue.
4. Repeat the process until no more augmenting paths
can be found.

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

16.(a) Discuss the


13 CO3 K3
(OR)

****************

Page 7 of 7

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy