Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Positional notation wikipedia , lookup
Large numbers wikipedia , lookup
Location arithmetic wikipedia , lookup
Collatz conjecture wikipedia , lookup
Halting problem wikipedia , lookup
Factorization of polynomials over finite fields wikipedia , lookup
Proofs of Fermat's little theorem wikipedia , lookup
Homework Two Design and Analysis of Algorithms Due: Wednesday, Feb. 11, 2009, in class 1(a) (b) Design an O(nlgn) algorithm to determine if the n numbers in A[1..n] are distinct. Prove that any comparison-based algorithm for part (a) needs (nlgn) time. 2 Page 85, Problem 4-2. Finding the missing integer. An array A[1..n] contains all the integers from 0 to n except one integer. It would be easy to determine the missing integer in O(n) time by using an auxiliary array B[0..n] to record which numbers appear in A. In this problem, however, we cannot access an entire integer in A with a single operation. The elements of A are represented in binary, and the only operation we can use to access them is “fetch the jth bit of A[i],” which takes constant time. Show that if we use only this operation, we can still determine the missing integer in O(n) time. You may assume that n = 2k-1 and every number is a k-bit number. 3 Problem 9.3-8 (p193) Let X[1..n] and Y[1..n] be two arrays, each containing n distinct numbers already in sorted order. Give an O(lgn)-time algorithm to find the median of all 2n elements in array X and Y. (Hint: if X[k] is the median, what conditions the X[k] must satisfy?) 4 Given an array A[1..n] of n numbers, the Min-first binary tree for the n numbers is constructed as follows. (1) The root holds the smallest number in A[1..n]. (2) Let A[r] be the root. Then, its left subtree is recursively constructed from A[1..r-1], and its right subtree is recursively constructed from A[r+1..n]. (3) If a subtree contains no number, it is a leaf and construction stops here. A small example is given below. A[1] = 6, A[2] = 10, A[3] = 2, A[4] = 7, A[5] = 9, A[6] = 5 2 A[1] = 6, A[3] A[1] A[1] = 6, 5 A[6] 6 A[2] A[1] = 6, A[4] 10 A[1] = 6, 7 A[1] A[5] = 6, 9 A[1] = 6, A[1] = 6, 1 (a) Draw the min-order binary tree for the following sequence. 6, 5, 2, 9, 7, 1, 3, 10, 9. (b) Design a divide and conquer algorithm to construct the min-order binary tree for numbers stored in A[1, n]. Pseudo-code is required. Also, you need to analyze the complexity of your algorithm for the best case, worst case and average case. (The average complexity must be O(nlgn). The worst complexity can be O(n2). 2