Chapter 2: Divide-and-conquer algorithms
Q11E
In justifying our matrix multiplication algorithm (Section 2.5), we claimed the following block wise property: if X and Y are n matrices, and
,
where A,B,C,D,E,F,G, and H are sub-matrices, then the product XY can be expressed in terms of these blocks:
Prove this property.
Q13E
A binary tree is full if all of its vertices have either zero or two children. Let denote the number of full binary trees with n vertices. (a)By drawing out all full binary trees with 3, 5, or 7 vertices, determine the exact values of , , and . Why have we left out even numbers of vertices, like ?
(b) For general n, derive a recurrence relation for .
(c) Show by induction that is .
Q14E
You are given an array of elements, and you notice that some of the elements are duplicates; that is, they appear more than once in the array. Show how to remove all duplicates from the array in time .
Q15E
In our median-finding algorithm (Section 2.4), a basic primitive is the split operation, which takes as input an array S and a value V and then divides S into three sets: the elements less than V , the elements equal to V , and the elements greater than V . Show how to implement this split operation in place, that is, without allocating new memory.
Q16E
Question: You are given an infinite array in which the first n cells contain integers in sorted order and the rest of the cells are filled with . You are not given the value of n. Describe an algorithm that takes an integer x as input and finds a position in the array containing x, if such a position exists, in O(log n) time. (If you are disturbed by the fact that the array A has infinite length, assume instead that it is of length n, but that you don鈥檛 know this length, and that the implementation of the array data type in your programming language returns the error message whenever elements are accessed.)
Q17E
Given a sorted array of distinct integers , you want to find out whether there is an index for which . Give a divide-and-conquer algorithm that runs in time .
Q19E
A way merge operation. Suppose you have sorted arrays, each with elements, and you want to combine them into a single sorted array of elements.
(a)Here鈥檚 one strategy: Using the merge procedure from Section 2.3, merge the first two arrays, then merge in the third, then merge in the fourth, and so on. What is the time complexity of this algorithm, in terms of and ?
(b) Give a more efficient solution to this problem, using divide-and-conquer.
Q1E
Question: Use the divide-and-conquer integer multiplication algorithm to multiply the two binary integers and .
Q22E
You are given two sorted lists of size mandn. Give an time algorithm for computing the th smallest element in the union of the two lists.
Q23E
An array A [1...n] is said to have a majority element if more than half of its entries are the same. Given an array, the task is to design an efficient algorithm to tell whether the array has a majority element, and, if so, to find that element. The elements of the array are not necessarily from some ordered domain like the integers, a A2 nd so there can be no comparisons of the form 鈥渋s ?鈥. (Think of the array elements as GIF files, say.) However you can answer questions of the form: 鈥渋s ..?鈥 in constant time.
(a) Show how to solve this problem in O(nlog n) time. (Hint: Split the array A into two arrays A1 and of half the size. Does knowing the majority elements of A1 and A2 help you figure out the majority element of A? If so, you can use a divide-and-conquer approach.)
(b) Can you give a linear-time algorithm? (Hint: Here鈥檚 another divide-and-conquer approach:
- Pair up the elements of A arbitrarily, to get n/2 pairs
- Look at each pair: if the two elements are different, discard both of them; if they are the same, keep just one of them
Show that after this procedure there are at most n/2 elements left, and that they have a majority element if A does.)