The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998 The.Algorithm.Design.Manual.Springer-Verlag.1998

18.04.2013 Views

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]

Exercises<br />

used to select the pivot. Although Quicksort is on average, certain partition rules cause<br />

Quicksort to take time if the array is already sorted.<br />

(a) Suppose we always pick the pivot element to be the key from the last position of the subarray.<br />

On a sorted array, does Quicksort now take , , or ?<br />

(b) Suppose we always pick the pivot element to be the key from the middle position of the<br />

subarray. On a sorted array, does Quicksort now take , , or ?<br />

(c) Suppose we always pick the pivot element to be the key of the median element of the first<br />

three keys of the subarray. (<strong>The</strong> median of three keys is the middle value, so the median of 5, 3, 8<br />

is five.) On a sorted array, does Quicksort now take , , or ?<br />

(d) Suppose we always pick the pivot element to be the key of the median element of the first,<br />

last, and middle elements of the subarray. On a sorted array, does Quicksort now take ,<br />

, or ?<br />

14. (*) Given a set S of n integers and an integer T, give an algorithm to test whether k of<br />

the integers in S sum up to T.<br />

15. (**) <strong>Design</strong> a data structure that allows one to search, insert, and delete an integer X in O(1) time<br />

(i.e. constant time, independent of the total number of integers stored). Assume that and<br />

that there are m+n units of space available for the symbol table, where m is the maximum number<br />

of integers that can be in the table at any one time. (Hint: use two arrays A[1..n] and B[1..m].) You<br />

are not allowed to initialize either A or B, as that would take O(m) or O(n) operations. This means<br />

the arrays are full of random garbage to begin with, so you must be very careful.<br />

16. (*) Let P be a simple, but not necessarily convex, polygon and q an arbitrary point not necessarily<br />

in P. <strong>Design</strong> an efficient algorithm to find a line segment originating from q that intersects the<br />

maximum number of edges of P. In other words, if standing at point q, in what direction should<br />

you aim a gun so the bullet will go through the largest number of walls. A bullet through a vertex<br />

of P gets credit for only one wall. An algorithm is possible.<br />

17. (**) <strong>The</strong> onion of a set of n points is the series of convex polygons that result from finding the<br />

convex hull, striping it from the point set, and repeating until no more points are left. Give an<br />

algorithm for determining the onion of a point set.<br />

Next: Implementation Challenges Up: Data Structures and Sorting Previous: War Story: String 'em<br />

<strong>Algorithm</strong>s<br />

file:///E|/BOOK/BOOK/NODE40.HTM (3 of 4) [19/1/2003 1:28:40]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!