





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This is the quiz on different topics of Big O notation, time complexity, code analysis,....
Typology: Quizzes
1 / 9
This page cannot be seen from the preview
Don't miss anything!






CS456 – Algorithm Analysis & Design - Quiz 02/
QUIZ 2
DATE: 29th September 2023
TOTAL = 5 0 Points
DURATION: 1hr 3 0 MINUTES
GROUP/COHORT: …………………… STUDENT NAME:
…………………………
Q1. A games company has developed a game called Kidz Arrowz. The players throw an arrow at a target board
and are awarded different points depending on which circle the arrow lands. Fig. 1 shows the board. A
computer program is required to keep track of the scores for each competition. The user will enter the
number of players, and the name of each player, in that competition to a maximum of 10.
(i) The programmer has decided to use a bubble sort to sort the players’ scores in descending order of score. Describe the disadvantages of using a bubble sort. [2 pts]
(ii) Despite the disadvantages, the programmer has decided to use a bubble sort for the players’ scores. Identify the characteristic of this problem that minimizes the disadvantages of a bubble sort. [1 pt]
CS456 – Algorithm Analysis & Design - Quiz 02/
(iii) Write a procedure/function/method/pseudocode, sortScores, to perform a bubble sort on the global array scores to sort the players’ scores into descending numeric order. [6 pts]
Sorts scores in reverse order (descending numeric order )
Algorithm sortScores(A[0..n-1]) for i <- 0 to n-1 do for j<-0 to n-i-1 do if(A[j]<A[j+1]) A[j], A[j+1] = A[j+1], A[j] , swap
OR
Algorithm sortScores(int []scores, int n) int k, j, temp; for(k ← n; k >= 0; k--) for(j ← n-2; j >= 0; j--) if(scores[j] < scores[j+1]) temp = scores[j]; scores[j] = scores[j+1]; scores[j+1] = temp; endif endfor endfor endAlg
CS456 – Algorithm Analysis & Design - Quiz 02/
A C B D F E
Q3. The adjacency matrix of a certain directed graph is shown below. Answer the
following questions concerning this adjacency matrix.
a. Draw the given graph whose adjacency
matrix is represented here [5 pts]
b. Find a topological sort for this matrix(DAG) [4 pts]
C h a d e f i g b
CS456 – Algorithm Analysis & Design - Quiz 02/
Q4. Using the Euclidean Algorithm ( see in the insert below) , find the greatest common
divisor (gcd) of 44 and 14 [4 pts]
Algorithm gcd(m, n) if (n == 0) return m else n = m; m = m % n gcd(n, m ) endAlg
gcd (44, 14)
= gcd(14, 2)
=gcd(7,0)
Hence gcd(44,14) = 2
Because there was an error in the code, accept
those who produce 0 as an answer per the code
instruction or 44.
CS456 – Algorithm Analysis & Design - Quiz 02/
Q7. A painter, Yaro, has been contracted to paint all the pigeons in a fictitious African
country called Sikakrom. While he was painting, there was a call from his boss. When he
returned to continue the painting, he had forgotten which pigeon was the last one he
painted. So, he wanted to start all over again, one after the other but you had a better
plan to assist Yaro. Suggest an algorithm and sketch the algorithm/pseudo-code below.
What is the time complexity of your suggested algorithm? Assume that the pigeons were
in a linear holes ( as shown below ) Suggestion[2 pt]: Binary Search
P1 (^) P2 P3 P4 … Pn-1 (^) Pn
Algorithm PigeonPaintingAlgorithm() [5 pts]
Algorithm BinarySearch( A, k, low , high):
if low > high then
return null
else
mid ← (low + high) / 2 ___
if k = key(mid) then
return elem(mid)
else if k < key(mid) then
return BinarySearch( A, k, low , mid − 1)
else
return BinarySearch( A, k, mid + 1 , high)
Time Complexity : O(log n) + (O(1) for checking the next cell.
This means 2*O(log n) This is simply an adaption of binary search and peeping to
see who is the next ( whether painted or not).
CS456 – Algorithm Analysis & Design - Quiz 02/
Q8. Find the time complexity of the following recurrence relations using the Master’s
theorem. [6 pts]
a. T(n) = 4T(n/2) + n.log n
We have a = 4, b = 2, k = 1 and p = 1. Compare a and b
k
. we have 4 > 2
1
hence case 1 which is given by: T(n) = Q( n
log b
a ) according to Master’s
theorem
Hence the answer is T(n) = ( n
log 2
4 )
=> T(n) = (n
2 )
b. T(n) = ½T(n/2) * 2
Not solvable by the Master’s theorem as a = 0.5 which is less than 1.
No solution.
For master’s theorem to work, T(n) = aT(n/b) + f(n), where f(n) is
given by (n
k log
p n), a >= 1, b > 1, k >= 0 and p is a real
number. Since a <= 1, we cannot use this theorem
c. T(n) = 3T(n/3) + n
We have a = 3, b = 3 k = 1, and p = 0
Compare a and b
k
. i.e. 3 = 3
1 , and p = 0 > -2 hence case 2a. Therefore, the answer is T(n) = ( nlogba^ logp+1^ n)
i.e. T(n) = ( n
log 3
3 log
0+ n)
= ( n
1 log
1 n)
= ( n log n)