The.Algorithm.Design.Manual.Springer-Verlag.1998
The.Algorithm.Design.Manual.Springer-Verlag.1998 The.Algorithm.Design.Manual.Springer-Verlag.1998
Exercises (a) Let S be an unsorted array of n integers. Give an algorithm that finds the pair that maximizes |x-y|. Your algorithm must run in O(n) worst-case time. (b) Let S be a sorted array of n integers. Give an algorithm that finds the pair that maximizes |x-y|. Your algorithm must run in O(1) worst-case time. (c) Let S be an unsorted array of n integers. Give an algorithm that finds the pair that minimizes |x-y|, for . Your algorithm must run in worst-case time. (d) Let S be a sorted array of n integers. Give an algorithm that finds the pair that minimizes |x-y|, for . Your algorithm must run in O(n) worst-case time. 7. (*) Describe how to modify any balanced tree data structure such that search, insert, delete, minimum, and maximum still take time each, but successor and predecessor now take O(1) time each. Which operations have to be modified to support this? 8. (*) In one of my research papers [Ski88], I give a comparison-based sorting algorithm that runs in . Given the existence of an lower bound for sorting, how can this be possible? 9. (*) Suppose you have access to a balanced dictionary data structure, which supports each of the operations search, insert, delete, minimum, maximum, successor, and predecessor in time. Explain how to modify the insert and delete operations so they still take but now minimum and maximum take O(1) time. (Hint: think in terms of using the abstract dictionary operations, instead of mucking about with pointers and the like.) 10. (*) Mr. B. C. Dull claims to have developed a new data structure for priority queues that supports the operations Insert, Maximum, and Extract-Max, all in O(1) worst-case time. Prove that he is mistaken. (Hint: the argument does not involve a lot of gory details-just think about what this would imply about the lower bound for sorting.) 11. Use the partitioning step of Quicksort to give an algorithm that finds the median element of an array of n integers in expected O(n) time. 12. (*) You are given the task of reading in n numbers and then printing them out in sorted order. Suppose you have access to a balanced dictionary data structure, which supports each of the operations search, insert, delete, minimum, maximum, successor, and predecessor in time. ❍ Explain how you can use this dictionary to sort in time using only the following abstract operations: minimum, successor, insert, search. ❍ Explain how you can use this dictionary to sort in time using only the following abstract operations: minimum, insert, delete, search. ❍ Explain how you can use this dictionary to sort in time using only the following abstract operations: insert and in-order traversal. 13. The running time for Quicksort depends upon both the data being sorted and the partition rule file:///E|/BOOK/BOOK/NODE40.HTM (2 of 4) [19/1/2003 1:28:40]
Exercises used to select the pivot. Although Quicksort is on average, certain partition rules cause Quicksort to take time if the array is already sorted. (a) Suppose we always pick the pivot element to be the key from the last position of the subarray. On a sorted array, does Quicksort now take , , or ? (b) Suppose we always pick the pivot element to be the key from the middle position of the subarray. On a sorted array, does Quicksort now take , , or ? (c) Suppose we always pick the pivot element to be the key of the median element of the first three keys of the subarray. (The median of three keys is the middle value, so the median of 5, 3, 8 is five.) On a sorted array, does Quicksort now take , , or ? (d) Suppose we always pick the pivot element to be the key of the median element of the first, last, and middle elements of the subarray. On a sorted array, does Quicksort now take , , or ? 14. (*) Given a set S of n integers and an integer T, give an algorithm to test whether k of the integers in S sum up to T. 15. (**) Design a data structure that allows one to search, insert, and delete an integer X in O(1) time (i.e. constant time, independent of the total number of integers stored). Assume that and that there are m+n units of space available for the symbol table, where m is the maximum number of integers that can be in the table at any one time. (Hint: use two arrays A[1..n] and B[1..m].) You are not allowed to initialize either A or B, as that would take O(m) or O(n) operations. This means the arrays are full of random garbage to begin with, so you must be very careful. 16. (*) Let P be a simple, but not necessarily convex, polygon and q an arbitrary point not necessarily in P. Design an efficient algorithm to find a line segment originating from q that intersects the maximum number of edges of P. In other words, if standing at point q, in what direction should you aim a gun so the bullet will go through the largest number of walls. A bullet through a vertex of P gets credit for only one wall. An algorithm is possible. 17. (**) The onion of a set of n points is the series of convex polygons that result from finding the convex hull, striping it from the point set, and repeating until no more points are left. Give an algorithm for determining the onion of a point set. Next: Implementation Challenges Up: Data Structures and Sorting Previous: War Story: String 'em Algorithms file:///E|/BOOK/BOOK/NODE40.HTM (3 of 4) [19/1/2003 1:28:40]
- Page 123 and 124: Modeling the Problem Figure: Modeli
- Page 125 and 126: About the War Stories Next: War Sto
- Page 127 and 128: War Story: Psychic Modeling Next: E
- Page 129 and 130: War Story: Psychic Modeling have pu
- Page 131 and 132: War Story: Psychic Modeling Next: E
- Page 133 and 134: Exercises (b) If I prove that an al
- Page 135 and 136: Fundamental Data Types Next: Contai
- Page 137 and 138: Containers Next: Dictionaries Up: F
- Page 139 and 140: Dictionaries Next: Binary Search Tr
- Page 141 and 142: Binary Search Trees BinaryTreeQuery
- Page 143 and 144: Priority Queues Next: Specialized D
- Page 145 and 146: Specialized Data Structures Next: S
- Page 147 and 148: Sorting Next: Applications of Sorti
- Page 149 and 150: Applications of Sorting Figure: Con
- Page 151 and 152: Data Structures Next: Incremental I
- Page 153 and 154: Incremental Insertion Next: Divide
- Page 155 and 156: Randomization Next: Bucketing Techn
- Page 157 and 158: Randomization Next: Bucketing Techn
- Page 159 and 160: Bucketing Techniques Algorithms Mon
- Page 161 and 162: War Story: Stripping Triangulations
- Page 163 and 164: War Story: Stripping Triangulations
- Page 165 and 166: War Story: Mystery of the Pyramids
- Page 167 and 168: War Story: Mystery of the Pyramids
- Page 169 and 170: War Story: String 'em Up We were co
- Page 171 and 172: War Story: String 'em Up Figure: Su
- Page 173: Exercises Next: Implementation Chal
- Page 177 and 178: Dynamic Programming Next: Fibonacci
- Page 179 and 180: Fibonacci numbers Next: The Partiti
- Page 181 and 182: Fibonacci numbers Next: The Partiti
- Page 183 and 184: The Partition Problem . What is the
- Page 185 and 186: The Partition Problem Figure: Dynam
- Page 187 and 188: Approximate String Matching Next: L
- Page 189 and 190: Approximate String Matching The val
- Page 191 and 192: Longest Increasing Sequence Will th
- Page 193 and 194: Minimum Weight Triangulation Next:
- Page 195 and 196: Limitations of Dynamic Programming
- Page 197 and 198: War Story: Evolution of the Lobster
- Page 199 and 200: War Story: Evolution of the Lobster
- Page 201 and 202: War Story: What's Past is Prolog Ne
- Page 203 and 204: War Story: What's Past is Prolog th
- Page 205 and 206: War Story: Text Compression for Bar
- Page 207 and 208: War Story: Text Compression for Bar
- Page 209 and 210: Divide and Conquer Next: Fast Expon
- Page 211 and 212: Fast Exponentiation Next: Binary Se
- Page 213 and 214: Binary Search Next: Square and Othe
- Page 215 and 216: Exercises Next: Implementation Chal
- Page 217 and 218: Exercises whose denominations are ,
- Page 219 and 220: The Friendship Graph Next: Data Str
- Page 221 and 222: The Friendship Graph friendship gra
- Page 223 and 224: Data Structures for Graphs linked t
Exercises<br />
(a) Let S be an unsorted array of n integers. Give an algorithm that finds the pair that<br />
maximizes |x-y|. Your algorithm must run in O(n) worst-case time.<br />
(b) Let S be a sorted array of n integers. Give an algorithm that finds the pair that<br />
maximizes |x-y|. Your algorithm must run in O(1) worst-case time.<br />
(c) Let S be an unsorted array of n integers. Give an algorithm that finds the pair that<br />
minimizes |x-y|, for . Your algorithm must run in worst-case time.<br />
(d) Let S be a sorted array of n integers. Give an algorithm that finds the pair that<br />
minimizes |x-y|, for . Your algorithm must run in O(n) worst-case time.<br />
7. (*) Describe how to modify any balanced tree data structure such that search, insert, delete,<br />
minimum, and maximum still take time each, but successor and predecessor now take O(1)<br />
time each. Which operations have to be modified to support this?<br />
8. (*) In one of my research papers [Ski88], I give a comparison-based sorting algorithm that runs in<br />
. Given the existence of an lower bound for sorting, how can this be<br />
possible?<br />
9. (*) Suppose you have access to a balanced dictionary data structure, which supports each of the<br />
operations search, insert, delete, minimum, maximum, successor, and predecessor in time.<br />
Explain how to modify the insert and delete operations so they still take but now<br />
minimum and maximum take O(1) time. (Hint: think in terms of using the abstract dictionary<br />
operations, instead of mucking about with pointers and the like.)<br />
10. (*) Mr. B. C. Dull claims to have developed a new data structure for priority queues that supports<br />
the operations Insert, Maximum, and Extract-Max, all in O(1) worst-case time. Prove that he is<br />
mistaken. (Hint: the argument does not involve a lot of gory details-just think about what this<br />
would imply about the lower bound for sorting.)<br />
11. Use the partitioning step of Quicksort to give an algorithm that finds the median element of an<br />
array of n integers in expected O(n) time.<br />
12. (*) You are given the task of reading in n numbers and then printing them out in sorted order.<br />
Suppose you have access to a balanced dictionary data structure, which supports each of the<br />
operations search, insert, delete, minimum, maximum, successor, and predecessor in time.<br />
❍ Explain how you can use this dictionary to sort in time using only the following<br />
abstract operations: minimum, successor, insert, search.<br />
❍ Explain how you can use this dictionary to sort in time using only the following<br />
abstract operations: minimum, insert, delete, search.<br />
❍ Explain how you can use this dictionary to sort in time using only the following<br />
abstract operations: insert and in-order traversal.<br />
13. <strong>The</strong> running time for Quicksort depends upon both the data being sorted and the partition rule<br />
file:///E|/BOOK/BOOK/NODE40.HTM (2 of 4) [19/1/2003 1:28:40]