Difference between pages "Chapter 5" and "Chapter 3"

From The Algorithm Design Manual Solution Wiki
(Difference between pages)
Jump to navigation Jump to search
 
 
Line 1: Line 1:
=Divide and Conquer=
+
=Data Structure=
  
===Binary Search===
+
===Stacks, Queues, and Lists===
  
:[[5.1]]. Suppose you are given a sorted array <math>A</math> of size n that has been circularly shifted <math>k</math> positions to the right. For example, [35, 42, 5, 15, 27, 29] is a sorted array that has been circularly shifted <math>k = 2</math> positions, while [27, 29, 35, 42, 5, 15] has been shifted <math>k = 4</math> positions.
+
[[3.1]]. A common problem for compilers and text editors is determining whether the parentheses in a string are balanced and properly nested. For example, the string <math>((())())()</math> contains properly nested pairs of parentheses, which the strings <math>)()(</math> and <math>())</math>
:• Suppose you know what <math>k</math> is. Give an <math>O(1)</math> algorithm to find the largest number in <math>A</math>.
+
do not. Give an algorithm that returns true if a string contains properly nested and balanced parentheses, and false if otherwise. For full credit, identify the position of the first offending parenthesis if the string is not properly nested and balanced.
:• Suppose you do not know what <math>k</math> is. Give an <math>O(lg n)</math> algorithm to find the largest number in <math>A</math>. For partial credit, you may give an <math>O(n)</math> algorithm.
+
[[3.1|Solution]]
[[5.1|Solution]]
 
  
  
:5.2. A sorted array of size n contains distinct integers between <math>1</math> and <math>n + 1</math> , with one element missing. Give an <math>O(log n)</math> algorithm to find the missing integer, without using any extra space.
+
3.2. Give an algorithm that takes a string <math>S</math> consisting of opening and closing parentheses, say )()(())()()))())))(, and finds the length of the longest balanced parentheses in <math>S</math>, which is 12 in the example above. (Hint: The solution is not necessarily a contiguous run of parenthesis from <math>S</math>.)
 +
  
 +
[[3.3]]. Give an algorithm to reverse the direction of a given singly linked list. In other words, after the reversal all pointers should now point backwards. Your algorithm should take linear time.
 +
[[3.3|Solution]]
  
:[[5.3]] Consider the numerical Twenty Questions game. In this game, the first player thinks of a number in the range 1 to <math>n</math>. 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 <math>n</math> in known?
 
:(b) What is a good strategy if <math>n</math> is not known?
 
[[5.3|Solution]]
 
  
 +
3.4. Design a stack <math>S</math> that supports ''S.push(x)'', ''S.pop()'', and ''S.findmin()'', which returns the minimum element of <math>S</math>. All operations should run in constant time.
  
:5.4. You are given a unimodal array of <math>n</math> 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 <math>O(log n)</math> time.
 
  
 +
[[3.5]]. We have seen how dynamic arrays enable arrays to grow while still achieving constant-time amortized performance. This problem concerns extending dynamic arrays to let them both grow and shrink on demand.
 +
:(a) Consider an underflow strategy that cuts the array size in half whenever the array falls below half full. Give an example sequence of insertions and deletions where this strategy gives a bad amortized cost.
 +
:(b) Then, give a better underflow strategy than that suggested above, one that achieves constant amortized cost per deletion.
 +
[[3.5|Solution]]
  
:[[5.5]]. Suppose that you are given a sorted sequence of distinct integers <math>[a_1, a_2, . . . , a_n]</math>. Give an <math>O(lg n)</math> algorithm to determine whether there exists an index <math>i</math> such that <math>a_i = i</math>. For example, in [-10, -3, 3, 5, 7], <math>a_3 = 3</math>. In [2, 3, 4, 5, 6, 7], there is no such <math>i</math>.
 
[[5.5|Solution]]
 
  
 +
3.6. Suppose you seek to maintain the contents of a refrigerator so as to minimize food spoilage. What data structure should you use, and how should you use it?
  
:5.6. Suppose that you are given a sorted sequence of distinct integers <math>a = [a_1, a_2, . . . , a_n]</math>, drawn from 1 to <math>m</math> where <math>n < m</math>. Give an <math>O(lg n)</math> algorithm to find an integer <math>\leq m</math> that is not present in <math>a</math>. For full credit, find the smallest such integer <math>x</math> such that <math>1 \leq x \leq m</math>.
 
  
 +
[[3.7]]. Work out the details of supporting constant-time deletion from a singly linked list as per the footnote from page 79, ideally to an actual implementation. Support the other operations as efficiently as possible.
 +
[[3.7|Solution]]
  
:[[5.7]]. Let <math>M</math> be an <math>n * m</math> 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 <math>x</math> in <math>M</math>, or to determine that <math>x</math> is not there. How many comparisons of <math>x</math> with matrix entries does your algorithm use in worst case?
+
===Elementary Data Structures===
[[5.7|Solution]]
 
  
===Divide and Conquer Algorithms===
+
3.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>.
 
  
 +
[[3.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]]
 
  
 +
3.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.
+
===Trees and Other Dictionary Structures===
:(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.
 
  
 +
[[3.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===
+
3.12
  
:5.12. In Section 5.3, it is asserted that any polynomial can be represented by a recurrence. Find a recurrence relation that represents the polynomial <math>a_n = n^2</math>.
 
  
 +
[[3.13]]
  
:[[5.13]]. Suppose you are choosing between the following three algorithms:
 
:• Algorithm <math>A</math> solves problems by dividing them into five subproblems of half the size, recursively solving each subproblem, and then combining the solutions in linear time.
 
:• Algorithm <math>B</math> solves problems of size <math>n</math> by recursively solving two subproblems of size <math>n - 1</math> and then combining the solutions in constant time.
 
:• Algorithm <math>C</math> solves problems of size <math>n</math> by dividing them into nine subproblems of size <math>n/3</math>, recursively solving each subproblem, and then combining the solutions in <math>\Theta(n^2)</math> time.
 
:What are the running times of each of these algorithms (in big O notation), and which would you choose?
 
[[5.13|Solution]]
 
  
 +
3.14
  
:5.14. Solve the following recurrence relations and give a <math>\Theta</math> bound for each of them:
 
:(a) <math>T(n) = 2T(n/3) + 1</math>
 
:(b) <math>T(n) = 5T(n/4) + n</math>
 
:(c) <math>T(n) = 7T(n/7) + n</math>
 
:(d) <math>T(n) = 9T(n/3) + n^2</math>
 
  
 +
[[3.15]]
  
:[[5.15]]. Use the master theorem to solve the following recurrence relations:
 
:(a) <math>T(n) = 64T(n/4) + n^4</math>
 
:(b) <math>T(n) = 64T(n/4) + n^3</math>
 
:(c) <math>T(n) = 64T(n/4) + 128</math>
 
[[5.15|Solution]]
 
  
 +
3.16
  
:5.16. Give asymptotically tight upper (Big Oh) bounds for <math>T(n)</math> 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:
+
 
:(a) <math>T(n) = T(n - 2) + 1</math>.
+
[[3.17]]
:(b) <math>T(n) = 2T(n/2) + n lg^2 n</math>.
+
 
:(c) <math>T(n) = 9T(n/4) + n^2</math>.
+
 
 +
3.18
 +
 
 +
 
 +
[[3.19]]
 +
 
 +
 
 +
3.20
 +
 
 +
 
 +
[[3.21]]
 +
 
 +
===Applications of Tree Structures===
 +
 
 +
3.22
 +
 
 +
 
 +
[[3.23]]
 +
 
 +
 
 +
3.24
 +
 
 +
 
 +
[[3.25]]
 +
 
 +
 
 +
3.26
 +
 
 +
 
 +
[[3.27]]
 +
 
 +
 
 +
3.28
 +
 
 +
 
 +
[[3.29]]
 +
 
 +
 
 +
3.30
 +
 
 +
 
 +
[[3.31]]
 +
 
 +
===Implementation Projects===
 +
 
 +
3.32
 +
 
 +
 
 +
[[3.33]]
 +
 
 +
===Interview Problems===
 +
 
 +
3.34
 +
 
 +
 
 +
[[3.35]]
 +
 
 +
 
 +
3.36
 +
 
 +
 
 +
[[3.37]]
 +
 
 +
 
 +
3.38
 +
 
 +
 
 +
[[3.39]]
 +
 
 +
 
 +
3.40
 +
 
 +
 
 +
[[3.41]]
 +
 
 +
 
 +
3.42
 +
 
 +
 
 +
[[3.43]]
 +
 
 +
 
 +
3.44
 +
 
 +
 
 +
[[3.45]]
  
  
 
Back to [[Chapter List]]
 
Back to [[Chapter List]]

Revision as of 20:38, 14 September 2020

Data Structure

Stacks, Queues, and Lists

3.1. A common problem for compilers and text editors is determining whether the parentheses in a string are balanced and properly nested. For example, the string [math]\displaystyle{ ((())())() }[/math] contains properly nested pairs of parentheses, which the strings [math]\displaystyle{ )()( }[/math] and [math]\displaystyle{ ()) }[/math] do not. Give an algorithm that returns true if a string contains properly nested and balanced parentheses, and false if otherwise. For full credit, identify the position of the first offending parenthesis if the string is not properly nested and balanced. Solution


3.2. Give an algorithm that takes a string [math]\displaystyle{ S }[/math] consisting of opening and closing parentheses, say )()(())()()))())))(, and finds the length of the longest balanced parentheses in [math]\displaystyle{ S }[/math], which is 12 in the example above. (Hint: The solution is not necessarily a contiguous run of parenthesis from [math]\displaystyle{ S }[/math].)


3.3. Give an algorithm to reverse the direction of a given singly linked list. In other words, after the reversal all pointers should now point backwards. Your algorithm should take linear time. Solution


3.4. Design a stack [math]\displaystyle{ S }[/math] that supports S.push(x), S.pop(), and S.findmin(), which returns the minimum element of [math]\displaystyle{ S }[/math]. All operations should run in constant time.


3.5. We have seen how dynamic arrays enable arrays to grow while still achieving constant-time amortized performance. This problem concerns extending dynamic arrays to let them both grow and shrink on demand.

(a) Consider an underflow strategy that cuts the array size in half whenever the array falls below half full. Give an example sequence of insertions and deletions where this strategy gives a bad amortized cost.
(b) Then, give a better underflow strategy than that suggested above, one that achieves constant amortized cost per deletion.

Solution


3.6. Suppose you seek to maintain the contents of a refrigerator so as to minimize food spoilage. What data structure should you use, and how should you use it?


3.7. Work out the details of supporting constant-time deletion from a singly linked list as per the footnote from page 79, ideally to an actual implementation. Support the other operations as efficiently as possible. Solution

Elementary Data Structures

3.8


3.9


3.10

Trees and Other Dictionary Structures

3.11


3.12


3.13


3.14


3.15


3.16


3.17


3.18


3.19


3.20


3.21

Applications of Tree Structures

3.22


3.23


3.24


3.25


3.26


3.27


3.28


3.29


3.30


3.31

Implementation Projects

3.32


3.33

Interview Problems

3.34


3.35


3.36


3.37


3.38


3.39


3.40


3.41


3.42


3.43


3.44


3.45


Back to Chapter List