You are on page 1of 2

COMP 6651: Algorithm Design Techniques Sample Mid-term Examination

Time: 1 hour 15 minutes


Notes: (a) Read every question very carefully before attempting to solve it. (b) Answers must be short and precise yet complete. Verbose explanations may reveal aws in your logic, which would incur penalties. (c) You may wish to make use of some facts and theorems that are listed on the next page. 1. [5] Solve the recurrence T (n) = T (n 1) + n2 ; T (1) = 1 2. [10] You are given a set of n blocks b1 , b2 , . . . , bn such that the block bi has weight wi . You need to rst stack up the blocks in some order; subsequently the blocks are removed in the same order. That is, the bottom-most block will be removed rst. The cost of removing the block is equal to the weight of the current stack. The problem is to determine an optimal order of the blocks, so that the cost of removing all the blocks is minimized. For example, suppose we have 3 blocks: b1 , b2 , b3 with weights 3, 7, 5 respectively. Then if we stack them up (bottom to top) in the order b1 , b2 , b3 , the cost of removing the blocks is (3 + 7 + 5) + (7 + 5) + 5 = 32. Alternatively, if we stack them up in the order b2 , b3 , b1 , then the cost of removing the blocks is (7 + 5 + 3) + (5 + 3) + 3 = 26. Thus the second order has smaller cost. (a) Suppose the stacking order from bottom to top is bi1 , bi2 , . . . , bin . Derive an expression for the cost of removing all the blocks in the same order, ie. bi1 rst. (b) Argue the property of greedy choice: an optimal solution can be arrived at by using the greedy heuristic of stacking the maximum weight block at the bottom of the stack. 3. [10] Given an array A of n integers, we say that A has a majority element if there is some value x such that |{i | A[i] = x}| > n/2 . Informally, there is some value x that appears at more than half the indices in the array. Give an algorithm that determines in linear time if A has a majority element. An informal description of the idea will suce - you dont have to give detailed pseudocode. However, you must provide an argument for why your algorithm would take linear time. For partial credit, solve the problem for the case when each element in A is chosen from the set {1, . . . , n}.

Useful facts and results 1. n k=1 k =


n(n+1) 2 n(n+1)(2n+1) . 6

2 2. n k=1 k =

3. Master Theorem: Let a 1 and b > 1 be constants, let f (n) ne a function and let T (n) be dened on the non-negative integers by the recurrence: T (n) = aT (n/b) + f (n) where we interpret n/b to mean either n/b or n/b , Then T (n) can be bounded asymptotically as follows: (a) If f (n) = O(nlogb a ) for some constant > 0 then T (n) = (nlogb a ). (b) If f (n) = (nlogb a ) then T (n) = (nlogb a log n). (c) If f (n) = (nlogb a+ ) for some > 0 and if af (n/b) cf (n) for some constant c < 1 and all suciently large n then T (n) = (f (n)).

You might also like