Difference between revisions of "Chapter 5"

From The Algorithm Design Manual Solution Wiki
Jump to navigation Jump to search
Line 33: Line 33:
 
===Divide and Conquer Algorithms===
 
===Divide and Conquer Algorithms===
  
:5.8
+
:5.8. Given two sorted arrays <math>A</math> and <math>B</math> of size <math>n</math> and <math>m</math> respectively, find the median of the <math>n + m</math> elements. The overall run time complexity should be <math>O(log(m + n))</math>.
  
  
:[[5.9]]
+
:[[5.9]]. The largest subrange problem, discussed in Section 5.6, takes an array <math>A</math> of <math>n</math> numbers, and asks for the index pair <math>i</math> and <math>j</math> that maximizes <math>S = \sum_{k=1}^j A[k]</math>. Give an <math>O(n)</math> algorithm for largest subrange.
 +
[[5.9|Solution]]
  
  
:5.10
+
:5.10. We are given <math>n</math> wooden sticks, each of integer length, where the <math>i</math>th piece has length <math>L[i]</math>. We seek to cut them so that we end up with <math>k</math> pieces of exactly the same length, in addition to other fragments. Furthermore, we want these <math>k</math> pieces to be as large as possible.
 +
:(a) Given four wood sticks, of lengths <math>L = {10, 6, 5, 3}</math>, what are the largest sized pieces you can get for <math>k = 4</math>? (Hint: the answer is not 3).
 +
:(b) Give a correct and efficient algorithm that, for a given <math>L</math> and <math>k</math>, returns the maximum possible length of the <math>k</math> equal pieces cut from the initial <math>n</math> sticks.
  
  
:[[5.11]]
+
:[[5.11]]. 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, “sh*t” should match both “shot” and “shut”.
 
+
[[5.11|Solution]]
  
 
===Recurrence Relations===
 
===Recurrence Relations===

Revision as of 18:40, 14 September 2020

Divide and Conquer

Binary Search

5.1. Suppose you are given a sorted array of size n that has been circularly shifted positions to the right. For example, [35, 42, 5, 15, 27, 29] is a sorted array that has been circularly shifted positions, while [27, 29, 35, 42, 5, 15] has been shifted positions.
• Suppose you know what is. Give an algorithm to find the largest number in .
• Suppose you do not know what is. Give an algorithm to find the largest number in . For partial credit, you may give an algorithm.

Solution


5.2. A sorted array of size n contains distinct integers between and , with one element missing. Give an algorithm to find the missing integer, without using any extra space.


5.3 Consider the numerical Twenty Questions game. In this game, the first player thinks of a number in the range 1 to . The second player has to figure out this number by asking the fewest number of true/false questions. Assume that nobody cheats.
(a) What is an optimal strategy if in known?
(b) What is a good strategy if is not known?

Solution


5.4. You are given a unimodal array of distinct elements, meaning that its entries are in increasing order up until its maximum element, after which its elements are in decreasing order. Give an algorithm to compute the maximum element of a unimodal array that runs in time.


5.5. Suppose that you are given a sorted sequence of distinct integers . Give an algorithm to determine whether there exists an index such that . For example, in [-10, -3, 3, 5, 7], . In [2, 3, 4, 5, 6, 7], there is no such .

Solution


5.6. Suppose that you are given a sorted sequence of distinct integers , drawn from 1 to where . Give an algorithm to find an integer that is not present in . For full credit, find the smallest such integer such that .


5.7. Let be an integer matrix in which the entries of each row are sorted in increasing order (from left to right) and the entries in each column are in increasing order (from top to bottom). Give an efficient algorithm to find the position of an integer in , or to determine that is not there. How many comparisons of with matrix entries does your algorithm use in worst case?

Solution

Divide and Conquer Algorithms

5.8. Given two sorted arrays and of size and respectively, find the median of the elements. The overall run time complexity should be .


5.9. The largest subrange problem, discussed in Section 5.6, takes an array of numbers, and asks for the index pair and that maximizes . Give an algorithm for largest subrange.

Solution


5.10. We are given wooden sticks, each of integer length, where the th piece has length . We seek to cut them so that we end up with pieces of exactly the same length, in addition to other fragments. Furthermore, we want these pieces to be as large as possible.
(a) Given four wood sticks, of lengths , what are the largest sized pieces you can get for ? (Hint: the answer is not 3).
(b) Give a correct and efficient algorithm that, for a given and , returns the maximum possible length of the equal pieces cut from the initial sticks.


5.11. 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, “sh*t” should match both “shot” and “shut”.

Solution

Recurrence Relations

5.12


5.13


5.14


5.15


5.16


Back to Chapter List