Skip to main content

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 aAa\in A when aa is an element of AA, and aAa\notin A otherwise. The same set can be described by roster notation, such as {1,2,3}\{1,2,3\}, or set-builder notation, such as {xZ:1x3}\{x\in\mathbb{Z}:1\le x\le3\}.

Two sets are equal exactly when they have the same elements:

A=Bif and only ifx(xAxB).A=B \quad\text{if and only if}\quad \forall x(x\in A \leftrightarrow x\in B).

AA is a subset of BB, written ABA\subseteq B, if every element of AA is also in BB:

ABif and only ifx(xAxB).A\subseteq B \quad\text{if and only if}\quad \forall x(x\in A \to x\in B).

If ABA\subseteq B and ABA\ne B, then AA is a proper subset of BB. The empty set \emptyset has no elements and is a subset of every set. The power set P(A)\mathcal{P}(A) is the set of all subsets of AA.

For sets within a universal set UU:

  • Union: AB={x:xAxB}A\cup B=\{x:x\in A\lor x\in B\}.
  • Intersection: AB={x:xAxB}A\cap B=\{x:x\in A\land x\in B\}.
  • Difference: AB={x:xAxB}A-B=\{x:x\in A\land x\notin B\}.
  • Complement: A=UA\overline{A}=U-A.
  • Symmetric difference: AB=(AB)(BA)A\oplus B=(A-B)\cup(B-A).
  • Cartesian product: A×B={(a,b):aAbB}A\times B=\{(a,b):a\in A\land b\in B\}.

Sets can contain other sets. For example, if A={1,2}A=\{1,2\}, then P(A)={,{1},{2},{1,2}}\mathcal{P}(A)=\{\emptyset,\{1\},\{2\},\{1,2\}\}. Notice that 1A1\in A, but {1}A\{1\}\subseteq A and {1}P(A)\{1\}\in\mathcal{P}(A).

Key results

Set identities mirror logical equivalences. De Morgan's laws for sets are:

AB=AB,AB=AB.\overline{A\cup B}=\overline{A}\cap\overline{B}, \qquad \overline{A\cap B}=\overline{A}\cup\overline{B}.

Proof of the first identity by element chasing: let xx be arbitrary. Then

xABxAB¬(xAxB)xAxBxAB.\begin{aligned} x\in\overline{A\cup B} &\leftrightarrow x\notin A\cup B\\ &\leftrightarrow \neg(x\in A\lor x\in B)\\ &\leftrightarrow x\notin A\land x\notin B\\ &\leftrightarrow x\in\overline{A}\cap\overline{B}. \end{aligned}

Since the equivalence holds for every xx, the sets are equal.

Other useful identities include:

A(BC)=(AB)(AC),A(BC)=(AB)(AC),A=A,AU=A,AA=U,AA=.\begin{aligned} A\cup(B\cap C)&=(A\cup B)\cap(A\cup C),\\ A\cap(B\cup C)&=(A\cap B)\cup(A\cap C),\\ A\cup\emptyset&=A,\\ A\cap U&=A,\\ A\cup\overline{A}&=U,\\ A\cap\overline{A}&=\emptyset. \end{aligned}

If AA is finite with nn elements, then P(A)=2n\vert \mathcal{P}(A)\vert =2^n. Each element of AA has two independent choices when forming a subset: include it or exclude it. By the product rule, this gives 2n2^n subsets.

For finite sets, inclusion-exclusion begins with

AB=A+BAB.|A\cup B|=|A|+|B|-|A\cap B|.

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 |
+------------------------------------------------+
IdentityMembership translationLogic law behind it
AB=AB\overline{A\cup B}=\overline{A}\cap\overline{B}not in AA or BB¬(PQ)¬P¬Q\neg(P\lor Q)\equiv\neg P\land\neg Q
AB=AB\overline{A\cap B}=\overline{A}\cup\overline{B}not in both¬(PQ)¬P¬Q\neg(P\land Q)\equiv\neg P\lor\neg Q
A(BC)=(AB)(AC)A\cap(B\cup C)=(A\cap B)\cup(A\cap C)in AA and one of B,CB,Cdistributive law
A(AB)=AA\cup(A\cap B)=AAA already contains ABA\cap Babsorption law

Worked example 1: Compute operations and check inclusion-exclusion

Problem. Let

U={1,2,3,4,5,6,7,8},A={1,2,3,5,8},B={2,4,5,6}.U=\{1,2,3,4,5,6,7,8\},\quad A=\{1,2,3,5,8\},\quad B=\{2,4,5,6\}.

Find ABA\cup B, ABA\cap B, ABA-B, A\overline{A}, and verify inclusion-exclusion.

Method.

  1. The union contains elements appearing in either set:
AB={1,2,3,4,5,6,8}.A\cup B=\{1,2,3,4,5,6,8\}.
  1. The intersection contains elements appearing in both:
AB={2,5}.A\cap B=\{2,5\}.
  1. The difference ABA-B keeps elements of AA that are not in BB:
AB={1,3,8}.A-B=\{1,3,8\}.
  1. The complement of AA is computed relative to UU:
A=UA={4,6,7}.\overline{A}=U-A=\{4,6,7\}.
  1. Check inclusion-exclusion:
A+BAB=5+42=7=AB.\begin{aligned} |A|+|B|-|A\cap B| &=5+4-2\\ &=7\\ &=|A\cup B|. \end{aligned}

Checked answer. The operations are correct, and the count agrees. Element 77 is in neither AA nor BB, 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

A(BC)=(AB)(AC).A-(B\cup C)=(A-B)\cap(A-C).

Method.

  1. Let xx be an arbitrary element.
  2. Translate membership in the left side:
xA(BC)xAxBCxA¬(xBxC)xAxBxC.\begin{aligned} x\in A-(B\cup C) &\leftrightarrow x\in A\land x\notin B\cup C\\ &\leftrightarrow x\in A\land \neg(x\in B\lor x\in C)\\ &\leftrightarrow x\in A\land x\notin B\land x\notin C. \end{aligned}
  1. Translate membership in the right side:
x(AB)(AC)xABxAC(xAxB)(xAxC)xAxBxC.\begin{aligned} x\in (A-B)\cap(A-C) &\leftrightarrow x\in A-B\land x\in A-C\\ &\leftrightarrow (x\in A\land x\notin B)\land(x\in A\land x\notin C)\\ &\leftrightarrow x\in A\land x\notin B\land x\notin C. \end{aligned}
  1. Both sides have the same membership condition for arbitrary xx.

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 \in and \subseteq. An element belongs to a set; a set can be a subset of another set.
  • Forgetting the universal set when taking complements. A\overline{A} depends on the surrounding UU.
  • Treating repeated roster entries as distinct. {1,1,2}={1,2}\{1,1,2\}=\{1,2\}.
  • Assuming AB=BAA-B=B-A. Set difference is not commutative.
  • Treating Cartesian products as unordered. Usually (a,b)(b,a)(a,b)\ne(b,a).
  • 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 {x:x2<4}\{x:x^2\lt 4\} means different things over integers, rationals, and real numbers. Writing {xZ:x2<4}\{x\in\mathbb{Z}:x^2\lt 4\} makes the intended set finite and equal to {1,0,1}\{-1,0,1\}, while the real-domain version is the interval (2,2)(-2,2).

To prove A=BA=B, use the element method or prove two subset inclusions. The element method shows xAxBx\in A\leftrightarrow x\in B for arbitrary xx. The subset method proves ABA\subseteq B and BAB\subseteq A 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 A={1,2}A=\{1,2\}, then {1}P(A)\{1\}\in\mathcal{P}(A) and {1}A\{1\}\subseteq A, but {1}A\{1\}\notin A unless AA itself contains the set {1}\{1\} 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 A=m\vert A\vert =m and B=n\vert B\vert =n, then A×B=mn\vert A\times B\vert =mn. If A=BA=B, the product A×AA\times A contains ordered pairs, so (a,b)(a,b) and (b,a)(b,a) are different when aba\ne b. Relations on AA are subsets of this ordered product, which is why there are 2A22^{\vert A\vert ^2} possible relations on a finite set AA.

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 Z\mathbb{Z}, but within N\mathbb{N} 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 A(BC)A-(B-C) is not the same as (AB)C(A-B)-C. 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 ABA\subseteq B and A=B\vert A\vert =\vert B\vert for finite sets, then equality follows; without the subset relation, equal size alone is not enough.

Connections