Sub topic 11
The pumping lemma and non-regular languages
This chapter assumes you can build DFAs and NFAs, know what a state is, and are comfortable with proof by contradiction and the pigeonhole principle. It also uses the closure properties of regular languages (intersection, complement, homomorphism). Those are covered in the previous chapter.
Every regular language is accepted by a machine with finitely many states, so the machine has only a bounded amount of memory. The pumping lemma turns that limit into a test. When a language needs unbounded counting, the test fails and the language is not regular.
The statement
Pumping lemma. If L is regular, there is an integer p ≥ 1 (the pumping length) such that every string w in L with |w| ≥ p can be split as w = xyz with:
- |y| ≥ 1 (the middle part is not empty).
- |xy| ≤ p (the pumped part lies within the first p symbols).
- x yⁱ z is in L for every i ≥ 0.
The lemma is a necessary condition for regularity. It can show that a language is not regular. It can never show that a language is regular.
Why it holds
Take a DFA for L with p states. Feed it a string of length at least p. After reading the first p symbols it has visited p + 1 states, counting the start state. By the pigeonhole principle, two of these visits are to the same state. The symbols read between the two visits form y, and they take the machine around a loop. Going around the loop zero times, once, or many times ends in the same state, so the machine accepts x yⁱ z for every i.
The code below finds this loop for a real DFA. It uses the DFA for "binary value divisible by 3" from earlier in the tutorial, which has 3 states.
def pump_split(delta, start, w):
# Return (x, y, z) from the first repeated state, or None if no state repeats.
seen = {start: 0} # state -> number of symbols read when first visited
state = start
for i, ch in enumerate(w, start=1):
state = delta[(state, ch)]
if state in seen:
j = seen[state]
return w[:j], w[j:i], w[i:]
seen[state] = i
return None
delta = {(r, b): (2 * r + int(b)) % 3 for r in range(3) for b in "01"}
def accepts(w):
state = 0
for ch in w:
state = delta[(state, ch)]
return state == 0
w = "110" # value 6, accepted
x, y, z = pump_split(delta, 0, w)
print(repr(x), repr(y), repr(z)) # '' '11' '0'
assert len(y) >= 1 and len(x + y) <= 3
for i in range(6):
assert accepts(x + y * i + z)
print("all pumped strings accepted")The split satisfies all three conditions. Conditions 1 and 2 hold because the repeat occurs within the first 3 symbols and consumes at least one symbol.
The proof as a game
A pumping-lemma proof is a game against an adversary. You choose the string, and the adversary chooses everything else.
Your argument must work for every legal split. You cannot pick a convenient y. You can only use what the rules guarantee, mainly that y lies inside the first p symbols of w.
Worked proofs
L = { aⁿbⁿ : n ≥ 0 }
- Assume L is regular, with pumping length p.
- Choose w = aᵖbᵖ. It is in L and |w| = 2p ≥ p.
- Because |xy| ≤ p, both x and y consist only of a's. So y = aᵏ with k ≥ 1.
- Pump up with i = 2. The result is aᵖ⁺ᵏbᵖ, which has more a's than b's, so it is not in L.
This contradicts the lemma, so L is not regular.
L = { aⁿ² : n ≥ 0 } (lengths that are perfect squares)
Choose w = aᵖ². Then y = aᵏ with 1 ≤ k ≤ p. Pump with i = 2. The new length is p² + k. Since p² < p² + k ≤ p² + p < (p + 1)², the length lies strictly between two consecutive squares, so the string is not in L.
L = { aᵠ : q is prime }
Choose w = aᵠ with q a prime and q ≥ p. Let |y| = k ≥ 1. Pump with i = q + 1. The new length is q + qk = q(1 + k). Both q ≥ 2 and 1 + k ≥ 2, so the length is composite and the string is not in L. This argument works for every legal split, because k only needs to be at least 1.
L = { aⁱbʲ : i > j }, pumping down
Choose w = aᵖ⁺¹bᵖ. Then y = aᵏ with k ≥ 1. Pump with i = 0. The result is aᵖ⁺¹⁻ᵏbᵖ, which has at most p a's and exactly p b's, so i > j fails. Pumping up would not work here, because extra a's keep the string in L. When pumping up does not work, try pumping down.
L = { ww : w ∈ {a, b}* }
Choose w = aᵖbaᵖb. Since |xy| ≤ p, y is a block of a's from the first run. Pump with i = 2. The string becomes aᵖ⁺ᵏbaᵖb with k ≥ 1. A string of the form uu has even length and both halves equal. Here the first b appears at position p + k + 1, which is after the midpoint of the string. So the first half contains no b, and the second half does. The halves differ, and the string is not in L.
Choosing a good string
| Language | String to choose | Why it works |
|---|---|---|
| aⁿbⁿ, equal a's and b's | aᵖbᵖ | The first p symbols are all a's, so y is all a's. |
| a's more than b's | aᵖ⁺¹bᵖ | Pumping down removes enough a's to break the inequality. |
| ww | aᵖbaᵖb | y is trapped in the first a-run. |
| Length is a square or prime | aᵠ with suitable length | Pump to a length that cannot be in the set. |
Put the symbols you want y to hit at the front of w, so that the condition |xy| ≤ p traps y there.
Alternatives that avoid pumping
Closure properties often give a shorter proof. To show that L = { w ∈ {a, b} : #a(w) = #b(w) } is not regular, intersect it with the regular language ab*. The result is { aⁿbⁿ }, which is not regular. Since regular languages are closed under intersection, L cannot be regular.
The language { aⁿbᵐ : n ≠ m } is another case. Its complement, intersected with ab, gives { aⁿbⁿ }. Closure under complement and intersection finishes the proof.
Common errors
- Using the lemma to prove regularity. The lemma gives a necessary condition only. Some non-regular languages satisfy the pumping conditions, so passing the test proves nothing. For a language that satisfies the lemma but is still not regular, use the Myhill-Nerode theorem (next chapters).
- Choosing the split. The adversary picks x, y, and z. If your proof says "let y be the first a", it is wrong. You must handle every split allowed by |y| ≥ 1 and |xy| ≤ p.
- Choosing p. The value p is fixed by the adversary. Write the string in terms of p. Never use a specific number such as 5.
- Forgetting the length conditions. w must be in L and have |w| ≥ p. A string with fewer than p symbols tells you nothing.
- Concluding from one bad i. One value of i that leaves the string outside L is enough, but it must work for every legal split.
- Treating finite languages as a problem. Every finite language is regular. The lemma is satisfied vacuously by taking p larger than the longest string.
- Reading "not regular" as "not context-free". { aⁿbⁿ } is not regular but is context-free. Non-regularity says nothing about higher classes.
Quick checklist
- Does the language need to compare two unbounded quantities (equal counts, ww, perfect squares, primes)? If so, it is probably not regular.
- Can you intersect with ab or another regular language to reach a known non-regular language? Try closure first.
- Otherwise, pick w using p, trap y in a known block, and choose i so the result leaves L.
Exercise
Show that L = { aⁿbⁿcⁿ : n ≥ 0 } is not regular using the pumping lemma.
Show answer
Assume L is regular with pumping length p. Take w = aᵖbᵖcᵖ. Since |xy| ≤ p, y consists only of a's, so y = aᵏ with k ≥ 1. Pump with i = 2 to get aᵖ⁺ᵏbᵖcᵖ. It has more a's than b's, so it is not in L. This contradicts the lemma, so L is not regular.
Exercise
A student shows that every long string in L = { aⁿbᵐ : n, m ≥ 0 } can be pumped, and concludes that L is regular. Is the reasoning valid? Is the conclusion true?
Show answer
The reasoning is not valid, because the pumping lemma is only a necessary condition and cannot establish regularity. The conclusion happens to be true: L is denoted by the regular expression ab.
Exercise
Use closure properties to show that L = { w ∈ {a, b}* : #a(w) = #b(w) } is not regular.
Show answer
Suppose L is regular. Regular languages are closed under intersection, so L ∩ ab is regular. But L ∩ ab = { aⁿbⁿ : n ≥ 0 }, which is not regular. This contradiction shows that L is not regular.