Sets, Relations & Functions
The vocabulary of discrete maths — sets and set operations, power sets and Cartesian products, relations and their properties, equivalence relations and partial orders, functions (injective, surjective, bijective), cardinality and countability, with connections to databases, types and hashing.
Sets, relations and functions are the vocabulary of computer science. Databases are relations, types are sets, hash functions are functions, dependency graphs are relations, and countability arguments show some problems can't be solved by any program.
Sets
A set is an unordered collection of distinct elements: . Order and duplicates don't matter: .
| Notation | Meaning |
|---|---|
| x is an element of A | |
| Every element of A is in B (subset) | |
| The empty set | |
| Cardinality (number of elements) | |
| Set-builder notation: positive integers | |
| Naturals, integers, rationals, reals |
Set operations
| Operation | Notation | Elements | Python | SQL |
|---|---|---|---|---|
| Union | In A or B | a | b | UNION | |
| Intersection | In both | a & b | INTERSECT / inner join | |
| Difference | In A but not B | a - b | EXCEPT | |
| Symmetric difference | In exactly one | a ^ b | — | |
| Complement | Not in A (within a universe U) | — | NOT IN |
followers_of_a = {"asha", "ben", "chen", "dev"}
followers_of_b = {"chen", "dev", "eli"}
print(followers_of_a & followers_of_b) # mutual followers: {'chen', 'dev'}
print(followers_of_a - followers_of_b) # only A's: {'asha', 'ben'}De Morgan's laws for sets mirror those of logic (Logic and proofs): .
Inclusion–exclusion: (Combinatorics).
Power sets and Cartesian products
- The power set is the set of all subsets. If , then — each element is in or out. This is why brute-forcing subsets is exponential (Backtracking), and why subsets map to n-bit masks (Bit manipulation).
- The Cartesian product — all ordered pairs. . A SQL
CROSS JOINis a Cartesian product; a struct/record type is a product of its field types.
Relations
A (binary) relation R from A to B is a subset of — a set of pairs that are "related". Write when .
Examples: "is less than" on integers; "follows" on users; "is a prerequisite of" on courses; a database table enrolments(student, course) is a relation — the relational model is named after this (Relational model). Relations on a single set are directed graphs (Graph theory).
Properties of relations on a set A
| Property | Definition | Example | Counterexample |
|---|---|---|---|
| Reflexive | a R a for all a | ≤, = | < |
| Symmetric | a R b ⇒ b R a | "is a sibling of", "is friends with" | "follows" |
| Antisymmetric | a R b and b R a ⇒ a = b | ≤, ⊆, "divides" (on positive integers) | "is friends with" |
| Transitive | a R b and b R c ⇒ a R c | <, "is an ancestor of" | "is a parent of" |
The transitive closure adds all pairs reachable through chains: from "is a parent of" you get "is an ancestor of"; from "depends on" you get "depends on, directly or indirectly" — computed by graph search or Floyd–Warshall (Shortest paths), or recursive SQL CTEs.
Equivalence relations
Reflexive + symmetric + transitive. An equivalence relation partitions the set into disjoint equivalence classes:
- "Has the same remainder mod 3" partitions integers into
{…, 0, 3, 6, …},{…, 1, 4, …}and{…, 2, 5, …}. - "Is connected to" partitions a network's nodes into connected components — exactly what union–find maintains (Union-find).
- "Are anagrams" groups words by their sorted letters — a common interview pattern using a hash map keyed by the class representative.
Partial orders
Reflexive + antisymmetric + transitive. Elements may be incomparable:
- ⊆ on sets (
{1}and{2}are incomparable). - "Must happen before" on tasks — dependencies, build steps, course prerequisites. A topological sort extends a partial order to a total order (Topological sort).
- Happens-before in distributed systems; vector clocks form a partial order (Clocks and ordering).
A total order additionally makes every pair comparable (≤ on numbers) — what sorting requires. Comparators in code must define a consistent (strict weak) order or sorting breaks.
Functions
A function assigns exactly one output in B (the codomain) to each input in A (the domain). The set of outputs actually produced is the range (image).
| Kind | Definition | Picture | Example |
|---|---|---|---|
| Injective (one-to-one) | Different inputs → different outputs | No collisions | User ID → email (if unique) |
| Surjective (onto) | Every element of B is hit | Codomain fully covered | Student → grade, if every grade is awarded |
| Bijective | Both | Perfect pairing; has an inverse | Encryption under a fixed key; a permutation |
- Hash functions map a huge domain to a small codomain, so they cannot be injective — collisions are guaranteed by the pigeonhole principle (Combinatorics); good ones spread inputs evenly (Hash tables).
- Composition — function pipelines, middleware,
composein functional programming. - Pure functions in programming match the mathematical notion: same input, same output, no side effects.
Cardinality and countability
Two sets have the same cardinality if there's a bijection between them. This extends "size" to infinite sets — with surprises:
- Countably infinite: can be listed as a sequence (bijection with ℕ). The integers (0, 1, −1, 2, −2, …), the rationals, all finite strings over an alphabet, and all programs are countable.
- Uncountable: no such listing exists. The real numbers, and the set of all subsets of ℕ (equivalently, all functions ℕ →
{0, 1}).
Cantor's diagonal argument proves the reals are uncountable: given any supposed list of all reals in (0, 1), construct a number whose n-th digit differs from the n-th digit of the n-th number. It's not in the list — contradiction.
The punchline for computer science: there are countably many programs but uncountably many problems (functions from inputs to yes/no). So most problems have no program that solves them. The halting problem is a concrete example, proved with the same diagonal trick (Turing machines and computability).
- Sets support union, intersection, difference and complement — mirrored by Python sets and SQL.
- The power set of an n-element set has 2ⁿ elements; Cartesian products form tuples (cross joins, record types).
- Relations are sets of pairs (database tables, graphs); key properties are reflexive, symmetric, antisymmetric, transitive.
- Equivalence relations partition sets into classes (union–find, grouping); partial orders model dependencies (topological sort).
- Functions are injective, surjective or bijective; hash functions must collide by pigeonhole.
- Countable vs uncountable: programs are countable, problems are not — so most problems are unsolvable.