Sub topic 1
Alphabets, strings, and languages
Prerequisites
This chapter assumes you already know:
- What a set is, and the basic set operations (union, intersection, complement).
- The idea of a finite automaton and a regular expression, at the level of a first course.
- Proof by contradiction.
You do not need any knowledge of grammars or pushdown automata. Those start in the next chapters. This chapter fixes the vocabulary and notation that every later chapter, and every GATE question on this topic, relies on.
Alphabets and strings
An alphabet is a finite, non-empty set of symbols. It is written Σ. Common examples are Σ = {0, 1} and Σ = {a, b}.
A string (also called a word) is a finite sequence of symbols from an alphabet. The length of a string w is written |w|. For w = abba, |w| = 4.
The empty string is written ε. It has length 0 and contains no symbols. It is a real string, not "nothing". Treating ε carelessly is the most common source of errors in this subject.
Two notations appear constantly:
- |w|a is the number of occurrences of the symbol a in w. For w = abba, |w|a = 2.
- a^n means the symbol a repeated n times, so a^3 = aaa and a^0 = ε.
Operations on strings
Concatenation of x and y is written xy. It places y after x. The facts to remember:
- |xy| = |x| + |y|.
- Concatenation is associative: (xy)z = x(yz).
- Concatenation is not commutative. In general xy ≠ yx.
- ε is the identity: εw = wε = w.
Reversal of w is written w^R. If w = abc, then w^R = cba. The reversal of a concatenation swaps the order: (xy)^R = y^R x^R.
Parts of a string
Take w = abc. The terms below are easy to confuse, so keep them apart.
| Term | Meaning | Examples from w = abc |
|---|---|---|
| Prefix | Leading part of w | ε, a, ab, abc |
| Suffix | Trailing part of w | ε, c, bc, abc |
| Substring | Contiguous block inside w | ε, b, bc, abc |
| Subsequence | Symbols in order, gaps allowed | ac is a subsequence but not a substring |
Every prefix and every suffix is a substring, and every substring is a subsequence. The reverse directions fail.
Counting facts for a string of length n:
- It has n + 1 prefixes (including ε and w itself).
- If all n symbols are distinct, it has n(n+1)/2 + 1 distinct substrings, counting ε. For abc this gives 7: ε, a, b, c, ab, bc, abc.
- If all n symbols are distinct, it has 2^n distinct subsequences, counting ε.
When symbols repeat, the distinct counts drop. A question that asks for distinct substrings of aaa has the answer 4 (ε, a, aa, aaa), not 7.
Σ* and languages
Σ\ is the set of all strings over Σ, including ε. Σ⁺ is the set of all non-empty strings, so Σ⁺ = Σ \ {ε}.
For Σ = {a, b}:
Σ* = {ε, a, b, aa, ab, ba, bb, aaa, ...}
The number of strings of length exactly n is |Σ|^n. So over {a, b} there are 2^n strings of each length n. Σ is infinite, but every string in it is finite. Each string can be listed at some finite position if you list by length and then alphabetically, so Σ is countably infinite.
A language over Σ is any subset of Σ*. Languages are sets of strings, so they can be finite or infinite. Examples over Σ = {a, b}:
- L1 = {a, ab, abb} is finite.
- L2 = {w : |w| is even} is infinite.
- L3 = {a^n b^n : n ≥ 1} = {ab, aabb, aaabbb, ...} is infinite. This is the running example for the rest of the tutorial.
- L4 = {w : w = w^R} is the set of palindromes over Σ.
Because there are uncountably many languages over any alphabet but only countably many finite descriptions (grammars, automata, programs), most languages cannot be described finitely at all. The classes studied in this tutorial are the describable ones, and the later chapters show which descriptions are more powerful than others.
Two languages that look alike
The empty language and the language containing only the empty string are different sets.
| Language | Contents | Number of strings |
|---|---|---|
| ∅ | Nothing | 0 |
| {ε} | Only the empty string | 1 |
Their different behavior shows up in the operations below, and GATE questions test it directly.
Operations on languages
Since languages are sets, union (L1 ∪ L2), intersection (L1 ∩ L2), and difference work as usual. Three more operations are specific to languages.
- Complement: the complement of L is Σ* \ L. It depends on Σ. The complement of "strings with an even number of a's" over {a, b} is "strings with an odd number of a's".
- Concatenation: L1L2 = {xy : x ∈ L1, y ∈ L2}.
- Kleene star: L = the set of all concatenations of zero or more strings from L. Kleene plus: L⁺ = LL, one or more strings.
The reversal L^R = {w^R : w ∈ L} is also used often.
Useful identities:
- ∅L = L∅ = ∅.
- {ε}L = L{ε} = L.
- ∅* = {ε}, because zero strings concatenated give ε.
- {ε}* = {ε}.
- L* always contains ε, even if L does not. L⁺ contains ε only if L does.
- (L) = L*.
Concatenation of languages can collapse duplicates. Let L1 = {a, ab} and L2 = {ε, b}. The products are a·ε = a, a·b = ab, ab·ε = ab, ab·b = abb. The result is the set {a, ab, abb}, with three elements, not four.
Exploring a language in code
You can approximate a language by testing every string up to some length. This is useful for checking a grammar or automaton you design later against a membership test you trust.
from itertools import product
def strings_up_to(alphabet, max_len):
"""Yield every string over `alphabet` of length 0..max_len, shortest first."""
for n in range(max_len + 1):
for symbols in product(alphabet, repeat=n):
yield "".join(symbols)
def in_anbn(w):
"""Membership test for { a^n b^n : n >= 1 }."""
n = len(w) // 2
return n >= 1 and len(w) % 2 == 0 and w == "a" * n + "b" * n
members = [w for w in strings_up_to("ab", 6) if in_anbn(w)]
print(members) # ['ab', 'aabb', 'aaabbb']
total = sum(1 for _ in strings_up_to("ab", 6))
print(total) # 127, which equals 2**7 - 1The total of 127 matches the counting rule. Over a two-symbol alphabet, the number of strings of length at most n is 1 + 2 + 4 + ... + 2^n = 2^(n+1) − 1.
Common errors
Other frequent mistakes:
- Treating a substring as a subsequence, or the reverse, when counting.
- Forgetting that a^n with n = 0 is ε, so a language written with n ≥ 0 contains ε, while n ≥ 1 does not.
- Writing a^n b^n as if it meant (ab)^n. The first puts all a's before all b's. The second alternates.
- Assuming L* excludes ε. It never does.
Why this matters for what follows
Every later object in this tutorial defines a language. A grammar generates a set of strings, and a pushdown automaton accepts a set of strings. The pumping lemma is a statement about all long strings in a language. Closure properties ask whether applying an operation from this chapter to languages in a class keeps the result in that class. If the definitions here are secure, those chapters reduce to bookkeeping.
Exercise
Let Σ = {a, b}, L1 = {a, ab} and L2 = {ε, b}. (1) Write L1L2 as a set. (2) How many distinct substrings, counting ε, does the string aba have?
Show answer
(1) L1L2 = {a·ε, a·b, ab·ε, ab·b} = {a, ab, ab, abb} = {a, ab, abb}, so it has three elements because ab appears twice. (2) The substrings of aba are ε, a, b, ab, ba, aba. The substring a occurs twice but counts once, so the answer is 6, not the 7 given by the formula for distinct symbols.
Exercise
Evaluate ∅, {ε}, and ∅{a, b}. Which of them contain the string ε?
Show answer
∅ = {ε}, because the star allows zero concatenations. {ε} = {ε}. ∅{a, b} = ∅, because concatenating with the empty language leaves no pairs to form. Only the first two contain ε.