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

BITS Pilani CS F364 Midterm Exam 2023

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)
56 views7 pages

BITS Pilani CS F364 Midterm Exam 2023

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

Birla Institute of Technology & Science- Pilani,

K K Birla, Goa Campus

Design & Analysis of Algorithms (CS F364)


Mid Semester Test, Semester – II, Year: 2022-2023

Date: 16-03-2023, Time: 90 minutes, Total Marks: 60 (Closed Book)

Q1. Maximize the payoff


Suppose you are given two sets A and B, each containing n positive integers. You can choose to reorder
each set however you like. After reordering, let ai be the ith element of set A, and let bi be the ith element
of set B. You then receive a payoff ∏𝒏𝒊=𝟏 𝒂𝒊 𝒃𝒊 . Give an algorithm that will maximize your payoff. Prove that
your algorithm maximizes the payoff, and state its running time.
[10]

Q2. Maximize Marks in Exam


You are given an exam with questions numbered 1,2,3,. . .,n. Each question i is worth pi points. You must
answer the questions in order, but you may choose to skip some questions. The reason you might choose
to do this is that even though you can solve any individual question i and obtain the pi points, some questions
are so long that after solving them you will be unable to solve any of the following fi questions. Suppose that
you are given the pi and fi values for all the questions as input. You want to devise an algorithm for choosing
set of questions to answer that maximizes your total points.
a. Describe a brute force algorithm to solve the problem and give its running time complexity.
Ans.
Generate all possible subsequences. 2 Marks
The possible number of subsequences is of O(2n) 2 Marks
b. Suppose you want to solve the problem using Dynamic Programming strategy. Just give the recursive
formulation of the Dynamic Programming solution. [4+4]
Ans.
Let S(i) be the maximum total score that can be obtained from questions i through n.
Any such score is obtained from a set of questions that either includes i or not.
In the first case, the best score is pi +S(i+fi +1), and in the second case, the best score is S(i+1).
S[i] = max(pi + S[i+f[i]+1], S[i+1]) 4 Marks

Q3. Minimize time in the system


A single server has N customers to serve. Customer i takes time ti, 1≤ i ≤ N to be served. Goal is to Minimize
average time that a customer spends in the system. Does this problem exhibit Optimal Substructure
Property? Justify your answer. [6]
Ans.
If we run the first task in an optimal solution, then we obtain an optimal solution by running the remaining
tasks in a way which minimizes the average completion time.

Q4. 0-1 Knapsack Problem


You have a set of n integers each in the range 0, . . . , K. Problem is to Partition these integers into two
subsets such that you minimize |S1 − S2|, where S1 and S2 denote the sums of the elements in each of the
two subsets. Is it possible to model this problem by 0-1 Knapsack Problem? Justify your answer.
[6]
Q5. Shortest Path Problem
Consider the problem of finding Single source all destination Shortest Path in a weighted graph. Brute force
method finds all possible paths and then chooses the one with minimum weight. Is brute force strategy a
practical strategy to solve the problem? Justify your answer. [6]

Q6. Big Oh Notation


Is 𝑓(𝑛) = ∑𝑛𝑖=1 𝑖! of order O(2n)? Justify your answer. [6]

Q7. Treap Insert


Insert the pair (8, 65) in the following treap structure. Show all the intermediate steps. [8]

Q8. RB Tree Insert


Consider a red-black tree formed by inserting n nodes into an initially empty red-black tree. Argue that if n
> 1, the tree has at least one red node. [10]
(Hint: Use Induction.) Also, see below the pseudo code for RB-Insert-FixUp.

Ans.
Proof by Induction:
Base Case:
N=2
By insertion algorithm while inserting the 2nd node, it is coloured Red and no RB Tree property is violated.
2 Marks
Inductive Hypothesis:
Assume that for a red-black tree of n nodes, where 1< n ≤ N, there exists at least 1 red node.
Inductive Step:
Prove that for a red-black tree of N +1 nodes there exists at least 1 red node.
There are 2 cases we have to of interest on the N +1 th insertion.
Case1:
Red Node N+1 is inserted as a child of a black node. This is same as base case which we have proved to be True
already. 2 Marks
Case2:
Red Node (‘x’) N+1 is inserted as a child of a red node.
We consider insertion cases that have x as a child of a red node P
Insertion Case 1:
X remains red after the recoloring of p[x], p[p[x]] & uncle[x].
Thus we have atleast 1 red node. 2 Marks
Insertion Case 2:
By performing left rotation at p[x], the Case 2 is reduced to Case 3.
No recoloring is done in this case. 2 Marks
Insertion Case 3:
In the recoloring and the rotations performed in Case 3, the color of x remains unchanged. Thus we have
atleast 1 red node. 2 marks

You might also like