Set b Daa Answer Key1
Set b Daa Answer Key1
CO5 2 1 2 1 - - - - 3 - 3 3 1 -
CO5 2 1 2 1 - - - - 3 - 3 3 1 -
Solution: 4 Marks
Time Complexity: O(n3) (1 Mark)
10 Consider the following tree structure. Implement Breadth-First 5 L3 4 2 2.1.2
Search (BFS) and Depth-First Search (DFS) algorithms to
explore this tree. Using appropriate data structures, provide the
order in which the nodes are visited for both BFS and DFS.
Demonstrate the traversal step-by-step. (5 Marks)
11 In the bustling kingdom of Eldoria, there exists a prestigious 5 L3 5 2 2.4.1
guild of skilled adventurers who seek employment
opportunities in various quests and missions. Each adventurer
possesses unique abilities and strengths, making them suitable
candidates for different quests. However, the guild master
faces the challenge of efficiently selecting the most qualified
adventurers for each quest. Write a pseudocode that employs a
randomized approach to select the most qualified adventurers
for a given quest, ensuring fairness and impartiality in the
selection process. Also, provide the time complexity for your
algorithm.
Pseudocode: 4 Marks
function selectAdventurers(adventurers, questRequirements):
selectedAdventurers = []
while (questRequirements not met):
randomly select an adventurer from the pool
if (adventurer meets questRequirements):
add adventurer to selectedAdventurers
return selectedAdventurers
Part – C
(3 x 9 = 27 Marks)
12. Suppose you are tasked with designing an efficient 9 L3 3 2 3.6.2
A transportation network at minimum cost for a city with
multiple neighbourhoods. Each neighborhood needs to be
connected to every other neighbourhood in the city to ensure
seamless travel for residents and visitors. However, building
roads between all pairs of neighbourhoods would be costly and
unnecessary. Your goal is to devise a strategy to connect the
neighbourhoods using the minimum number of roads while
ensuring that every neighbourhood remains accessible from
any other. Using a greedy approach, describe how you would
go about selecting the roads to build to connect the
neighbourhoods efficiently. Provide a detailed explanation of
your strategy and justify your decisions at each step to achieve
the most cost-effective transportation network for the city.
Additionally, provide a pseudocode and time complexity
outlining the steps of your greedy approach to constructing the
transportation network.
Example: 4 Marks
Pseudocode: marks
Time complexity: 1 Mark
Prims algorithm:
Total Cost = 5 + 3 + 3 + 4 + 4 = 19
Pseudocode:
Prim (G, start):
Initialize an empty set MST to store the minimum
spanning tree
Initialize a priority queue PQ to store vertices and
their weights
Add start vertex to MST and PQ with weight 0
while PQ is not empty:
u = extract_min(PQ)
Add u to MST
for each neighbor v of u:
if v is not in MST:
Add v to PQ with weight of edge (u, v)
return MST
Kruskal Algorithm
Total Cost = 3 + 3 + 4 + 4 + 5 = 19
Pseudocode:
Kruskal(G):
Sort the edges of G in non-decreasing order of their
weights
Initialize an empty set MST to store the minimum
spanning tree
for each vertex v in G:
Make a set containing only v
for each edge (u, v) in sorted edges:
if find(u) is not equal to find(v): // check if adding
the edge creates a cycle
Add (u, v) to MST
Union the sets containing u and v
return MST
(or)
12. Imagine you are a puzzle enthusiast who loves solving word 9 L3 3 2 3.6.2
B puzzles. You have two sets of word tiles, each containing a
sequence of letters arranged in a particular order. Your goal is
to find the longest sequence of letters that appears in the same
order in both sets of word tiles. However, you can only remove
letters from the tiles, not rearrange them, to form the common
sequence.
Example Problem: 5 Marks
Consider two sets of word tiles:
Set A: {M, Z, J, A, W, X, U}
Set B: {X, M, J, Y, A, U, Z}
Determine the length of the longest sequence of letters that
appears in the same order in both sets of word tiles using a
dynamic Programming Approach. Provide the pseudocode for
the same.
Solution:
Pseudocode: 4 Marks
If(a[i]=b[j])
LCS [I,j] = 1+LCS[i-1, j-1]
Else
LCS[I,j] = max ( LCS [i-1,j], LCS [i, j-1])
Solution: 5 Marks
Pseudocode: 4 Marks
(or)
13. Alice is preparing for a wilderness expedition where she needs 9 L3 4 2 3.6.2
B to carry a limited weight of supplies in her backpack. She has
a list of items with their weights and values. However, her
backpack has a strict weight limit. Alice wants to maximize the
total value of the items she carries while ensuring she doesn't
exceed the weight limit of her backpack. Derive the following
example and provide the pseudocode for the same.
Pseudocode: 4 Marks
// Output:
fp: Final profit
Fw: Final weight
X: Solution tuple
cp ← 0
cw ← 0
k←1
b ← cp
c ← cw
for i ← k + 1 to n do
if (c + w[i] ≤ M) then
c ← c + w[i]
b ← b – p[i]
end
end
return b
Example: 4 Marks
Use Randomized quick sort to sort the array.
Sorted Array: 10, 25, 30, 45, 50, 65, 70, 85
Pseudocode: 5 Marks
function randomizedQuicksort(array):
if length(array) <= 1:
return array
(or)
14. Write an algorithmic approach to solve the Traveling Salesman 9 L3 5 2 2.1.1
B Problem (TSP), a well-known NP-complete problem, using
dynamic programming. Analyze the time complexity of your
proposed solution and compare it with the Brute Force
algorithm.
*Program Indicators are available separately for Computer Science and Engineering in AICTE
examination reforms policy.
29