Sets and Set Operations
Sets are the basic containers of discrete mathematics. Relations are sets of ordered pairs, graphs are sets of vertices and edges, languages are sets of strings, and events in probability are sets of outcomes. The discipline around sets is simple but strict: order does not matter, repetition does not matter, and membership is the central question.
Set notation gives a compact way to describe large or infinite collections. Instead of listing every object, we can define a set by a property, combine sets with operations, and prove two descriptions match by checking membership element by element. This makes set theory the bridge between logic and the rest of the subject.
Definitions
A set is an unordered collection of distinct elements. Write when is an element of , and otherwise. The same set can be described by roster notation, such as , or set-builder notation, such as .
Two sets are equal exactly when they have the same elements:
is a subset of , written , if every element of is also in :
If and , then is a proper subset of . The empty set has no elements and is a subset of every set. The power set is the set of all subsets of .
For sets within a universal set :
- Union: .
- Intersection: .
- Difference: .
- Complement: .
- Symmetric difference: .
- Cartesian product: .
Sets can contain other sets. For example, if , then . Notice that , but and .
Key results
Set identities mirror logical equivalences. De Morgan's laws for sets are:
Proof of the first identity by element chasing: let be arbitrary. Then
Since the equivalence holds for every , the sets are equal.
Other useful identities include:
If is finite with elements, then . Each element of has two independent choices when forming a subset: include it or exclude it. By the product rule, this gives subsets.
For finite sets, inclusion-exclusion begins with
The subtraction corrects the double count of elements lying in both sets. This same idea grows into the larger inclusion-exclusion formulas used in counting.
Visual
Universal set U
+------------------------------------------------+
| |
| A only A and B B only |
| +---------+ +---------+ +---------+ |
| | A - B | | A cap B | | B - A | |
| +---------+ +---------+ +---------+ |
| |
| outside both = complement of A union B |
+------------------------------------------------+
| Identity | Membership translation | Logic law behind it |
|---|---|---|
| not in or | ||
| not in both | ||
| in and one of | distributive law | |
| already contains | absorption law |
Worked example 1: Compute operations and check inclusion-exclusion
Problem. Let
Find , , , , and verify inclusion-exclusion.
Method.
- The union contains elements appearing in either set:
- The intersection contains elements appearing in both:
- The difference keeps elements of that are not in :
- The complement of is computed relative to :
- Check inclusion-exclusion:
Checked answer. The operations are correct, and the count agrees. Element is in neither nor , which is why it does not appear in the union even though it is in the universal set.
Worked example 2: Prove a set identity by element chasing
Problem. Prove
Method.
- Let be an arbitrary element.
- Translate membership in the left side:
- Translate membership in the right side:
- Both sides have the same membership condition for arbitrary .
Checked answer. Since every element belongs to the left side exactly when it belongs to the right side, the sets are equal. This proof also shows why the formula is a set version of De Morgan's law.
Code
from itertools import chain, combinations
def powerset(items):
items = list(items)
for r in range(len(items) + 1):
for combo in combinations(items, r):
yield set(combo)
U = set(range(1, 9))
A = {1, 2, 3, 5, 8}
B = {2, 4, 5, 6}
print("union", A | B)
print("intersection", A & B)
print("difference", A - B)
print("complement", U - A)
print("powerset size", len(list(powerset(A))))
Python sets implement union, intersection, difference, and symmetric difference directly. The powerset function uses combinations of all possible subset sizes.
Common pitfalls
- Confusing and . An element belongs to a set; a set can be a subset of another set.
- Forgetting the universal set when taking complements. depends on the surrounding .
- Treating repeated roster entries as distinct. .
- Assuming . Set difference is not commutative.
- Treating Cartesian products as unordered. Usually .
- Proving a set identity by drawing only one picture. Diagrams build intuition, but element chasing provides a proof.
Set-builder notation should always include a domain when ambiguity is possible. The expression means different things over integers, rationals, and real numbers. Writing makes the intended set finite and equal to , while the real-domain version is the interval .
To prove , use the element method or prove two subset inclusions. The element method shows for arbitrary . The subset method proves and separately. Both approaches are formal versions of "same elements," and both avoid relying on a diagram that may not capture all cases.
Power sets are a common source of type errors. If , then and , but unless itself contains the set as an element. Keeping track of whether an object is an element or a set of elements prevents many mistakes in relations and functions.
Cartesian products are ordered and can change size quickly. If and , then . If , the product contains ordered pairs, so and are different when . Relations on are subsets of this ordered product, which is why there are possible relations on a finite set .
For complements and differences, always name the universal set. In probability, the complement of an event is relative to the sample space. In number theory, the complement of even integers might mean odd integers within , but within it means positive odd integers. The notation alone does not determine the surrounding universe.
For finite examples, verify identities by testing membership rather than by comparing roster lists too early. Roster lists can hide duplicates or ordering changes. The element method forces the exact logical condition on both sides, which is why it scales from three-element examples to arbitrary sets.
When a problem involves several operations, add parentheses before simplifying. Set difference binds less predictably in students' minds than union and intersection, and is not the same as . Translating every operation into membership logic is the most reliable way to disambiguate the expression.
For finite sets, cardinality checks are useful but not complete proofs of equality unless one subset relation is already known. Two sets can have the same size and different elements. If you prove and for finite sets, then equality follows; without the subset relation, equal size alone is not enough.
Connections
- Propositional logic explains the logical laws behind set identities.
- Predicates and quantifiers gives the formal language for subset and equality proofs.
- Counting principles uses set sizes, products, and unions.
- Pigeonhole and inclusion-exclusion extends the counting formula for unions.
- Relations treats relations as sets of ordered pairs.