Você está na página 1de 2

Assignment #2 CS4/531

Due Date: Mon. Oct. 3, 2011 UNSUPPORTED SOLUTIONS RECEIVE NO CREDIT. Total points: 53

You MUST turn in your HW by 2:10pm on Oct 3. After that, I will NOT accept your HW. This rule will be STRICTLY ENFORCED. Please PRINT YOUR LAST NAME, FIRST NAME and UB number on the rst page. Write solution of each problem on a separate sheet. Staple them in the order of problem numbers. If your homework solution deviates signicantly from these guidelines, TA may deduct up to 20% of the points.

1. (5 pts) Let a be a real number and n a positive integer. We want to compute an . This, of course, can be done using n 1 multiplications. Describe an algorithm that computes an using O(log n) basic arithmetic operations. Note: A basic arithmetic operation is one of the following: x + y, x y, x y, x/y (which returns the integer part of x divided by y); and (x mod y) (which returns the remainder of x divided by y). But, xy is NOT a basic arithmetic operation. Although most programming languages allow this operation, xy is calculated by a subroutine, not by a CPU instruction. 2 (8 pts). We discussed the Fibonacci numbers in class. To calculate Fib(n), the recursive function (using the recursive denition) takes exponential time. The simple for loop function takes O(n) time. We can do better than this: only in O(log n) time. We have shown that: n n Fib(n) = 1 2 where 1 = 5 5
1+ 5 2

and 1 =

1 5 2 .

By using this formula and the algorithm in Problem 1, we easily get an algorithm for computing Fib(n) in O(log n) time. The drawback of this algorithm, however, is that it uses real arithmetic operations, which takes more time than integer arithmetic operations. (Roughly speaking, a real arithmetic operation is 5 times slower than the corresponding integer arithmetic operation). Describe an O(log n) time algorithm that uses ONLY integer arithmetic operations. Hint: Derive a formula for computing Fib(n) using matrix and vector multiplication. 3 (8 pts). Maximum Contiguous Subsequence Sum Problem Let A[1..n] be an array of numbers. The elements in A can be either positive or negative. We want to nd the indices k, l so that the sum l A[i] is maximum among all possible i=k choices of k, l. For example if A = {3, 12, 6, 10, 5, 2}, the answer is k = 2, l = 4, since A[2] + A[3] + A[4] = 12 + (6) + 10 = 16 is the maximum sum of all possible choices. It is easy to nd an O(n2 ) time algorithm for solving this problem. Describe a divide-andconquer algorithm for solving this problem with run time at most O(n log n).

4 (8 pts). In the linear time selection algorithm, we divide the array elements into 5-element groups. (a) If we divide array elements into 3-element groups (and change nothing else), will it still be a linear time algorithm? Explain why. (b) If we divide array elements into 7-element groups (and change nothing else), will it still be a linear time algorithm? Explain why. 5 (8 pts). Let A[1..n] be an array of unsorted numbers. We want to nd the minimum element (min) and the maximum element (max) in A. We want to design an algorithm so that the number of comparisons between array elements is as small as possible. The simple way to solve the problem is: scan A[1..n] to nd max, using n 1 comparisons, then scan A[1..n] again to nd min, again using n 1 comparisons. Thus the total number of array element comparisons used by the algorithm is 2n 2. Design an algorithm for solving this problem, using at most 3n/2 array element comparisons. (You may assume n is a power of 2). Hint: Use divide and conquer strategy. 6 (8 pts). In the closest-pair problem, the distance between two points p1 = (x1 , y1 ) and p2 = (x2 , y2 ) is dened by d(p1 , p2 ) = [(x1 x2 )2 + (y1 y2 )2 ]1/2 (this is called Euclidean distance). In some applications, it is more suitable to use the so called Manhattan distance, dened by: d(p1 , p2 ) = |x1 x2 | + |y1 y2 |. Now consider the closest-pair problem where the distance is dened by the Manhattan distance. Modify the closed-pair algorithm discussed in class so that it works for the Manhattan distance. 7 (8 pts). Let p1 = (x1 , y1 ) and p2 = (x2 , y2 ) be two points on 2D plane. We say p1 is dominated by p2 if x1 x2 and y1 y2 . Let P = {p1 , p2 , . . . , pn } be a set of n points. The set of minimum points of P is dened to be: min(P ) = {pi P | pi does not dominate any other points in P }. For example, if P = {(1, 2), (1, 3), (2, 1), (2, 3), (3, 2)}, then min(P ) = {(1, 3), (1, 2), (2, 1)}. (a) Show that if the points in min(P ) are sorted in increasing x-coordinate, then they are sorted in decreasing y-coordinate. (b) Suppose that the points in P are given in increasing x-coordinate. Describe an O(n) time algorithm for nding min(P ). (To simplify the problem, you may assume no two points in P have the same x-coordinate.) Note: There are several solutions. One of them is by using divide and conquer strategy, and similar to the algorithm for solving the closest pair of points problem.

Você também pode gostar