Discrete Mathematics Module

Set Theory & Cardinality of Infinities

Set operations, power sets, Russell's paradox, Cantor's diagonal argument, countable vs uncountable infinities, and uncomputable functions.

1. The Language of Mathematical Objects

In modern mathematics and computer science, almost every structure—from graphs and relational tables to formal languages and type systems—is constructed from Sets.
A set is an unordered collection of distinct objects, called elements or members. We write $x \in A$ if $x$ belongs to $A$, and $x \notin A$ otherwise.
text
+-----------------------------------------------------------------------------------------+
|                                    SET BUILDER NOTATION                                 |
+-----------------------------------------------------------------------------------------+
|                                                                                         |
|       S = { x in Universe : P(x) is True }                                              |
|                                                                                         |
|  Examples:                                                                              |
|  - Even integers:       E = { n in Z : n mod 2 == 0 }                                   |
|  - Valid IP addresses:  IP = { (a, b, c, d) in N^4 : 0 <= a, b, c, d <= 255 }          |
|  - Halting programs:    HALT = { (M, w) : Turing machine M halts on input w }           |
+-----------------------------------------------------------------------------------------+

2. Fundamental Set Operations

Let $A$ and $B$ be subsets of a universal set $\mathcal{U}$:
text
+-----------------------------------------------------------------------------------------+
|                                  SET OPERATIONS TAXONOMY                                |
+-----------------------------------------------------------------------------------------+
|  UNION:                A u B = { x : x in A OR x in B }          (Logical OR)           |
|  INTERSECTION:         A n B = { x : x in A AND x in B }         (Logical AND)          |
|  DIFFERENCE (Relative):A \ B = { x : x in A AND x NOT in B }     (Set subtraction)      |
|  COMPLEMENT:           A^c   = { x in U : x NOT in A }           (Logical NOT)          |
|  SYMMETRIC DIFFERENCE: A Delta B = (A \ B) u (B \ A)             (Logical XOR)          |
+-----------------------------------------------------------------------------------------+

2.1 The Power Set $\mathcal{P}(A)$

The power set of $A$ is the set of all subsets of $A$, including $\emptyset$ and $A$ itself:
$$\mathcal{P}(A) = { S : S \subseteq A }$$

Theorem: Power Set Cardinality

If $|A| = n$, then $|\mathcal{P}(A)| = 2^n$.
Proof by Combinatorial Encoding: Let $A = {a_1, a_2, \dots, a_n}$. Every subset $S \subseteq A$ can be represented as an $n$-bit binary characteristic string $b_1 b_2 \dots b_n$, where: $$b_i = \begin{cases} 1 & \text{if } a_i \in S \ 0 & \text{if } a_i \notin S \end{cases}$$ There are $2$ independent choices for each of the $n$ bits. By the Rule of Product, there are $2 \times 2 \times \dots \times 2 = 2^n$ distinct bit strings, establishing a direct bijection with $\mathcal{P}(A)$. $\blacksquare$

3. Russell's Paradox & The Limits of Naive Set Theory

In early "naive" set theory, it was assumed that any arbitrary logical predicate could define a set (Unrestricted Comprehension): $$S = { x : P(x) }$$
In 1901, Bertrand Russell constructed the paradox that collapsed naive set theory:
Consider the set $R$ of all sets that do not contain themselves: $$R = { x : x \notin x }$$ Question: Does $R$ contain itself? ($R \in R$?)
  • If $R \in R$: By definition of $R$, $R$ must satisfy $R \notin R$. A logical contradiction!
  • If $R \notin R$: By definition of $R$, $R$ must belong to $R$ ($R \in R$). Another contradiction!
$$R \in R \iff R \notin R$$

The Modern Solution: ZFC Axiomatic Set Theory

To eliminate paradoxes, modern mathematics relies on Zermelo-Fraenkel Set Theory with Choice (ZFC). In ZFC, you cannot define a set from the entire universe; you can only filter subsets from an already existing set (Axiom of Restricted Comprehension). In computer science, this same principle prevents circular recursive type definitions that would crash compilers.

4. Cardinality of Infinities: Cantor's Revolution

How do we measure the "size" of infinite sets? Georg Cantor proved that some infinities are strictly larger than others!

Definition of Cardinality Equivalence

Two sets $A$ and $B$ have the same cardinality ($|A| = |B|$) if and only if there exists a bijective function $f: A \to B$.
text
+-----------------------------------------------------------------------------------------+
|                                    COUNTABLE VS UNCOUNTABLE                             |
+-----------------------------------------------------------------------------------------+
|  COUNTABLE (|A| <= aleph_0):  Can be arranged in an infinite sequence: a_1, a_2, a_3...  |
|                               Examples: N (Naturals), Z (Integers), Q (Rationals).      |
|                                                                                         |
|  UNCOUNTABLE (|A| > aleph_0): CANNOT be listed in any sequence without missing items!   |
|                               Examples: R (Real numbers), P(N) (Power set of Naturals). |
+-----------------------------------------------------------------------------------------+

4.1 The Rational Numbers $\mathbb{Q}$ are Countable

It seems intuitive that fractions $\mathbb{Q}$ must be vastly larger than integers $\mathbb{N}$ because between any two integers there are infinite fractions. Yet, $|\mathbb{Q}| = |\mathbb{N}| = \aleph_0$!

Proof via Cantor's Zig-Zag Diagonal Enumeration:

Arrange all positive fractions $p/q$ in a 2D infinite grid:
text
      q=1      q=2      q=3      q=4
p=1   1/1 ---> 1/2      1/3 ---> 1/4 ...
       |      /        /        /
p=2   2/1    2/2      2/3      2/4 ...
             /        /
p=3   3/1    3/2      3/3      3/4 ...
       |
p=4   4/1 ...
By tracing the serpentine diagonal path: $$1/1, ; 1/2, ; 2/1, ; 3/1, ; 2/2 \text{ (skip)}, ; 1/3, ; 1/4, ; 2/3, ; 3/2, ; 4/1, \dots$$ Every single rational number is reached at a finite index $k \in \mathbb{N}$. This provides an explicit bijection $f: \mathbb{N} \to \mathbb{Q}$, proving $|\mathbb{Q}| = \aleph_0$. $\blacksquare$

4.2 The Real Numbers $\mathbb{R}$ are Uncountable: Cantor's Diagonal Argument

Cantor proved that the real interval $[0, 1)$ is strictly larger than $\mathbb{N}$ ($|[0, 1)| > \aleph_0$).

Proof by Contradiction:

Assume for contradiction that the real numbers in $[0, 1)$ are countable. Then they can be completely listed in a sequence:
$$\begin{aligned} r_1 &= 0.d_{11} , d_{12} , d_{13} , d_{14} \dots \ r_2 &= 0.d_{21} , d_{22} , d_{23} , d_{24} \dots \ r_3 &= 0.d_{31} , d_{32} , d_{33} , d_{34} \dots \ r_4 &= 0.d_{41} , d_{42} , d_{43} , d_{44} \dots \ &\vdots \end{aligned}$$
Now, construct a brand new real number $x = 0.x_1 x_2 x_3 x_4 \dots$ by modifying the diagonal digits $d_{ii}$:
$$x_i = \begin{cases} 4 & \text{if } d_{ii} = 5 \ 5 & \text{if } d_{ii} \neq 5 \end{cases}$$
  • $x \neq r_1$ because their 1st digit differs ($x_1 \neq d_{11}$).
  • $x \neq r_2$ because their 2nd digit differs ($x_2 \neq d_{22}$).
  • In general, $x \neq r_k$ for every index $k \in \mathbb{N}$ because their $k$-th digit differs ($x_k \neq d_{kk}$).
Therefore, $x \in [0, 1)$ is a valid real number that was omitted from the list. This contradicts our assumption that the list was complete. Hence, the real numbers are strictly uncountable! $|[0, 1)| = 2^{\aleph_0} = \mathfrak{c} > \aleph_0$. $\blacksquare$

5. Profound CS Consequence: The Existence of Uncomputable Problems

Why does Cantor's diagonal argument matter to a software engineer? It sets the absolute mathematical limits on what computers can ever solve!
  1. How many computer programs can exist? Every software program (in C, Python, Rust, machine code) is a finite sequence of ASCII/binary characters. The set of all possible computer programs is a countable infinity: $$|\text{All Programs}| = \aleph_0$$
  2. How many computational problems exist? Consider the set of all decision problems (functions mapping input strings to boolean outputs ${0, 1}^* \to {0, 1}$). This is the power set $\mathcal{P}({0, 1}^*)$: $$|\text{All Decision Problems}| = 2^{\aleph_0} > \aleph_0$$
  3. The Inevitable Conclusion: $$|\text{All Programs}| \ll |\text{All Computational Problems}|$$
There are uncountably more problems than programs! Even if humanity wrote computer programs for the next trillion years, the fraction of solvable problems is literally $\frac{\aleph_0}{2^{\aleph_0}} = 0%$. Most mathematical questions cannot be solved by any algorithm!

6. Python Implementation: Simulating Diagonalization & Set Operations

python
def demonstrate_set_operations():
    # Bitwise set representations
    A = {1, 2, 3, 4, 5}
    B = {4, 5, 6, 7, 8}

    print("Set A:", A)
    print("Set B:", B)
    print("Union (A | B):              ", A | B)
    print("Intersection (A & B):       ", A & B)
    print("Difference (A - B):         ", A - B)
    print("Symmetric Diff (A ^ B, XOR):", A ^ B)

def cantors_diagonal_demo():
    """
    Demonstrate Cantor's construction: given a finite sample of real decimal expansions,
    construct a real number that differs from every single row on the diagonal.
    """
    # 5 arbitrary real numbers in [0, 1)
    listed_numbers = [
        "0.14159265",
        "0.27182818",
        "0.57721566",
        "0.41421356",
        "0.73205080",
    ]

    new_digits = []
    for i, num_str in enumerate(listed_numbers):
        # Digit at index i after "0."
        diag_digit = int(num_str[2 + i])
        # Invert: if 5, make it 4; otherwise make it 5
        flipped = 4 if diag_digit == 5 else 5
        new_digits.append(str(flipped))

    constructed_number = "0." + "".join(new_digits)
    print("\n=== Cantor's Diagonal Construction ===")
    for idx, num in enumerate(listed_numbers):
        print(f"r_{idx + 1}: {num}")
    print(f"Constructed Number x: {constructed_number}")

    # Verify difference on every diagonal index
    for idx, num in enumerate(listed_numbers):
        assert constructed_number[2 + idx] != num[2 + idx]
    print("Verification Successful: x differs from every listed number on the diagonal!")

if __name__ == "__main__":
    demonstrate_set_operations()
    cantors_diagonal_demo()

7. Key Takeaways

  1. Power Set Cardinality: $|\mathcal{P}(A)| = 2^{|A|}$ for both finite and infinite sets.
  2. Countable Infinities: Sets that can be placed in 1-to-1 bijection with $\mathbb{N}$ ($\aleph_0$), including $\mathbb{Z}$ and $\mathbb{Q}$.
  3. Cantor's Diagonalization: Proves the continuum $\mathbb{R}$ is uncountable ($2^{\aleph_0} > \aleph_0$).
  4. Limits of Computation: Because programs are countable ($\aleph_0$) but decision problems are uncountable ($2^{\aleph_0}$), uncountably many problems are fundamentally uncomputable.