MathLabs

Foundations of mathematics

Functions and cardinality

A function f:A→Bf:A\to B is injective if it never sends two different elements to the same place, surjective if every element of BB is hit, and bijective if both hold. Bijections let us compare the sizes of infinite sets: ∣N∣=∣Z∣=∣Q∣=ℵ0|\mathbb{N}| = |\mathbb{Z}| = |\mathbb{Q}| = \aleph_0, since all three admit an explicit bijection with N\mathbb{N}. Cantor's theorem, ∣A∣<∣P(A)∣|A| < |\mathcal{P}(A)|, proved by a diagonal argument using D={x∈A:x∉f(x)}D = \{x \in A : x \notin f(x)\}, shows the power set is always strictly bigger, so there is an endless hierarchy of infinities; the Cantor–Bernstein–Schröder theorem shows ∣A∣≤∣B∣|A|\le|B| và ∣B∣≤∣A∣  ⟹  ∣A∣=∣B∣|B|\le|A| \implies |A|=|B|, letting cardinality be compared with two easy one-directional injections instead of one hard bijection. These ideas underlie hash collisions, the limits of data compression, and the uncomputability of certain problems.

IntuitionMatching two piles, and which pile is bigger

A function f:A→Bf : A \to B assigns to every element of AA exactly one element of BB. Picture it as arrows from AA to BB: injective means no two arrows land on the same point of BB, surjective means every point of BB is hit by some arrow, and bijective means both at once, a perfect one-to-one matching. When AA and BB are infinite, a bijection is exactly how we say they have the same size, the same cardinality, even though counting one by one is impossible.

Interactive plot of the cubic function y=x^3 showing it is a bijection on the real numbers
The strictly increasing cubic y=x3y = x^3 is a bijection R→R\mathbb{R}\to\mathbb{R}: every horizontal line meets the curve exactly once.

UndergraduateInjective, surjective, bijective: formal definitions

Definition: Injective, surjective, bijective

A function f:A→Bf : A \to B is injective if distinct elements of AA always map to distinct elements of BB. It is surjective if every element of BB is the image of some element of AA. It is bijective if both hold, in which case it has a well-defined inverse f−1:B→Af^{-1} : B \to A.

∀x1,x2∈A, f(x1)=f(x2)  ⟹  x1=x2\forall x_1, x_2 \in A,\ f(x_1) = f(x_2) \implies x_1 = x_2

This is the formal test for injectivity: whenever two inputs give the same output, they must already have been the same input. Surjectivity is tested the other way, requiring every target to be reachable.

∀y∈B, ∃x∈A, f(x)=y\forall y \in B,\ \exists x \in A,\ f(x) = y
Comparing the three properties for f:R→Rf:\mathbb{R}\to\mathbb{R}
PropertyExampleInjective?Surjective?
Bijectivef(x)=x3f(x)=x^3YesYes
Injective onlyf(x)=exf(x)=e^xYesNo
Surjective onlyf(x)=x3−xf(x)=x^3-xNoYes
Neitherf(x)=x2f(x)=x^2NoNo

UndergraduateTwo key theorems, with full proofs

For every set AA, ∣A∣<∣P(A)∣|A| < |\mathcal{P}(A)|: there is no surjection from AA onto its power set P(A)\mathcal{P}(A), so the power set is strictly larger.

Why is it true?

This shows there is no single "biggest infinity" — from any set, however large, you can always build a strictly larger one just by taking its power set. It is the theorem that makes an infinite hierarchy of infinities unavoidable, and its diagonal-argument proof technique is the direct ancestor of the arguments used to show some problems are uncomputable.

Proof

Suppose, for contradiction, that there is a surjection f:A→P(A)f : A \to \mathcal{P}(A); we will derive a contradiction, which shows no such surjection can exist.

Define the "diagonal" set D={x∈A:x∉f(x)}D = \{x \in A : x \notin f(x)\}: it collects every element of AA that is not a member of its own image under ff. Since DD is a subset of AA, it is an element of P(A)\mathcal{P}(A).

Because ff is assumed surjective, there must be some a0∈Aa_0 \in A with f(a0)=Df(a_0) = D. Now ask the deciding question: is a0∈Da_0 \in D?

If a0∈Da_0 \in D, then by the definition of DD, a0∉f(a0)a_0 \notin f(a_0); but f(a0)=Df(a_0) = D, so this says a0∉Da_0 \notin D — a contradiction. If instead a0∉Da_0 \notin D, then by the definition of DD (which excludes exactly the elements with a0∈f(a0)a_0 \in f(a_0)), this forces a0∈f(a0)=Da_0 \in f(a_0) = D — again a contradiction.

Either way we reach a contradiction, so no surjection f:A→P(A)f : A \to \mathcal{P}(A) can exist. Combined with the injection x↦{x}x \mapsto \{x\} from AA into P(A)\mathcal{P}(A), this gives exactly ∣A∣<∣P(A)∣|A| < |\mathcal{P}(A)|.

If ∣A∣≤∣B∣|A|\le|B| và ∣B∣≤∣A∣  ⟹  ∣A∣=∣B∣|B|\le|A| \implies |A|=|B|, meaning there is an injection A→BA \to B and an injection B→AB \to A, then there is a bijection between AA and BB.

Why is it true?

Comparing cardinalities via injections only (each set fits inside the other) already forces the sets to have exactly the same size, just as with finite sets. This lets us prove ∣A∣=∣B∣|A|=|B| by building two easy one-directional embeddings instead of one hard direct bijection — this is exactly how the examples below show intervals like [0,1][0,1] and (0,1)(0,1) have the same cardinality.

Proof

Let f:A→Bf : A \to B and g:B→Ag : B \to A be the two given injections. The idea is to trace, for each element, the chain of ancestors obtained by repeatedly undoing ff and gg, and to build the final bijection piece by piece depending on where each chain "starts".

For a∈Aa \in A, define its backward chain a,g−1(a),f−1(g−1(a)),…a, g^{-1}(a), f^{-1}(g^{-1}(a)), \ldots, continuing as long as the needed inverse is defined, and symmetrically for b∈Bb \in B. Every element's chain either goes back forever, or stops at an element of AA with no gg-preimage, or stops at an element of BB with no ff-preimage. This partitions AA into three parts AAA_A (chain stops in AA), ABA_B (chain stops in BB), A∞A_\infty (chain never stops), and likewise partitions BB into BA,BB,B∞B_A, B_B, B_\infty.

On chains that stop in AA or never stop, ff itself already gives a bijection from that part of AA onto the corresponding part of BB (since these elements were reached by injections and nothing "runs out" on the BB side first). On chains that stop in BB, it is gg that gives a bijection from the corresponding part of BB back onto that part of AA, so its inverse g−1g^{-1} gives a bijection from that part of AA onto that part of BB.

Define h:A→Bh : A \to B by h(a)=f(a)h(a) = f(a) if a∈AA∪A∞a \in A_A \cup A_\infty, and h(a)=g−1(a)h(a) = g^{-1}(a) if a∈ABa \in A_B. Since the three pieces are disjoint and each piece maps bijectively onto its matching piece of BB (ff onto BA∪B∞B_A \cup B_\infty, g−1g^{-1} onto BBB_B), the combined map hh is a bijection from all of AA onto all of BB, proving ∣A∣=∣B∣|A|=|B|.

UndergraduateReal-World Applications and Worked Examples

Cardinality is not just abstract bookkeeping. Hash functions map a huge domain (all possible files, all possible passwords) into a much smaller codomain (a 32-bit or 64-bit integer); since the domain is strictly larger than the codomain, Cantor's pigeonhole-style reasoning guarantees collisions must exist, no matter how clever the hash function is. Lossless data compression is exactly an injective map from longer bit-strings to shorter ones; since there are strictly fewer short strings than long ones, no compressor can shrink every possible input — some inputs must grow or stay the same size. Uncomputability results (such as the undecidability of the halting problem) use the same diagonal argument as Cantor's theorem: there are uncountably many possible functions N→{0,1}\mathbb{N}\to\{0,1\} but only countably many computer programs, so almost every function is simply not computable by any program.

Example: A concrete bijection between N\mathbb{N} and Z\mathbb{Z}

Define f:Z→Nf : \mathbb{Z} \to \mathbb{N} by f(n)=2nf(n) = 2n if n≥0n \ge 0 and f(n)=−2n−1f(n) = -2n-1 if n<0n < 0. Show that ff is a bijection, and use it to conclude ∣Z∣=ℵ0|\mathbb{Z}| = \aleph_0. What is f(−5)f(-5)?

Solution

This ff sends non-negative integers to even naturals and negative integers to odd naturals, zig-zagging outward: f(0)=0f(0)=0, f(−1)=1f(-1)=1, f(1)=2f(1)=2, f(−2)=3f(-2)=3, f(2)=4,…f(2)=4, \ldots

Injectivity: even outputs come only from n≥0n\ge 0 (where f(n)=2nf(n)=2n determines nn uniquely) and odd outputs come only from n<0n<0 (where f(n)=−2n−1f(n)=-2n-1 determines nn uniquely), and within each case ff is strictly monotonic, so no two distinct integers share an output.

Surjectivity: every even natural 2k2k (k≥0k\ge 0) is f(k)f(k), and every odd natural 2k+12k+1 (k≥0k\ge 0) is f(−(k+1))f(-(k+1)), so every natural number is hit.

Since ff is a bijection Z→N\mathbb{Z} \to \mathbb{N}, Z\mathbb{Z} and N\mathbb{N} have the same cardinality, ℵ0\aleph_0, confirming ∣Z∣=ℵ0|\mathbb{Z}| = \aleph_0 — despite Z\mathbb{Z} looking twice as large.

Finally, f(−5)f(-5): since −5<0-5 < 0, use f(n)=−2n−1f(n) = -2n - 1, so f(−5)=−2×(−5)−1=10−1=9f(-5) = -2 \times (-5) - 1 = 10 - 1 = 9.

Example: Pigeonhole reasoning for hash collisions

A hash function maps arbitrary files to a 3232-bit code, so there are exactly 2322^{32} possible codes. A company stores 55 billion distinct files (5,000,000,0005{,}000{,}000{,}000). Explain, using cardinality/pigeonhole reasoning, why at least two files are guaranteed to share the same hash code, and estimate how many "collision pairs" are unavoidable at minimum.

Solution

The codomain of the hash function has exactly 232=4,294,967,2962^{32} = 4{,}294{,}967{,}296 elements — call this set BB. The domain of files being hashed, AA, has 5,000,000,0005{,}000{,}000{,}000 elements, and ∣A∣>∣B∣|A| > |B| since 5,000,000,000>4,294,967,2965{,}000{,}000{,}000 > 4{,}294{,}967{,}296.

By the (finite) pigeonhole principle — the finite counterpart of "no injection can exist from a larger finite set into a smaller one" — the hash function, viewed as a map A→BA \to B, cannot possibly be injective: if it were injective it would give ∣A∣≤∣B∣|A| \le |B|, contradicting ∣A∣>∣B∣|A| > |B|. So at least two distinct files must be assigned the same hash code.

To estimate how many collisions are unavoidable at minimum: distributing 5,000,000,0005{,}000{,}000{,}000 files as evenly as possible over 4,294,967,2964{,}294{,}967{,}296 buckets puts at least ⌈5,000,000,000/4,294,967,296⌉=2\lceil 5{,}000{,}000{,}000 / 4{,}294{,}967{,}296 \rceil = 2 files in some bucket, and the number of "excess" files that must double up is at least 5,000,000,000−4,294,967,296=705,032,7045{,}000{,}000{,}000 - 4{,}294{,}967{,}296 = 705{,}032{,}704.

So at least about 705705 million files are forced into buckets already used by another file — a direct, unavoidable consequence of ∣A∣>∣B∣|A| > |B|, regardless of how well-designed the hash function is.

Which property must f:A→Bf:A\to B have so that distinct elements of AA always map to distinct elements of BB?

If ∣A∣=5|A|=5, what does Cantor's theorem say ∣P(A)∣|\mathcal{P}(A)| is?

A 16-bit hash function has 2162^{16} possible outputs. If a system hashes 100,000100{,}000 distinct files, what does the pigeonhole/cardinality argument guarantee?

What do the hypotheses of the Cantor–Bernstein–Schröder theorem require?

References

  1. Paul R. Halmos (1960). Naive Set Theory
  2. Karel Hrbacek, Thomas Jech (1999). Introduction to Set Theory