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 when there is a bijection between and . For infinite sets, a proper subset can have the same size as the whole set:
- via .
- via , that is for even and for odd .
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 : its elements can be listed as so that each element appears at some finite position. Countable sets include:
- (listed above),
- : list pairs by diagonals, ,
- : 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, : list by length, then alphabetically.
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 appears at position , a formula called the Cantor pairing function. It is a bijection .
Cantor's diagonal argument
Theorem. The set of infinite bit sequences, , is not countable.
Proof. Suppose lists all infinite bit sequences. Build a new sequence by flipping the diagonal: the -th bit of is the opposite of the -th bit of . Then differs from every at position , so it is not in the list. Contradiction.
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 has a binary expansion, the same argument shows is uncountable. More generally (Cantor's theorem), for every set , there is no surjection from onto its power set : the set is never for any . 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 in the set?") is a subset of , and 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 and input , whether halts on . 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 to the odd natural numbers and its inverse.
Answer
, .
Solution
is injective () and every odd is , so it is surjective. The inverse undoes it.
Exercise 2: Pairing positions
Using the Cantor pairing , find and the pair at position 12.
Answer
; position 12 is .
Solution
. For 12: the diagonals hold pairs (positions 0 to 9), so 12 is on diagonal at offset : the pair . Check: .
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 ; (b) all subsets of ; (c) all Python programs; (d) all functions .
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 , 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…
- one is a subset of the other
- there is a bijection between them
- both contain 0
- both are subsets of
Which set is uncountable?
Why must some decision problems be unsolvable by programs?
- Computers are too slow
- Programs are countable but sets of naturals are uncountable
- Python lacks recursion
- Because of floating point