11110 計算方法設計 許建平 quiz1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

1. (10%) Please complete the following table.

Answer:

Insertion sort Merge sort Heap sort Quick sort

Average case O(n2) O(n lg n) O(n lg n) O(n lg n)

Worst
O(n2) O(n lg n) O(n lg n) O(n2)
case

Stable or not Stable Stable Not stable Not stable

2. (Chapter 3 5%) Prove or Disprove: f(n) = Ө(g(n)) implies h(f(n)) = O(h(g(n)), where
h() is an increasing function.
Answer:
Assume f(n) = 2n, g(n) = n, h(n) = 2n
We have f(n) = 𝜃𝜃(g(n)). But h(f(n)) = 22n ≠ O(h(g(n))=O(2n)

3. (Chapter3 5%) Explain why the statement, "The running time of algorithm A is
at least O(n)," is meaningless.
Answer:
The definition of big-O is f(n) = O(g(n)) if and only if there exist two positive constants
c and n 0 , such that f(n) ≤ c∙g(n) for all n ≥ n 0 . Therefore, f(n) = 0 for all n is in O(n).
So, when you say the running time of algorithm A is "at least" O(n), the time complexity
of algorithm A can be any time complexity bigger than function f(n) = 0. Since running
times are nonnegative, the statement tells us nothing about the running time.
4. (Chapter 4 10%) Using the master method in Section 4.5, you can show that the
solution to the recurrence T (n) = 4T (n /2) + n is T (n) = Θ(n 2). Show that a
substitution proof with the assumption T(n) ≤ cn2 fails. Then show how to
subtract off a lower-order term to make a substitution proof work.
Answer:
Show that a substitution proof with the assumption T(n) ≤ cn2 fails:
Assume T (n) ≤ cn2 for all n ≥ n 0 , where c ≥ 0, n 0 ≥ 0.
T (n) = 4T (n / 2) + n
≤ 4c(n / 2)2 + n
= cn2 + n
With this, we haven't proved the exact form of the inductive hypothesis.

Show how to subtract off a lower-order term to make a substitution proof work:
Assume T (n) ≤ cn2 – bn for all n ≥ n 0 , where b ≥ 0, c ≥ 0, n 0 ≥ 0.
T (n) = 4T (n / 2) + n
≤ 4[ c(n / 2)2 – b(n / 2) ] + n
= cn2 – 2bn + n
= cn2 – bn – (b – 1)n
When (b – 1) ≥ 0, we have T (n) ≤ cn2 – bn,
so if we set n 0 = 1, our hypothesis works for any b ≥ 1.

5. (Chapter 6 10%) With what kind of input can we get the best case and worst case
of the running time of Heapsort? Show the time complexity and explain why?
Answer:
Suppose we use min heap to do Heapsort.
Worst case :
All elements in the list are distinct.
Once we do Extract-Min, we get an element from the leaf to heapify the heap tree.
The number of swap elements is lg n times.
So, the time complexity of Extract-Min is O(lg n).
Therefore, the total time complexity of Heapsort is n x O(lg n) = O(n lg n)

Best case :
All elements in the list are identical.
Once we do Extract-Min, we don't have to swap any elements when we do heapify.
The time complexity of Extract-Min is O(1).
The total time complexity of heap sort is O(n).
6. (Chapter 7 20%)
a. (5%) When all elements of array A have the same value, what is the running time of
QUICKSORT with the following PARTITION (A, p, r) procedure?
b. (10%) Please modify the PARTITION procedure to produce a procedure PARTITION'
(A, p, r) such that the QUICKSORT performs better than using PARTITION. You need to
provide the pseudo codes of PARTITION' (A, p, r) and QUICKSORT(A, p, r).
c. (5%) What is the time complexity QUICKSORT with the procedure PARTITION' when
all array A elements have the same value?

Answer:
(a) The partition will take N times and return N, so in the next iteration, we will have
to do Quicksort(A, 0, N-1) and Quicksort(A, N, N), which will take N-1 times.
Hence, the total run time is N+(N-1)+(N-2)+…..1, which is O(N2)

(b)
PARTITION(A, p, r)
1 x = A[r]
2 i=p-1
3 for k = p to r-1
4 if A[k] < x
5 i=i+1
6 exchange A[i] with A[k]
7 j=i
8 for k = i+1 to r-1
9 if A[k] == x
10 j = j+1
11 exchange A[j] with A[k]
12 exchange A[j+1] with A[r]
13 return [i+1, j+1]
QUICKSORT(A, p, r)
1 if (p < r)
2 a1, a2 = PARTITION(A, p, r)
3 QUICKSORT(A, p, a1-1)
4 QUICKSORT(A, a2+1, r)

In the normal case, it can perform as usual. For the case with a large portion of the
same value, it can save time on the modified partition.

(c) With the whole array with the same value, the time complexity is O(N) because,
after the first partition, we get Quicksort(A,0,0) and Quicksort(A, N, N), so we
only take O(n) for the first partition.

7. (Chapter 8 10%) Suppose we have a set S of n integers, and each integer is in the
range from [0. . 𝑘𝑘]. We want to preprocess the set S, such that when a user gives us
any integers 𝑎𝑎 and 𝑏𝑏 where 1 ≤ 𝑎𝑎, 𝑏𝑏 ≤ 𝑘𝑘, we can use Ο(1) time to count how
many integers in S are within the range [𝑎𝑎. . b]. Show how to accomplish the task
using Ο(𝑛𝑛 + 𝑘𝑘) preprocessing time.
Answer:
1. Count how often value i appears in S to generate an array 𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶[0. . 𝑘𝑘] such that:
𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶[𝑖𝑖] = # items in 𝑆𝑆 with value 𝑖𝑖
It takes O(𝑛𝑛) time.
2. Compute the prefix sum of array Count and store in an array 𝑆𝑆𝑆𝑆𝑆𝑆[0. . 𝑛𝑛] such
that: 𝑆𝑆𝑆𝑆𝑆𝑆[i] = 𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶[0] + ⋯ + 𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶𝐶[𝑖𝑖]
It takes O(𝑘𝑘) time.
Total running time: O(𝑛𝑛) + O(𝑘𝑘) = O(𝑛𝑛 + 𝑘𝑘).
To count the number of integers that are within the range [𝑎𝑎. . 𝑏𝑏], we return
𝑆𝑆𝑆𝑆𝑆𝑆[𝑏𝑏] − 𝑆𝑆𝑆𝑆𝑆𝑆[𝑎𝑎 − 1]. It takes O(1) time.
8. (Chapter 8 10%) Please give a decision tree for insertion sort operating on four
elements a, b, c, and d.
Answer:

9. (Chapter 9 10%) The SELECT algorithm divides the input elements into groups of
5. Will the algorithm work in linear time if they are divided into groups of 7?
Please prove your answer.
Answer:
Assume there are n input elements, and m is the median-of-median. There are at least
𝑛𝑛 1 7
half of the subgroups � � ∗ having half of the items � � = 4 greater than m. Except
7 2 2

one group has fewer than 7 items if 7 can't divide n exactly, and one group contains m.
𝑛𝑛 1 4
4 ∗ ��� � ∗ � − 2� ≥ 𝑛𝑛 − 8
7 2 14
4
Similarly, at least 𝑛𝑛 − 8 items are smaller than m.
14

Let 𝑇𝑇(𝑛𝑛) be the worst-case running time of the SELECT algorithm.


10
Since we'll never call recursion with more than 𝑛𝑛 + 8 items,
14

𝑛𝑛 10
then 𝑇𝑇(𝑛𝑛) ≤ 𝑇𝑇 � � + 𝑇𝑇( 𝑛𝑛 + 8) + Ο(𝑛𝑛).
7 14

Suppose 𝑇𝑇(𝑛𝑛) ≤ 𝑐𝑐𝑐𝑐, for some constant 𝑐𝑐 > 0. We use the substitution method to
solve the recurrence relations.
𝑛𝑛 10
𝑇𝑇(𝑛𝑛) ≤ 𝑇𝑇 �� �� + 𝑇𝑇 � 𝑛𝑛 + 8� + Ο(𝑛𝑛)
7 14
𝑛𝑛 5𝑛𝑛
≤ 𝑐𝑐 � � + 𝑐𝑐 � + 8� + a𝑛𝑛
7 7
𝑐𝑐𝑐𝑐 5𝑐𝑐𝑐𝑐
≤ + 𝑐𝑐 + + 8𝑐𝑐 + 𝑎𝑎𝑎𝑎
7 7
6𝑐𝑐𝑐𝑐
= + 9𝑐𝑐 + 𝑎𝑎𝑎𝑎
7
𝑐𝑐𝑐𝑐
= 𝑐𝑐𝑐𝑐 + �− + 9𝑐𝑐 + 𝑎𝑎𝑎𝑎�
7
𝑐𝑐𝑐𝑐
≤ 𝑐𝑐𝑐𝑐 , if − + 9𝑐𝑐 + 𝑎𝑎𝑎𝑎 ≤ 0.
7

𝑐𝑐𝑐𝑐 𝑛𝑛
→ �− + 9𝑐𝑐 + 𝑎𝑎𝑎𝑎� = 𝑐𝑐 �− + 9� + 𝑎𝑎𝑎𝑎 ≤ 0
7 7
7𝑎𝑎𝑎𝑎
→ 𝑐𝑐 ≥ � � when 𝑛𝑛 > 63
𝑛𝑛−63

𝑛𝑛
If we assume 𝑛𝑛 > 2 ∗ 63 = 126, we have � � ≤ 2. So we choose 𝑐𝑐 ≥ 14𝑎𝑎.
𝑛𝑛−63

The conditions for choosing c can be satisfied, and the SELECT algorithm still runs in
linear time.

10. (Chapter 9 10%) Given a set of n numbers, we wish to find the first k largest
numbers in sorted order using a comparison-based algorithm. Find the algorithm
that implements each of the following methods with the best asymptotic worst-
case running time, and analyze the running times of the algorithms in terms of n
and k.
a. Sort the n numbers, and list the k largest numbers.
b. Build a max-priority queue from the n numbers, and call EXTRACT-
MAX k times.
c. Use an order-statistic algorithm to find the kth largest number, partition
the n numbers around the kth largest number, and sort the k largest
numbers.
Answer:
a. Sorting takes time n lg n, and listing them out takes time k, so the total runtime is
O(n lg n + k) = O(n lg n)
b. BUILD_MAX_HEAP takes time O(n), and each Extract-Max takes time lg n, so
the total runtime is O(n + k lg n)
c. Find and partitioning around the kth largest takes time n. Then, sorting the
subarray of length k coming from the partition takes time k lg k. So, the total
runtime is O(n + k lg k).

You might also like