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.6 Sets

A set is a container that stores a collection of unique values. The elements or members of a set are not stored in any particular order and can not be accessed by position. Like the Bag ADT introduced in Chapter 2, the set is a nonlinear data structure. The Set ADT is a common container used in computer science for a variety of problems. The operations available for use with a set, which are explored in the next section, are the same as the operations performed on sets in mathematics.

While Python provides a set container as part of the language itself, other languages may make it available as part of the standard library. Still others, like the C language, does not provide a set type, even as part of its standard library. A set is another excellent example of an abstract data type that can be implemented using different data structures and organizing the data in a variety of ways with some being more efficient than others.

The Set ADT

The definition of the set abstract data type is provided here, followed by an implementation using a list. In later chapters, we will provide and evaluate alternate implementations for the Set ADT.

The Set ADT

A set is a container that stores a collection of unique values over a given comparable domain in which the stored values have no particular ordering.

  • Set()

    Creates a new set initialized to the empty set.

  • length()

    Returns the number of elements in the set, also known as the cardinality.

  • isEmpty()

    Returns a Boolean value that indicates whether the set is empty.

  • contains(element)

    Determines if the given value is an element of the set and returns the appropriate Boolean value.

  • add(element)

    Modifies the set by adding the given value or element to the set if the element is not already a member. If the element is not unique, no action is taken and the operation is skipped.

  • remove(element)

    Removes the given element from the set, which must be in the set.

  • clear()

    Removes all elements from the set resulting in an empty set.

  • equals(setB)

    Determines if the set is equal to another set and returns a Boolean value. For two sets, A and B, to be equal, both A and B must contain the same number of elements and all elements in A must also be elements in B. If both sets are empty, the sets are equal.

  • isSubsetOf(setB)

    Determines if the set is a subset of another set and returns a Boolean value. For set A to be a subset of B, all elements in A must also be elements in $B$.

  • union(setB)

    Creates and returns a new set that is the union of this set and setB. The new set created from the union of two sets, A and B, contains all elements in $A$ plus those elements in B that are not in A. Neither set A nor set B is modified by this operation.

  • intersection(setB)

    Creates and returns a new set that is the intersection of this set and setB. The intersection of sets A and B contains only those elements that are in both A and B. Neither set A nor set B is modified by this operation.

  • difference(setB)

    Creates and returns a new set that is the difference of this set and setB. The set difference, - B, contains only those elements that are in A but not in B. Neither set A nor set B is modified by this operation.

  • iterator()

    Returns an iterator that can be used to iterate over the collection of elements.

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