Sets ====== Definition and Operations --------------------------- The mathematical concept of a set is an unordered collection of unique values. We can perform several operations with sets including determining membership in a set (is the given value an element inside the collection?). We can also take the intersection of two sets which is the collection of elements that they have in common. And we can take the union of two sets which is a new collection that contains the unique elements from both sets together. Finally, we can take the set difference or elements in one set but not in the other. Implementation ---------------- To implement this all efficiently, we take the collection and sort it from the outset. This seems counterintuitive since the set is supposed to be unordered by definition. But, consider that if the set is unordered, any order will do for representation. And even mathameticians often list the elements of a set manually in some sort of order to make it easier to keep track of things. With the data sorted, we can implement things like set membership in logarithmic time instead of linear time, for instance. The other operations are sped up as well. Ordered representations of sets are often coded as search trees but these are not the only way. Others use hash tables to store sets in an unordered way. Disjoint Sets --------------- A related concept is that of set disjointedness. Two sets are considered disjoint if their intersection is the empty set. That is, they have no overlapping elements. The disjoint set data structure can be used in its own right for many interesting things and will be studied separately.