0% found this document useful (0 votes)
15 views4 pages

q1 New

Uploaded by

vikstonee
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)
15 views4 pages

q1 New

Uploaded by

vikstonee
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/ 4

Name: Rollno:

CS345: Design and Analysis of Algorithms (Quiz 1)

4th September 2024

Total Number of Pages: 4 Time: 1 hr Total Points 50

Instructions Question Points Score


1. All questions are compulsory. 1 3
2. Answer all the questions in the space provided 2 3
in the question paper booklet.
3 4
3. Use the space provided in the paper for rough
work. 4 4

S
4. The symbols or notations mean as usual unless 5 6
stated. 6 5

N
5. You may cite and use algorithms and their com- 7 4
plexity as done in the class.
8 3
6. Cheating or resorting to any unfair means will
IO
be severely penalized. 9 3
7. Superfluous and irrelevant writing will result in 10 15
negative marking.
Total: 50
8. Using pens (blue/black ink) and not pencils.
Do not use red pens. for answering.
T

Helpful hints
1. It is advisable to solve a problem first before
writing down the solution.
LU

2. The questions are not arranged according to the


increasing order of difficulty.
SO

Page 1 of 4
Name: Rollno:
Question 1. (3 points) Consider the following interval tree (BST).

[3,6]

[2,5] [5,9]

[1,10] [6,8]

If we add the interval [4,10] to the tree, how many nodes (out of the already existing nodes) will have a
change in their Max-high value(as discussed in class)?
A. 1
B. 2
C. 3
D. 4
Question 2. (3 points) Given the recurrence relation:
n
T (n) = 2T + n log n
2
What is the time complexity of this recurrence relation?
A. Θ(n log n)
B. Θ(n log2 n)
C. Θ(n2 )
D. Θ(n2 log n)

Question 3. (4 points) Which of the following options can be a possible preorder traversal of binary search
tree ?
A. 59, 46, 45, 49, 67, 71, 65
B. 5, 6, 7, 8, 9, 10, 12, 11
C. 17, 13, 16, 20, 19, 22, 15
D. 2, 5, 8, 6, 7, 10, 9
Question 4. (4 points) Consider a directed graph G with n vertices and m edges. Then which of the
following statements is/are true.
A. If DFS(G) identifies a back edge, then G has at least one cycle.
B. If DFS(G) does not find any cross edges, then G has a valid topological ordering.
C. Every cross edge of a DFS traversal must connect nodes that are in the same strongly connected
component.
D. For a vertex u in G, the time complexity of DFS(u) is O(m).
Question 5. A networking company uses the Huffman compression technique to encode the message be-
fore transmitting over the network. Suppose the message contains the following characters with their
frequency:

Page 2 of 4
Name: Rollno:
Character a b c d e f
Frequency 5 9 12 13 16 45

(a) (4 points) Draw the binary prefix tree corresponding to Huffman coding

Solution:
f
c d e a b

(b) (2 points) If each character in the input message takes 1 byte (i.e. 8 bits), then how many bits will
be saved in the message by using Huffman encoding?
A. 224
B. 800
C. 576
D. 324

Question 6. Consider a job scheduling problem with 4 jobs J = (J1 , J2 , J3 , J4 ) with corresponding process-
ing times T = (1, 4, 5, 2) and deadlines D = (3, 8, 5, 2).
(a) (3 points) What is sequence in which the jobs can be arranged so that the total lateness is mini-
mized? J4 , J1 , J3 , J2
(b) (2 points) The total lateness in the above sequence is 7 .

Question 7. Let u and v be two vertices in a graph G. Let d(·) and f (·) be functions representing the
discovery time and finish time of vertices respectively, corresponding to a DFS of G.
(a) (2 points) Which of the following statements is false?
A. d(u) < f (u) < d(v) < f (v)
B. d(v) < f (v) < d(u) < f (u)
C. d(u) < d(v) < f (u) < f (v)
D. d(u) < d(v) < f (v) < f (u)
(b) (2 points) If (u, v) is an edge in G, then which of the following statements is false?
A. d(u) < f (u) < d(v) < f (v)
B. d(v) < f (v) < d(u) < f (u)
C. d(u) < d(v) < f (u) < f (v)
D. d(u) < d(v) < f (v) < f (u)
Question 8. (3 points) The number of topological orderings of the following graph is 10 .

b f g

a d e j

c h i

Question 9. (3 points) Consider the circuit synchronization problem discussed in class. Let DL (u) and
DR (u) denote the maximum delay along any leftward path and rightward path from u respectively.
Then there is an optimal solution where the delay enhancement by u is |DL (u) − DR (u)|.

Page 3 of 4
Name: Rollno:
Question 10. Let S = {x1 , x2 , x3 , . . . , xn } be a set of n positive numbers. Let µ be the mean of the set S
i.e. µ = (x1 + · · · + xn )/n. The mean deviation δ of the set S is defined as follows:
δ = (| x1 − µ | + | x2 − µ | + · · · + | xn − µ |)/n
Design a data structure that maintains a set S of positive numbers such that each of the following
operations can be performed in O(log n) time.

• Insertion of an element into S


• Deletion of an element from S
• Querying the mean deviation δ of S
(a) (5 points) Give a formal description of the data structure that you design and explain in brief the
preprocessing (if any) to construct the data structure from the initial set.

Solution: We can use a red-black (or any height-balanced) binary search tree. For each node
in the tree, we will add the following extra fields (apart from the usual ones required to form
a red-black binary search tree):-

• num : number of elements in the subtree rooted at this node


• sum : sum of the values of all the elements in the subtree rooted at this node

We can construct the tree from the set, starting from a null tree and then adding the elements
of the set to the tree one by one. This would take O(n log n) time.

(b) (5 points) Write the pseudocode for the operation of querying the mean deviation δ of S.

Solution: x=root
n = x.num
µ = x.sum/x.num
δ=0

while(x! =NULL)
if(x.val == µ)
δ+ = (x.lef t).num ∗ (µ) − (x.lef t).sum + (x.right).sum − (x.right).num ∗ (µ)
return δ/n
else if(x.val < µ)
δ+ = ((x.lef t).num + 1) ∗ (µ) − (x.lef t).sum − x.val
x = x.right
else
δ+ = (x.right).sum + x.val − ((x.right).num + 1) ∗ (µ)
x = x.lef t

return δ/n

(c) (5 points) Explain in brief how the data structure will support the insertion and deletion operations.

Solution: Insertion and Deletion operation works as discussed in class.

Page 4 of 4

You might also like