Final Term Test Soln 2020
Final Term Test Soln 2020
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).
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.
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{𝑛,𝑖+𝑓(𝑖)}
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.
i=0; step=0;
While (i<n) {
i = i+f(i);
step++;
}
return step;
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.
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
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 1i<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]?
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
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 𝑗).
-- 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
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.)
-- 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.
-- Page 9 of 9 --