DAA-Test-1-2022(2)
DAA-Test-1-2022(2)
Computing Discipline
TIME ALLOWED:
AIDS ALLOWED:
GENERAL INSTRUCTIONS:
Name: ____________________________________________
(i) (1 mark). State the asymptotic lower bound and the asymptotic upper bound
time complexity of f(n). You need not prove your statement. However, your
solution must be as tight as possible.
• f(n) = O(n3).
• f(n) = O(n).
Note: you must show the values of c and n0 to prove its correctness or show
that there is no pair of values of c and n0 to disprove.
Answer:
(i)
• f(n) = O(n3).
• f(n) = O(n).
Design and Analysis of Algorithms 3/12 Test 1 – Semester 1 2022
b) (7 marks). State the worst case, the best case, and the average case scenario of the
algorithm. Then, compute the worst case, the best case, and the average case upper
bound time complexity of the algorithm, if the time complexity of function X is
O(n2).
for i = 1 to n do
if A[i] <= 0
stop // exit the loop
else
call function X (A[i]) // O(n2)
Answer:
Worst case:
Best case:
Average case:
If it can, use the Master method to compute its solution. Otherwise, explain why it
cannot be used to solve it.
Answer:
Answer:
Design and Analysis of Algorithms 5/12 Test 1 – Semester 1 2022
Answer:
=============================================================
END OF QUESTION ONE
Design and Analysis of Algorithms 6/12 Test 1 – Semester 1 2022
INSERTION-SORT (A)
for j=2 to length(A) do // Line A
key = A[j]
// insert A[j] into the sorted sequence A[1 … j-1]
i = j-1
while i>0 and A[i] > key do // Line B
A[i+1] = A[i]
i = i-1
A[i+1] = key
(i) (2 marks). In the worst case, how many times Line B is executed when j =
length(A)? Justify your answer.
(ii) (1 mark). How many times Line A will be exactly executed? Why?
(iii) (2 marks). Consider an array A that contains integers that have been sorted in
decreasing order, e.g., A = <9, 7, 5, 3, 1> . Is this input the best-case or the
worst-case scenario for the Insertion-sort? For this case, what is its upper
bound time complexity? Explain your answer.
Answer:
(i)
(ii)
(iii)
Design and Analysis of Algorithms 7/12 Test 1 – Semester 1 2022
(i) (4 marks). Suppose you want to solve the problem as follows. First, you
insert B[1] into array A such that A[1 … n+1] is in sorted order. Then, you
repeat the step for B[2], B[3], … B[n], and thus eventually array A[1 ... 2n] is
sorted in increasing order. Note that this step is like the approach used in the
insertion sort. Compute the upper bound time complexity of this solution.
Explain your answer.
Hint. You can use the following steps. Compute the number of comparisons
required for each insertion. Take the total number of comparisons to compute
the time complexity. Also, you may need the following equation: ∑!"(2𝑖 −
1) = 𝑛# .
(ii) (4 marks). Suppose you want to solve the problem as follows. First, you put
the content of array B, in reverse order, to the back of array A. For example,
for A = <1, 3, 5, 7> and B = <8, 6, 4, 2>, this first step will create A = <1, 3, 5,
7, 2, 4, 6, 8>. Then, you use the Quicksort algorithm to sort the new array A.
Is this case the best or the worst case for Quicksort? What is its upper bound
time complexity for this case? Explain your answer. You are not required to
formally prove the time complexity. You are allowed to use an example for
your explanation.
Suggestion: Use a small sized input, e.g., A = < 1, 3, 5, 7> and B = <8, 6, 4,
2> to see the result of each call to partition.
Answer:
(i)
(ii)
Design and Analysis of Algorithms 8/12 Test 1 – Semester 1 2022
!!! !!" ! ! ! !
= !! !" !×! !! !" !
!!" !!! !!" !!! !!" !!!
As discussed in lecture, using the conventional matrix multiplication method, we
have:
C11 = A11 × B11 + A12 × B21 C12 = A11 × B12 + A12 × B22
C21 = A21 × B11 + A22 × B21 C22 = A21 × B12 + A22 × B22
Strassen (A, B)
1 n = rows[A]
2 Let C be a new n × n matrix
3 if n = 1
4 c11 = a11 × b11
5 else
6 P1 = Strassen (A11, B12 – B22)
7 P2 = Strassen (A11 + A12, B22)
8 P3 = Strassen (A21 + A22, B11)
9 P4 = Strassen (A22, B21 – B11)
10 P5 = Strassen (A11 + A22, B11 + B22)
11 P6 = Strassen (A12 – A22, B21 + B22)
12 P7 = Strassen (A11 – A21, B11 + B21)
13 C11 = P5 + P4 – P2 + P6
14 C12 = P1 + P2
15 C21 = P3 + P4
16 C22 = P5 + P1 – P3 – P7
17 return C
(ii) (3 marks). The recurrence of the time complexity of the Strassen’s algorithm
is T(n) = 7 T(n/2) + 18(n/2)2. Using the given Strassen’s algorithm, explain
why the recurrence is correct.
Note: in your answer, you must explicitly state which lines of the algorithm
contribute to the first term and second term of the recurrence T(n), i.e.,
7T(n/2) and 18(n/2)2, respectively. Further, you must explain why the first
term and the second term uses “n/2”.
Design and Analysis of Algorithms 9/12 Test 1 – Semester 1 2022
(iii) (3 marks). According to Strassen, C21 = P3 + P4. Prove its correctness, i.e.,
shows that C21 = A21 × B11 + A22 × B21.
Hint.
S1 = B12 – B22 S2 = A11 + A12 S3 = A21 + A22 S4 = B21 – B11 S5 = A11 + A22
S6 = B11 + B22 S7 = A12 – A22 S8 = B21 + B22 S9 = A11 – A21 S10 = B11 + B12
Answer:
(i)
(ii)
Design and Analysis of Algorithms 10/12 Test 1 – Semester 1 2022
(iii)
======================================================================
END OF TEST
Design and Analysis of Algorithms 11/12 Test 1 – Semester 1 2022
Attachment
Assume the following:
Let lgx represent log2x; lg3 » 1.5, lg5 » 2.3, lg6 » 2.5, lg7 » 2.8, lg9 » 3.1, lg10 » 3.3.
!
𝑛(𝑛 + 1)
*𝑖 =
2
"$%
Master Theorem:
(
*
*
*Θ( n log b a ) f (n) = O( n log b a−ε ) → f (n) < n log b a
*
*
*
*
T(n) = )Θ( n log b a lg n ) f (n) = Θ( n log b a ) → f(n) = n log b a
*
*
*
*
*Θ( f (n)) f (n) = Ω( n log b a +ε ) → f (n) > n log b a
* if af (n /b) ≤ cf (n) for c < 1 and large n
*
+
MERGESORT(A, l, r)
Input : an array A in the range 1 to n. Output: Sorted array A.
if l < r
€
then q ¬ ë(l+r )/2û
MERGESORT(A, l, q)
MERGESORT(A, q+1, r)
MERGE (A, l, q, r)
MERGE(A, l, m, r)
Inputs: Two sorted sub-arrays A(l, m) and A(m+1, r) Output: Merged and sorted array A(l, r)
i=1
j = m+1
k=1
while (i £ m) and ( j £ r) do // check if not at end of each sub-array
if A[i] £ A[j] then // check for smaller element
TEMP[k++] = A[i++]
else // copy smaller element
TEMP[k++] = A[j++] // into temp array
while (i £ m) do
TEMP[k++] = A[i++] // copy all other elements
while (j £ r) do // to temp array
TEMP[k++] = A[j++]
Design and Analysis of Algorithms 12/12 Test 1 – Semester 1 2022
Quicksort(A,l,r)
Input :Unsorted Array (A,l,r); Output : Sorted subarray A(0..r)
if l < r
then q ¬ PARTITION(A,l,r)
QUICKSORT(A,l,q-1)
QUICKSORT(A,q+1,r)
PARTITION(A, l, r)
Input: Array A(l .. r)
Output: A and m such that A[i] £ A[m] for all i £ m and A[j] > A[m] for all j > m
x = A[r]
i=l–1
for j = l to r - 1 do
if A[j] ≤ x then
i = i +1
exchange A[i] « A[j]
Counting-Sort (A, B, k)
1 let C [0 .. k] be a new array // note that the index of array C starts from 0
2 for i = 0 to k
3 C [i] = 0
4 for j = 1 to A.length
5 C [ A [j] ] = C [ A [j] ] + 1
6 // C [i] now contains the number of elements equal to i
7 for i = 1 to k
8 C [i ] = C [i] + C [i - 1]
9 // C [i] now contains the number of elements less than or equal to i
10 for j = A.length downto 1
11 B [ C [ A [j] ]] = A [j]
12 C [ A [j] ] = C [ A [j] ] - 1
END OF PAPER