TExcelTAD
Home › Discrete Mathematics › Set Operations

Set Operations

Sets combine with a handful of operations that mirror the logical connectives: union is OR, intersection is AND, complement is NOT. That correspondence means every law of logic gives a law of sets, and you can prove set identities with the same tools.

The operations

Operation Math Definition Python
union A∪BA \cup B {x∣x∈A∨x∈B}\{x \mid x \in A \lor x \in B\} A | B
intersection A∩BA \cap B {x∣x∈A∧x∈B}\{x \mid x \in A \land x \in B\} A & B
difference A−BA - B (or A∖BA \setminus B) {x∣x∈A∧x∉B}\{x \mid x \in A \land x \notin B\} A - B
symmetric difference A⊕BA \oplus B in exactly one of AA, BB A ^ B
complement A‾\overline{A} {x∈D∣x∉A}\{x \in D \mid x \notin A\} for a domain DD D - A

Sets with no common element are disjoint: A∩B=∅A \cap B = \emptyset.

python · try it yourself
D = set(range(1, 11))
A = {x for x in D if x % 2 == 0}     # even
B = {x for x in D if x % 3 == 0}     # multiples of 3
print("A =", sorted(A), " B =", sorted(B))
print("A union B =", sorted(A | B))
print("A inter B =", sorted(A & B))
print("A - B =", sorted(A - B), " B - A =", sorted(B - A))
print("A xor B =", sorted(A ^ B))
print("complement of A =", sorted(D - A))
A = [2, 4, 6, 8, 10]  B = [3, 6, 9]
A union B = [2, 3, 4, 6, 8, 9, 10]
A inter B = [6]
A - B = [2, 4, 8, 10]  B - A = [3, 9]
A xor B = [2, 3, 4, 8, 9, 10]
complement of A = [1, 3, 5, 7, 9]

Set identities

Every propositional equivalence becomes a set identity. For example De Morgan's laws:

A∪B‾=A‾∩B‾A∩B‾=A‾∪B‾\overline{A \cup B} = \overline{A} \cap \overline{B} \qquad \overline{A \cap B} = \overline{A} \cup \overline{B}

and the distributive law A∩(B∪C)=(A∩B)∪(A∩C)A \cap (B \cup C) = (A \cap B) \cup (A \cap C).

Proof of the distributive law (chain of iffs). For any xx:

x∈A∩(B∪C)  ⟺  x∈A∧(x∈B∨x∈C)definitions  ⟺  (x∈A∧x∈B)∨(x∈A∧x∈C)distributive law of logic  ⟺  x∈(A∩B)∪(A∩C)definitions\begin{aligned} x \in A \cap (B \cup C) &\iff x \in A \land (x \in B \lor x \in C) && \text{definitions} \\ &\iff (x \in A \land x \in B) \lor (x \in A \land x \in C) && \text{distributive law of logic} \\ &\iff x \in (A \cap B) \cup (A \cap C) && \text{definitions} \end{aligned}

Since xx is in the left set exactly when it is in the right set, the sets are equal. ■\blacksquare

The same identity can be tested on random sets. A test like this does not replace the proof, but it catches wrong identities quickly:

python · try it yourself
import random

random.seed(7)
U = range(20)
rand_set = lambda: {x for x in U if random.random() < 0.5}
ok_dist = ok_morgan = True
for _ in range(1000):
    A, B, C = rand_set(), rand_set(), rand_set()
    ok_dist &= A & (B | C) == (A & B) | (A & C)
    ok_morgan &= set(U) - (A | B) == (set(U) - A) & (set(U) - B)
print("distributive law held:", ok_dist, " De Morgan held:", ok_morgan)

# a FALSE "identity": A - (B - C) == (A - B) - C
A, B, C = {1, 2, 3}, {2, 3}, {3}
print("A - (B - C) =", A - (B - C), "  (A - B) - C =", (A - B) - C)
distributive law held: True  De Morgan held: True
A - (B - C) = {1, 3}   (A - B) - C = {1}

Difference is not associative: A−(B−C)A - (B - C) keeps the elements of CC that B−CB - C removed from the subtraction.

Venn diagrams and counting

For finite sets, the size of a union follows from the overlap:

∣A∪B∣=∣A∣+∣B∣−∣A∩B∣\lvert A \cup B \rvert = \lvert A \rvert + \lvert B \rvert - \lvert A \cap B \rvert

The elements of A∩BA \cap B are counted twice in ∣A∣+∣B∣\lvert A \rvert + \lvert B \rvert, so we subtract them once. In the example above, 5+3−1=75 + 3 - 1 = 7. The general version for many sets is inclusion-exclusion.

In software

Set operations are built into SQL: UNION, INTERSECT and EXCEPT (difference). Python's sets make tasks like "which users are in the beta group but not admins" one expression, beta - admins. Bitsets store a set of small integers as the bits of an integer, so union is | and intersection is & on whole machine words at once.

Exercises

Exercise 1: Compute

With D={1,…,8}D = \{1, \ldots, 8\}, A={1,2,3,4}A = \{1, 2, 3, 4\}, B={3,4,5,6}B = \{3, 4, 5, 6\}: find A∪BA \cup B, A∩BA \cap B, A−BA - B, A⊕BA \oplus B and A∪B‾\overline{A \cup B}.

Answer

{1,…,6}\{1, \ldots, 6\}, {3,4}\{3, 4\}, {1,2}\{1, 2\}, {1,2,5,6}\{1, 2, 5, 6\}, {7,8}\{7, 8\}.

Solution

A⊕B=(A−B)∪(B−A)={1,2}∪{5,6}A \oplus B = (A - B) \cup (B - A) = \{1, 2\} \cup \{5, 6\}. The complement of the union is what is left of DD: {7,8}\{7, 8\}.

Exercise 2: Prove an identity

Prove A−B=A∩B‾A - B = A \cap \overline{B}.

Answer

x∈A−B  ⟺  x∈A∧x∉B  ⟺  x∈A∧x∈B‾  ⟺  x∈A∩B‾x \in A - B \iff x \in A \land x \notin B \iff x \in A \land x \in \overline{B} \iff x \in A \cap \overline{B}.

Solution

Each step uses only a definition: difference, then complement (relative to the domain), then intersection. Since membership is equivalent for every xx, the sets are equal. This identity is how SQL engines often implement EXCEPT: an intersection with "not in".

Exercise 3: Union size

In a class of 40 students, 25 take Python, 18 take SQL and 9 take both. How many take neither?

Answer
Solution
∣P∪S∣=25+18−9=34,40−34=6\lvert P \cup S \rvert = 25 + 18 - 9 = 34, \qquad 40 - 34 = 6

Exercise 4: Test a conjecture (code)

Is (A∪B)−C=(A−C)∪(B−C)(A \cup B) - C = (A - C) \cup (B - C) always true? Test on random sets.

Answer

Yes (no counterexample appears, and it follows from the distributive law).

Solution

(A∪B)−C=(A∪B)∩C‾=(A∩C‾)∪(B∩C‾)(A \cup B) - C = (A \cup B) \cap \overline{C} = (A \cap \overline{C}) \cup (B \cap \overline{C}) by distributivity.

python · try it yourself
import random

random.seed(1)
r = lambda: {x for x in range(12) if random.random() < 0.5}
print(all((A | B) - C == (A - C) | (B - C) for A, B, C in ((r(), r(), r()) for _ in range(2000))))
True

Test yourself

Which logical connective corresponds to set intersection?

  1. OR
  2. AND
  3. NOT
  4. XOR

By De Morgan, A∪B‾\overline{A \cup B} equals…

  1. A‾∪B‾\overline{A} \cup \overline{B}
  2. A‾∩B‾\overline{A} \cap \overline{B}
  3. A∩BA \cap B
  4. A−BA - B

If ∣A∣=10\lvert A \rvert = 10, ∣B∣=7\lvert B \rvert = 7 and ∣A∪B∣=12\lvert A \cup B \rvert = 12, what is ∣A∩B∣\lvert A \cap B \rvert?

  1. 3
  2. 5
  3. 17
  4. 2