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

DAA-Test-1-2022(2)

Uploaded by

niggy0705
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)
18 views

DAA-Test-1-2022(2)

Uploaded by

niggy0705
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 2022

SUBJECT: Design and Analysis of Algorithm COMP3001

TIME ALLOWED:

60 minutes test. 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 Time/Tutor: __________________________________


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

QUESTION ONE (Total: 28 marks)

a) (7 marks). Consider function f(n) = n2 + 3n + 20 to answer the following


questions.

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

(ii) (6 marks). Formally prove or disprove the correctness of each of the


following two equations.

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

(ii) Prove or disprove.

• 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:

c) (4 marks). Can Master method be used to solve the following recurrence?


T(n) = 8T(n/4) + nlgn

If it can, use the Master method to compute its solution. Otherwise, explain why it
cannot be used to solve it.

Hint. log4 8 = 1.5; log8 4 = 0.67


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

Answer:

d) (6 marks). Prove by induction that T(n) = 4T(n/4) + n2 = O(n2).

Answer:
Design and Analysis of Algorithms 5/12 Test 1 – Semester 1 2022

e) (4 marks). Explain / show if the following statement is True or False.


Given n integers a1, a2, . . . , an, the third smallest number among them can be
computed in O(n) time.

Note: You are not required to explain your answer in a pseudocode.

Answer:

=============================================================
END OF QUESTION ONE
Design and Analysis of Algorithms 6/12 Test 1 – Semester 1 2022

QUESTION TWO (Total: 22 marks).

a) (Total: 5 marks). Consider the following pseudocode of the insertion sort


algorithm.

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

b) (Total: 8 marks). Consider an array A of n integers, and another array B of n


integers. The array A contains n consecutive odd numbers in increasing order,
i.e., A = <1, 3, 5, …>, while array B contains n consecutive even numbers in
decreasing order, i.e., B = <… 6, 4, 2>. Suppose you want to combine the
contents of the two arrays and sort them in increasing order, i.e., the result is an
array that contains <1, 2, 3, …, 2n>. For example, for n = 4, you have A = <1, 3,
5, 7>, B = <8, 6, 4, 2>, and the sorted result is <1, 2, 3, 4, 5, 6, 7, 8>.

(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

c) (Total: 9 marks). Let A and B be two n × n matrices, where n is a power of 2,


and C = A × B. Using the divide and conquer method, we divide A and B, and their
product C each into four n/2 × n/2 matrices, and thus C = A × B becomes

!!! !!" ! ! ! !
= !! !" !×! !! !" !
!!" !!! !!" !!! !!" !!!
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

The following is the Strassen’s algorithm as discussed in lecture.

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). The recurrence of the conventional method is T(n) = 8T(n/2) +


4(n/2)2. Compute its asymptotic tight bound time complexity.

(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

P1 = A11× S1 P2 = S2 × B22 P3 = S3 × B11 P4 = A22× S4


P5 = S5 × S6 P6 = S7 × S8 P7 = S9 × S10

C11 = P5 + P4 – P2 + P6 C12 = P1 + P2 C21 = P3 + P4 C22 = P5 + P1 – P3 – P7

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:

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

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

exchange A[i] « A[r]


return i

SELECTION_SORT (A[1, …, n])


Input: unsorted array A Output: sorted array A
1. for i ¬ 1 to n-1
2. small ¬ i
3. for j ¬ i+1 to n // Set small as the pointer to the smallest element in A[i+1..n]
4. if A[j] < A[small] then
5. small ¬ j
6. temp ¬ A[small] // Swap A[i] and smallest
7. A[small] ¬ A[i]
8. A[i] ¬ temp

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

You might also like