Unit 1 · 4 hrs · CEUC302 Theory of Computation
What exactly is a language?
Every later chapter builds a machine or a grammar for some language. This
chapter is where language itself gets a precise meaning, along with the
vocabulary — alphabet, string, grammar, token, ambiguity — that the rest of the
course assumes you already have. Two small languages over the alphabet {a,b}
run through every section: Leq = {anbn : n ≥ 0}
and Leven, the strings with an even number of a's. They look
similarly simple. One of them a finite-memory machine can recognise; the other, as you'll
prove in Unit 2, no finite-memory machine ever can.
Definition-first
A language is nothing but a set
L is a language over Σ whenever L ⊆ Σ*.
That's the whole definition. No mention of a machine, no mention of a grammar. A string is either in the set or it isn't — membership is a yes/no question, and everything else in this course is machinery built to answer that one question.
The three strings drawn outside the Leq region aren't malformed or illegal in any sense — they're perfectly good strings over {a,b}. They simply don't happen to satisfy "equal a's then equal b's, a's first."
Before you start
This is the first chapter, so there's no prior chapter to have read — only the course's own prerequisites: sets and set operations, relations, mathematical induction, and basic graph terms. Section 1.4 leans directly on two of those (pigeonhole counting, the adjacency matrix), so a five-minute refresher there is time well spent before you hit it.
The vocabulary everything else is built from
Four words — alphabet, string, Kleene star, language — and one quiet fact about infinity that trips up almost everyone the first time.
What does it mean for a piece of text to be "valid"? A valid Python identifier, a valid IPv4 address, a valid arithmetic expression, a valid English sentence — every one of these is really the same question in disguise: given this string of symbols, does it belong to the set of strings we've agreed to call valid? Theory of Computation exists because that question, asked precisely, turns out to have beautiful and sometimes surprising structure. This section makes the question precise.
The intuition, no symbols yet
Think of an alphabet as just a bag of allowed symbols — letters, digits, punctuation, whatever a problem calls for. A string is any sequence you build by pulling symbols from the bag, one after another, as many times as you like, including zero times. A language is simply the collection of sequences you've decided to call "good." That's genuinely all of it. The formalism below exists only to let us reason about that idea precisely enough to prove things, not to complicate it.
The formal treatment
An alphabet, written Σ, is a finite, non-empty set of indivisible symbols. Throughout this chapter Σ = {a, b}. A string (equivalently, a word) over Σ is a finite sequence of symbols from Σ. The number of symbols in a string w is its length, written |w|. There is exactly one string of length 0, called the empty string and written ε — it is a string, not a set, and not "nothing."
Σ* is called the Kleene star of Σ (after mathematician Stephen Kleene). Two strings concatenate by writing one after the other: if u = "aa" and v = "b" then uv = "aab". A language L over Σ is any subset of Σ* — formally, L ⊆ Σ*. Nothing more is required. L is a finite language if it has finitely many strings as members (for example Ltiny = {a, ab, abb, aabb}, exactly four strings, full stop) and an infinite language otherwise.
Don't confuse these
- ε vs ∅. ε is the empty string — one specific, perfectly good member of Σ*. ∅ is the empty set — a language with no members at all. {ε} and ∅ are different languages: the first contains one string, the second contains none.
- "Finite alphabet" does not mean "finite language." Σ = {a,b} has only 2 symbols, yet Σ* itself is infinite — there is no longest string. Confusing the size of the alphabet with the size of a language over it is the single most common slip in this unit.
- A language need not look like a "pattern." The definition L ⊆ Σ* puts no requirement on L being describable by a short rule. Whether a language can be described compactly — by a grammar, by a machine — is exactly what the rest of this course investigates. It is a property some languages have, not part of what a language is.
Why this matters · CO1
A compiler's lexical analyser turns raw characters into tokens —
categories like IDENTIFIER, NUMBER, or the
keyword IF. Each token category is itself defined as a language: the
set of character-strings that count as, say, a valid identifier. "Is x9
a valid identifier?" and "is ab in Leq?" are the exact same
kind of question. Unit 2 shows that token languages are almost always regular, which is
precisely why lexical analysers can be built from small, fast machines.
Worked example · classifying the spine strings
Recall the chapter's two running languages over Σ = {a, b}:
Ten strings, sorted by hand against both definitions:
| string | len | #a | #b | ∈ Leq? | ∈ Leven? |
|---|---|---|---|---|---|
| ε | 0 | 0 | 0 | yes | yes |
| a | 1 | 1 | 0 | no | no |
| b | 1 | 0 | 1 | no | yes |
| ab | 2 | 1 | 1 | yes | no |
| aa | 2 | 2 | 0 | no | yes |
| ba | 2 | 1 | 1 | no | no |
| aabb | 4 | 2 | 2 | yes | yes |
| abab | 4 | 2 | 2 | no | yes |
| aaabbb | 6 | 3 | 3 | yes | no |
| aaabb | 5 | 3 | 2 | no | no |
Two rows worth staring at. aabb and abab have the same letter counts (two a's, two b's) yet land in different places for Leq — because Leq cares about order (a's strictly before b's), while Leven never looks at order at all, only at the count of a's. Same alphabet, same string lengths, genuinely different questions being asked.
Worked example
enumerating Σ* by handSeeing Σ* · a language is just a chosen set of dots
Σ* as a branching tree, Σ = {a, b}
fig 1.1Practice · 1.1
Seven problems
direct → variation → interpretation → synthesis1.1.1 · For Σ={a,b}, classify each string as: in Leq only, in Leven only, in both, or in neither — aaaabbbb, ab, aa, ba.
Show solution
aaaabbbb: n=4 a's then 4 b's → Leq yes; #a=4 even → Leven yes. Both.
ab: n=1=1 → Leq yes; #a=1 odd → Leven no. Leq only.
aa: not of form anbn with equal counts (2 a's, 0 b's) → Leq no; #a=2 even → Leven yes. Leven only.
ba: starts with b, so it can't be anbn → Leq no; #a=1 odd → Leven no. Neither.
1.1.2 · Without listing its elements, state |Σ5| and |Σ0 ∪ Σ1 ∪ Σ2| for Σ = {a,b}.
Show solution
|Σ5| = 25 = 32. |Σ0∪Σ1∪Σ2| = 1+2+4 = 7 (the sets are disjoint — different lengths can never coincide — so their sizes simply add).
1.1.3 · Variation: define Lodd-b = strings with an odd number of b's. Classify aabbb, bb, ab, ε.
Show solution
aabbb: #b=3, odd → yes. bb: #b=2, even → no. ab: #b=1, odd → yes. ε: #b=0, even → no. Same style of question as Leven, different symbol and parity — the counting idea transfers directly.
1.1.4 · Variation: if Σ = {a,b,c}, how many strings are there of length exactly 4? Of length at most 4 (including ε)?
Show solution
Exactly 4: |Σ|4 = 34 = 81. At most 4: ∑k=04 3k = 1+3+9+27+81 = 121. Growing the alphabet from 2 to 3 symbols changes a count of 2k to 3k at every length — the growth rate, not just the count, changes.
1.1.5 · Interpretation: a classmate argues "ba isn't in Leq, so it must be in Leven." What's wrong with this reasoning?
Show solution
It assumes Leq and Leven are complements of each other — that every string is in exactly one of the two. They aren't complements; they're two unrelated conditions. ba fails both tests (row 6 of the worked table: neither), which is a perfectly consistent outcome. Not-in-A tells you nothing about membership in an unrelated set B.
1.1.6 · Interpretation: you're told a language L over {a,b} has exactly 4 strings, full stop. Could L be Leq? Could it be Leven? One line each.
Show solution
Neither. Leq already contains anbn for every n ≥ 0 — infinitely many strings — so no 4-element set can equal it. Leven likewise contains infinitely many strings (b, bb, aab, aabb, … every string with an even a-count). A language with "only a few short members visible" is not the same claim as "a finite language" — Leq has few short members but is still infinite overall.
1.1.7 · Synthesis (challenge) · Using only L ⊆ Σ*, argue there are more languages over {a,b} than there are strings over {a,b} — even though both are built from the same two-symbol alphabet.
Show solution
Σ* is countably infinite: every string has a finite length, so you can list all strings by increasing length (and alphabetically within a length) and eventually reach any given one — that's exactly what "countable" means. A language, though, is any subset of Σ*, and the collection of all subsets of a countably infinite set is uncountable (Cantor's diagonal argument, which you may have seen in the prerequisite set-theory material). Uncountable is strictly bigger than countable. So most languages over {a,b} can't even be listed one string-rule at a time — a fact this chapter returns to in Section 1.4.
Writing an infinite set on a finite page
A grammar is a rewriting recipe. Follow it and you generate exactly the strings of some language — never more, never fewer.
Section 1.1 defined Leq by a description in English: "all a's, then equally many b's." That description is a set-builder rule, and it's fine for a human reader, but it isn't yet something you can mechanically follow, one small step at a time, to actually produce strings. A grammar is what closes that gap.
The intuition, no symbols yet
Picture a recipe card with one starting stand-in symbol — not a real ingredient yet, just a slot — and a short list of allowed substitutions. You keep substituting, choosing freely among the rules that apply, until every slot is filled and only real ingredients remain. Different sequences of choices can produce different finished dishes; the set of every dish reachable this way is the language the recipe generates.
The formal treatment
A grammar is a 4-tuple G = (V, Σ, R, S):
- V, a finite set of non-terminals (also called variables) — stand-in symbols that exist only inside the grammar and must never appear in a finished, fully-generated string.
- Σ, the familiar alphabet of terminals — the real symbols a finished string is allowed to contain. V and Σ never share a symbol.
- R, a finite set of production rules, each written α → β and read "α may be rewritten as β."
- S ∈ V, the designated start symbol — every derivation begins here.
Starting from S, repeatedly pick some rule whose left side appears in your current string and replace it with the rule's right side. Each such step is written with ⇒ ("yields"); a full chain of steps is a derivation, written S ⇒* w. Any string that appears partway through a derivation — terminals and leftover non-terminals mixed together — is called a sentential form. Only once every non-terminal is gone is the result a genuine member of the language generated by G:
Different grammars can generate the very same language — a fact Unit 3 leans on heavily when it converts grammars into standard forms (CNF, GNF) without changing what they generate. "The grammar" and "the language" are not the same object; a language is a set, a grammar is one particular recipe for that set, and a set can have many recipes.
Ambiguity, in one clean example
A grammar is ambiguous if some string it generates has more than one distinct derivation tree (equivalently, more than one distinct leftmost derivation). The smallest example worth memorising: Gamb has V={S}, Σ={a}, start S, and rules S → SS | a. It generates a+ = {a, aa, aaa, …}. Watch what happens for "aaa":
tree 1 · grouped as a(aa)
tree 2 · grouped as (aa)a
Both trees are built entirely from legal rules of Gamb, both have exactly three leaves reading "aaa," and they are structurally different trees. One string, two trees — that's the entire definition of ambiguity, made concrete. (Non-terminals are drawn violet throughout this book, per the colour key — that's the standing meaning of violet from here on.)
Ambiguity is a property of the grammar, not the language
Gamb is ambiguous, but the language it generates, a+, is not stuck being ambiguous: the grammar S → aS | a generates the exact same language and has only one derivation tree per string (check "aaa" yourself). Trading an ambiguous grammar for an unambiguous one that generates the same language is usually possible — Unit 3's grammar-restructuring techniques do exactly this for arithmetic expressions. A small number of languages resist this entirely; Unit 3 calls those inherently ambiguous and it's a genuinely deeper result, not something to worry about yet.
Worked example · deriving aaabbb from Geq
Worked example
Geq: S → aSb | εaSb — one terminal on each side, one non-terminal still in the middle.aaSbb.aaaSbbb.Try other values of n
Building anbn from Geq
fig 1.2 · interactiveDon't confuse these
- A sentential form is not a generated string.
aaSbbabove still has an S in it — it's mid-derivation, not a member of L(G). - The grammar isn't the language. "What does Geq look like" and "what is Leq" are different questions with the same answer set; a language can be written by more than one grammar, and Unit 3 will hand you several equivalent ones for the same CFL.
- Ambiguity lives in the grammar, not the string. "aaa" isn't an inherently ambiguous string — it merely has two trees under Gamb. Under S→aS|a it has exactly one.
Practice · 1.2
Seven problems
direct → variation → interpretation → synthesis1.2.1 · Write out the full derivation Geq uses to produce "aabb," step by step.
Show solution
S ⇒ aSb ⇒ a(aSb)b = aaSbb ⇒ aa(ε)bb = aabb. Two firings of S→aSb, one firing of S→ε — n=2, matching the 1.1 table.
1.2.2 · For Gamb (S→SS|a), draw or list all distinct parse trees for "aaaa." How many are there?
Show solution
Four a-leaves combined pairwise by SS have C3 = 5 distinct binary groupings (the Catalan numbers: 1,1,2,5,14,… for 1,2,3,4,5 leaves): a(a(aa)), a((aa)a), (aa)(aa), (a(aa))a, ((aa)a)a. Five trees — ambiguity gets worse, not better, as the string grows.
1.2.3 · Variation: write a grammar Gpal that generates exactly the palindromes over {a,b} of the form w wR (w followed by w reversed), e.g. "abba", "aa", "ε". Hint: mimic Geq's outside-in shape.
Show solution
V={S}, Σ={a,b}, rules: S → aSa | bSb | ε. Each rule adds a matching symbol on both ends and recurses on the middle, exactly like Geq's aSb but now the two added symbols must match each other rather than being fixed as "a on the left, b on the right." Check: S⇒aSa⇒a(bSb)a=abSba⇒ab(ε)ba=abba. ✓
1.2.4 · Variation: is the grammar from 1.2.3 ambiguous? Explain in one line, don't enumerate all trees.
Show solution
No. At every sentential form there is exactly one non-terminal S, and the target string's own first and last remaining symbols force exactly one applicable rule (aSa if they're both a, bSb if both b, ε if the string is exhausted) — there is never a real choice, so every string it generates has exactly one derivation. Same reasoning as Geq.
1.2.5 · Interpretation: a classmate writes S→aSb|ab|ε for Leq and says "this is wrong, it has an extra rule." Is the extra rule a problem?
Show solution
Not necessarily wrong, just redundant here: S→ab is already reachable via S→aSb followed by S→ε (giving aεb = ab). The extra rule generates no new strings and creates no new derivation for any string that didn't already have one via the other two rules (it's a shortcut, not an alternate path to the same string, so it doesn't introduce ambiguity either). A grammar with redundant-but-harmless rules is still a correct grammar for the language — correctness is about L(G) matching the target, not about the rule set being minimal.
1.2.6 · Interpretation: after two derivation steps from Geq, you reach the sentential form "aSb." Can you conclude n will end up being 1?
Show solution
No — "aSb" is one step in, and the derivation isn't finished. From here you could still fire S→aSb any number of further times before finally firing S→ε. "aSb" is consistent with the final string being ab (n=1), aabb (n=2), aaabbb (n=3), or any larger n. A sentential form mid-derivation under-determines the final answer; only reaching an all-terminal string is conclusive.
1.2.7 · Synthesis · Geq has exactly one non-terminal in every sentential form it ever produces. Explain why this single fact guarantees Geq is unambiguous, without checking any specific string.
Show solution
Ambiguity requires two different sequences of rule-firings to land on the same terminal string. But if there is only ever one non-terminal present, there is only one place a rule can even be applied — the only remaining choice at each step is which rule to fire on it, and for a fixed target string the required count of "expand" vs. "terminate" firings is forced (you need exactly n expansions before the one termination to reach anbn). With no branching in where to rewrite and no freedom in how many times to expand for a given target, there is exactly one derivation per string. This same one-non-terminal-at-a-time argument is why every right-linear grammar (the kind Unit 2 connects to finite automata) is automatically unambiguous.
Not all languages need the same amount of memory
The whole rest of this course is organised around one question: how much bookkeeping does recognising a language actually require?
Leq and Leven looked, in Section 1.1, like two similarly simple rules about the letters a and b. They are not similarly simple. Recognising Leven needs one bit of memory: is the a-count so far odd or even? Recognising Leq needs to remember exactly how many a's you've seen, with no upper bound — one bit will never be enough, no matter how cleverly you design the bit. That gap in memory requirement is not a curiosity; it's the organising idea of the entire syllabus.
The intuition, no symbols yet
Picture a family of nested boxes, each one demanding a more powerful kind of machine to check membership than the box inside it. The innermost box holds languages a machine with a fixed, finite memory can check (Unit 2). The next box out needs a machine with a stack — unbounded, but restricted to last-in-first-out access (Unit 4). Further out still, machines need general read-write memory with no restrictions at all (Unit 5). Every language in an inner box is automatically also a member of every box surrounding it — being easy to recognise never disqualifies a language from the harder categories.
The formal treatment · four grammars, four restrictions
Noam Chomsky's classification sorts languages by placing a restriction on the shape of the production rules (Section 1.2) allowed to generate them. Using A, B ∈ V for non-terminals, a ∈ Σ for a terminal, and α, β, γ for arbitrary strings over V ∪ Σ:
| Type | Name | Rule shape allowed | Recognising machine |
|---|---|---|---|
| 3 | Regular | A → aB or A → a | finite automaton · Unit 2 |
| 2 | Context-free | A → γ | pushdown automaton · Unit 4 |
| 1 | Context-sensitive | αAβ → αγβ, |γ| ≥ 1 | linear-bounded automaton · Unit 4 preview |
| 0 | Recursively enumerable | α → β, no restriction | Turing machine · Unit 5 |
Type 2's rule shape is exactly the grammar shape Section 1.2 already used for Geq and Gamb — every grammar you wrote there was already, without saying so, context-free. Type 3 tightens this further: only one terminal per rule, always paired with at most one trailing non-terminal. Type 1's requirement |γ| ≥ 1 means a rule can never shrink the string — the surrounding context α, β must survive unchanged, which is where the name comes from.
Where does "Decidable" fit? It isn't a fifth type.
Chomsky's four types are all defined by grammar shape. Decidable (also called recursive) is defined a completely different way — by machines, not grammars: a language is decidable if some Turing machine (Unit 5) halts on every input and correctly says yes or no. It so happens that every context-sensitive language is decidable, and every decidable language is recursively enumerable — so it slots in naturally between Type 1 and Type 0, and nearly every course draws it as a ring there, but it doesn't get a Chomsky number of its own.
Here is the one-non-terminal, right-linear grammar that proves Leven is Type 3 — note every rule is exactly the shape A→aB or A→a the table demands:
Every rule matches Type 3's shape exactly, so this alone certifies Leven is regular — no separate machine needed to make the claim, though Unit 2 will hand you the matching finite automaton directly from this grammar's two states.
Worked example · four languages, four tiers
Worked example
placing four languages in the hierarchyWhere each language actually sits
The hierarchy as nested rings
fig 1.3 · interactiveDon't confuse these
- The rings are nested, not exclusive. Leven being regular does not disqualify it from also being context-free, context-sensitive, decidable, and RE — every regular language sits inside all four wider rings simultaneously. "Classify this language" means find its tightest ring, not its only ring.
- The hierarchy is about required memory, not surface complexity. A language with a short, innocent-looking definition can still fail to be regular (Leq), and a language defined by a long, fussy-looking condition can still be perfectly regular. Judge the tier by what a recogniser needs to remember, never by how the definition reads.
- "Undecidable" does not mean "unknowable" or "false." It means no single algorithm decides every instance correctly and halts. Plenty of individual programs can still be proven to halt or not — what's missing is a universal procedure that works for all of them at once.
A depth box, entirely skippable · most languages aren't even in the outer ring
Σ* is countably infinite (Problem 1.1.7), so there are only countably many finite strings — and, crucially, only countably many Turing machines, since each is itself just a finite string of instructions. But a language is any subset of Σ*, and Cantor's theorem says the collection of all subsets of a countably infinite set is uncountable. Countably many machines chasing uncountably many possible languages means almost every language has no machine at all, of any kind — not even one that runs forever without answering. The five nested rings above, however wide the outermost one looks, are a vanishingly small sliver of everything L ⊆ Σ* could be.
Practice · 1.3
Seven problems
direct → variation → interpretation → synthesis1.3.1 · Which Chomsky type's rule shape did Gamb (S→SS|a, Section 1.2) use? Is Gamb itself regular, context-free, both, or neither?
Show solution
Both rules have a single non-terminal on the left and an arbitrary string of symbols on the right — exactly Type 2's shape (A→γ). S→SS is not of the form A→aB or A→a, so it is not Type 3 as written. That only shows this particular grammar isn't right-linear — and in fact a+ genuinely is regular (grammar S→aS|a, Problem 1.2, is Type 3), so a language can be context-free via one grammar and still be regular via a different, better-chosen one. Tier membership is a property of the language, not of any one grammar for it.
1.3.2 · True or false, with a one-line reason: "every context-sensitive language is context-free."
Show solution
False. The rings nest the other way — every context-free language is context-sensitive, not vice versa. L3eq={anbncn} is the standing counterexample: context-sensitive, but not context-free.
1.3.3 · Variation: L3even = strings over {a,b,c} with an even number of a's, no constraint on b's or c's. Which tier, and what does the Type-3 grammar look like?
Show solution
Regular. Reuse the Leven grammar almost unchanged: V={S,A}, and now both b and c behave like the old "b" (they don't affect the a-parity): S→aA|bS|cS|ε, A→aS|bA|cA. Adding symbols the property doesn't care about never raises the tier.
1.3.4 · Variation: L4 = {anbncndn : n ≥ 0}, four letters instead of three. Still just "context-sensitive," or does it need to move up a further ring?
Show solution
Still context-sensitive — it doesn't need Type 0. Context-sensitive grammars can enforce any fixed number of simultaneous matching counts (three, four, forty), not only two; the ring boundary is about needing non-contracting, context-dependent rewriting at all, which one extra letter doesn't cross. It is, like L3eq, still not context-free, for the same one-non-terminal-at-a-time reason.
1.3.5 · Interpretation: a classmate says "Leq is context-free, so it can't be regular." Fix the reasoning.
Show solution
Being context-free doesn't forbid also being regular — the rings nest, they don't partition. The correct statement needs an extra fact: Leq is context-free and, separately, provably not regular (via the pumping lemma, Unit 2). It's that second, independent fact that fixes its tightest tier at context-free, not the mere fact of being context-free.
1.3.6 · Interpretation: you're handed a language L and told "no Turing machine can enumerate L's strings at all, not even one that runs forever on non-members." What's the tightest tier L could possibly occupy?
Show solution
Not even RE. Recursively enumerable is defined as exactly "some Turing machine lists/accepts its members (possibly running forever on non-members)." If no machine does that, L sits entirely outside all five rings — in the vast region Problem 1.3's depth box describes, alongside almost every subset of Σ*.
1.3.7 · Synthesis (challenge) · Leven needs 2 states of memory (Section 1.3's grammar). Guess, without proof, roughly how the minimum memory needed to check anbn for n up to some bound N would have to scale with N, if you insisted on using only a fixed-memory (regular) machine. Why does this make "just use a bigger finite machine" a losing strategy as N grows?
Show solution
You'd need roughly N+1 distinct memory states just to count the a's seen so far, up to N (one state per count 0,1,…,N, so you can check the b's tally against the right number). Since Leq has no bound on n at all, "fixed, finite memory" would have to mean literally infinite memory to cover every n — a contradiction, since regular languages are defined by machines with a genuinely finite, fixed number of states decided in advance. This scaling argument is exactly the intuition Unit 2's pumping lemma turns into a rigorous proof that Leq is not regular.
Two habits of mind, borrowed early
The pigeonhole principle and the adjacency matrix aren't new theory — they're prerequisite discrete-math tools this course is about to lean on hard. Closure properties and the pumping lemma get named here and proved properly in Units 2–3.
Almost every "prove this language is not regular" argument in Unit 2, and every "prove this language is not context-free" argument in Unit 3, is the same one idea wearing different clothes: if you don't have enough memory, something has to repeat, and that repeat can be exploited. The counting fact behind "something has to repeat" is the pigeonhole principle, and it's worth having cold before it shows up disguised as a five-line lemma with quantifiers.
The intuition, no symbols yet
If you have more letters than mailboxes and every letter must go in some mailbox, at least one mailbox ends up with more than one letter. That's the entire idea. It sounds too simple to be useful — it becomes useful the moment "mailboxes" means "the finite number of states a machine can be in," and "letters" means "the positions along an impossibly long input string."
The formal treatment
Pigeonhole principle. If n+1 objects are distributed among n boxes, at least one box receives 2 or more objects. More generally, if kn+1 objects are distributed among n boxes, some box receives at least k+1. Nothing here says which box, or how many extra — only that a collision is unavoidable once the counts cross that threshold.
Worked example
the classic pigeonhole count5 objects, 4 boxes — a collision is forced
fig 1.4aAdjacency matrix. For a directed graph G = (V, E), the adjacency matrix M is a |V|×|V| table of 0s and 1s where M[i][j] = 1 exactly when there is an edge from node i to node j. Row i is "edges leaving i"; column j is "edges arriving at j." This convention (row = from, column = to) is a choice, not a law of nature — state it explicitly whenever you draw one, since some textbooks flip it.
a 3-node directed graph
its adjacency matrix
| from ↓ / to → | 1 | 2 | 3 |
|---|---|---|---|
| 1 | 0 | 1 | 0 |
| 2 | 0 | 0 | 1 |
| 3 | 1 | 0 | 0 |
Row 1 reads "1→2 exists, 1→1 and 1→3 don't" — read any row left to right and you're reading that node's outgoing edges. Unit 2 draws exactly this kind of picture for every finite automaton, then asks you to write down its matrix.
Closure properties & the pumping lemma — named now, proved later
A class of languages C is closed under an operation if applying that operation to member(s) of C always produces another member of C. As a trivial but genuine example: the class of finite languages is closed under union — take any two finite languages, their union just has (at most) the combined number of strings, still a finite count. Unit 2 asks the much sharper question "is the class of regular languages closed under union, intersection, complement, concatenation, star?" (spoiler: yes to all five, and each proof teaches a different construction), and Unit 3 asks the same question for context-free languages (spoiler: closure holds for some of these operations and breaks for others).
The pumping lemma, in one sentence with no quantifiers yet: any sufficiently long string in a regular (or context-free) language must contain a short piece that can be repeated, or removed, arbitrarily, while the result stays in the language. "Sufficiently long" and "short piece" get exact meanings in Units 2 and 3; what matters now is why this is true at all — and that's just pigeonhole again. A machine with a fixed number of states, forced to process a string much longer than its state count, must revisit some state twice (pigeonhole: more input positions than states). Between those two visits sits a loop the machine is willing to go around any number of times — including zero — without changing its final verdict. That loop is exactly the "piece that can be pumped."
Don't confuse these
- Pigeonhole tells you a collision exists, never which one. It's an existence proof, not a construction — don't expect it to hand you the repeated state itself.
- Adjacency matrix orientation is a convention, not a fact. Always state whether row-to-column means "from→to" or the reverse before reading numbers off a matrix someone else drew.
- Closure under an operation says nothing about languages outside the class. "Regular languages are closed under union" does not mean "the union of any two languages is regular" — both inputs must already be in the class for the guarantee to apply.
Practice · 1.4
Seven problems
direct → variation → interpretation → synthesis1.4.1 · A drawer has socks in only 5 colours. What is the fewest socks you must pull out, without looking, to guarantee two of the same colour?
Show solution
6. With 5 socks you could have exactly one of each colour (no collision forced yet); the 6th sock must repeat some colour by pigeonhole (n=5 boxes, need n+1=6 objects).
1.4.2 · Write the adjacency matrix for the graph in fig 1.4b if you also add an edge 1→3.
Show solution
Only row 1 changes, M[1][3] flips from 0 to 1:
Row1: 0 1 1 · Row2: 0 0 1 · Row3: 1 0 0
1.4.3 · Variation: is the class of finite languages closed under concatenation (L1L2 = every string uv with u∈L1, v∈L2)? Justify briefly.
Show solution
Yes. If L1 has p strings and L2 has q strings, L1L2 has at most p×q strings (some concatenations might coincide, so it's an upper bound, not necessarily exact) — still a finite number either way.
1.4.4 · Variation: with kn+1 objects in n boxes, what's the strongest "some box has at least …" claim you can make for n=6 boxes and 20 objects?
Show solution
20 = 6k+1 ⇒ k ≥ 19/6 = 3.166…, so the largest integer k with 6k+1 ≤ 20 is k=3 (6×3+1=19 ≤ 20). The generalised principle then guarantees some box holds at least k+1 = 4 objects.
1.4.5 · Interpretation: a proof claims "this machine has 5 states and processes a 4-symbol string, so by pigeonhole some state must repeat." What's wrong?
Show solution
Wrong direction of inequality. A machine visits one state per position it has read, so a 4-symbol string touches at most 5 states-visited (including the start), which is ≤ the 5 available states — no forced repeat. Pigeonhole only bites once the number of visits exceeds the number of available states; the string needs to be longer than the state count, not merely close to it.
1.4.6 · Interpretation: someone says "regular languages are closed under union, so Leq ∪ Leven must be regular." Evaluate the argument.
Show solution
Invalid as stated — the closure theorem needs both inputs to already be regular. Leq is not regular (Section 1.3), so the theorem simply doesn't apply here; it makes no claim at all about this particular union. (It happens that Leq∪Leven is in fact not regular either, but that has to be shown separately — not concluded from a closure property whose hypothesis already fails.)
1.4.7 · Synthesis (challenge) · Sketch, in your own words and without formal notation, why a machine with only 3 states could never correctly recognise Leq restricted to strings of length up to 100. Use pigeonhole explicitly.
Show solution
Feed the machine a50 one symbol at a time. It visits 51 states-in-sequence (start plus one per symbol read) but has only 3 distinct states to be in — by pigeonhole, some state repeats after reading, say, i a's and again after j a's (i<j≤50). Since the machine's future behaviour depends only on its current state, not on how it got there, it will treat "i a's so far" and "j a's so far" identically from that point on. Feed both continuations the same 50 b's: if aib50 is (wrongly) accepted, so is ajb50, but only one of those has matching counts. The 3-state machine is forced into an error somewhere in this family of strings. This is the pumping lemma for regular languages, one paragraph early.
Cheat sheet
One line per idea. If a line doesn't ring a bell, that section needs a re-read before Unit 2.
Vocabulary · 1.1
Σ — finite, non-empty alphabet
Σ* — every finite string over Σ, always infinite
ε — the empty string (≠ ∅, the empty set)
L ⊆ Σ* — a language is just a set of strings
finite language — finitely many member strings, e.g. {a,ab,abb}
Grammars · 1.2
G = (V, Σ, R, S) — non-terminals, terminals, rules, start
sentential form — mid-derivation string, may still hold non-terminals
L(G) = {w : S ⇒* w} — only all-terminal results count
ambiguous — some string has ≥2 distinct parse trees
ambiguity is a grammar property — a language can often be re-written unambiguously
Chomsky hierarchy · 1.3
Type 3 Regular — A→aB | A→a · finite automaton
Type 2 CF — A→γ · pushdown automaton
Type 1 CS — αAβ→αγβ, |γ|≥1 · LBA
Type 0 RE — α→β, unrestricted · Turing machine
Decidable sits between Type 1 and Type 0, defined by machines, not grammar shape
Proof tools · 1.4
pigeonhole — n+1 objects, n boxes ⇒ some box has ≥2
adjacency matrix — M[i][j]=1 iff edge i→j; fix the convention first
closure — op stays inside the class for members of the class, nothing about outsiders
pumping (preview) — long enough string ⇒ some piece repeats & can be pumped
Ten questions, no section labels
Exams don't tell you which lecture a question came from. Neither does this.
ST1 · Classify "aabbab" against Leq and Leven.
Show solution
Sequence is a,a,b,b,a,b — an a appears after some b's, so it can never be of the "all a's then all b's" shape regardless of counts ⇒ Leq: no. #a = 3, odd ⇒ Leven: no. Neither.
ST2 · Which Chomsky type does the single rule A → aBc belong to, taken on its own (a, c terminals, B a non-terminal)?
Show solution
Type 2 (context-free) as written — single non-terminal on the left, arbitrary string on the right. It is not Type 3: a non-terminal followed by a trailing terminal (Bc) doesn't match either A→aB or A→a.
ST3 · 100 pigeons, 9 boxes. What's the strongest "some box has at least …" guarantee?
Show solution
⌈100/9⌉ = ⌈11.11⌉ = 12. Equivalently 100 = 9×11 + 1, so k=11 and the guarantee is k+1=12.
ST4 · Give any finite language over {a,b} with exactly 3 members.
Show solution
Any explicit 3-string set works, e.g. {ε, a, bb}. There's no single correct answer — only "does it have exactly three strings, full stop, with no generating pattern extending it."
ST5 · True or false: every context-free language is decidable.
Show solution
True. The rings nest Regular ⊂ CF ⊂ CS ⊂ Decidable ⊂ RE, so anything in the CF ring is automatically inside the Decidable ring too — this is the "forward" direction of ring-nesting that Section 1.3's trap box warns people forget to use.
ST6 · Minimum number of derivation steps for Geq to generate a5b5?
Show solution
n applications of S→aSb plus one final S→ε — for n=5, that's 6 steps total (matches the n+1 pattern seen for n=3 taking 4 steps in Section 1.2's worked example).
ST7 · A grammar generates exactly one string, "ab," and nothing else. Finite or infinite language?
Show solution
Finite — one string is still a fully valid, finite count. "Generated by a grammar" says nothing by itself about finiteness in either direction.
ST8 · Is ∅ (the language with no strings at all) a legitimate language? Finite or infinite?
Show solution
Yes — ∅ ⊆ Σ* holds for every Σ, satisfying the definition trivially. It's finite (zero is a finite count) and, importantly, not the same object as {ε} (one string) — the classic ε-vs-∅ trap from Section 1.1.
ST9 · If a grammar's every sentential form contains exactly one non-terminal throughout every derivation, can that grammar ever be ambiguous?
Show solution
No. Same argument as Problem 1.2.7: with only one rewritable position at any time, the only freedom is which rule to fire, and for a fixed target string the required sequence of expand/terminate choices is forced — leaving exactly one derivation per generated string.
ST10 · Where does a language sit if it's known to be decidable but provably not context-sensitive?
Show solution
In the Decidable-only band — inside the Decidable ring but outside the Context-Sensitive ring, the same style of "one ring further out" placement as Lhalt occupied the RE-only band in Section 1.3's figure (there, relative to Decidable rather than Context-Sensitive).
Where this goes next
- Leven's Type-3 grammar (1.3) becomes an actual 2-state finite automaton in Chapter 2.
- The "not regular" claim about Leq, only argued informally here, gets a full pumping-lemma proof.
- The DFA/NFA equivalence and the lexical-analysis application (CO1) both build directly on this chapter's language/token framing.
Further reading
- Sipser, Introduction to the Theory of Computation, 3rd ed. — the "Introduction" chapter covers exactly this material (sets, strings, languages, proof styles) at a similarly gentle pace; good for a second pass in different words.
- J. C. Martin, Introduction to Languages and the Theory of Computation. — its early chapters on strings and the Chomsky hierarchy use largely the same notation used here; useful if the lecture follows this text specifically.
Next: Chapter 2 · Finite Automata I — DFA, NFA & Regular Languages