Sub topic 2
Where regular languages stop: a short recap
This chapter assumes you know the following:
- You can define alphabets, strings, and languages as sets of strings.
- You can read a DFA or NFA and write a regular expression.
- You are comfortable with proof by contradiction.
The pumping lemma for regular languages is recapped here, so you do not need to have memorised it.
What a finite automaton can remember
A DFA has a fixed number of states, say k. After reading a prefix, the only thing it knows about that prefix is which state it is in. Two prefixes that lead to the same state are indistinguishable to the machine. Every future input will be treated identically for both.
Regular languages are exactly the languages decided with this bounded memory. That covers a lot:
- Strings ending in
01. - Strings with an even number of
as. - Strings whose number of
as is a multiple of 5.
Each of these needs only a constant amount of information, such as a remainder or the last two symbols.
The standard example that breaks it
Take L = { aⁿbⁿ | n ≥ 1 }. To accept it, a machine must compare the number of as with the number of bs. The count n is unbounded, so the machine needs unbounded memory.
The argument is the pigeonhole principle. Suppose a DFA with k states read the prefixes ε, a, aa, …, aᵏ. That is k + 1 prefixes and only k states, so two of them, aⁱ and aʲ with i < j, end in the same state. Now append bⁱ to each:
- aⁱbⁱ is in L.
- aʲbⁱ is not in L, because j ≠ i.
Both strings end in the same state, so the DFA accepts both or rejects both. Either way it is wrong. The code below runs this argument on a concrete DFA that counts as up to 3 and then saturates.
def run(delta, start, w):
state = start
for ch in w:
state = delta[(state, ch)]
return state
def find_collision(delta, start, limit):
"""Return (i, j), i < j, with a^i and a^j ending in the same state."""
seen = {}
for i in range(limit + 1):
s = run(delta, start, "a" * i)
if s in seen:
return seen[s], i
seen[s] = i
return None
# States 0..3 count a's (3 saturates). State 4 is where any b leads.
delta = {}
for q in range(4):
delta[(q, "a")] = min(q + 1, 3)
delta[(q, "b")] = 4
delta[(4, "a")] = 4
delta[(4, "b")] = 4
i, j = find_collision(delta, 0, 10)
print(i, j) # 3 4
print(run(delta, 0, "a" * i + "b" * i)) # 4
print(run(delta, 0, "a" * j + "b" * i)) # 4 (same state)Whatever accepting states you choose, the two final states are identical. This machine cannot separate aⁿbⁿ from aⁿ⁺¹bⁿ.
The pumping lemma for regular languages
The pigeonhole argument is packaged as a general tool.
If L is regular, there is a pumping length p ≥ 1 such that every string z in L with |z| ≥ p can be written as z = xyw with:
- |xy| ≤ p
- |y| ≥ 1
- xyⁱw ∈ L for every i ≥ 0
The reason is that the first p symbols of z visit p + 1 states, so some state repeats. The loop between the two visits is y, and it can be repeated or deleted.
To show that aⁿbⁿ is not regular, choose z = aᵖbᵖ. Because |xy| ≤ p, the part y consists only of as, and |y| ≥ 1. Then xy²w has more as than bs, so it is not in L. This contradicts condition 3.
What stays regular and what does not
Regular languages are closed under union, intersection, complement, concatenation, Kleene star, reversal, and difference. These closure properties are often the fastest way to show non-regularity. For example, if L were regular, then L ∩ a\b\ would be regular. That intersection is useful when the original language is messy.
The table below sorts typical languages. Over the alphabet {a, b}, ww^R means a string followed by its reverse.
| Language | Regular? | Why |
|---|---|---|
| aⁿbᵐ, n, m ≥ 0 | Yes | Regular expression a\*b\* |
| aⁿbⁿ, n ≥ 1 | No | Needs to compare two unbounded counts |
| Strings with equal numbers of `a` and `b` | No | Intersecting with a\*b\* gives aⁿbⁿ (plus ε) |
| Balanced parentheses | No | Needs unbounded nesting depth |
| ww^R | No | Must remember the whole first half |
| Strings with an even number of `a`s | Yes | Two states suffice |
| Any finite language | Yes | Union of finitely many strings |
| aᵖ, p prime | No | Gaps between primes grow without bound |
Every non-regular language in this table except the last one is context-free, and the first four are the motivation for the rest of this tutorial. The prime-length language is not context-free either, which shows that the next class also has limits.
Common errors
- Letting yourself choose the split. In the proof, the adversary chooses the pumping length and the split xyw. You choose only the string z and the exponent i. Show a contradiction for every valid split.
- Forgetting the bound |xy| ≤ p. That bound is what forces y into the leading block of
as. If you pick a poorly shaped z, the argument fails. - Choosing a z that is too short. You must have |z| ≥ p, and z should be written in terms of p, not a fixed number.
- Assuming "infinite" means "not regular". a\* is infinite and regular. Non-regularity needs an unbounded comparison between parts of the string.
- Treating the language aⁿbᵐ as a counting language. There is no relationship between n and m, so no memory of n is needed.
What the extra power must provide
A machine for aⁿbⁿ needs a place to store n. A stack is the smallest structure that works. The machine pushes a symbol for each a, pops one for each b, and accepts if the stack empties exactly when the input ends. The same stack handles balanced parentheses and ww^R. It does not handle aⁿbⁿcⁿ, because one stack cannot be compared against two later blocks. That limit is proved in the pumping lemma chapters, using the same pumping idea you saw here.
Exercise
Use the regular pumping lemma to show that L = { aⁿb²ⁿ | n ≥ 0 } is not regular. State your choice of z and the step that gives the contradiction.
Show answer
Assume L is regular with pumping length p. Choose z = aᵖb²ᵖ, which is in L and has length at least p. Any split z = xyw with |xy| ≤ p puts y inside the leading as, so y = aᵏ with k ≥ 1. Then xy²w = aᵖ⁺ᵏb²ᵖ has p + k as but only 2p bs, and 2(p + k) ≠ 2p. So xy²w is not in L, which contradicts the lemma. Hence L is not regular.
Exercise
Is L = { w ∈ {a, b}* | w has equal numbers of a and b } regular? Justify your answer with a closure property.
Show answer
No. Regular languages are closed under intersection, so if L were regular, then L ∩ ab would be regular. That intersection is { aⁿbⁿ | n ≥ 0 }, which is not regular. This is a contradiction, so L is not regular.