Test 1 - 2023
Test 1 - 2023
Computing Discipline
TIME ALLOWED:
AIDS ALLOWED:
GENERAL INSTRUCTIONS:
Name: ____________________________________________
Answer:
Exponent1(a, k)
if (k = 1) then
return a
else
return (Exponent1 (a, k/2) * Exponent1 (a, k/2))
Exponent2(a, k)
if (k = 1) then
return a
else
x = Exponent2(a, k/2)
return (x * x)
(i) (4 marks). Give the recurrence function for the time complexity of each of
the two functions. Explain your answers.
(ii) (7 marks). Which of the two functions is more time efficient? Justify your
answer by solving their time complexities.
Hint. Can you use the master method?
Design and Analysis of Algorithms 3/12 Test 1 – Semester 1 2023
Answer:
(i) recurrence
c) (Total: 7 marks). Consider the following function. Note: the array A starts from
index 1, i.e., A[1 .. n].
for i 1 to n do
ji
while j > 0 do
if A[j] != x then // x is a constant integer
j j /2 // This is an integer division, e.g., 3/2 = 1
else
return j // returns from this function
return -1
(i) (2 marks). State the best-case scenario and the worst-case scenario of the
function.
(ii) (5 marks). Give the best-case asymptotic upper bound complexity of the
function and the worst-case asymptotic upper bound complexity of the
function. Explain your answer.
Answer:
(i)
(ii)
Design and Analysis of Algorithms 5/12 Test 1 – Semester 1 2023
Answer:
(i) (3 marks). State the recurrence function of the time complexity of Mergesort
on the input. Explain your answer.
(ii) (3 marks). State the recurrence function of the time complexity of Quicksort
on the input. Explain your answer.
Answer:
(i)
(ii)
(i) (4 marks). Design an O(nlgn) algorithm to solve the problem. Present your
solution in a pseudocode. Explain why your algorithm has time complexity of
O(nlgn).
Answer:
(i) Pseudocode
(ii) Trace
x = 9:
x = 8:
Design and Analysis of Algorithms 8/12 Test 1 – Semester 1 2023
c) (Total: 8 marks). The following is the Strassen’s divide and conquer algorithm to
compute the multiplication of matrices A and B.
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
(i) (3 marks). What is the time complexity to compute the sum of two
submatrices, e.g., A11 + A12? Explain your answer.
(iii) (3 marks). Explain how Strassen’s algorithm, in addition to using the divide
and conquer technique, reduces the time complexity of matrix multiplication
from O(n3) to O(n2.807).
Answer:
(i)
Design and Analysis of Algorithms 9/12 Test 1 – Semester 1 2023
(ii)
(iii)
Attachment
Assume the following:
Let lgx represent log2x; lg1 0, lg2 1, lg3 1.5, lg4 2, lg5 2.3, lg6 2.5, lg7 2.8, lg9 3.1, lg10
3.3.
𝑛
𝑛(𝑛 + 1)
∑𝑖 =
2
𝑖=1
Master Theorem:
(ii) f(n) satisfies the regularity condition af(n/b) ≤ c f(n) for some constant c < 1
and all sufficiently 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 11/12 Test 1 – Semester 1 2023
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]
A blank page for students who need extra space for their answers