0% found this document useful (0 votes)
13 views6 pages

Final Sem 23

Uploaded by

yocepi7889
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)
13 views6 pages

Final Sem 23

Uploaded by

yocepi7889
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/ 6

Birla Institute of Technology & Science, Pilani

Work Integrated Learning Programmes Division


First Semester 2023-2024

Comprehensive Examination
(EC-3 Regular)

Course No. : SE ZG519


Course Title : Data Structures and Algorithms Design
Nature of Exam : Open Book
Pattern of Exam : Typed Only
Weightage : 40% No. of Pages = 6
Duration : 2 ½ Hours No. of Questions = 6
Date of Exam : 25/11/2023 (FN)
Note to Students:
1. Please follow all the Instructions to Candidates given on the cover page of the answer book.
2. All parts of a question should be answered consecutively. Each answer should start from a fresh page.
3. Assumptions made if any, should be stated clearly at the beginning of your answer.

Q-1 Determine the value of x and y (in terms of n) at the end of the execution of the 10 Marks
(Set A) following algorithms algo1 and algo2. (5 marks + 5 marks)
(Note: Partial marking will not be done for this question.)

Algorithm algo1(integer n) Algorithm algo1(integer n)


x←0 y←0
for i = 0; i < n; i = i+1 do for i = 0; i < n; i = i+1 do
for j = 0; j < 2*n; j = j+1 do for j = 0; 2*j < n; j = j+1 do
x ← x+1 y ← y+1
Q-1 Determine the value of x and y (in terms of n) at the end of the execution of the 10 Marks
(Set B) following algorithms algo1 and algo2. (5 marks + 5 marks)
(Note: Partial marking will not be done for this question.)

Algorithm algo1(integer n) Algorithm algo1(integer n)


x←1 y←0
for i = 0; i < n; i = i+1 do for i = 0; i < n; i = i+1 do
for j = 0; j < 2*n; j = j+1 do for j = 0; 2*j < n; j = j+1 do
x ← x*2 y ← y*2
Q-2 Prof. Forget was working on the Merge sort algorithm. He got to sleep while writing the 10 Marks
(Set A) algorithm and forgot to write the underlined steps in MERGE algorithm. If he executes
his faulty algorithm MERGE_SORT(arr, 0, 5) on arr = {16, 13, 11, 15, 12, 14}, then
what will be the modified arr at the end of execution? You are supposed to write the
values of modified arr only as your answer.
(Note 1: Partial marking will not be done for this question.)
(Note 2: Consider array arr starts from the index 0 here.)

MERGE_SORT(arr, beg, end) MERGE(arr, beg, mid, end){


if beg < end then n1 ← mid - beg + 1
mid ← (beg + end)/2 n2 ← end - mid
MERGE_SORT(arr, beg, mid) Initialize LeftArray[n1], RightArray[n2]
MERGE_SORT(arr, mid + 1, end) for i = 0 to n1 do
MERGE (arr, beg, mid, end)
END MERGE_SORT LeftArray[i] ← arr[beg + i]
for j = 0 to n2 do
RightArray[j] ← arr[mid + 1 + j]
i←0
j←0
k ← beg
while i < n1 and j < n2 do
if(LeftArray[i] <= RightArray[j])then
arr[k++] ← Le Array[i++]
else
arr[k++] ← RightArray[j++]
while i<n1 do
arr[k++] ← Le Array[i++]
while j<n2 do
arr[k++] ← RightArray[j++]
}
Q-2 Mr. Raju was devising the Merge sort algorithm. He got to confused while writing the 10 Marks
(Set B) algorithm and wrote faulty steps as underlined in MERGE algorithm. If he executes his
faulty algorithm MERGE_SORT(arr, 0, 5) on arr = {16, 13, 11, 15, 12, 14}, then what
will be the modified arr at the end of execution? You are supposed to write the values of
modified arr only as your answer.
(Note 1: Partial marking will not be done for this question.)
(Note 2: Consider array arr starts from the index 0 here.)

MERGE_SORT(arr, beg, end) MERGE(arr, beg, mid, end){


if beg < end then n1 ← mid - beg + 1
mid ← (beg + end)/2 n2 ← end - mid
MERGE_SORT(arr, beg, mid) Initialize LeftArray[n1], RightArray[n2]
MERGE_SORT(arr, mid + 1, end) for i = 0 to n1 do
MERGE (arr, beg, mid, end) LeftArray[i] ← arr[beg + i]
END MERGE_SORT for j = 0 to n2 do
RightArray[j] ← arr[mid + 1 + j]
i←0
j←0
k ← beg
while i < n1 and j < n2 do
if(LeftArray[i] <= RightArray[j])then
arr[k++] ← RightArray[j++]
else
arr[k++] ← Le Array[i++]
while i<n1 do
arr[k++] ← Le Array[i++]
while j<n2 do
arr[k++] ← RightArray[j++]
}
Q-3 Prof. Forget used to make tricky questions for students. He gave postorder traversal 10 Marks
(Set A) sequence x y z + w * + for a tree of an expression. He also gave a formation of this tree
with labels {1,2,3,4,5,6,7} as follows.
(a) If x = 10, y = 20, z = 30, and w = 40, then identify the value after evaluating this
expression.
(b) Write values of labels 1 = __, …, 7 = __. (5 Marks)
(Note: Partial marking will not be done for this question.)

Q-3 Prof. Forget used to make tricky questions for students. He gave prefix traversal 10 Marks
(Set B) sequence + + a * b c * d + e f for a tree of an expression. He also gave a formation of
this tree with labels {L1, L2, L3, L4, L5, L6, L7, L8, L9, L10, L11} as follows.
(a) If x = 10, y = 20, z = 30, and w = 40, then identify the value after evaluating this
expression. (5 Marks)
(b) Identify the values of labels L1 = __, …, L11 = __. (5 Marks)
(Note: Partial marking will not be done for this question.)

Q-4 Sometimes students challenge Prof. Forget by writing a faulty algorithm. A student 10 Marks
(Set A) wrote two algorithms for checking balanced parentheses and challenged Prof. Forget to
find input strings for which her algorithms give wrong output. You need to write one
input string for algorithm Parenthesis1 and one input string for algorithm Parenthesis2
as your answer. (5 marks + 5 marks)
(Note: Partial marking will not be done for this question.)
Algorithm Parenthesis1 (IP) Algorithm Parenthesis2 (IP)
//IP is a string of parenthesis //IP is a string of parenthesis
//S is stack that is initially empty //S is stack that is initially empty
for i = 1 to IP.length do for i = 1 to IP.length do
if IP[i] = '(' then if IP[i] = '(' then
PUSH(S, IP[i]) PUSH(S, IP[i])
else if IP[i] = ')' AND else if IP[i] = ')' OR
Stack.empty( ) = FALSE Stack.empty = FALSE
POP(S) POP(S)
else else
PRINT("Unbalanced") PRINT("Unbalanced")
Exit Exit
PRINT("Balanced") PRINT("Balanced")

Q-4 Ms. Neha is learning recursive algorithms. She made the following algorithms for stack 10 Marks
(Set B) and queue data structures. Find out the values in stack and queue after executing the
following algorithms on the given stack and queue. You need to write values of stack
and queue with values of top of the stack, front-rear of the queue as your answer.
(5 marks + 5 marks)
(Note: Partial marking will not be done for this question.)

Algorithm Unknown1 (S) Algorithm Unknown2(Q)


//S is a stack with values //Q is a queue with values
{11,12,13,14,15} where top of stack (tos) {11,12,13,14,15}, where front (f) is
is pointing to 15. pointing to 11 and rear (r) is pointing to
15.
d←0 d←0
if tos != 0 then if f != 0 then
d ← pop(S) d ← dequeue(Q)
Unknown1(S) Unknown2(Q)
push(S, d) enqueue(Q, d)
Q-5 Prof. Bhide likes to trick Mr. Jethalal. He has a graph of Gokuldham as shown in the 10 Marks
(Set A) following figure. Prof. Bhide asked Mr. Jethalal to visit all 0 to 8 houses with minimum
energy requirement. Edges on graph are showing energy required to walk from a house
to another house. During odd hours, Mr. Jethalal needs to spend energy. Hence, during
odd hours, edges are having positive values, (e.g, edge between 0 and 1 will have value
4). During even hours, Mr. Jethalal gains energy through Fafda-Jalebi food on the route.
Hence, during even hours, all edges are having same negative values, (e.g., edge
between 0 and 1 will have value -4).

Use Prim's algorithm to find minimum spanning tree for Mr. Porter to cover all rooms
with minimum energy. Assume that you starts with odd hours and require exact 1 hour
to find a minimum edge. Assume that starting vertex for Prim's algorithm is 0.

As your answer, you need to write the sequence of edges chosen during execution of
Prim's algorithm and the total minimum energy requirements to visit all the rooms.

Sequence of edges = {_,_},{_,_},...


Total Energy Required = __________
(5 Marks + 5 Marks)
(Note: Partial marking will not be done for this question.)
Q-5 Prof. Snape likes to trick Mr. Porter. He has a graph of Hogwarts as shown in the 10 Marks
(Set B) following figure. Prof. Snape asked Mr. Porter to visit all 0 to 8 rooms with minimum
energy requirement. Edges on graph are showing energy required to walk from a room
to another room. During odd hours, Mr. Porter needs to spend energy. Hence, during
odd hours, edges are having positive values, (e.g, edge between 0 and 1 will have value
4). During even hours, Mr. Porter gains energy through chocolates on the route. Hence,
during even hours, all edges are having same negative values, (e.g., edge between 0 and
1 will have value -4).
Use Kruskal's algorithm to find minimum spanning tree for Mr. Porter to cover all
rooms with minimum energy. Assume that you starts with odd hours and require exact 1
hour to find a minimum edge.
As your answer, you need to write the sequence of edges chosen during execution of
Kruskal's algorithm and the total minimum enery requirements to visit all the rooms.
Sequence of edges = {_,_},{_,_},...
Total Energy Required = __________
(5 Marks + 5 Marks)
(Note: Partial marking will not be done for this question.)

Q-6 A mathematician came to Prof. Forget for solving a large binomial coefficients using 30 Marks
(Set A) algorithms. He gave the following formula to Prof. Forget for solving the problem.
BC(n, k) = 0 (if k > n)
or = 1 (if k = 0 or k = n)
or = BC(n-1, k-1) + BC(n-1, k) (Otherwise)

Prof. Forget made the following recursive algorithm for the same. But he is not happy as
this algorithm computes the same BC(i, j) multiple times as shown in below table.

Algorithm BC(integer n, integer k) List of recursive calls for BC(4,2):


if k > n then BC(4,2) = BC(3,1) + BC(3,2)
Return 0 BC(3,1) = BC(2,0) + BC(2,1)
else if k = 0 or k = n then BC(2,0) = BC(1,-1) + BC(1,0)
Return 1 BC(2,1) = BC(1,0) + BC(1,1)
else BC(1,0) = BC(0,-1) + BC(0,0)
Return BC(n-1, k-1) + BC(n-1, k) BC(1,1) = BC(0,0) + BC(0,1)
BC(3,2) = BC(2,1) + BC(2,2)
BC(2,1) = BC(1,0) + BC(1,1)
BC(1,0) = BC(0,-1) + BC(0,0)
BC(1,1) = BC(0,0) + BC(0,1)
BC(2,2) = BC(1,1) + BC(1,2)

He asked you to devise an algorithm using dynamic programming. You need to write
formula of dynamic programming for this problem (6 marks), algorithm using dynamic
programming (12 marks), and derivation matrix of BC(4,2) using your algorithm (12
marks). (Hint: Recall Fibonacci Problem using dynamic programming).
Q-6 Prof. Bhide gave a tough question paper for a given time limit. Tapu went through the 30 Marks
(Set B) question paper and noted marks and no. of minutes required to solve the each question.
Tapu gave this note to you and asked to get the sequence of questions numbers for
scoring as much as possible.
(a) You need to write an algorithm using dynamic programming to find such sequence
of question numbers (15 marks).
(b) You need to write matrix (5 marks), possible maximum marks (5 marks), and
sequence of question numbers to be attempted (5 marks) using your algorithm for the
following input. Assume that maximum time for test is 10 minutes.
Q-1 Q-2 Q-3 Q-4 Q-5 Q-6 Q-7

Time 1 1 2 3 3 4 5
required

Marks 5 8 4 9 10 7 15

You might also like