0% found this document useful (0 votes)
94 views

Final Term Test Soln 2020

The document is the final term test for the course CS3230 - Design and Analysis of Algorithms at the National University of Singapore. It contains instructions for the exam, which consists of 4 questions worth a total of 80 points. The exam is open book and students must submit their answers in PDF format within the allotted 2 hours.

Uploaded by

Sourabh Raj
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)
94 views

Final Term Test Soln 2020

The document is the final term test for the course CS3230 - Design and Analysis of Algorithms at the National University of Singapore. It contains instructions for the exam, which consists of 4 questions worth a total of 80 points. The exam is open book and students must submit their answers in PDF format within the allotted 2 hours.

Uploaded by

Sourabh Raj
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/ 9

CS3230

NATIONAL UNIVERSITY OF SINGAPORE


SCHOOL OF COMPUTING

FINAL TERM TEST FOR


Semester 2, AY2019/2020

CS3230 – DESIGN AND ANALYSIS OF ALGORITHMS

5 May 2020 Time Allowed: 2 hours

Instructions to Candidates:
1. This paper consists of FOUR questions (each consisting of TWO sub-questions)
and comprises NINE (9) printed pages, including this page.
2. Answer ALL questions.
3. This is an OPEN BOOK examination.
4. Write down Metric No. on top of every page of your answer sheet

5. SUBMISSION: Page limit for each one of FOUR questions is three. After writing
down your answers scan them to convert to .pdf file. Create exactly one file for
each of FOUR questions and rename your file as <Metric No.>_T<tutorial group
no.>_Q<question no.>.pdf (for example, A324516H_T01_Q3.pdf). Then submit
each file separately in the corresponding LumiNUS folder (you can see four
different folders for each of FOUR questions).

QUESTION POSSIBLE SCORE


Q1 20
Q2 20
Q3 20
Q4 20

TOTAL 80
-- Page 1 of 9 --
CS3230

IMPORTANT NOTE:
• You can freely quote standard algorithms and data structures covered in the lectures,
tutorials and assignments. Explain any modifications you make to them.
• Unless otherwise specified, you are expected to prove (justify) your results.

Q1. (20 points)


(a) Consider a line with n+1 stations S0, S1, …, Sn, where S0 is the initial station
and Sn is the final station. We want to transfer an object from S0 to Sn. At every
station, we have a catapult. We can use the catapult at station Si to throw the
object to any station Sj where j = i+1, …, i+f(i). Note that f(i)1 for all i. We
want to transfer the object from S0 to Sn using the minimum number of
shoots. You believe that this problem can be solved using dynamic
programming. Given f(i) for i=0, 1, …, n-1 (as an array F[0…n-1]), can you
give an O(n2)-time dynamic programming algorithm to solve this problem?
Please detail your algorithm and argue that it is correct. Please also state the
time complexity of your algorithm.

Soln: Let V(i) = the minimum number of rounds of shoots to transfer the object
from Si to Sn.

We have V(n) = 0.
For 0  i < n, 𝑉(𝑖) = min 𝑉(𝑗) + 1.
𝑖+1≤𝑗≤min{𝑛,𝑖+𝑓(𝑖)}

The dynamic programming algorithm is as follows.

V(n)=0;
For i = n-1 down to 0
V(i) = V(i+1)+1
For j = i+2 to min{𝑛, 𝑖 + 𝑓(𝑖)}
V(i) = min { V(i), V(j)+1 };
Report V(0);

We need to fill in n entries. Each entry can be computed in n steps. The running
time of the algorithm is O(n2).

-- Page 2 of 9 --
CS3230

Q1. (Continued …)
(b) Suppose we have i+f(i)j+f(j) for all i>j. Under this extra assumption can you
give an O(n)-time algorithm to solve this problem? Please detail your
algorithm and argue that it is correct. Please also state the time complexity of
your algorithm.

Soln: We claim that 𝑉(𝑖) = min 𝑉(𝑗) + 1 = 𝑉(min{𝑛, 𝑖 + 𝑓(𝑖)}) + 1.


𝑖+1≤𝑗≤min{𝑛,𝑖+𝑓(𝑖)}

Based on the claim, the following algorithm returns the answer.

i=0; step=0;
While (i<n) {
i = i+f(i);
step++;
}
return step;

The running time of the algorithm is O(n).

Now, we show that the claim is correct by exchange argument.

Suppose the optimal solution passes the object in the following sequence of
stations: 0, i1, i2, …, ik, n.

Assume the algorithm passes the object in the following sequence of stations: 0,
j1, j2, …, jh, n.

By contrary, assume the two sequences are different.


Let p be the smallest index such that ipjp. Note that there exists an optimal
solution that pass the object in the following sequence 0, i1, …, ip-1, jp, ip+1, …, ik,
n.

The note is correct as our greedy choice always choose the further station to go.
So, jp = ip-1 + f(ip-1) > ip. This implies jp+f(jp)ip+f(ip). Hence, from jp, we can go to
ip+1. The claim is correct.

-- Page 3 of 9 --
CS3230

Q2. (20 points)


(a) You are given an integer array A[1..n] and an integer x. Can you develop an
efficient algorithm Partition(A, x) that partitions A into two arrays X and Y
such that all numbers in X are smaller than x and all numbers in Y are bigger
than or equal to x? What is the running time of your algorithm?

Soln: It can be done in O(n) time.

Partition(A, x) {
p=1; q=1;
For i = 1 to n {
If (A[i]<x) {
X[p]=A[i]; p++;
} else {
Y[q]=A[i]; q++;
}
}
return (X, Y);
}

-- Page 4 of 9 --
CS3230

Q2. (Continued …)
(b) You have an unsorted integer array A[1..n]. You also have a sorted integer
array B[1..k] (k<n), where B[1]<B[2]<…<B[k]. For any 1i<k, let Si be the set
{ A[j] | B[i]A[j]<B[i+1] }. You need to compute the array C[1..k-1] where C[i]
is the median of all integers in Si if Si is a non-empty set; otherwise set C[i]=0.
Can you propose an 𝑂(𝑛 log 𝑘) time algorithm to compute C[1..k-1]?

Soln: We can compute C[1..k-1] by ComputeMedian(A, B[1..k]). The running


time is O(n lg k).

ComputeMedian(A[1..n], B[i..j]) {
If (i==j) return an empty list;
If (j==i+1) {
(X’,X)=Partition(A, B[i]);
(Y’,Y)=Partition(X, B[i+1]);
Compute and return the median of Y’;
}
m=(i+j)/2;
(X, Y) = Partition(A, B[m]);
C1=ComputeMedian(X, B[i..m]);
C2=ComputeMedian(Y, B[m..j]);
return C1•C2;
}

-- Page 5 of 9 --
CS3230

Q3. (10+10= 20 points)


(a) Suppose you are given two strings 𝑥 and 𝑦 of length 𝑛 each, over ternary
alphabet, i.e., 𝑥, 𝑦 ∈ {0,1,2}𝑛 . Now consider the problem of finding a
Longest Common Subsequence of 𝑥 and 𝑦. Design an 𝑂(𝑛) time algorithm
with approximation ratio 1/3. Provide clear description of your algorithm
and its proof of correctness. (Note, in the lecture we have seen an algorithm
that finds Longest Common Subsequence of 𝑥 and 𝑦 in 𝑂(𝑛2 ) time.)

Soln: Count the number of 0,1 and 2 in both the strings. Let 𝑐0 , 𝑐1 , 𝑐2be the count
of number of 0,1 and 2 respectively in the string 𝑥. Similarly let
𝑑0 , 𝑑1 , 𝑑2 be the count of number of 0,1 and 2 respectively in the string 𝑦.
Next compute 𝑚𝑖 = min⁡{𝑐𝑖 , 𝑑𝑖 } for all 𝑖 ∈ {0,1,2}. Then find the 𝑖 with
the largest 𝑚𝑖 value, and say it is 𝑗. Output the string 𝑗 𝑚𝑗 (string of length
𝑚𝑗 that contains only the character 𝑗).

Consider any largest common subsequence, which is of length say 𝑙.


Now consider the character that occurs maximum number of times in it
and say count of that character is 𝑟. Clearly 𝑙⁡ ≤ 3⁡𝑟 and 𝑟⁡ ≤ ⁡ 𝑚𝑗 . This
implies that 𝑚𝑗 ≥ 𝑙/3, and hence approximation ration of our algorithm
is 1/3.

-- Page 6 of 9 --
CS3230

Q3. (Continued …)
(b) The Independent Set problem is defined as: Given an undirected graph 𝐺 =
(𝑉, 𝐸) find a maximum sized subset 𝑋 ⊆ 𝑉such that for all 𝑢, 𝑣 ∈ 𝑋, (𝑢, 𝑣) ∉
𝐸.
(c) Now consider the following randomized algorithm. Among 𝑛!(where |𝑉| =
𝑛) possible orderings of the vertices pick one uniformly at random. Then
start processing each vertex based on this selected ordering. Initially take
an empty set 𝑆. Then at any particular step, if 𝑢 ∈ 𝑉 is processed and for all
(𝑢, 𝑣) ∈ 𝐸, 𝑣 ∉ 𝑆, add 𝑢 to 𝑆. Otherwise continue with the next vertex based
on the previously selected ordering. Stop when all the vertices have been
processed, and output the set 𝑆.
Show that the set 𝑆 output by the algorithm is an independent set of
1
expected size at least 𝑑+1 𝑂𝑃𝑇, where 𝑂𝑃𝑇 denotes the size of the maximum
independent set and 𝑑 is the maximum degree of any vertex.

Soln:
By the construction the set 𝑆 output by the algorithm is an independent set,
since we add a vertex in it only if its neighbors have not been added.
Note that a vertex 𝑢 will be added to 𝑆 if it is the first vertex in its neighborhood
to be processed. Given that the process order is selected uniformly at random
the probability that a vertex will be processed before all of its neighbors is at
1
least 𝑑 +1.
𝑢
Let 𝑋𝑢 be an indicator variable for whether or not 𝑢 ∈ 𝑆. So by linearity of
expectation expected size of 𝑆 is
1 1 𝑛
∑𝑢∈𝑉 𝐸[𝑋𝑢 ] = ⁡ ∑𝑢∈𝑉 Pr[𝑢 ∈ 𝑆] ≥ ⁡ ∑𝑢∈𝑉 ≥ ⁡ ∑𝑢∈𝑉 𝑑+1 = 𝑑+1.
𝑑 +1
𝑢
Clearly 𝑂𝑃𝑇 ≤ 𝑛, and this completes the proof.

-- Page 7 of 9 --
CS3230

Q4. (10+10 = 20 points)


(a) In the Set Union problem we have 𝑛 elements, that each are initially in 𝑛
singleton sets, and we want to support the following operations:
➢ Union(A,B): Merge the two sets A and B into one new set C = A∪ B
destroying the old sets.
➢ SameSet(x, y): Return true, if x and y are in the same set, and false
otherwise.
We implement it in the following way. Initially, give each set a distinct color.
When merging two sets, recolor the smaller (in size) one with the color of the
larger one (break ties arbitrarily). Note, to recolor a set you have to recolor all
the elements in that set.
To answer SameSet queries, check if the two elements have the same color.
(Assume that you can check the color an element in O(1) time, and to recolor
an element you also need O(1) time. Further assume that you can know the size
of a set in O(1) time.)

Use Aggregate method to show that the amortized cost is O(log n) for Union.
That means, show that any sequence of 𝑚 union operations takes time
𝑂(𝑚 log 𝑛). (Note, we start with 𝑛 singleton sets.)

Soln: Initially we have 𝑛 singleton set. So any sequence of 𝑚 union operations


can involve at most 2𝑚 many elements. By our implementation, cost of a union
operation is equal to the number of elements recolored during that operation.
Hence total cost of 𝑚 operations is at most twice the total number of recoloring
happens. Now count the total number of recoloring by element wise. Observe
that each element can be recolored at most log 𝑛 times. This is because since we
are recoloring smaller set, an element is recolored 𝑘 times means it was part of
smaller set 𝑘 times, and each time the size doubles. More specifically, if we
consider all these 𝑘 unions where that element is part of the smaller set, and let
𝑠1 , … , 𝑠𝑘 be the sizes of those smaller sets. Clearly 𝑠𝑖 ⁡ ≥ 2𝑠𝑖−1 , and so 𝑘 ≤
log 𝑛.Thus total number of recoloring is bounded by 𝑂(𝑚 log 𝑛).

-- Page 8 of 9 --
CS3230

Q4. (Continued …)
(b) Suppose Alice insists Bob to maintain a dynamic table (that supports both
insertion and deletion) in such a way its size must always be a Fibonacci
number. She insists on the following variant of the rebuilding strategy. Let
𝐹𝑘 denote the 𝑘-th Fibonacci number. Suppose the current table size is 𝐹𝑘 .
After an insertion, if the number of items in the table is 𝐹𝑘−1 , allocate a new
table of size 𝐹𝑘+1 , move everything into the new table, and then free the old
table.
After a deletion, if the number of items in the table is 𝐹𝑘−3 , we allocate a
new hash table of size 𝐹𝑘−1 , move everything into the new table, and then
free the old table.
Use either Potential method or Accounting method to show that for any
sequence of insertions and deletions, the amortized cost per operation is
still O(1). (If you use Potential method clearly state your potential function.
If you use Accounting method clearly state your charging scheme.)

Soln: Suppose the current table size is 𝐹𝑘 , then there must be at most
𝐹𝑘−1 elements present in the table.
Consider the following charging scheme for insertion: Charge $4 for each
insertion, use $1 for insertion and remaining $3 put in the bank. Between the
creation of table of size 𝐹𝑘+1 and 𝐹𝑘+2 there must be at least 𝐹𝑘 − 𝐹𝑘−1 = 𝐹𝑘−2
elements being inserted. So the bank balance at the time of creating table of size
𝐹𝑘+2 must be 3𝐹𝑘−2 > ⁡ 𝐹𝑘−3 + 2𝐹𝑘−2 = 𝐹𝑘−1 + ⁡ 𝐹𝑘−2 = 𝐹𝑘 . So we can move all the
𝐹𝑘 ⁡elements to the new table using the bank balance (free of cost). Hence the
amortization cost of insertion is O(1).
For the deletion consider the following charging scheme: Charge $3 for each
deletion, use $1 for deletion and remaining $2 put in the bank. Between the
creation of table of size 𝐹𝑘−1 and 𝐹𝑘−2 there must be at least 𝐹𝑘−3 − 𝐹𝑘−4 = 𝐹𝑘−5
elements being deleted. So the bank balance at the time of creating table of size
𝐹𝑘−2 must be 2𝐹𝑘−5 > ⁡ 𝐹𝑘−6 + 𝐹𝑘−5 = 𝐹𝑘−4 . So we can move all the 𝐹𝑘−4 ⁡elements
to the new table using the bank balance (free of cost). Hence the amortization
cost of deletion is O(1).
Note, at any point bank balance is some constant times the number of elements
present in the table, and hence is non-negative.

-- End of Paper ---

-- Page 9 of 9 --

You might also like