Divide and Conquer Algorithms (part 2 of CSC 282), 1 Schedule

advertisement
Divide and Conquer Algorithms (part 2 of CSC 282),
http://www.cs.rochester.edu/~stefanko/Teaching/08CS282
1
Schedule
The problem sessions are in CSB 601 on each Monday, 8pm.
Part 1 of he homework is due Sep 25, 2008.
Part 2 of he homework is due Sep 30, 2008.
The QUIZ will be on Thursday, Oct. 2.
2
List of algorithms covered in the class
(B-basic, I-intermediate, A-advanced):
I:
B:
I:
A:
A:
3
Karatsuba-Offman (p.47, DSV).
Mergesort (p.50, DSV).
Linear-time deterministic selection (p.189, CLRS).
Expected-linear-time selection (p.53, DSV).
FFT (p.57, DSV).
Basic material
Important concepts, problems, theorems, and algorithms:
• Merge-sort, linear-time select algorithm (median).
• Evaluation, interpolation, convolution.
Testing method:
• Find the unique polynomial of degree d which has given values on a set of d + 1 points (problems with d ≤ 3).
• Apply master’s theorem to recurrence T (n) = aT (n/b) + O(nd ).
• Trace the execution of the expected linear-time select algorithm (p.54 of DPV).
• Trace the first step of the deterministic linear-time select algorithm.
• Compute the convolution of two sequences (multiply two polynomials).
• Evaluate a polynomial on the n-th roots of unity (fourier transform), for n = 2, 4.
Example problems:
2.1 (due Sep 25, 2008) Find the unique polynomial of degree 2 such that p(−1) = 5, p(0) = 7, and p(1) = 3.
2.2 (due Sep 25, 2008) Solve the following recurrences: T (n) = 3T (n/2) + O(n), T (n) = 3T (n/2) + O(n2 ).
2.3 (due Sep 25, 2008) Let A[1..10] = [7, 2, 4, 9, 5, 1, 3, 10, 6, 8]. Assume that we run the randomized algorithm
with the following parameters: SELECT(A, 3). Suppose that the following sequence of v’s picked by the algorithm
is 7,2,4. Trace the execution of the algorithm.
1
2.4 (due Sep 25, 2008) Let
A[1..25] = [7, 2, 23, 4, 9, 5, 1, 24, 3, 10, 6, 8, 17, 12, 14, 19, 25, 22, 15, 11, 13, 20, 16, 21, 18].
Suppose that we use the deterministic linear-time select algorithm on A. Compute the pivot used by the algorithm
to split A.
2.5 (due Sep 25, 2008) Compute the convolution of (1, 1, 2, 1) with (4, 1, 3, 0, 0, 1). (Definition: convolution of
two sequences a0 , . . . , an−1 and b0 , . . . , bm−1 is c0 , . . . , cm+n−1 , where
min(k,n−1)
ck =
X
ai bk−i ,
i=max(0,k−m+1)
i. e., c0 , . . . , cm+n−1 is the coefficient sequence of the polynomial (a0 +a1 x+· · ·+an−1 xn−1 )(b0 +b1 x+· · ·+bn−1 xn−1 )).
2.6 (due Sep 25, 2008) The convolution of the sequence (1, 2, 3, 4) with an unknown sequence S is the sequence
(5, 11, 17, 23, 4). Compute the sequence S.
2.7 (due Sep 25, 2008) Evaluate the polynomial x3 + 3x2 − x + 1 on the 4-th roots of unity. (Definition: n-th
roots of unity are exp(2πik/n), where k = 0, . . . , n − 1. For example 4-th roots of unity are: i, −1, −i, 1.)
4
Additional homework
2.8 (due Sep 30, 2008)
We are given an array A with n elements and a number C. Assume that the sum of the elements in A is larger than
C. We would like to compute the smallest number of elements from A whose sum is at least C. (For example, if
A = [8, 3, 9, 2, 7, 1, 5] and C = 18 then the answer is 3.) Give a linear-time algorithm for this problem. (HINT: use
the linear-time SELECT algorithm)
2.9 (due Sep 30, 2008) Let m ≥ n. You are given two sequences A = (a1 , . . . , an ) and B = (b1 , . . . , bm ). Assume
that there exists a sequence S such that the convolution of A with S is B. Give an O(m log m) algorithm to find S.
You can assume that operations on complex numbers can be performed in time O(1).
2.10 (due Sep 30, 2008) We are given k sorted lists A1 , . . . , Ak . The total number of elements in the lists is n.
We would like to merge the lists into one sorted list B (the number of elements in B will be n). Give an algorithm
which solves this problem in time O(n log k).
2.11 (due Sep 30, 2008) We are given two sorted arrays A and B, each containing n elements. Assume that the
arrays do not contain duplicates, and the elements in A are different from elements in B. We would like to compute
the median of A ∪ B. For example if A = [1, 2, 3, 4, 5] and B = [6, 7, 8, 9, 10] then the median is 5; if A = [1, 3, 5, 7, 9]
and B = [2, 4, 6, 8, 10] then the median is again 5. Give an O(log n) algorithm for this problem.
2.12 (due Sep 30, 2008) We are given an array of integers A[1..n]. We would like to determine whether there
exists an integer x which occurs in A more than n/2 times (i. e., whether A has a majority element). Give an
algorithm which runs in time O(n). (HINT: use the linear-time SELECT algorithm.)
Example: For A = [3, 1, 2] the answer is NO. For A = [3, 1, 3] the answer is YES.
2.13 (due Sep 30, 2008) We are given an array of integers A[1..n]. We would like to determine whether there
exists an integer x which occurs in A more than n/3 times. Give an algorithm which runs in time O(n). (HINT: use
the linear-time SELECT algorithm)
2.14 (due Sep 30, 2008) We are given an array of integers A[1..n] which is almost sorted in the following sense:
for all i ∈ {1, . . . , n − k} we have A[i] ≤ A[i + k]. Give an algorithm which sorts the array A. Your algorithm should
run in time O(n log k).
2.15 (due Sep 30, 2008) We are given two arrays of integers A[1..n] and B[1..n], and a number X. Design an
algorithm which decides whether there exist i, j ∈ {1, . . . , n} such that A[i] + B[j] = X. Your algorithm should run
in time O(n log n).
2
5
Additional problems from the book (do not turn in)
Try to solve the following problems. A few of them will be on the quiz. We will go over the ones that you choose in
the problem sessions.
• 2.4, 2.5, 2.8, 2.10, 2.12, 2.16, 2.17, 2.18, 2.21, 2.26, 2.27, 2.28, 2.29, 2.30, 2.31, 2.34.
3
Download