Sets


A collection of items which does not contain duplicates.

  • Storing an item that is already in the set does not change the set.  
  • If an item is not in a set, deleting that item from the set does not change the set.  
  • Sets are not ordered.

Base type: The type of the items in the set

Cardinality: The number of items in a set

Cardinality of the base type: The number of items in the base type

Operators

Union of two sets: A set made up of all the items in either sets

Intersection of two sets: A set made up of all the items in both sets

Difference of two sets: A set made up of all the items in the first set that are not in the second set

Implementation - bit vector

 

Each item in the base type has a representation in each instance of a set.

The representation is either true (item is in the set) or false (item is not in the set).  

Space is proportional to the cardinality of the base type.  

Algorithms use Boolean operations.

Implementation - Lists

The items in an instance of a set are on a list that represents the set.

Those items that are not on the list are not in the set.  

Space is proportional to the cardinality of the set instance.  

Algorithms use ADT List operations.

© Lynne Grewe