Sub topic 1
Alphabets, strings, and languages
Prerequisites
This chapter assumes you know basic set operations (union, intersection, complement, Cartesian product) and what a function is. You should be comfortable with simple induction proofs. You should also be able to read a string as a sequence of symbols. Nothing about automata is assumed. This chapter supplies the vocabulary that every later chapter uses.
Why the vocabulary matters
Every automaton, regular expression, and grammar in this tutorial describes a set of strings. If you misread what a string, a language, or an operation on languages means, you will miscount states and misjudge closure properties later. GATE questions often hinge on small points, such as whether a language contains the empty string, or whether L* and L+ differ. This chapter fixes those points.
Alphabets and strings
An alphabet is a finite, non-empty set of symbols. It is written Σ (sigma). Common examples:
- Σ = {0, 1}, the binary alphabet.
- Σ = {a, b}, the usual alphabet in GATE questions.
- Σ = {a, b, ..., z}, the lowercase letters.
A string (also called a word) over Σ is a finite sequence of symbols from Σ. Some basic facts:
- The length of a string w is written |w|. For example, |abba| = 4.
- The empty string, written ε (epsilon), has length 0. It contains no symbols. It is a string, not a symbol, and it belongs to no alphabet.
- |w|ₐ is the number of occurrences of the symbol a in w. For example, |abba|ₐ = 2.
- Σ is the set of all strings over Σ, including ε. Σ⁺ is the set of all non-empty strings. So Σ⁺ = Σ \ {ε}.
- Σᵏ is the set of all strings of length exactly k. Σ⁰ = {ε}.
For Σ = {a, b}, the set Σ is infinite, but each Σᵏ is finite and has exactly 2ᵏ strings. Every string in Σ is finite. No string has infinite length.
Operations on strings
Concatenation joins two strings end to end. If x = ab and y = ba, then xy = abba. Concatenation is associative, since (xy)z = x(yz). It is not commutative, since xy is usually different from yx. The empty string is the identity: εw = wε = w. Also, |xy| = |x| + |y|.
The reverse of w, written wᴿ, lists the symbols in the opposite order. (abb)ᴿ = bba. A string w with w = wᴿ is a palindrome. Also, (xy)ᴿ = yᴿxᴿ.
The power wᵏ means w concatenated with itself k times. w⁰ = ε. For example, (ab)³ = ababab.
Prefixes, suffixes, and substrings
If w = xyz, then x is a prefix of w, z is a suffix, and y is a substring. Any of x, y, z may be empty. So ε and w itself are both a prefix, a suffix, and a substring of w.
A subsequence differs from a substring. A subsequence keeps the order of symbols but may skip some. For w = abc, the string ac is a subsequence but not a substring.
A string of length n has at most n + 1 distinct prefixes (lengths 0 to n) and at most n(n + 1)/2 + 1 distinct substrings, counting ε. The substring bound is reached only when all the symbols are different.
Languages
A language over Σ is any subset of Σ*. A language may be finite or infinite. Some examples over Σ = {a, b}:
- L₁ = {ε, a, ab}, a finite language.
- L₂ = {w : |w|ₐ is even}, an infinite language.
- L₃ = {aⁿbⁿ : n ≥ 0}, an infinite language that is not regular. A later chapter proves this.
- ∅, the empty language, which contains no strings.
- {ε}, the language that contains only the empty string.
The difference between ∅ and {ε}
This is a common source of errors. ∅ has zero strings. {ε} has exactly one string, the empty one. They behave differently under concatenation (see below), and a finite automaton for {ε} accepts one input while an automaton for ∅ accepts none.
Because languages are sets, they inherit union, intersection, and difference. The complement of L is taken relative to Σ: L̄ = Σ \ L. Complement depends on the alphabet. The complement of {a}ⁿ-style languages changes if you change Σ.
Operations on languages
Three operations are specific to languages, and regular expressions are built from them.
| Operation | Definition | Example with L = {a, ab}, M = {b, ε} |
|---|---|---|
| Union | L ∪ M | {a, ab, b, ε} |
| Concatenation | LM = {xy : x ∈ L, y ∈ M} | {ab, a, abb, ab} = {a, ab, abb} |
| Kleene star | L* = L⁰ ∪ L¹ ∪ L² ∪ ... | {ε, a, ab, aa, aab, aba, abab, ...} |
| Positive closure | L⁺ = L¹ ∪ L² ∪ ... | L* without ε, unless ε ∈ L |
Here L⁰ = {ε} and Lᵏ⁺¹ = LᵏL. Note that the concatenation example produced "ab" twice, once from a·b and once from ab·ε. A set keeps only one copy.
Facts that GATE questions test
- ∅L = L∅ = ∅. Concatenating with the empty language gives the empty language, because no pair (x, y) exists.
- {ε}L = L{ε} = L. The language {ε} is the identity for concatenation.
- ∅* = {ε}. The star always includes L⁰ = {ε}, even for the empty language.
- {ε}* = {ε}.
- L = L⁺ exactly when ε ∈ L. Otherwise L⁺ = L \ {ε}.
- (L) = L*.
- L always contains ε, so L is never empty.
- |LM| is at most |L| · |M| for finite languages. It can be smaller when different pairs give the same string, as in the example above.
- Concatenation distributes over union: L(M ∪ N) = LM ∪ LN. It does not distribute over intersection. Taking L = {a, ab}, M = {b}, N = {bb} shows this: M ∩ N = ∅, so L(M ∩ N) = ∅, but LM ∩ LN = {ab, abb} ∩ {abb, abbb} = {abb}.
Counting strings
If Σ has k symbols, then |Σⁿ| = kⁿ. The number of strings of length at most n is 1 + k + ... + kⁿ = (kⁿ⁺¹ − 1)/(k − 1). For k = 2 and n = 3, that is 15 strings.
Languages as problems
A language encodes a yes/no question. The language L = {w ∈ {0,1}* : w is the binary form of a multiple of 3} asks "is this number divisible by 3?". An automaton that accepts L answers that question. This view is the reason the course treats machines and languages together: each class of machine defines a class of languages, and the interesting questions compare these classes.
The set of all languages over Σ is uncountable, because it is the power set of the countably infinite set Σ*. Any description of a language, whether an automaton or a regular expression, is a finite string. There are only countably many finite descriptions, so most languages have no finite description at all. This is the first hint that some languages cannot be recognized by any machine of a given kind.
Checking languages in code
You can experiment with small languages directly. The snippet below builds all strings over {a, b} up to a given length, then computes a language concatenation and a bounded Kleene star.
from itertools import product
def sigma_star_upto(alphabet, n):
out = set()
for k in range(n + 1):
out.update("".join(t) for t in product(alphabet, repeat=k))
return out
def concat(L, M):
return {x + y for x in L for y in M}
def star_upto(L, max_len):
result = {""}
frontier = {""}
while frontier:
frontier = {w for w in concat(frontier, L) if len(w) <= max_len} - result
result |= frontier
return result
L = {"a", "ab"}
M = {"b", ""}
print(len(sigma_star_upto("ab", 3))) # 15
print(sorted(concat(L, M))) # ['a', 'ab', 'abb']
print(sorted(concat(set(), L))) # [] since the empty language absorbs
print(sorted(star_upto(set(), 5))) # [''] since the star of the empty language is {epsilon}
print(sorted(star_upto(L, 4)))The empty string is written "" in Python, and the empty language is the empty set set(). Keeping them apart in code is a good habit for the theory too. Because L* is infinite, the code can only list the part of it up to a length bound.
A diagram of the hierarchy of objects
Common errors
- Treating ε as a symbol of Σ. It is a string of length 0.
- Writing ∅ when you mean {ε}, or the reverse.
- Forgetting that complement is relative to Σ*, so L̄ contains ε whenever L does not.
- Assuming L* is a finite set when L contains any non-empty string. It is then infinite.
- Mixing up substring and subsequence.
- Assuming LM = ML. Concatenation of languages is not commutative.
Exercise
Let Σ = {a, b}, L = {ε, a}, and M = {a, ab}. (1) List LM. (2) What is ∅*? (3) What is ∅M? (4) How many strings of length at most 4 exist over Σ?
Show answer
(1) LM = {ε·a, ε·ab, a·a, a·ab} = {a, ab, aa, aab}. (2) ∅* = {ε}, because L⁰ = {ε} is always included. (3) ∅M = ∅, because there is no x in ∅ to pair with a string of M. (4) 1 + 2 + 4 + 8 + 16 = 31 strings.
Exercise
Give an example of languages L, M, N with L(M ∩ N) different from LM ∩ LN, and say what the two sides are.
Show answer
Take L = {a, ab}, M = {b}, N = {bb}. Then M ∩ N = ∅, so L(M ∩ N) = ∅. But LM = {ab, abb} and LN = {abb, abbb}, so LM ∩ LN = {abb}. The two sides differ, so concatenation does not distribute over intersection.