6.11 Improved Set ImplementationIn the previous section, we used a sorted list and the binary search to provide an alternative implementation for the Set ADT. That implementation provided better time-complexities for several of the operations, but we only achieved a time of for the subset and equals operations and maintained the same quadratic time for the set union, intersection, and difference operations. A more efficient implementation is possible if we take into account that we know the underlying lists of the two sets are in sorted order and work directly with the sorted lists instead of calling various set operations. New Set Equals MethodFor the set equals operation, instead of calling the setA = Set() setA.add(7) setA.add(15) setA.add(38) setA.add(24) setB = Set() setB.add(24) setB.add(19) setB.add(45) setB.add(7) setC = Set() setC.add(15) setC.add(38) setC.add(24) setC.add(7) The physical view of the three sets are illustrated in Figure 6.11.1. Take a moment and observe the elements within each of the sorted lists. ![]() Figure 6.11.1: The physical view for three sample sets implemented using sorted lists.
If we compare ![]() you can see that the two lists have the same size and all of the elements are in corresponding positions. Here, the two sets are equal. On the other hand, if we compare ![]() the two lists are of the same size, but notice that the corresponding elements in the second position are not equal. Hence, the two sets can not be equal. A new implementation of the set equals operation is provided below. The two lists are traversed simultaneously during which corresponding elements are compared. If a single instance occurs where corresponding elements are not identical, then the two sets cannot be equal. Otherwise, having traversed the entire list and finding no mismatched items, the two sets must be equal. Program Listing
The new implementation only requires time since it only involves one complete simultaneous traversal of the two lists. A similar approach can be used to improve the efficiency of the New Set Union MethodThe efficiency of the set union operation can also be improved from the original version. Set union using two sorted lists is very similar to the problem of merging two sorted lists that was introduced in the previous section. In that problem, the entire contents of the two sorted lists were merged into a third list. For the Set ADT implemented using a sorted list, the result of the set union must be a new sorted list merged from the unique values contained in the sorted lists used to implement the two source sets. The implementation of the new Program Listing
Comparing the Set ImplementationsThe 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 operation. This resulted in better times for the
Table 6.11.1: Comparison of the three Set ADT implementations: (1) the original version using an unsorted list, (2) the improved version using a sorted list with a binary search, and (3) the efficient implementation using a sorted list and list merging.
|