Sets, Relations & Functions

The language of collections and mappings. Learn set notation, operations (union, intersection, difference, complement), Venn diagrams, power sets and Cartesian products, relations and their properties (reflexive, symmetric, transitive), equivalence relations and partial orders, and injective, surjective and bijective functions.

Databases are sets of records, relations model 'depends on' in build systems, equivalence classes group identical parts, and bijections ensure encodings can be decoded.

An analogy

A set is a bag of distinct items; a relation is a list of pairs saying which items are connected; a function is a rule that sends every item to exactly one partner.

Set operations

Venn diagrams visualise unions and intersections. De Morgan's laws: (A ∪ B)ᶜ = Aᶜ ∩ Bᶜ. Sets have no duplicates and no order.

Relations

A relation on A is a subset of A × A. Equivalence relations (reflexive, symmetric, transitive) partition sets into classes (e.g. congruence mod n). Partial orders (reflexive, antisymmetric, transitive) model precedence — task scheduling, dependency graphs.

Functions

Injective (one-to-one) functions lose no information; surjective (onto) cover the codomain; bijections are invertible. Hash functions are deliberately not injective (collisions).

Cardinality

Finite sets are compared by counting; infinite sets by bijections — integers and rationals are countable; reals are not (Cantor's diagonal argument).

Formulas

Worked example: Inclusion–exclusion

Given: Integers 1–1000 divisible by 3 or 5

Find: Count.

Answer: 467

Worked example: Equivalence classes

Given: a ~ b if a ≡ b (mod 3) on ℤ

Find: Classes.

Answer: Three classes [0], [1], [2]

Common mistakes

Applications

Key takeaways

Going further