Data Structures and Algorithms using Python
Copyright © 2023 by Rance Necaise
Table Of Contents
Data Structures and Algorithms using Python
Copyright © 2023
by Rance Necaise

5.9 Evaluating the Set Implementation

We can use complexity analysis to determine the efficiency of the Set ADT operations as implemented in the previous section. For convenience, the relevant portions of that implementation are shown again in the program listing below. The evaluation is quite simple since the ADT was implemented using the list and we just evaluated the methods for that structure. Table 5.9.1 provides a summary of the worst case time-complexities for the set operations implemented earlier in the chapter.

OperationWorst Case
s = Set() O(1)
len(s) O(1)
x in s O(n)
s.add(x) O(n)
s.isSubsetOf(t) O(n2)
s == t O(n2)
s.union(t) O(n2)
traversal O(n)
Table 5.9.1: Time-complexities for the Set ADT implementation from Section 5.9 using an unsorted list.
Program Listing
Listing of the Set ADT implementation.

Simple Operations

set contains
set add
worst-case: O(n)

Evaluating the constructor and length operation is straightforward as they simply call the corresponding list operation. The __contains__ method, which determines if an element is contained in the set, uses the in operator to perform a linear search over the elements stored in the underlying list. The search operation, which requires O(n), will be presented in the next chapter and we postpone its analysis until that time. The add method also requires O(n) time in the worst case since it uses the in operator to determine if the element is unique and the append method to add the unique item to the underlying list, both of which require linear time in the worst case.

Operations of Two Sets

The remaining methods of the Set class involve the use of two sets, which we label A and B, where A is the self set and B is the argument passed to the given method. To simplify the analysis, we assume each set contains n elements. A more complete analysis would involve the use of two variables, one for the size of each set. But the analysis of this more specific case is sufficient for our purposes.

set subset
set equals
worst-case: O(n2)

The isSubsetOf method determines if A is a subset of B. It iterates over the n elements of set A, during which the in operator is used to determine if the given element is a member of set B. Since there are n repetitions of the loop and each use of the in operator requires O(n) time, the isSubsetOf method has a quadratic run time of O(n2). The set equality operation is also O(n2) since it calls isSubsetOf after determining the two sets are of equal size.

Set Union Operation

The set union operation creates a new set, C, that contains all of the unique elements from both sets A and B. It requires three steps.

  • The first step creates the new set C, which can be done in constant time.
  • The second step fills set C with the elements from set A, which requires O(n) time since the extend list method is used to add the elements to set C.
  • The last step iterates over the elements of set B during which the in operator is used to determine if the given element is a member of set A. If the element is not a member of set A, it's added to set C by applying the append list method. We know from earlier the linear search performed by the in operator requires O(n) time and we can use the O(1) amortized cost of the append method since it is applied in sequence. Given that the loop is performed n times and each iteration requires n+1 time, this step requires O(n2) time.
set union
worst-case: O(n2)

Combining the times for the three steps yields a worst case time of O(n2).

Page last modified on August 26, 2023, at 07:32 AM