0% found this document useful (0 votes)
9 views12 pages

Test 1 - 2023

This document is a test paper for the course 'Design and Analysis of Algorithms' at Curtin University, consisting of two questions worth a total of 50 marks. The test includes various problems related to algorithm complexity, recursion, and sorting algorithms such as Mergesort and Quicksort. Students are required to attempt all questions within a 70-minute timeframe, with specific instructions regarding allowed aids and the structure of answers.

Uploaded by

Mia Tran
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)
9 views12 pages

Test 1 - 2023

This document is a test paper for the course 'Design and Analysis of Algorithms' at Curtin University, consisting of two questions worth a total of 50 marks. The test includes various problems related to algorithm complexity, recursion, and sorting algorithms such as Mergesort and Quicksort. Students are required to attempt all questions within a 70-minute timeframe, with specific instructions regarding allowed aids and the structure of answers.

Uploaded by

Mia Tran
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/ 12

CURTIN UNIVERSITY

School of Electrical Engineering, Computing, and Mathematical Sciences

Computing Discipline

Test 1 – Semester 1 2023


SUBJECT: Design and Analysis of Algorithm COMP3001

TIME ALLOWED:

70 minutes test preceded by a 5-MINUTE READING PERIOD


during which time only notes may be made. The supervisor will
indicate when answering may commence.

AIDS ALLOWED:

To be supplied by the Candidate: Nil


To be supplied by the University: Nil
Calculators are NOT allowed.

GENERAL INSTRUCTIONS:

This paper consists of Two (2) questions with a total of 50 marks.

ATTEMPT ALL QUESTIONS

Name: ____________________________________________

Student No: _______________________________________

Tutorial Day/Time/Tutor: __________________________________


Design and Analysis of Algorithms 2/12 Test 1 – Semester 1 2023

QUESTION ONE (28 marks)


a) (4 marks). Prove T(n) = n3/3 – 3n = O(n3).

Answer:

b) (Total: 11 marks). Consider the following two alternative recursive functions to


compute an, for a power of 2 positive integer n. For example, for a = 5 and n = 8,
compute 58 = 390625. Note: n = 8 = 23 (thus n is a power of 2), and n = 6 is NOT
a power of 2.

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

(ii) Time complexity.


Design and Analysis of Algorithms 4/12 Test 1 – Semester 1 2023

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
ji
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

d) (Total: 6 marks). Consider a recurrence T(n) = T(n/2) + n. Use induction to


prove that T(n) = O(n). You can assume T(1) = 1.

Answer:

END OF QUESTION ONE


Design and Analysis of Algorithms 6/12 Test 1 – Semester 1 2023

QUESTION TWO (Total: 22 marks).

a) (Total: 6 marks). Consider a set of n elements that are already sorted in


increasing order.

(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)

b) (Total: 8 marks). Consider a set of n integers in an array S and another integer x.


The problem is to determine whether there exist two elements in S whose
multiplication is exactly x. For example, for input S = (6, 2, 3, 3, 1, 7) and x = 6,
the solution to the problem is true because there are two elements in S, e.g., 6 and
1, whose multiplication is equal to x = 6. However, for x = 8, the solution should
returns false.

(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).

(ii) (4 marks). Trace your algorithm for input


• S = (6, 2, 3, 3, 1, 7) and x = 9 and
• S = (6, 2, 3, 3, 1, 7) and x = 8
Design and Analysis of Algorithms 7/12 Test 1 – Semester 1 2023

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.

(ii) (2 marks). The recurrence function of the time complexity of Strassen’s


algorithm is T(n) = 7T(n/2) + 18(n/2)2. Explain why the recurrence has the
7T(n/2)?

(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)

END OF QUESTION TWO


Design and Analysis of Algorithms 10/12 Test 1 – Semester 1 2023

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:

if T(n) = aT(n/b) + f(n) then

1) 𝑇(𝑛) = (𝑛𝑙𝑜𝑔𝑏𝑎 ) // case 1


if there exists a constant  such that f(n) = O(𝑛log𝑏𝑎− )

2) 𝑇(𝑛) = (𝑛𝑙𝑜𝑔𝑏𝑎 𝑙𝑔𝑘+1 𝑛) // case 2


if there exists a constant k ≥ 0 such that f(n) =(𝑛log𝑏𝑎 𝑙𝑔𝑘 𝑛)

3) 𝑇(𝑛) = (𝑓(𝑛)) // case 3


if there exists a constant  such that

(i) f(n) = Ω(𝑛log𝑏𝑎+ ), and

(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]

exchange A[i]  A[r]


return i

END OF TEST PAPER


Design and Analysis of Algorithms 12/12 Test 1 – Semester 1 2023

A blank page for students who need extra space for their answers

You might also like