Algorithms Midterm 2020
Algorithms Midterm 2020
8. ∑ lg 𝑖 ≈ 𝑛 lg 𝑛
𝑖=1
1. ∑ 𝑐𝑎𝑖 = 𝑐 ∑ 𝑎𝑖
𝑖=𝑙 𝑖=𝑙
𝑢 𝑢 𝑢
2. ∑(𝑎𝑖 ± 𝑏𝑖 ) = ∑ 𝑎𝑖 ± ∑ 𝑏𝑖
𝑖=𝑙 𝑖=𝑙 𝑖=𝑙
𝑢 𝑚 𝑢
3. ∑ 𝑎𝑖 = ∑ 𝑎𝑖 + ∑ 𝑎𝑖 , where 𝑙 ≤ 𝑚 < 𝑢
𝑖=𝑙 𝑖=𝑙 𝑖=𝑚+1
𝑢
P.1
Attention: 1. For fairness and justice, students should be self-disciplined to maintain school’s and students’ honor.
2. Chatting, taking examination papers out, peeping, and other misconduct are prohibited during the
examination. Violators will be seriously punished.
ATRX-Q03-001-FM256-01
1. Consider the following algorithm:
ALGORITHM: BruteForceStringMatch(T [0..n−1], P [0..m−1])
// Input: An text array T [0..n − 1] of n characters and an pattern array P [0..m−1] of m characters
for i 0 to n−m do
j 0
while j < m and P[ j ] = T [ i + j ] do
j j+1
if j = m then return i
return −1
(a) (18%) Search the pattern (P = A C D E ) in the text (T = A A C E D A C D E A C ) by the Brute Force
String Match algorithm, and record the values of the parameters of this algorithm in the following table
after the execution of each statement.
i j P[ j ] = ? T[ i + j ] = ? return
P.2
(b) (8%) How many Comparisons (both successful and unsuccessful) are made by the brute-force string-
matching algorithm in search for the pattern 0011100111 in the binary text of four hundred zeros?
You must show your work to receive credit. A correct answer without showing your reasoning
process will not receive credit.
P.3
2. Consider the following sorting algorithm: ALGORITHM Y
// Input: An array A[0..n − 1] of orderable elements
for i 0 to n − 2 do
for j 0 to n − 2 − i do
if A[ j + 1 ] < A[ j ]
swap A[ j ] and A[ j + 1 ]
(a) (15 %) Apply this algorithm to the input array A = [45, 55, 35, 55, 15], and record the values of the
parameters of this algorithm in the following table after the execution of each statement.
P.4
(c) (8%) Consider the input size as n and the basic operation as the key comparison A[ j + 1 ] < A[ j ].
How many times is the basic operation executed? You must show your work to receive credit.
A correct answer without showing your reasoning process will not receive credit.
P.5
Multiple-choice Questions (48%)
( C ) 1. What is the order of growth of the function C(𝑛) = ∑𝒏𝒊=𝟎 ∑𝒏𝒋=𝟏 (𝒊 ∗ 𝒋) ?
(A) C(n) (n2) (B) C(n) (n3)
(C) C(n) (n4) (D) C(n) (n5)
(Question 2 – 3) Consider the following algorithm for checking whether all the elements in a given array
are distinct.
//Input: An array A[0…(n–1)]
//Output: Returns “true” if all the elements in A are distinct and “false” otherwise
for i 0 to n–2 do
for j X to Y do
if A[i ] = A[j ] return false
return true
( D ) 2. Which of the following is correct?
(A) X = 0, Y = n–2 (B) X = i, Y = n–2
(C) X = 1, Y = n–1 (D) X = i + 1, Y = n–1
( B ) 3. Apply this algorithm to the array A = [40, 10, 50, 30, 50, 90]. How many comparisons
(A[i ] = A[j ]) are needed before returning false?
(A) 15 (B) 11
(C) 10 (D) 3
( D ) 4. Which of the following functions has the largest order of growth?
(A) log2 (n3 + n + 1) (B) log10 (n2 + 1)2
(C) ln (n + 10)3 (D) They have the same order of growth
( C ) 5. Which of the following is wrong?
(A) If f1(n) O(g1(n)) and f2(n) O(g2(n)), then [f1(n) + f2(n)] O( max{g1(n) + g2(n)})
(B) If f(n) O(g(n)), then g(n) (f(n))
(C) If f(n) O(g(n)), then f(n) (g(n)) and f(n) (g(n))
(D) If f(n) (g(n)), then g(n) O(f(n))
(Question 6 – 7) (Tower of Hanoi) We have n disks of different sizes and three pegs. Initially, all the disks are
on the first peg (peg 1) in order of size, the largest on the bottom and the smallest on top. The
objective of this puzzle is to move the entire stack to peg 3, obeying the following three rules:
(1) Move one disk at a time. (2) Larger disk cannot be placed on top of a smaller one.
(3) Using the peg 2 as an auxiliary.
( A ) 6. How many moves at least are required at least to move 3 disks from peg1 to peg3?
(A) 7 (B) 15
(C) 8 (D) 16
( B ) 7. The time complexity for the Towers of Hanoi problem with n disks is .
(A) O(n2) (B) O(2n)
(C) O(n3) (D) O(3n)
( A ) 8. Which of the following is wrong?
(A) n(n – 1)/2 (n3) (B) n(n – 1)(n + 1)/2 (n3)
(C) n(n – 1)/2 O(n3) (D) n(n – 1)(n + 1)/2 (n3)
P.6