CS Atlas GATE 2027
TOC course
0 / 91 complete

TOC/Phase 5 · Undecidability

Lesson 5.1 · Item 77 of 91

Countable vs uncountable; diagonalization

Read at your own pace. Try the questions before opening the answers.

On this page

Recall warm-up

  1. Why are TMs countable?
    Stuck? Peek at 4.4 Encoding TMs, the universal TM, and the Church–Turing thesis
    • ⟨M⟩: binary code of transitions (0ⁱ1 0ʲ1 0ᵏ1 0ˡ1 0ᵐ, separated by 11). TMs are countable.
    • Universal TM U: simulates M on w; L(U) = {⟨M,w⟩ : M accepts w} is RE.
    • Church–Turing thesis: algorithm ⇔ TM (thesis, not theorem).

    Read lesson 4.4

  2. What is a bijection?
    Stuck? Peek at 0.2.2 Functions, relations and equivalence classes
    • Relation ⊆ A × B. Function: every input exactly one output. Partial function: some inputs have none.
    • #functions A→B = |B|^|A|. #one-to-one (|A| ≤ |B|) = |B|·(|B|−1)···. #relations on n-set = 2^(n²).
    • Equivalence = reflexive + symmetric + transitive. Classes partition the set; number of classes = index.
    • Finer relation (refinement) ⇒ more classes; every fine class sits inside a coarse class.

    Read lesson 0.2.2

  3. Shortlex order lists Σ* how?
    Stuck? Peek at 0.3.2 Σᵏ, Σ*, Σ⁺ and languages
    • |Σᵏ| = |Σ|ᵏ; Σ⁰ = {ε}; Σ* = ⋃ Σᵏ (k ≥ 0); Σ⁺ = Σ* − {ε}.
    • #strings of length ≤ n over k symbols = (kⁿ⁺¹ − 1)/(k − 1); binary: 2ⁿ⁺¹ − 1.
    • Language = any subset of Σ*. ∅ (no strings) ≠ {ε} (one string). Exactly r of one symbol in length n (binary): C(n, r).
    • Shortlex order lists every string at a finite position.

    Read lesson 0.3.2

The Hook

Here is the shortest proof that some problems have no algorithm: count. There are only countably many programs (each is a finite string), but uncountably many problems (sets of strings). More problems than programs — so most problems have no program at all. This lesson makes “countably many” and “uncountably many” precise, using Cantor’s diagonal trick, the same trick that proves the halting problem undecidable.

Predict First

Are there “more” even numbers than natural numbers, fewer, or the same number?

Core Concept

Prediction check

The same. n ↦ 2n pairs every natural number with exactly one even number (a bijection). For infinite sets, “same size” means “a bijection exists”, and a part can be as big as the whole.

ℕ01234…
even02468…
A perfect pairing: same size.

Countable sets

A set is countable if it is finite or there is a bijection with ℕ = {0, 1, 2, …} — equivalently, its elements can be listed as a sequence x₀, x₁, x₂, … in which every element appears.

Countable: ℕ, the integers (0, 1, −1, 2, −2, …), pairs ℕ × ℕ (walk the diagonals), Σ* (shortlex order), the set of all TMs (list their codes), every language’s set of strings.

position0123456…
string of {0,1}*ε0100011011…
Σ* is countable: shortlex gives every string a finite position.

Uncountable sets and Cantor’s diagonal

Cantor’s theorem. The power set of a countably infinite set is uncountable. In particular, the set of all languages over Σ (= P(Σ*)) is uncountable.

Proof: suppose the languages could be listed L₁, L₂, …. Build D = { wᵢ : wᵢ ∉ Lᵢ } — for each i, D disagrees with Lᵢ about the string wᵢ. So D is not on the list, contradicting that the list was complete. ∎

steps illustration
The diagonal argument — the most important picture in Phase 5.

More languages than machines

TMs are countable; languages are uncountable. Each TM recognizes one language, so only countably many languages are RE. Therefore uncountably many languages are not RE — in fact “almost all” languages have no recognizer, let alone a decider.

TMs
countable (finite codes)
RE languages
at most countable
all languages
uncountable (P(Σ*))
so
most languages are not RE
The counting argument.

Diagonalization as a technique

Whenever a list claims to contain every object of some kind, build an object that differs from the i-th one at position i. If your object belongs to the kind, the list was incomplete. Next lesson aims this weapon at Turing machines themselves.

claimed list
every language / every TM decision
diagonal object
differs from item i at position i
conclusion
the list misses it — contradiction
The recipe.

Putting it together

  • Countable means listable; Σ* and the set of TMs are countable.
  • The diagonal argument shows P(Σ*) is not.
  • So almost all languages have no recognizer.

Visual

countable:   ℕ  ℤ  ℚ  ℕ×ℕ  Σ*  {all TMs}  {all RE languages}  {all regular languages}
uncountable: P(ℕ)  ℝ  P(Σ*) = {all languages}  {infinite binary sequences}

Worked Examples

Easy

Is the set of all regular languages countable?

Show the full solution

Yes: each is described by a finite DFA (or regex), and finite descriptions can be listed.

Medium

Is the set of all infinite binary sequences countable?

Show the full solution

No: it corresponds to P(ℕ) (positions of the 1s); the diagonal argument applies.

GATE-level

Which are countable? (i) set of all CFGs over {a,b} (ii) set of all languages over {a} (iii) set of all RE languages (iv) set of all finite subsets of ℕ

Show the full solution

(i) countable (finite descriptions). (ii) uncountable: P({a}*) and {a}* is countably infinite. (iii) countable. (iv) countable (list by sum of elements / size). Answer: (i), (iii), (iv).

GATE Traps

  • Σ* is countable (infinite but listable); P(Σ*) is uncountable.
  • Even a one-letter alphabet has uncountably many languages.
  • Countably many TMs ⇒ only countably many RE languages ⇒ most languages are not RE.
  • Finite subsets of ℕ: countable. All subsets of ℕ: uncountable.

Memory Hook

A hotel with numbered rooms holds every program, but the crowd of all problems never fits — the diagonal guest always finds no room.

Connect the Dots

Counting proves non-RE languages exist but names none. Next: diagonalize against TMs directly to get concrete ones — L_d, and then the most famous of all, the halting problem.

Self-Test

  1. MSQ

    Which sets are countable?

    1. Σ*
    2. the set of all TMs
    3. the set of all languages over Σ
    4. ℕ × ℕ
  2. MCQ

    The counting argument shows:

    1. every language is RE
    2. some languages are not RE
    3. the halting problem is RE
    4. Σ* is uncountable
  3. MCQ

    Cantor’s diagonal language D = { wᵢ : wᵢ ∉ Lᵢ } differs from Lᵢ:

    1. nowhere
    2. at the string wᵢ
    3. at every string
    4. only on ε
  4. MCQ

    The set of all languages over the alphabet {a} is:

    1. finite
    2. countably infinite
    3. uncountable
    4. empty

Rule Card

  • Countable = finite or bijection with ℕ (listable). Σ*, ℕ×ℕ, TMs, RE, CFG, regular languages: countable.
  • P(ℕ), P(Σ*) (all languages), infinite binary sequences, ℝ: uncountable (diagonal).
  • Countably many TMs ⇒ most languages are not RE.

Concept Ledger

  • countable set
  • bijection with ℕ
  • uncountable set
  • Cantor’s diagonal argument
  • diagonalization
  • counting argument (non-RE languages exist)

Answer Vault

Reveal answers when you’re ready

Warm-up answers

  1. Each has a finite binary code, so they can be listed.
  2. A function that is one-to-one and onto.
  3. Shorter strings first, then dictionary order — every string at a finite position.

Self-test answers

  1. ABD — All listable.
    • C: P(Σ*) is uncountable.
  2. B — Countable TMs vs uncountable languages.
    • A: Opposite.
    • C: True but not from counting.
    • D: Σ* is countable.
  3. B — Built to disagree on the diagonal.
    • A: It differs.
    • C: Only one position is guaranteed.
    • D: No.
  4. C — P of a countably infinite set.
    • A: No.
    • B: No.
    • D: No.

Flashcards

  1. Is Σ* countable?
    Reveal answerYes (shortlex).
  2. Is the set of all languages over Σ countable?
    Reveal answerNo — it is P(Σ*), uncountable.
  3. Why must non-RE languages exist?
    Reveal answerTMs are countable; languages are uncountable.

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.