This action might not be possible to undo. Are you sure you want to continue?

Problems Contributed by the Instructor

September 9, 2011

Homework # 2 (Instructor)

Due date: September 15, 2011 (Thursday)

Instruction: Attempt these problems on sheet(s) separate from the ones you used for answering Session 2 Group’s and Session 3 Group’s problems. (A) [20 points] In class, we learnt an algorithm that multiplies two n-bit binary integers x and y in time na , where a = log2 3. Call this procedure fastmultiply(x, y). (a) [10 points] We want to convert the decimal integer 10n (a 1 followed by n zeros) into binary. Here is the algorithm (assume n is a power of 2): function pwr2bin(n) if n = 1: return 10102 else: z = ??? return fastmultiply(z, z) Fill in the missing details (i.e., replace ‘???’ by some meaningful expression). Then give a recurrence relation for the running time of the algorithm, and solve the recurrence using the master method. (b) [10 points] We want to convert any decimal integer x with n digits (where n is a power of 2) into binary. The algorithm is the following: function dec2bin(x) if n = 1: return binary[x] else: split x into two decimal numbers xL , xR with n/2 digits each return ??? Here binary[·] is a vector that contains the binary representation of all one-digit integers. That is, binary[0] = 02 , binary[1] = 12 , up to binary[9] = 10012 . 1

n] to A[(k/2 + 1) . . Give a recurrence for the running time of the algorithm. n] and integer k ≤ min(m. m] and B[1 . . and that k reduces to k/2.. . (B) [20 points] Given two sorted lists A[1 . 2 . let us say that A[k/2] < B[k/2]. Otherwise. . If the two are equal. . argue that the search space for the kth smallest element changes from A[1 . The following approach might be useful: Compare A[k/2] with B[k/2]. then show that the desired element can be easily found. .. give an O(log m + log n) time divide-and-conquer based algorithm for computing the kth smallest element in the merged list of size m + n. Repeating these steps a suitable number of times leads to the base case (i. Fill in the missing details (i. replace ‘???’ by some meaningful expression). and solve it using the master method. In this case. m] and B[1 .e. k = 1). . . . You are allowed to call the procedure pwr2bin and function fastmultiply to ﬁll in the missing details.Assume that a lookup in binary takes O(1) time. . . m] and B[1 . k/2]. .e. n).

Sign up to vote on this title

UsefulNot useful