Divide and Conquer

Table of Contents

Divide and Conquer

Prev: sorting Next: hashing-and-randomized-algorithms

Divide and Conquer Algorithms

5-8. Given two sorted arrays A and B of size n and m respectively, find the median of the n + m elements. The overall run time complexity should be O(log(m + n)). 5-9. [8] The largest subrange problem, discussed in Section 5.6, takes an array A of n numbers, and asks for the index pair i and j that maximizes S = ∑j k=i A[k]. Give an O(n) algorithm for largest subrange.

5-10. [8] We are given n wooden sticks, each of integer length, where the ith piece has length L[i]. We seek to cut them so that we end up with k pieces of exactly the same length, in addition to other fragments. Furthermore, we want these k pieces to be as large as possible. (a) Given four wood sticks, of lengths \(L = {10, 6, 5, 3}\), what are the largest sized pieces you can get for k = 4? (Hint: the answer is not 3). (b) Give a correct and efficient algorithm that, for a given L and k, returns the maximum possible length of the k equal pieces cut from the initial n sticks.

5-11. [8] Extend the convolution-based string-matching algorithm described in the text to the case of pattern matching with wildcard characters “”, which match any character. For example, “sht” should match both “shot” and “shut”.

Recurrence Relations

5-12. [5] In Section 5.3, it is asserted that any polynomial can be represented by a recurrence. Find a recurrence relation that represents the polynomial \(a_n = n^2\).

5-13. Suppose you are choosing between the following three algorithms:

  1. T (n) = 2T (n/3) + 1
  2. T (n) = 5T (n/4) + n
  3. T (n) = 7T (n/7) + n
  4. T (n) = 9T (n/3) + n^2 5-15. [3] Use the master theorem to solve the following recurrence relations:
  5. T (n) = 64T (n/4) + n^4
  6. T (n) = 64T (n/4) + n^3
  7. T (n) = 64T (n/4) + 128 5-16. [3] Give asymptotically tight upper (Big Oh) bounds for T (n) in each of the following recurrences. Justify your solutions by naming the particular case of the master theorem, by iterating the recurrence, or by using the substitution method:
  8. T (n) = T (n − 2) + 1.
  9. T (n) = 2T (n/2) + n lg2 n.
  10. T (n) = 9T (n/4) + n2.

Prev: sorting Next: hashing-and-randomized-algorithms