Sei sulla pagina 1di 3

Exercises 8 1.

We are given an innite array A[1::] in which the rst n cells contain n distinct integers in sorted order, and the rest of the cells are lled with a special symbol 1. We are not given the value of n as part of the input. Devise an O(log n) algorithm to search in the array A for an integer x. Solution. It is clear that, since the required running time is O(log n), we cannot search the array sequentially. A well known algorithm for arrays of length n which has the running time O(log n) was binary search. The idea behind binary search was to halve, at each step, the interval in which we look for the given element. We cannot apply this idea here, since we don t know the bounds of A, but we can do something similar: at each step we double the interval in which we are looking for x. Let i be an integer we ll use as index in the array A: Initially i = 1: One step of the algorithm consists of the following: - if A[2i] = 1 then look for x in the interval A[i:::2i] using binary search - else, if A[2i] < 1 - if x < A[2i] then look for x in the interval A[i:::2i] using binary search - otherwise i ! 2i and repeat this step It is clear that, once we identied a possible interval A[i:::2i] in which x might be, its length is at most n (since we have only n numbers in the array A); so searching for x using binary search takes O(log n) time. Also, since at each step, if we do not identify a possible interval for x, we double the index i, after at most dlog ne such steps we reach the end of the integers in the array and thus we can identify an interval in which to search for x using binary search. Thus the running time is O(log n): 2: We have a test with n questions. For each i = 1; :::; n, question i is worth vi points and solving it takes mi minutes. Given vi ; mi ; i = 1; :::; n; compute the minimum number of minutes required to earn at least V points and output the list P of the problems to solve. Solution. Let M(i; v) the minimum number of minutes required to earn v points from questions 1; :::; i 1. Then the answer to our problem is M(n; V): We note that: M(i,v)=0 if v=0 M(0,v)=1 if v>0 M(i,v)=min(mi +M(i-1,v-vi ),M(i 1; v)) (*) The last relation says that we can choose either to solve problem i or not. It is clear that computing the matrix M and nding the value 1

M(n; V) takes O(nV ) time. To reconstruct the solution, for each M(i; v) we keep an additional bit which is 1 if the minimum in the relation (*) was mi +M(i-1,v-vi ) (so it was better to solve question i), and 0 if the minimum was M(i 1; v), (it was better not to solve question i). Then we start from M(n; V) and trace back: if the bit for M(n; V) is 1, then we include i in P and go to M(n 1; V vi ); otherwise go to M(n 1; V): Repeat the process until the value of the current element in the matrix M is 0. It is better to do this reconstruction using a recursive procedure. 3. Balanced partition Input: n integers a1 ; :::; an in the range 0:::k Output: partition them into two subsets S1 and S2 such that jsum(S1 ) sum(S2 )j to be as small as possible. Your algorithm should be polynomial in n and k: Solution. Let P (i; j ) = 1 if some partition of a1 ; :::; ai has sum j , and 0 otherwise. i can take values from 1 to n; and j from 0 to nk , so we have O(n2 k ) subproblems. Then P (i; j ) = 1 if P (i 1; j ) = 1 (we already have a subset of the rst i 1 elements that sum to j ) P (i; j ) = 1 if P (i 1; j ai ) = 1 (there is a subset of the rst i 1 elements which sums to j ai ): In one relation: P (i; j ) = max(P (i 1; j ); P (i 1; j ai )): Now, let S = (a1 + :::an )=2: We would like to nd a subset of a1 ; :::; an such that the sum of its elements to be as close to S as possible. In other words, we want to nd mink S (S k such that P (n; k ) = 1): Let k the optimum value of k and S1 such that the sum of the elements in S1 is k ; and S2 the remaining elements. Then sum(S1 ) sum(S2 ) = 2(S k ), which, from our choice of k , is minimum. The running time is dominated by the calculation of the matrix P and it is O(n2 k ): 4. Moving on a checkerboard Suppose that you are given an n n checkerboard and a checker. You must move the checker from the bottom edge of the board to the top edge of the board according to the following rule. At each step you may move the checker to one of three squares: - the square immediately above - the square that is one up and one to the left (but only if the checker is not already in the leftmost column) - the square that is one up and one to the right (but only if the checker is not already in the rightmost column)

Each time you move from square x to square y; you receive p(x; y ) dollars. You are given p(x; y ) for all pairs (x; y ) for which a move from x to y is legal. Give a polynomial algorithm in n that gures the set of moves that will move the checker from somewhere along the bottom row to somewhere along the top edge while gathering as many dollars as possible. Solution. For each square x, let M (x) denote the maximum prot one can gain via a sequence of legal moves, from a square on the bottom row to x. We want to nd maxy M (y ); where y is a square on the top row. 0, if x is on the bottom row Then M (x) = maxy (M (y ) + p(y; x); y ! x is a valid move). The algorithm computes M (x) for the squares x in the rows n; n 1; :::; 1: Running time: for each square x, the computation of M (x) takes constant time. Thus we achieve a running time O(n2 ):

Potrebbero piacerti anche