TExcelTAD
Home › Discrete Mathematics › Infinite Sets and Countability

Infinite Sets and Countability

The mapping rules for finite sets (Finite Cardinality) become definitions for infinite ones: two sets have the same size when there is a bijection between them. This leads to surprising results: there are as many even numbers as natural numbers, as many rationals as integers, but strictly more real numbers. And there are more problems than programs, so some problems cannot be solved by any program.

Same size means a bijection

Write A∼BA \sim B when there is a bijection between AA and BB. For infinite sets, a proper subset can have the same size as the whole set:

  • N∼{0,2,4,…}\mathbb{N} \sim \{0, 2, 4, \ldots\} via n↦2nn \mapsto 2n.
  • N∼Z\mathbb{N} \sim \mathbb{Z} via 0,−1,1,−2,2,…0, -1, 1, -2, 2, \ldots, that is n↦n2n \mapsto \frac{n}{2} for even nn and n↦−n+12n \mapsto -\frac{n+1}{2} for odd nn.

Galileo found this paradoxical; Dedekind turned it into the definition of an infinite set.

Countable sets

A set is countable if it is finite or ∼N\sim \mathbb{N}: its elements can be listed as a0,a1,a2,…a_0, a_1, a_2, \ldots so that each element appears at some finite position. Countable sets include:

  • Z\mathbb{Z} (listed above),
  • N×N\mathbb{N} \times \mathbb{N}: list pairs by diagonals, (0,0),(0,1),(1,0),(0,2),(1,1),(2,0),…(0,0), (0,1), (1,0), (0,2), (1,1), (2,0), \ldots,
  • Q\mathbb{Q}: every rational is a pair of integers, so a sub-list of a countable list,
  • the set of all finite strings over a finite alphabet, Σ∗\Sigma^*: list by length, then alphabetically.
python · try it yourself
from fractions import Fraction
from math import gcd

def pairs():
    d = 0
    while True:                       # walk the diagonals x + y = d
        for x in range(d + 1):
            yield (x, d - x)
        d += 1

def positive_rationals():
    for a, b in pairs():
        p, q = a + 1, b + 1
        if gcd(p, q) == 1:            # skip duplicates such as 2/4
            yield Fraction(p, q)

gen = pairs()
print("N x N:", [next(gen) for _ in range(10)])
gen = positive_rationals()
print("Q+:   ", [str(next(gen)) for _ in range(15)])
N x N: [(0, 0), (0, 1), (1, 0), (0, 2), (1, 1), (2, 0), (0, 3), (1, 2), (2, 1), (3, 0)]
Q+:    ['1', '1/2', '2', '1/3', '3', '1/4', '2/3', '3/2', '4', '1/5', '5', '1/6', '2/5', '3/4', '4/3']

Each pair (x,y)(x, y) appears at position (x+y)(x+y+1)2+x\frac{(x + y)(x + y + 1)}{2} + x, a formula called the Cantor pairing function. It is a bijection N×N→N\mathbb{N} \times \mathbb{N} \to \mathbb{N}.

Cantor's diagonal argument

Theorem. The set of infinite bit sequences, {0,1}ω\{0, 1\}^\omega, is not countable.

Proof. Suppose s0,s1,s2,…s_0, s_1, s_2, \ldots lists all infinite bit sequences. Build a new sequence dd by flipping the diagonal: the nn-th bit of dd is the opposite of the nn-th bit of sns_n. Then dd differs from every sns_n at position nn, so it is not in the list. Contradiction. ■\blacksquare

python · try it yourself
listing = [
    "0000000",
    "1111111",
    "0101010",
    "1100110",
    "0010010",
    "1011101",
    "0110011",
]
d = "".join("1" if listing[n][n] == "0" else "0" for n in range(len(listing)))
print("diagonal flipped:", d)
for n, s in enumerate(listing):
    print(f"s{n} = {s}  differs from d at position {n}: {s[n] != d[n]}")
diagonal flipped: 1011110
s0 = 0000000  differs from d at position 0: True
s1 = 1111111  differs from d at position 1: True
s2 = 0101010  differs from d at position 2: True
s3 = 1100110  differs from d at position 3: True
s4 = 0010010  differs from d at position 4: True
s5 = 1011101  differs from d at position 5: True
s6 = 0110011  differs from d at position 6: True

Since a real number in [0,1)[0, 1) has a binary expansion, the same argument shows R\mathbb{R} is uncountable. More generally (Cantor's theorem), for every set AA, there is no surjection from AA onto its power set P(A)\mathcal{P}(A): the set D={a∈A∣a∉f(a)}D = \{a \in A \mid a \notin f(a)\} is never f(a)f(a) for any aa. Infinite sets come in infinitely many sizes.

Uncomputable problems exist

Every program is a finite string, so the set of programs is countable. A decision problem about natural numbers ("is nn in the set?") is a subset of N\mathbb{N}, and P(N)\mathcal{P}(\mathbb{N}) is uncountable. So there are more problems than programs, and most problems cannot be solved by any program. A concrete one is the halting problem: no program can decide, for every program PP and input xx, whether PP halts on xx. Its proof is the diagonal argument again: given a supposed decider halts, build a program that does the opposite of what halts predicts about itself.

In software

Python generators like pairs() above are how code enumerates infinite countable sets lazily. Fuzzers and property-based testers enumerate inputs in a similar diagonal fashion so every input of every size is eventually reached. The halting problem is why linters and type checkers must be conservative: perfect bug detection for all programs is impossible (Rice's theorem).

Exercises

Exercise 1: A bijection

Give a bijection from N\mathbb{N} to the odd natural numbers {1,3,5,…}\{1, 3, 5, \ldots\} and its inverse.

Answer

f(n)=2n+1f(n) = 2n + 1, f−1(m)=m−12f^{-1}(m) = \frac{m - 1}{2}.

Solution

ff is injective (2n+1=2k+1→n=k2n + 1 = 2k + 1 \to n = k) and every odd mm is f(m−12)f(\frac{m-1}{2}), so it is surjective. The inverse undoes it.

Exercise 2: Pairing positions

Using the Cantor pairing π(x,y)=(x+y)(x+y+1)2+x\pi(x, y) = \frac{(x + y)(x + y + 1)}{2} + x, find π(2,3)\pi(2, 3) and the pair at position 12.

Answer

π(2,3)=17\pi(2, 3) = 17; position 12 is (2,2)(2, 2).

Solution

π(2,3)=5⋅62+2=17\pi(2, 3) = \frac{5 \cdot 6}{2} + 2 = 17. For 12: the diagonals d=0,1,2,3d = 0, 1, 2, 3 hold 1+2+3+4=101 + 2 + 3 + 4 = 10 pairs (positions 0 to 9), so 12 is on diagonal d=4d = 4 at offset x=2x = 2: the pair (2,2)(2, 2). Check: 4⋅52+2=12\frac{4 \cdot 5}{2} + 2 = 12.

python · try it yourself
pi = lambda x, y: (x + y) * (x + y + 1) // 2 + x
print(pi(2, 3), [(x, d - x) for d in range(10) for x in range(d + 1) if pi(x, d - x) == 12])
17 [(2, 2)]

Exercise 3: Countable or not?

Classify: (a) all finite subsets of N\mathbb{N}; (b) all subsets of N\mathbb{N}; (c) all Python programs; (d) all functions N→{0,1}\mathbb{N} \to \{0, 1\}.

Answer

(a) countable; (b) uncountable; (c) countable; (d) uncountable.

Solution

(a) A finite subset can be written as a finite string such as {3,7,12}, and finite strings are countable. (b) is P(N)\mathcal{P}(\mathbb{N}), uncountable by Cantor. (c) programs are finite strings. (d) such a function is an infinite bit sequence, uncountable by the diagonal argument.

Test yourself

Two infinite sets have the same cardinality when…

  1. one is a subset of the other
  2. there is a bijection between them
  3. both contain 0
  4. both are subsets of R\mathbb{R}

Which set is uncountable?

  1. Z\mathbb{Z}
  2. Q\mathbb{Q}
  3. N×N\mathbb{N} \times \mathbb{N}
  4. R\mathbb{R}

Why must some decision problems be unsolvable by programs?

  1. Computers are too slow
  2. Programs are countable but sets of naturals are uncountable
  3. Python lacks recursion
  4. Because of floating point