Lecture 24: the pumping lemma [018I]
- February 16, 2025
- Jon Sterling, with contributions from Andrew Pitts, Ian Leslie, Frank Stajano
These lecture notes were prepared by Jon Sterling by adapting prior notes created by Andrew Pitts that I inherited from Frank Stajano.
In Lecture 21, we asked ourselves several questions concerning regular expression matching. We have answered three of those questions in Lecture 23 by means of Kleene’s theorem, which proves regular expressions and finite automata generate the same class of formal languages. Today’s lecture will provide evidence for our response to the final question, that not every formal language is going to be regular. The theorem that we will use to verify non-regularity for our counterexamples is (annoyingly) called the pumping lemma.
Example Some non-regular languages [018J]
- February 16, 2025
- Jon Sterling
The following languages will turn out to non-regular:
- The set of strings over {\mathopen {}\left \{(,),a,b,\ldots ,z\right \}\mathclose {}} in which parentheses are well-nested.
- The set of strings over {\mathopen {}\left \{a,b,\ldots ,z\right \}\mathclose {}} that are palindromes, i.e. that read the same backwards as forwards.
- The set of strings over {\mathopen {}\left \{a,b\right \}\mathclose {}} consisting of some number of as followed by the same number of bs.
Definition Pumping bracket [018K]
- February 16, 2025
- Jon Sterling
Let L be a formal language over an alphabet \Sigma . A pumping bracket in L is defined to be a tuple of strings {\mathopen {}\left (u_1,v,u_2\right )\mathclose {}} over \Sigma with v\not = \epsilon such that for any n\geq 0, we have u_1 v^n u_2\in L.
We will refer to the string u_1vu_2\in L as the concatenation of a pumping bracket {\mathopen {}\left (u_1,v,u_2\right )\mathclose {}}. We will refer to u_1v\in \Sigma ^\star as the left-hand component of the pumping bracket.
Definition The pumping property [018L]
- February 16, 2025
- Jon Sterling
Let L be a formal language over an alphabet \Sigma . A number \ell >1 is said to have the pumping property with respect to L when any string w\in L satisfying {\mathopen {}\left \lvert w\right \rvert \mathclose {}}\geq \ell is the concatenation of some pumping bracket {\mathopen {}\left (u_1,v,u_2\right )\mathclose {}} in L whose left-hand component u_1v has length less than or equal to \ell .
In other words, \ell has the pumping property when for all w\in L we have w=u_1vu_2 for some pumping bracket {\mathopen {}\left (u_1,v,u_2\right )\mathclose {}} such that {\mathopen {}\left \lvert u_1v\right \rvert \mathclose {}}\leq \ell .
Lemma The pumping lemma for DFAs [018M]
- February 16, 2025
- Jon Sterling
Let M be a deterministic finite automaton. Then the cardinality \mathord {\#}{Q_{M}} of the set of states of M satisfies the pumping property with respect to \mathcal {L}{\mathopen {}\left (M\right )\mathclose {}}.
Proof.
- February 16, 2025
- Jon Sterling
Let \ell =\mathord {\#}{Q_{M}} for short. To show that \ell satisfies the pumping property with respect to \mathcal {L}{\mathopen {}\left (M\right )\mathclose {}}, we fix a string w\in \mathcal {L}{\mathopen {}\left (M\right )\mathclose {}} with {\mathopen {}\left \lvert w\right \rvert \mathclose {}}\geq \ell which we must deconcatenate into some pumping bracket {\mathopen {}\left (u_1,v,u_2\right )\mathclose {}} in \mathcal {L}{\mathopen {}\left (M\right )\mathclose {}} such that {\mathopen {}\left \lvert u_1v\right \rvert \mathclose {}}\leq \ell .
Letting n={\mathopen {}\left \lvert w\right \rvert \mathclose {}}, we know that w=a_1\ldots a_n and that we have a transition path of the following form in M:
s_{M}=q_0\mathrel {\xrightarrow {a_1}} q_1\mathrel {\xrightarrow {a_2}} q_2\cdots \mathrel {\xrightarrow {a_n}}q_n\in F_{M}We focus on just the first \ell transitions in the path above, which pass through \ell +1 states:
s_{M}= \overbrace { \underbrace { q_0\mathrel {\xrightarrow {a_1}} q_1\mathrel {\xrightarrow {a_2}} q_2\cdots \mathrel {\xrightarrow {a_\ell }}q_\ell }_{\text {$\scriptstyle \ell $ transitions through $\scriptstyle \ell +1$ states}} \cdots \mathrel {\xrightarrow {a_n}}q_n }^{\text {$\scriptstyle n$ transitions through $\scriptstyle n+1$ states}} \in F_{M}Because there are no more than \ell states total in M, we know for sure that in the depicted prefix, there must be at least one state visited twice—or, in other words, we must have some 0\leq i<j\leq \ell such that q_i=q_j. We can therefore decompose the entire transition sequence as w=u_1 v u_2 where u_1 = a_1\ldots a_i, v=a_{i+1}\ldots a_j, and u_1 = a_{j+1}\ldots a_n as depicted below:
s_{M}=q_0\mathrel {\vphantom {\xrightarrow {u_1}}\smash {\xrightarrow {u_1}}^*} q_i \mathrel {\vphantom {\xrightarrow {v}}\smash {\xrightarrow {v}}^*} q_j\mathrel {\vphantom {\xrightarrow {u_2}}\smash {\xrightarrow {u_2}}^*} q_n \in F_{M}We know that {\mathopen {}\left \lvert u_1 v\right \rvert \mathclose {}}\leq \ell because i<j\leq \ell . It remains to check that we can repeat the central component v any number of times (including zero), i.e. that for all n:\mathbb {N} we have u_1vu_2\in \mathcal {L}{\mathopen {}\left (M\right )\mathclose {}}. But this is immediate because q_i=q_j, and so the transition path q_j\mathrel {\vphantom {\xrightarrow {u_2}}\smash {\xrightarrow {u_2}}^*} q_n can occur after any number of round-trips consuming v each time.
Corollary The pumping lemma for regular languages [018N]
- February 16, 2025
- Jon Sterling
For any regular language L there exists a number \ell >1 satisfying the pumping property with respect to L.
Proof.
- February 16, 2025
- Jon Sterling
By Kleene’s theorem, there exists some DFA M satisfying \mathcal {L}{\mathopen {}\left (M\right )\mathclose {}}=L; the rest follows from Lemma [018M].
Applications of the pumping lemma [018P]
- February 16, 2025
- Jon Sterling
We can use the pumping lemma to show that the examples described in Example [018J] are not regular. First, it is helpful to transform the pumping lemma into a more convenient contrapositive form.
Corollary Contrapositive of the pumping lemma [018O]
- February 16, 2025
- Jon Sterling
Let L be a formal language such that for each \ell \geq 1, there exists some string w\in L satisfying {\mathopen {}\left \lvert w\right \rvert \mathclose {}}\geq l such that for any decomposition w=u_1 v u_2 with {\mathopen {}\left \lvert u_1v\right \rvert \mathclose {}}\leq \ell and v non-empty, there exists some n such that u_1 v^n u_2 lies outside L. Then L is not regular.
Proof.
- February 16, 2025
- Jon Sterling
Now we are ready to proceed.
Corollary Well-bracketing is not regular [018Q]
- February 16, 2025
- Jon Sterling
The set of strings over {\mathopen {}\left \{(,),a,b,\ldots ,z\right \}\mathclose {}} in which parentheses are well-nested is not regular.
Proof.
- February 16, 2025
- Jon Sterling
Write L for the language in question. Applying Corollary [018O], we fix \ell \geq 1 and consider the string w=\lparen ^\ell \rparen ^\ell consisting of \ell left parentheses followed by the same number of right parentheses; we evidently have w\in L and {\mathopen {}\left \lvert w\right \rvert \mathclose {}}\geq \ell . But any deconcatenation w=u_1 v u_2 with v non-empty and \vert {u_1v}\leq \ell must have either v=\lparen ^k for some non-zero k; there is no other possibility, because we have assumed {\mathopen {}\left \lvert u_1v\right \rvert \mathclose {}}\leq \ell . Therefore, even though w=u_1 v u_2 lies in L, we would not have u_1 v^n u_2\in L unless n=1: increasing or decreasing n will result in unbalanced parentheses.
The same argument shows that set of strings over {\mathopen {}\left \{a,b\right \}\mathclose {}} consisting of some number of as followed by the same number of bs is not regular.
Corollary Palindromes are not regular [018R]
- February 16, 2025
- Jon Sterling
The set of palindromes over {\mathopen {}\left \{a,b\right \}\mathclose {}} is not regular.
Proof.
- February 16, 2025
- Jon Sterling
Write L_p for the set of palindromes over {\mathopen {}\left \{a,b\right \}\mathclose {}}. Applying Corollary [018O], we fix \ell \geq 1 and consider the string a^\ell b a^\ell , which lies in L_p and has length greater than \ell . Given a decomposition w= u_1 v u_2 with v non-empty and {\mathopen {}\left \lvert u_1v\right \rvert \mathclose {}}\leq \ell , the only possibility is that u_1 = a\ldots and v=a, and u_2 is some number of as followed by a b followed by some number of as. If we remove v we immediately go outside L by introducing an asymmetry between the number of as on the left and on the right.
Warning Limitations of the pumping lemma [018S]
- February 16, 2025
- Jon Sterling
Although the pumping lemma can be used to prove that many specific languages are not regular there nonetheless exist non-regular languages L for which there exists \ell \geq 1 satisfying the pumping property with respect to L. This means that the pumping lemma cannot be used to prove that such L are not regular. For example, the number 1 satisfies the pumping property for the following non-regular language:
L = {\mathopen {}\left \{c^m a^n b^n\,\middle \vert \,m\geq 1 \land n\geq 0\right \}\mathclose {}} \cup {\mathopen {}\left \{a^m b^n\,\middle \vert \,m,n\geq 0\right \}\mathclose {}}Proof.
- February 16, 2025
- Jon Sterling
For non-empty w=xw'\in L for x\in {\mathopen {}\left \{a,b,c\right \}\mathclose {}}, we can use the pumping bracket {\mathopen {}\left (\epsilon ,x,w'\right )\mathclose {}}. Indeed, {\mathopen {}\left \lvert \epsilon x\right \rvert \mathclose {}}={\mathopen {}\left \lvert x\right \rvert \mathclose {}}=1\geq 1, and so we need only check that x^nw'\in L for all n. We consider all the possibilities for x and w' that follow from our assumption that xw'\in L and we see that we can repeat x any number of times without departing L.
That’s the end! But you will learn more of the amazing mysteries of languages and models of computation in IB Computation Theory.