• Aucun résultat trouvé

Comparing the Implementations

Dans le document This page intentionally left blank (Page 172-175)

Searching and Sorting

5.4 The Set ADT Revisited

5.4.2 Comparing the Implementations

The implementation of the Set ADT using an unsorted list was quick and easy, but after evaluating the various operations, it became apparent many of them were time consuming. A new implementation using a sorted list to store the elements of the set and the binary search algorithm for locating elements improved the contains method. This resulted in better times for the isSubsetOf() and eq methods, but the set union, intersection, and difference operations remained quadratic. After observing several operations could be further improved if they were implemented to directly access the list instead of using the contains method, we were able to provide a more efficient implementation of the Set ADT.

Table 5.1 compares the worst case time-complexities for the Set ADT operations using an unsorted list and the improved sorted list version using the binary search and the list merging operation.

Operation Unordered Improved

s = Set() O(1) O(1)

len(s) O(1) O(1)

x in s O(n) O(logn)

s.add(x) O(n) O(n)

s.isSubsetOf(t) O(n2) O(n)

s == t O(n2) O(n)

s.union(t) O(n2) O(n)

Table 5.1: Comparison of the two Set ADT implementations using an unsorted list and the improved sorted list with binary search and list merging.

Exercises

5.1 Given an unsorted list ofnvalues, what is the time-complexity to find thekth smallest value in the worst case? What would be the complexity if the list were sorted?

5.2 What is theO(·) for thefindSortedPosition()function in the worst case?

5.3 Consider the new implementation of theSetclass using a sorted list with the binary search.

(a) Prove or show the worst case time for the add()method isO(n).

(b) What is the best case time for the add() method?

Programming Projects 153 5.4 Determine the worst case time complexity for each method of the Map ADT

implemented in Section 3.2.

5.5 Modify the binary search algorithm to find the position of the first occurrence of a value that can occur multiple times in the ordered list. Verify your algorithm is stillO(logn).

5.6 Design and implement a function to find all negative values within a given list.

Your function should return a new list containing the negative values. When does the worst case occur and what is the run time for that case?

5.7 In this chapter, we used a modified version of themergeSortedLists() func-tion to develop a linear timeunion()operation for our Set ADT implemented using a sorted list. Use a similar approach to implement new linear time ver-sions of theisSubsetOf(),intersect(), and difference()methods.

5.8 Given the following list of keys (80, 7, 24, 16, 43, 91, 35, 2, 19, 72), show the contents of the array after each iteration of the outer loop for the indicated algorithm when sorting in ascending order.

(a) bubble sort (b) selection sort (c) insertion sort

5.9 Given the following list of keys (3, 18, 29, 32, 39, 44, 67, 75), show the contents of the array after each iteration of the outer loop for the

(a) bubble sort (b) selection sort (c) insertion sort

5.10 Evaluate the insertion sort algorithm to determine the best case and the worst case time complexities.

Programming Projects

5.1 Implement the Bag ADT from Chapter 1 to use a sorted list and the binary search algorithm. Evaluate the time complexities for each of the operations.

5.2 Implement a new version of the Map ADT from Section 3.2 to use a sorted list and the binary search algorithm.

5.3 The implementation of the Sparse Matrix ADT from Chapter 4 can be im-proved by storing the MatrixElement objects in a sorted list and using the binary search to locate a specific element. The matrix elements can be sorted based on the row and column indices using an index function similar to that used with a 2-D array stored in a MultiArray. Implement a new version of the Sparse Matrix ADT using a sorted list and the binary search to locate elements.

5.4 Implement a new version of the Sparse Life Grid ADT from Chapter 4 to use a sorted list and the binary search to locate the occupied cells.

154 CHAPTER 5 Searching and Sorting

5.5 A colormap is a lookup table or color palette containing a limited set of colors.

Early color graphics cards could only display up to 256 unique colors at one time. Colormaps were used to specify which 256 colors should be used to display color images on such a device. Software applications were responsible for mapping each color in the image that was to be displayed to a color in the limited color set specified by the colormap. We can define a Colormap ADT for storing a limited set of colors and for use in mapping one of the 16.7+ million colors possible in the discrete RGB color space to a color in the colormap. Given the description below of various operations, implement the Colormap ADT using a 1-D array structure.

ColorMap( k ): Creates a new empty colormap that is capable of storing up to kcolors.

length(): Returns the number of colors currently stored in the colormap.

contains( color ): Determines if the given color is contained in the col-ormap.

add( color ): Adds the given color to the colormap. Only one instance of each color can be added to the colormap. In addition, a color cannot be added to a full colormap.

remove( color ): Removes the given color from the colormap. The color must be contained in the colormap in order to be removed.

map( color ): Maps the given color to an entry in the colormap and returns that color. A common approach is to map thecolorto its nearest neighbor in the colormap. The nearest neighbor of a color is the entry in the colormap that has the minimum Euclidean distance squared between the two colors. If there is more than one nearest neighbor in the colormap, only one is returned. In addition, the colormap must contain at least one color in order to perform the mapping operation.

iterator(): Creates and returns an iterator object that can be used to iterate over the colors in the colormap.

5.6 Evaluate the map() method of your implementation of the Colormap ADT from the previous question to determine the worst case time-complexity.

5.7 Colormaps are used in color quantization, which is the process of reducing the number of colors in an image while trying to maintain the original appearance as much as possible. Part of the process recolors an original image using a reduced set of colors specified in a colormap.

(a) Implement the function recolorImage( image, colormap ), which pro-duces a newColorImagethat results from mapping the color of each pixel in the givenimage to its nearest neighbor in the givencolormap.

(b) What is the worst case time-complexity for your implementation?

CHAPTER 6

Dans le document This page intentionally left blank (Page 172-175)