10910 計算方法設計 許健平 mid1
10910 計算方法設計 許健平 mid1
10910 計算方法設計 許健平 mid1
Suggested Solutions
(2020/11/04)
(Note that, if you design an algorithm, you must have pseudo code to represent
your algorithm and analyze the algorithm time complexity. You can put comments
after your pseudocode to clarify your presentation.)
1. (Chapter 2)(10%) Multiple choice questions (You need to explain your answer
briefly. Otherwise, you cannot get a score.)
(a) (5%) How many inversions are in the array [5, 9, 4, 8, 7]? (A) 1 (B) 2 (C) 3 (D) 4 (E) 5
(b) (5%) Suppose we have 28 identical looking balls numbered 1 through 28, and only
one of them is a counterfeit ball whose weight is heavier than others. Suppose further
that you have one balance scale. What is the minimum number of weighing times
required to find the counterfeit ball in the worst case? (A) 1 (B) 2 (C) 3 (D) 4 (E) 5
(a) (E): There are five inversions in the array: (5, 4), (9, 4), (9, 8), (9, 7), (8, 7).
(b) (D): If there are n balls, we can remove at least ہ2𝑛/3 ۂballs for each weighting.
Therefore, the number of weighting is 𝑔𝑜𝑙ڿ3 28 = ۀ4.
a. False.
Let 𝑓ሺ𝑛ሻ = 𝑛2 and 𝑔ሺ𝑛ሻ = 𝑛.
𝛩(𝑚𝑖𝑛(𝑓ሺ𝑛ሻ, 𝑔ሺ𝑛ሻ)) = 𝛩ሺ𝑛ሻ
𝛩(𝑓ሺ𝑛ሻ + 𝑔ሺ𝑛ሻ) = 𝛩ሺ𝑛2 ሻ
b. False.
Let 𝑓ሺ𝑛ሻ = 2𝑛 and 𝑔ሺ𝑛ሻ = 𝑛.
2𝑛 = 𝑂ሺ𝑛ሻ, but 22𝑛 ≠ 𝑂ሺ2𝑛 ሻ
1
3. (Chapter 4)(6%) We saw that the solution of 𝑇ሺ𝑛ሻ = 2𝑇(උ𝑛ൗ2ඏ) + 𝑛 is O(n lg n).
Show that the solution of this recursion is also Ω(n lg n). Conclude that the solution
is Θ(n lg n).
Let’s guess 𝑇ሺ𝑛ሻ ≥ 𝑐ሺ𝑛 + 2ሻlgሺ𝑛 + 2ሻ, for some constant 𝑐 and sufficiently large 𝑛.
𝑇ሺ𝑛ሻ ≥ 2𝑐ሺ𝑛ہ⁄2 ۂ+ 2ሻሺlg𝑛ہ⁄2 ۂ+ 2ሻ + 𝑛
≥ 2𝑐ሺ𝑛/2 − 1 + 2ሻሺlgሺ𝑛/2 − 1 + 2ሻ + 𝑛
𝑛+2 𝑛+2
= 2𝑐 lg +𝑛
2 2
= 𝑐ሺ𝑛 + 2ሻlgሺ𝑛 + 2ሻ − 𝑐ሺ𝑛 + 2ሻlg2 + 𝑛
= 𝑐ሺ𝑛 + 2ሻlgሺ𝑛 + 2ሻ + ሺ1 − 𝑐ሻ𝑛 − 2𝑐
2𝑐
≥ 𝑐ሺ𝑛 + 2ሻlgሺ𝑛 + 2ሻ for 𝑛 ≥ 1−𝑐 and 0 ≤ 𝑐 < 1.
Since T(n) = Ω(n lg n) and T(n) = O(n lg n), we have T(n) = Θ(n lg n).
5. (Chapter 6)(10%) Give an 𝑂ሺ𝑛 𝑙𝑔 𝑘ሻ-time algorithm to merge 𝑘 sorted lists into
one sorted list, where 𝑛 is the total number of elements in all the input lists. (Hint:
use a min-heap for 𝑘 -way merging)
K_Way_Merge(A):
Let the k lists be A[1], A[2] …, A[k]
Let A[i][j] denote the jth element of the i-th list.
Prepare k integers p[1] = p[2] = … = p[k] = 1
Mark each element A[i][j] = i for all j
Prepare a min heap m
Prepare a length n list result
Let the 𝑘 lists be 𝐴1 , 𝐴2 , … , 𝐴𝑘 . For all elements in these lists, we mark them with the list
index 𝑖 if they belong to 𝐴𝑘 (i.e., all elements in 𝐴3 are marked with 3). We take the smallest
element from each list (i.e., 𝐴𝑖 ሾ1ሿ for all 𝑖 ’s) and insert them into a min heap
(time complexity = 𝑂ሺ𝑘 ⋅ lg 𝑘ሻ) so that the heap has 𝑘 elements. Then repeat the following
step 𝑛 times: (1) Extract an element 𝑥 from the min heap. (2) Suppose 𝑥 came from the list
𝐴𝑖 . If 𝐴𝑖 is not empty, we then take the next element from 𝐴𝑖 and insert it into the min heap.
2
The time complexity of each insertion and extraction of elements from the heap is 𝑂ሺlg 𝑘ሻ.
Thus, the total time complexity is 𝑂ሺ𝑘 lg 𝑘ሻ + 𝑂ሺ𝑛 lg 𝑘ሻ = 𝑂ሺ𝑛 lg 𝑘ሻ.
The worst cast happens when the sequence is sorted, or sorted in reverse order (i.e.,
ሾ1, 2, 3, 4, 5, 6ሿ or ሾ6, 5,4,3,2,1ሿ). Each PARTITION picks the smallest or the largest
element, thus producing two partitions one partition has only one element and another
one has 𝑛 − 1 elements. The upper-bounded time complexity of the QUICKSORT
can be expressed as follows:
𝑇ሺ𝑛ሻ ≤ 𝑐𝑛 + 𝑇ሺ𝑛 − 1ሻ ≤ 𝑐ሺ𝑛 + ሺ𝑛 − 1ሻ + ⋯ + 1ሻ = 𝑂ሺ𝑛2 ሻ
First, we make an array C such that C[i] is the number of elements less than or equal
to i in the array. When queried about how many integers fall into a range [a .. b], we
can simply compute C[b] - C[a - 1].
The preprocess has one 𝑂ሺ𝑛ሻ loop and one 𝑂ሺ𝑘ሻ loop, so the time complexity of the
preprocess is 𝑂ሺ𝑛 + 𝑘ሻ. The time complexity of each query is clearly 𝑂ሺ1ሻ.
8. (Chapter 8)(12%) Design an O(N ) algorithm that solves the following problem and
write the pseudocode. Given an array A of length N with all elements in the range
[1, N] and a nonnegative integer X, count the number of pairs (i, j) such that 1 ≤ i <
j ≤ N, and |A[i] − A[ j]| ≥ X. For example, if the array is [3, 1, 4, 1, 5] and X = 2
then the answer is 7 since the possible (i, j) are (1, 2), (1, 4), (1, 5), (2, 3), (2, 5), (3,
4), and (4, 5).
3
the amount of elements in 𝐴 that does not exceed 𝑖 . Then iterate over each elements
𝐴ሾ𝑖ሿ, and suppose 𝐴ሾ𝑖ሿ is the bigger one in the pair. Then the other element must not
exceed 𝐴ሾ𝑖ሿ − 𝑋. There are 𝐵ሾ𝐴ሾ𝑖ሿ − 𝑋ሿ elements satisfying the condition, so add it to
the answer. Check the below C program for details.
(To make sure there is no out of bound error, write 𝑚𝑎𝑥ሺ0, 𝐴ሾ𝑖ሿ − 𝑋ሻ instead of
𝐴ሾ𝑖ሿ − 𝑋. However, this is not required since this is pseudocode.)
Use SELECT to choose the r-th smallest element R in S. Create a new array S’ in
which each element is the absolute value of the original value subtracting R.
Select the (n-k)-th smallest element M in S’. The original value of the elements larger
than M are the answer.
Function (S, r, k)
N = size of S
R = SELECT( S, r )
S’ = [] // empty array
foreach e in S
add abs(e – R) to S’
M = SELECT( S’, N - k )
ANS = []
foreach e in S’
if e > M
original_e = e’s original value in S
add original_e to ANS
return ANS
(12%) Given a set 𝑆 of size 𝑛, and a real number 𝑥, we seek to design an algorithm to
determine whether there are 2 integers in 𝑆 whose sum is exactly 𝑥 (find them if they
exist).
a. (6%) Suppose the array is not sorted. Give an 𝑂ሺ𝑛 𝑙𝑔 𝑛ሻ time algorithm to
solve the problem. Prove the time complexity of your algorithm correctly.
b. (6%) Suppose the array is sorted. Give an 𝑂ሺ𝑛ሻ time algorithm to solve the
4
problem. Prove the time complexity of your algorithm correctly.
(Hint: consider a sequence 𝑇, where 𝑇 ሾ𝑖 ሿ = 𝑥 − 𝑆ሾ𝑖ሿ)
a.
First, sort the array using any 𝑂ሺ𝑛 ⋅ lg 𝑛ሻ algorithm. For each element 𝐴ሾ𝑖ሿ in the
array, search for 𝑥 − 𝐴ሾ𝑖ሿ using binary search. Any results found in the searches is a
valid solution.
b.
Solution 1:
Define 𝑇ሾ𝑗ሿ as 𝑥 − 𝑆ሾ𝑗ሿ for all 𝑗, being a monotonically decreasing sequence.
Then for all 𝑖, 𝑗, if 𝑆ሾ𝑖ሿ = 𝑇ሾ𝑗ሿ then 𝑆ሾ𝑖ሿ + 𝑆ሾ𝑗ሿ = 𝑥
Starting from 𝑖 = 1, 𝑗 = 𝑛, if 𝑆ሾ𝑖ሿ < 𝑇ሾ𝑗ሿ we increase 𝑖, trying to find a matching
element. Decrease 𝑗 if 𝑆ሾ𝑖ሿ > 𝑇ሾ𝑗ሿ.
The iteration is at most 2𝑛 for 𝑖 and 𝑗 with constant operations. Thus the time
complexity is 𝑂ሺ𝑛ሻ
Let T[j] = x – S[j] for all j
Let i = 1, j = n
While i <= n && j >= 1:
If S[i] == T[j]:
Return (i, j)
Else:
If S[i] < T[j]: i++
Else: j--
Return false
Solution 2:
We can also find the matching elements from the two sides of the sequence, where for
each 𝑖, we linearly search for a matching 𝑗 s.t. 𝑆ሾ𝑖ሿ + 𝑆ሾ𝑗ሿ = 𝑥.
If it is not found, let 𝑗 be the element where 𝑆ሾ𝑖ሿ + 𝑆ሾ𝑗ሿ is just smaller than 𝑥. With the
fact that all 𝑆ሾ𝑗ሿ's right to that element cannot match with larger 𝑆ሾ𝑖ሿ's, the iteration of
𝑗 does not have to be reset to 𝑛 every time. Then we proceed to the next 𝑖, and repeat
the process.
Since the iteration of 𝑖 and 𝑗 are both linear and stops when the two collide, there are
at most 𝑐𝑛 operations. Thus the complexity is 𝑂ሺ𝑛ሻ
int i = 1, j = n;
for (i = 1; i < j; i++)
{
while (i < j && S[j] + S[i] > x) j--;
if (x == S[j] + S[i])
return (i, j);
}
return false;
11. (Chapter 9)(12%) SELECT works with groups of 5 elements. Prove that SELECT
cannot work in worst-case linear time with groups of 3 elements with the
following questions.
5
a. (4%) After choosing the median of medians, divide the original group into 𝑋
and 𝑌. 𝑀𝑎𝑥 ሺ𝑋, 𝑌 ሻ =?
b. (4%) T(n) =? Briefly explain each term.
c. (4%) Disprove by assuming 𝑇ሺ𝑛ሻ ≤ 𝑐𝑛 and getting a conflict.
1 𝑛 2
a. 𝑚𝑎𝑥{|𝑋|, |𝑌|} = 𝑛 − 2 ∗ ሺ⌈2 ⌈3 ⌉⌉ − 2ሻ ≤ 3 𝑛 + 4
|𝑆|
b. 𝑇ሺ|𝑆|ሻ = 𝑂ሺ|𝑆|ሻ + 𝑇ሺ⌈ ⌉ሻ + 𝑇ሺ𝑚𝑎𝑥{|𝑋|, |𝑌|}ሻ
3
c.
𝑛 2
𝑇ሺ𝑛ሻ = 𝑂ሺ𝑛ሻ + 𝑇 (⌈ ⌉) + 𝑇ሺ 𝑛 + 4ሻ
3 3
𝑛 2
≤ 𝑎𝑛 + 𝑐 ⌈ ⌉ + 𝑐 ( 𝑛 + 4)
3 3
𝑛 2
≤ 𝑎𝑛 + 𝑐 ( + 1) + 𝑐 ( 𝑛 + 4)
3 3
= 𝑐𝑛 + ሺ𝑎𝑛 + 5𝑐ሻ
To make 𝑇ሺ𝑛ሻ ≤ 𝑐𝑛 → 𝑎𝑛 + 5𝑐 ≤ 0
Conflict. No constant c can make 𝑎𝑛 + 5𝑐 ≤ 0