Download doc

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Algorithm wikipedia , lookup

Numerical continuation wikipedia , lookup

Recurrence relation wikipedia , lookup

Factorization of polynomials over finite fields wikipedia , lookup

Weber problem wikipedia , lookup

System of linear equations wikipedia , lookup

Transcript
Algorithms’99
Midterm Exam (Yen)
Solution
1. (10 pts) Prove that (2n+1)2=O(n2) by deriving the constants c and n0
in the definition of O-notation. Show your derivation in sufficient detail.
(Solution) (2n+1)2 <= cn2. Pick c=9 and n0=1, then (2n+1)2 <= 4n2+4n+1
<= 4n2+4n2+1n2=9n2.
2. (15 pts) Consider the following algorithm to sort the values in array
A[p..q]. To sort the entire array, call New-Sort(A, 1, n). (
denotes
`assignment’ and
represents `swap’.)
New-sort(A, p, q)
Begin
If p > q then return endif
i
index of minimum value of A[p..q]
j
index of maximum value of A[p..q]
A[p]
A[i]
A[q]
A[j]
New-sort(A, p+1, q-1)
end
(a) Write down a recurrence for the worst case running time of this
algorithm, and solve it.
(Solution) T(n)=T(n-2)+(n); T(1)=1, T(0)=1.
(b) Is New-Sort stable? Justify your answer.
(Solution) No. For example, 22134  12234 …
3. (10 pts) Give an algorithm with worst-case running time O(n log n) to
determine the number of inversions in an array A of n distinct integers.
(A pair (i, j) is an inversion if i < j and A[i] > A[j].) Hint: Use
divide-and-conquer (analogous to the idea of merge-sort).
(Solution) The simplest idea is to modify merge-sort to compute the
number
of
inversions.
Suppose A=BC,
then
inv(A)=inv
(B)+inv(C)+inversions from B to C. Given B and C sorted in
nondecreasing order, inversions from B to C can be calculated as follows.
In the process of the merge, if B[i] > C[j] , then C[1..j-1] are less than
B[i], so the number of inversions having i in the first component is j-1.
4. (30 pts) Indicate whether each of the following statements is true or
false. If the statement is true, briefly state why. If the statement is false,
correct it. The more content you provide in your justification or
correctness, the higher your grade, but be brief. One-sentence
explanations should suffice.
1. Using the Master theorem, the solution to the recurrence
T(n)=3T(n/3)+O(log n) is T(n)=(n log n).
(Solution) False. T(n)=(n).
2. The worst-case running time of randomized quick-sort on an array of
length n is (n2).
(Solution) True.
3. The (log log n)-th smallest element of n unsorted numbers can be
determined in O(n) worst-case time.
(Solution) True.
4. Strassen’s algorithm for matrix multiplication achieves its O(n log 7)
running time by exploiting the commutativity of multiplication (i.e.,
A*B=B*A, for square matrices A and B).
(Solution) False. Divide-and-Conquer.
5. Insertion sort is likely to outperform quicksort on inputs that are
almost sorted.
(Solution) True.
6. The analysis of the average-case running time of quicksort is an
example of amortized analysis.
(Solution) False. Probabilistic analysis.
7. The polynomial x4+2x2+1 can be evaluated using one addition and
two multiplications.
(Solution) True. x4+2x2+1=(x2+1)2.
8. (n log n) comparisons are necessary to sort any sequence of n
distinct integers.
(Solution) False. (n log n) is the lower bound only for
comparison-based sorting.
9. A binary search tree containing n elements has height O(n).
(Solution) True.
10. The maximum the minimum elements of an array of n elements can
be found using less than 2n-3 comparisons.
(Solution) True.
5. (10 pts) The longest increasing subsequence problem is as follows:
Given numbers a1, a2, …, an, find the maximum value of k such that ai1
< ai2 < … < aik and i1 < i2 < … < ik. As an example, if the input is 3, 1,
4, 1, 5, 9, 2, 6, 5, the longest increasing subsequence has length 4 (`1, 4, 5,
9’ is one of such sequences). Give an O(n2) algorithm to solve the
problem. Show your algorithm in Chinese or English, and explain why
your algorithm runs in O(n2) time.
(Solution) Let b1, b2, …, bm be the increasing sequence of
distinct elements in a1, a2, …, an. The question is tantamount to
finding the longest common subsequence between the above two
sequences – solvable in O(n2), as discussed in class.
6.
(10 pts) Recall that in the dynamic table example discussed in class,
we use the following potential function:
i = 2 *numi - sizei if i > 1/2;
sizei /2 – numi if i < 1/2
Derive in detail the amortized cost of a deletion under the condition when
i-1 = 1/4. Notice that i-1 is the load factor prior to the deletion.
(Solution) numi –1 + ((sizei-1 /2)/2 - (numi-1 –1)) –( (sizei-1 /2)numi-1 )=0, for numi-1 = sizei-1 /4.
7.
(15 pts) Solve the following recurrence relation exactly
for n a power of 2:
T(n) = 5T(n/2) + n2 log2 n
subject to T(1)=1.
(Solution) 21*5 log n -20 n2 - 4 n2 log2 n