10–12 min read Discrete Mathematics Lesson 6 of 32
Part 2 — Discrete Mathematics

Lesson 14.06 — Set Theory

The mathematical backbone of structured data: elements, membership, subsets, set operations, De Morgan's laws, cardinality, and applications in Python and SQL

What We Will Master in This Lesson

The fundamental language of discrete mathematics and computational data systems:

  • Well-defined sets, distinct elements, and membership (∈, ∉)
  • Roster Form vs. Set-Builder Form representations
  • Empty Set (∅) vs. Universal Set (U) and boundary conditions
  • Subsets (⊆) and Proper Subsets (⊂) distinctions
  • 4 Core Operations: Union (∪), Intersection (∩), Difference (∖), Complement
  • Disjoint sets and mutually exclusive data states
  • Cardinality |A| and the Principle of Inclusion-Exclusion
  • Power Set P(A) and 2n combinatorial explosion
  • Algebraic laws of sets and De Morgan's dual laws
  • Applications in Python sets, SQL queries, Graph theory, and AI
1

Introduction: How Do We Organize and Filter Data?

Imagine you operate an e-commerce tech platform. Your warehouse stores hundreds of distinct devices: laptops, smartphones, mechanical keyboards, wireless mice, 4K monitors, and external SSDs.

As a software engineer or database administrator, you are tasked with generating two specific product groups:

List A: Portable Smart Devices

A = { Laptop, Smartphone, Tablet }

List B: Premium Items Priced > $500

B = { Laptop, Smartphone, 4K Monitor, Gaming Rig }

Now consider typical analytical questions:

  • • Which devices belong to both collections? (e.g., Laptop, Smartphone)
  • • Which devices are portable smart devices, but are not premium items over $500?
  • • What is the complete catalog of all unique devices across both lists combined?

To express, manipulate, and compute these questions rigorously, mathematics and computer science rely on the foundational concept of a Set.

Formal Definition of Set Theory

Set Theory is the branch of mathematical logic that studies well-defined collections of objects, the relations between them, and the algebraic operations (such as union, intersection, and difference) used to combine and analyze them.

Every layer of modern computing is anchored in set theory: relational databases execute relational algebra queries (UNION, INTERSECT), programming languages provide constant-time hash sets, graph algorithms map vertices and edges as sets, and machine learning partitions datasets into non-overlapping training and testing groups.

2

What is a Set? The 3 Golden Rules

In mathematics, a Set is a well-defined collection of distinct objects.

Each individual entity contained within a set is called an Element or Member. By convention, sets are denoted by uppercase letters ($A, B, C, X, Y$) and their elements are enclosed within curly braces { }, separated by commas.

A = { 1, 2, 3, 4, 5 } The set of the first five positive natural numbers
V = { a, e, i, o, u } The set of all vowels in the English alphabet

The 3 Fundamental Characteristics of Sets:

1. Elements are Distinct

Writing an item multiple times inside curly braces does not increase the count; duplicate items collapse into a single element:

{ 1, 2, 2, 3, 3, 3 } = { 1, 2, 3 }
2. Order Does Not Matter

Sets are fundamentally unordered. Rearranging the sequence of items results in the identical set:

{ 1, 2, 3 } = { 3, 1, 2 }
3. Strictly Well-Defined

There must be an unambiguous, objective test to decide whether any given object belongs to the set or not:

“Integers < 10” ✓ Valid
“Set of nice numbers” ✗ Invalid
3

Methods of Representing Sets: Roster vs. Set-Builder Form

Sets are formally specified using two primary representations:

3.1 Roster Form (Tabular Notation)

Explicit Listing

In Roster Form, all elements are listed explicitly inside curly brackets, separated by commas. It is ideal for small, finite collections.

Even positive integers up to 10: A = { 2, 4, 6, 8, 10 }
Prime numbers less than 12: B = { 2, 3, 5, 7, 11 }

*For infinite sequences with an established pattern, an ellipsis (…) indicates continuation: ℕ = { 1, 2, 3, 4, 5, … }

3.2 Set-Builder Form (Descriptive Notation)

Rule-Based Definition

When sets are very large or infinite, listing every item is impossible. Set-Builder notation describes the qualifying predicate or condition that every member must satisfy.

A = { x ∣ P(x) }  or  A = { x : P(x) }
x = arbitrary element variable ∣ (vertical bar) = “such that” P(x) = qualifying property / boolean condition
Set-Builder: A = { x ∈ ℕ ∣ x < 10, x is even } Roster equivalent: A = { 2, 4, 6, 8 }
Set-Builder: B = { x ∣ x = 5n, n ∈ ℕ } Roster equivalent: B = { 5, 10, 15, 20, … }
4

Set Membership: Elements and Membership Symbols (∈ vs. ∉)

To declare whether an individual object resides within a given set, we use the formal membership operators:

Symbol Pronunciation & Meaning Example (let A = {1, 2, 3, 4, 5}) Explanation
∈ Is an element of / belongs to 3 ∈ A The number 3 is an active member of set A.
∉ Is not an element of / does not belong 7 ∉ A The number 7 is absent from set A.
Critical Caution: Element vs. Set

The membership symbol (∈) relates an element to a set. Relationships between two sets use the subset operator (⊆). Thus, 3 ∈ A is valid, whereas {3} ∈ A is false unless {3} is literally an element inside A. The proper relationship is {3} ⊆ A.

5

The Empty Set and the Universal Set

Empty Set (∅ or { })

A set containing zero elements is called the Empty Set or Null Set. It is denoted by the Norwegian letter ∅ or simply an empty pair of braces { }.

A = { x ∈ ℝ ∣ x > 5 and x < 3 }
⇒ A = ∅
Avoid the classic trap: ∅ ≠ { 0 }. The empty set has cardinality 0 (contains nothing), whereas { 0 } has cardinality 1 (it contains the single number 0).
Universal Set (U)

The overarching set of all possible entities under consideration within a particular mathematical discussion or context is the Universal Set, denoted by U.

U = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
A = Even numbers = { 2, 4, 6, 8, 10 }
B = Odd numbers = { 1, 3, 5, 7, 9 }
Context-dependent: The universal set is not absolute. If our domain of discourse changes to ASCII characters, U becomes the set of all 128 standard ASCII bytes.
6

Subsets (⊆) and Proper Subsets (⊂)

Understanding how sets nest inside one another is central to hierarchies in type systems and relational schemas:

⊆ Subset (A ⊆ B)

If every element of set A is also contained within set B, then A is defined as a subset of B. Formally: A ⊆ B ⇔ (∀x)(x ∈ A ⇒ x ∈ B).

A = { 1, 2, 3 } and B = { 1, 2, 3, 4, 5 }
⇒ A ⊆ B (A is a subset of B)
Two universal properties:
• Every set is a subset of itself: A ⊆ A
• The empty set is a subset of every set: ∅ ⊆ A

⊂ or ⊊ Proper Subset (A ⊂ B)

If A is a subset of B, but A and B are not equal (meaning B contains at least one item not in A), then A is a Proper Subset of B.

A = { 1, 2 } and B = { 1, 2, 3 }
Since A ⊆ B and A ≠ B, A ⊂ B (A is a proper subset of B).
Remember the distinction: • A ⊆ B allows for the possibility that A and B are identical ($A = B$).
• A ⊂ B strictly guarantees that A is strictly smaller than B ($A e B$).
7

The 4 Core Set Operations: Union, Intersection, Difference, and Complement

Algebra on sets is governed by four primary operators that combine, filter, or isolate subsets:

∪ Union Logical OR

Contains all distinct elements that exist in set A, set B, or both. Overlapping elements appear exactly once.

A = { 1, 2, 3, 4 }, B = { 3, 4, 5, 6 }
A ∪ B = { 1, 2, 3, 4, 5, 6 }
∩ Intersection Logical AND

Contains only those items that exist simultaneously in both set A and set B.

A = { 1, 2, 3, 4 }, B = { 3, 4, 5, 6 }
A ∩ B = { 3, 4 }
∖ Difference (Relative Complement / A − B) Exclusive A

Contains elements in set A that do not appear in set B. It is non-commutative (A ∖ B ≠ B ∖ A).

A = { 1, 2, 3, 4 }, B = { 3, 4, 5, 6 }
A ∖ B = { 1, 2 }  |  B ∖ A = { 5, 6 }
Ac Complement (A') Logical NOT

All elements in the Universal Set U that are not members of A: Ac = U ∖ A.

U = { 1..6 }, A = { 2, 4, 6 }
Ac = { 1, 3, 5 } (relative to U)

Summary Matrix of Core Set Operations

Operation Notation Set-Builder Definition Python Operator SQL Equivalent
Union A ∪ B { x ∣ x ∈ A or x ∈ B } A | B UNION
Intersection A ∩ B { x ∣ x ∈ A and x ∈ B } A & B INTERSECT
Difference A ∖ B { x ∣ x ∈ A and x ∉ B } A - B EXCEPT
Complement Ac { x ∣ x ∈ U and x ∉ A } U - A WHERE NOT IN
8

Disjoint Sets

Two sets are declared to be Disjoint if they share no common elements whatsoever.

Mathematical Criterion: A ∩ B = ∅

Two sets are disjoint if and only if their intersection is the empty set.

✓ Disjoint Sets Example: A = Even numbers = { 2, 4, 6 }
B = Odd numbers = { 1, 3, 5 }
A ∩ B = ∅ (Completely Disjoint)
✗ Non-Disjoint Sets Example: C = { 1, 2, 3 }
D = { 3, 4, 5 }
C ∩ D = { 3 } ≠ ∅ (Overlapping)

Software Engineering Connection: Disjoint sets model mutually exclusive domain states (such as payment statuses: PAID vs. FAILED). In algorithms, the Disjoint Set Union (DSU) data structure powers cycle detection and Kruskal's Minimum Spanning Tree algorithm.

9

Cardinality and the Principle of Inclusion-Exclusion

The total number of distinct members inside a set is called its Cardinality, denoted as |A| or n(A).

A = { 2, 4, 6, 8, 10 } |A| = 5
B = { a, b, c } |B| = 3
Empty Set ∅ |∅| = 0
C = { 1, 1, 2, 2, 3 } |C| = 3

The Principle of Inclusion-Exclusion:

Calculating the cardinality of a union is not a simple sum of both sizes. Common elements in the intersection would be counted twice. Hence, the universal theorem:

|A ∪ B| = |A| + |B| − |A ∩ B|

The overlapping items were incorporated in |A| and again in |B|, so subtracting |A ∩ B| once balances the arithmetic.

Concrete Calculation: Let A = { 1, 2, 3, 4 } and B = { 3, 4, 5, 6 }
Here |A| = 4, |B| = 4, and |A ∩ B| = |{ 3, 4 }| = 2
⇒ |A ∪ B| = 4 + 4 − 2 = 6 (matching { 1, 2, 3, 4, 5, 6 } with 6 distinct items).
10

The Power Set and 2n Combinatorial Explosion

The set of all possible subsets of a given set A is called its Power Set, denoted by P(A) or ℙ(A).

Example: Let A = { 1, 2 }

The possible subsets are: the empty set (∅), the singletons { 1 } and { 2 }, and the whole set { 1, 2 }.

P(A) = { ∅, { 1 }, { 2 }, { 1, 2 } }

Notice: The empty set (∅) and the original set (A) are always guaranteed members of the power set.

Cardinality of the Power Set Formula

|P(A)| = 2n

If a set contains n distinct elements, its power set contains exactly 2n subsets. For every item, there are exactly 2 choices: include it (1) or exclude it (0).

n = 1 ⇒ 21 = 2 subsets
n = 2 ⇒ 22 = 4 subsets
n = 3 ⇒ 23 = 8 subsets
n = 4 ⇒ 24 = 16 subsets

Computational Consequence: NP-hard optimization problems (e.g., Subset Sum, 0/1 Knapsack) examine the search space of subsets. Because $2^n$ grows exponentially, $n = 30$ elements yields over $10^9$ subsets. This is known in algorithmic complexity as Combinatorial Explosion.

11

Algebraic Laws of Set Operations and De Morgan's Laws

Set operations obey universal algebraic laws that enable query optimization and logic gate simplification:

Law Name Algebraic Identities Meaning & Implication
Commutative Laws A ∪ B = B ∪ A
A ∩ B = B ∩ A
Order of operands does not affect union or intersection.
Associative Laws (A ∪ B) ∪ C = A ∪ (B ∪ C)
(A ∩ B) ∩ C = A ∩ (B ∩ C)
Grouping parentheses can be rearranged across identical operators.
Distributive Laws A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C)
A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C)
Intersection distributes over union, and union distributes over intersection.
Absorption Laws A ∪ (A ∩ B) = A
A ∩ (A ∪ B) = A
Subordinate operations collapse back into the enclosing primary set.

De Morgan's Laws

De Morgan's dual laws are foundational to computer science, compiler design, and digital hardware logic:

Law 1: Complement of a Union (A ∪ B)c = Ac ∩ Bc “Not in either A or B” equals “not in A AND not in B” (NOT(A OR B) ≡ NOT A AND NOT B).
Law 2: Complement of an Intersection (A ∩ B)c = Ac ∪ Bc “Not in both A and B” equals “not in A OR not in B” (NOT(A AND B) ≡ NOT A OR NOT B).
12

Real-World Applications in Computer Science and Artificial Intelligence

Set Theory is directly implemented in software engineering, database management, and AI models:

1. Python Set Data Structure (O(1) Lookups)

Python natively provides a set data type backed by a hash table, ensuring distinctness and lightning-fast set operations:

A = {1, 2, 3, 4}
B = {3, 4, 5, 6}

print(A | B)  # Union: {1, 2, 3, 4, 5, 6}
print(A & B)  # Intersection: {3, 4}
print(A - B)  # Difference: {1, 2}
print(A ^ B)  # Symmetric Difference: {1, 2, 5, 6}

# Instant membership check:
if 3 in A:
    print("O(1) instantaneous lookup verified!")

2. Relational Databases and SQL Queries

Codd's relational database model is formalized relational algebra on tuples. SQL filters map directly to set operations: AND represents intersection, OR represents union, and NOT represents complement:

-- Filter items priced > $1000 AND currently in stock (Intersection A ∩ B)
SELECT product_name, price 
FROM products 
WHERE price > 1000 
  AND stock_quantity > 0;

-- Set operation across tables:
SELECT user_id FROM premium_users
INTERSECT
SELECT user_id FROM beta_testers;

3. Data Ingestion & Deduplication

Web crawlers, analytics streams, and telemetry pipelines encounter duplicate events. Passing streams through a set guarantees uniqueness in $O(n)$ total time:

raw_emails = ["salim@byteion.com", "user@test.com", "salim@byteion.com", "dev@byteion.com"]
unique_emails = set(raw_emails)

print(len(unique_emails)) # 3 (duplicates removed automatically)

4. Graph Theory

Graphs are formally ordered pairs: G = (V, E), where V is a set of vertices (nodes) and E is a set of 2-element subsets of V (edges). Route planning and social graph analysis rely on this structure.

5. AI & Machine Learning

When training classifiers, datasets must be cleanly partitioned into disjoint sets: Training ∩ Validation ∩ Test = ∅. Any non-empty intersection causes catastrophic data leakage and corrupted benchmark scores.

13

6 Common Pitfalls in Set Theory

1
Believing Element Order Matters

{ 1, 2, 3 } and { 3, 2, 1 } are identical sets. Reordering members never produces a new or different set.

2
Counting Duplicate Entries

The set { 1, 1, 2, 2, 3 } has cardinality 3, not 5. Cardinality counts only unique, distinct members.

3
Confusing Membership (∈) with Subset (⊆)

1 ∈ { 1, 2, 3 } is an element assertion, whereas { 1 } ⊆ { 1, 2, 3 } is a subset relation. An item alone is an element; wrapped in braces, it is a subset.

4
Mixing Up Union (∪) and Intersection (∩)

Union corresponds to OR (all items across both sets), while Intersection corresponds to AND (strictly the shared elements).

5
Treating Set Difference as Commutative

A ∖ B and B ∖ A almost always yield completely different sets. The difference retains only the unique elements of the left-hand set.

6
Evaluating Complements Without Defining U

A complement Ac is undefined without specifying the Universal Set U. Changing U fundamentally alters Ac.

Interactive Math Workbench

Set Theory & Operations Lab

Visual Venn diagram simulator, inclusion-exclusion & power set calculator, and Python/SQL query workbench

|A| 4
|B| 5
|A ∩ B| 2
|Result| 7
Universal Set (U) Set A Set B
Operation Result: A ∪ B 7 elements

The collection of all distinct elements residing in set A, set B, or both.

15

Key Takeaways & Summary

Set

A well-defined collection of distinct items; unordered and duplicate-free.

Membership (∈, ∉)

Indicates whether a specific single item is an element (∈) or absent (∉).

Empty Set (∅)

Contains zero elements with cardinality 0; ∅ ≠ { 0 }.

Universal Set (U)

The encompassing context containing all potential elements under consideration.

Subset (⊆) vs. Proper (⊂)

⊆ permits equivalence; ⊂ requires strict inequality with fewer elements.

Union (∪) vs. Intersection (∩)

Union corresponds to logical OR; Intersection corresponds to logical AND.

Difference (∖) & Complement

A ∖ B holds items exclusive to A; Ac holds all items in U outside A.

Disjoint Sets

Sets with zero overlap (A ∩ B = ∅); models mutually exclusive states.

Power Set & 2n

The set of all subsets; size grows exponentially at 2n.