G27
Self-checkedTier 1 13 min readtoc.pumping-lemma-regular

Pumping lemma for regular languages

The pumping lemma as a necessary condition for regularity: the statement with its quantifiers, the pigeonhole proof from a DFA with p states, the game view used to prove that a language is not regular (a^n b^n, ww, squares, primes, balanced parentheses), the standard mistakes (the lemma never proves regularity, the cut is chosen by the adversary), the pumpable-but-not-regular counterexample, and the DFA tests for emptiness and finiteness.

Why it exists

DFAs have finitely many states, so a long enough input must repeat a state, and the part of the input between the two visits can be repeated any number of times without the machine noticing. The pumping lemma turns this observation into a test: if a language has no such repeatable part, it is not regular. GATE asks it constantly: "which of these languages is not regular?" and "which step of this proof is wrong?"

Prerequisites

You need closure properties of regular languages (closure properties): many non-regularity proofs combine the pumping lemma with intersection or complement.

Precise definitions

Pumping lemma. If LL is a regular language, then there is an integer p≥1p \ge 1 (the pumping length) such that every string w∈Lw \in L with ∣w∣≥p|w| \ge p can be written as w=xyzw = xyz with

  1. ∣y∣≥1|y| \ge 1 (the pumped part is not empty),
  2. ∣xy∣≤p|xy| \le p (the pumped part lies within the first pp symbols),
  3. xyiz∈Lx y^i z \in L for every i≥0i \ge 0 (pumping up and also "pumping down" with i=0i = 0).

Quantifier structure (this is where mistakes come from):

L regular⇒∃p ∀w∈L, ∣w∣≥p  ∃x,y,z (w=xyz, ∣y∣≥1, ∣xy∣≤p, ∀i≥0: xyiz∈L).L \text{ regular} \Rightarrow \exists p \ \forall w \in L,\ |w| \ge p \ \ \exists x, y, z \ \bigl(w = xyz,\ |y| \ge 1,\ |xy| \le p,\ \forall i \ge 0:\ xy^iz \in L\bigr).

It is only a necessary condition. The lemma can prove a language is not regular; it can never prove that a language is regular.

Proof sketch. Take a DFA for LL and let pp be its number of states. Let w∈Lw \in L, ∣w∣≥p|w| \ge p. Reading the first pp symbols visits p+1p + 1 states (counting the start state), so by the pigeonhole principle some state qq occurs twice among them. Let xx be the string that leads from the start to the first visit of qq, yy the nonempty string that leads from qq back to qq, and zz the rest. Then ∣xy∣≤p|xy| \le p, ∣y∣≥1|y| \ge 1 and going round the loop ii times (or not at all) still ends in the same accepting state: xyiz∈Lx y^i z \in L.

How it works

The game

To prove LL is not regular, play against an adversary who claims LL is regular.

  1. The adversary chooses pp (you cannot choose it).
  2. You choose a string w∈Lw \in L with ∣w∣≥p|w| \ge p (it may depend on pp; choose it so that the first pp symbols are very restricted).
  3. The adversary chooses any cut w=xyzw = xyz with ∣xy∣≤p|xy| \le p and ∣y∣≥1|y| \ge 1.
  4. You choose i≥0i \ge 0 so that xyiz∉Lx y^i z \notin L.

If you have a winning strategy for every pp and every cut, LL is not regular. The table plays the game with p=5p = 5 for six languages: for each, the string you choose, the number of legal cuts (1515 when ∣w∣≥5|w| \ge 5: the pairs (∣x∣,∣y∣)(|x|, |y|) with ∣x∣+∣y∣≤5|x| + |y| \le 5, ∣y∣≥1|y| \ge 1), how many cuts survive pumping with i=0,…,9i = 0, \ldots, 9 and which ii breaks them.

The pumping lemma game with p = 5: start

Computed by a script, not typed
languagethe string s chosen (|s| ≥ p)cuts x y z with |xy| ≤ 5, |y| ≥ 1cuts that survive i = 0 ... 9 (or 11)the pumping exponent i that breaks a cut
a^n b^n
ww (a word followed by a copy of itself)
a^(n^2) (squares)
a^p, p a prime number
balanced parentheses
a^i b^j with i > j

Step 0

For each non-regular language a string s of length at least p = 5 is chosen and every legal cut into x, y, z is tried with i = 0, 1, 2, ...; a cut 'survives' if all the pumped strings stay in the language. No cut survives, which is the contradiction the proof needs.

0 / 6
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc2-traces.py

In every row no cut survives, which is the proof. Below are the arguments in words.

Standard proofs

L={anbn:n≥0}L = \{a^n b^n : n \ge 0\}. Choose w=apbpw = a^p b^p. Since ∣xy∣≤p|xy| \le p, yy consists only of aa's. Then xy0z=ap−∣y∣bpx y^0 z = a^{p - |y|} b^p has fewer aa's than bb's, so it is not in LL.

L={ww:w∈{a,b}∗}L = \{ww : w \in \{a, b\}^*\}. Choose s=apbapb=uus = a^p b a^p b = uu with u=apbu = a^p b. The cut lies inside the first block of aa's, so y=aky = a^k with k≥1k \ge 1 and xy2z=ap+kbapbx y^2 z = a^{p + k} b a^p b, of length 2p+k+22p + k + 2. If kk is odd the length is odd, so it is not of the form vvvv. If kk is even then k≥2k \ge 2, and a hypothetical vv would have length p+1+k/2≤p+kp + 1 + k/2 \le p + k, so vv would lie inside the leading block of aa's and contain no bb, while vvvv contains two bb's: contradiction. (A careless choice such as s=a2ps = a^{2p} fails: the cut y=aay = aa pumps to a2p+2j=(ap+j)2a^{2p + 2j} = (a^{p + j})^2 and always survives.)

L={an2:n≥0}L = \{a^{n^2} : n \ge 0\}. Choose w=ap2w = a^{p^2}. After pumping once, the length is p2+kp^2 + k with 1≤k≤p1 \le k \le p, strictly between p2p^2 and (p+1)2=p2+2p+1(p + 1)^2 = p^2 + 2p + 1, so it is not a perfect square.

L={aq:q prime}L = \{a^q : q \text{ prime}\}. Choose a prime q≥pq \ge p. Pump with i=q+1i = q + 1: the length is q+k⋅q=q(1+k)q + k \cdot q = q(1 + k) with k=∣y∣≥1k = |y| \ge 1, which is composite. (This works for every cut.)

Balanced parentheses. Choose w=(p)pw = (^p )^p. The cut lies inside the opening parentheses, so pumping down gives more closing than opening ones.

Palindromes. Choose w=apbapw = a^p b a^p. The cut lies inside the first block of aa's; pumping up moves the bb away from the centre.

L={an:n=2k}L = \{a^n : n = 2^k\}. Choose w=a2mw = a^{2^m} with 2m>p2^m > p; after one pump the length lies strictly between 2m2^m and 2m+12^{m + 1}.

Languages that look hard but are regular. {w∈{0,1}∗:#01(w)=#10(w)}\{w \in \{0, 1\}^* : \#01(w) = \#10(w)\} (equal numbers of occurrences of the substrings 0101 and 1010) is regular: the occurrences alternate, so the counts are equal iff the string is empty or begins and ends with the same symbol (checked for all strings up to length 1414). Do not be fooled by "equal counts".

Using it together with closure

For languages defined by counts of two symbols, intersect with a∗b∗a^* b^* first (closure properties): if LL were regular, L∩a∗b∗L \cap a^* b^* would be regular; it is {anbn}\{a^n b^n\} for #a=#b\#a = \#b. This is often shorter than the pumping argument.

The lemma is not an "if and only if"

There are non-regular languages that satisfy the pumping condition. Example:

L={aibjck:i,j,k≥0, if i=1 then j=k}.L = \{a^i b^j c^k : i, j, k \ge 0,\ \text{if } i = 1 \text{ then } j = k\}.

Every string w=aibjckw = a^i b^j c^k in LL with ∣w∣≥3|w| \ge 3 can be pumped with p=3p = 3 (verified for all strings up to length 99). The cut always has x=εx = \varepsilon: if i=0i = 0, let yy be the first symbol (the string stays of the form b∗c∗b^* c^*); if i=1i = 1, let y=ay = a (pumping gives atbjcja^t b^j c^j with t=0t = 0 or t≥2t \ge 2, which is in LL since i≠1i \ne 1, and t=1t = 1 is ww itself); if i=2i = 2, let y=aay = aa (the number of aa's becomes 0,2,4,…0, 2, 4, \ldots, never 11); if i≥3i \ge 3, let y=ay = a (the number of aa's stays at least 22). Yet LL is not regular, because L∩ab∗c∗={abjcj}L \cap a b^* c^* = \{a b^j c^j\} is not. So: pumping condition holds does not mean regular. The exact characterisation is the Myhill-Nerode theorem (Myhill-Nerode): a language is regular iff it has finitely many classes of equivalent strings. For {anbn}\{a^n b^n\} the strings a0,a1,a2,…a^0, a^1, a^2, \ldots all have different futures, so there are infinitely many classes; in the check, no DFA with 33 states agrees with anbna^n b^n on all strings up to length 88 (all 58325832 machines tried).

Useful consequences for DFAs

Let a DFA have nn states.

  • LL is non-empty iff it accepts a string of length <n< n.
  • LL is infinite iff it accepts a string of length ≥n\ge n and <2n< 2n.
  • If LL is finite and its longest string has length mm, the pumping lemma holds with p=m+1p = m + 1 (no string is long enough, so the condition is empty): every finite language is regular.

Both tests agree with a direct graph search on 300 random DFAs.

Worked examples

EX 1Find a pumping cut

Easy

L=(01)∗L = (01)^* has a minimal DFA with 33 states, so p=3p = 3. Take w=0101w = 0101 (∣w∣=4≥3|w| = 4 \ge 3). The cuts with ∣xy∣≤3|xy| \le 3 and ∣y∣≥1|y| \ge 1: 66 of them; only x=ε,y=01,z=01x = \varepsilon, y = 01, z = 01 and x=0,y=10,z=1x = 0, y = 10, z = 1 survive every ii (checked up to i=9i = 9). For example 0(10)31=010101010 (10)^3 1 = 01010101, which is in (01)∗(01)^*. A cut such as x=εx = \varepsilon, y=0y = 0, z=101z = 101 fails: y2=00y^2 = 00 gives 0010100101.

EX 2Why a^n b^n cannot be regular

Easy

A DFA with pp states reading apa^p visits p+1p + 1 states, so two of the states after aja^j and aka^k (j<k≤pj < k \le p) are equal. Then ajbja^j b^j and akbja^k b^j end in the same state, but only the first is in the language: a contradiction. (This is the pumping argument without the formal quantifiers.)

EX 3Choosing the string

Medium

Prove L={aibj:i>j}L = \{a^i b^j : i > j\} is not regular. Take w=ap+1bp∈Lw = a^{p+1} b^p \in L. The cut lies in the aa's, y=aky = a^k. Pump down: xy0z=ap+1−kbpx y^0 z = a^{p + 1 - k} b^p has p+1−k≤pp + 1 - k \le p aa's, so i≤ji \le j and it is not in LL. Pumping up would not work here (i=2i = 2 keeps i>ji > j): this is the example that needs i=0i = 0.

EX 4A wrong choice of string

Medium

To show that {ww}\{ww\} is not regular, the string s=a2p=apaps = a^{2p} = a^p a^p is in the language and long enough, but it does not work: the cut x=εx = \varepsilon, y=aay = aa, z=a2p−2z = a^{2p - 2} gives a2p+2ja^{2p + 2j}, which is (ap+j)2(a^{p + j})^2 for every j≥0j \ge 0. The adversary wins. The string must make every cut harmful: apbapba^p b a^p b has a bb in a fixed place, so pumping the leading aa's shifts it. The rule: choose a string whose first pp symbols are a single repeated symbol (so every cut is y=aky = a^k) and whose rest makes any change of that block visible.

EX 5Typical questions

GATE level

(a) Which of these is not regular: {anbm}\{a^n b^m\}, {anbn}\{a^n b^n\}, {a2n}\{a^{2n}\}, {(ab)n}\{(ab)^n\}? {anbn}\{a^n b^n\}. (b) "If LL satisfies the pumping lemma then LL is regular." False (counterexample above). (c) "If LL does not satisfy the pumping lemma then LL is not regular." True (contrapositive). (d) "The pumping length pp of a regular language is unique." False: any larger pp also works; for a language with a DFA of nn states one valid choice is p=np = n. (e) "Every finite language satisfies the pumping lemma." True, vacuously. (f) "Is {w:#01(w)=#10(w)}\{w : \#01(w) = \#10(w)\} regular?" Yes. (g) Number of ways to cut a string of length at least pp into xyzxyz with ∣xy∣≤p|xy| \le p, ∣y∣≥1|y| \ge 1: p(p+1)/2p(p + 1)/2 (1515 for p=5p = 5).

Cheat box

Cheat box

  • Regular ⇒\Rightarrow ∃p ∀w∈L,∣w∣≥p ∃xyz\exists p\ \forall w \in L, |w| \ge p\ \exists xyz: ∣y∣≥1|y| \ge 1, ∣xy∣≤p|xy| \le p, xyiz∈Lxy^iz \in L for all i≥0i \ge 0.
  • Used only to prove NON-regularity. Never proves regularity. The converse is false.
  • You choose ww (long, restricted first pp symbols); the adversary chooses pp and the cut; you choose ii (i=0i = 0 pumps down).
  • pp = number of states of a DFA. Finite languages are always regular.
  • DFA with nn states: non-empty iff a string shorter than nn is accepted; infinite iff a string of length in [n,2n)[n, 2n) is accepted.
  • Non-regular: anbna^n b^n, wwww, palindromes, an2a^{n^2}, primes, balanced parentheses, a2na^{2^n}. Regular though they look hard: equal numbers of 0101 and 1010.

Traps and GATE patterns

Connections

The same idea (a repeated state in a bounded computation) gives the pumping lemma for context-free languages with two pumped parts (pumping lemma for CFLs); the Myhill-Nerode theorem is the exact test that the pumping lemma only approximates (Myhill-Nerode); closure properties shorten many proofs (closure properties).

PYQs

11 previous year questions from your book. I re-solved each one myself (6 by running or simulating, 5 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2025 · Set 1 · MCQ · 2 marksQ6 · page 197 of the book
Consider the following two languages over the alphabet {a,
b}:
L1 = { αβα | α ∈ {a, b}+ AND β ∈ {a, b}+ }
L2 = { αβα | α ∈ {a}+ AND β ∈ {a, b}+ }
Which ONE of the following statements is CORRECT?
  • (a). Both L1 and L2 are regular languages.
  • (b). L1 is a regular language but L2 is not a regular language.
  • (c). L1 is not a regular language but L2 is a regular language.
  • (d). Neither L1 nor L2 is a regular language.
GATE 2025 · Set 2 · MSQ · 2 marksQ10 · page 198 of the book
Let Σ = {a, b, c}. For x ∈ Σ* , and α ∈ Σ, let #α(x) denote
the number of occurrences of α in x. Which one or more of
the following option(s) define(s) regular language(s)?
  • (a){ambn | m, n ≥ 0}
  • (b){a, b}* ∩ {ambn cm - n | m ≥ n ≥ 0}
  • (c){w | w ∈ {a, b}* , #a (w) ≡ 2 (mod 7), and #b (w) ≡ 3 (mod 9)}
  • (d){w | w ∈ {a, b}* , #a (w) ≡ 2 (mod 7), and #a (w) ≡ #b(w)}
GATE 2019 · MCQ · 2 marksQ26 · page 200 of the book
For Σ = {a,b}, let us consider the regular language L = {x|x
= a 2+3k or x = b 10+12k , k ≥ 0}. Which one of the following
can be a pumping length (the constant guaranteed by the
pumping lemma) for L?
  • (a)3
  • (b)9
  • (c)5
  • (d)24
GATE 2015 · Set 2 · MCQ · 2 marksQ40 · page 202 of the book
Which of the following language is/are regular
languages?

L1 : {wxwR|w,x ε {a, b}* and |w|, |x| > 0}, wR is the
reverse of string w
L2 : {anbm | m != n and m, n >= 0}
L3 : {apbqcr | p, q, r >= 0}
  • (a)L1 and L3 only
  • (b)L2 only
  • (c)L2 and L3 only
  • (d)L3 only
GATE 2014 · Set 2 · MCQQ44 · page 202 of the book
Let L1 = {w ∈ {0,1}* | w has at least as many occurrences
of (110)'s as (011)'s}. Let L2 = {w ∈ {0,1}*|w has at least
as many occurrences of (000)'s as (111)'s}. Which one of
the following is TRUE?
  • (a)L1 is regular but not L2.
  • (b)L2 is regular but not L1.
  • (c)Both L1 and L2 are regular.
  • (d)Neither L1 nor L2 are regular THEORY OF COMPUTATION
GATE 2014 · Set 1 · MCQQ46 · page 203 of the book
Which one of the following is TRUE?
  • (a)The language L = {anbn | n >= 0} is regular.
  • (b)The language L = {an | n is prime} is regular.
  • (c)The language L = {w | w has 3k + 1b's for some k ε N with Σ = {a, b} } is regular.
  • (d)The language L = (ww| wεΣ* with Σ = {0, 1} } is regular.
GATE 2008 · MCQ · 2 marksQ65 · page 207 of the book
Which of the following are regular sets?
I. { anb2m | n ≥ 0, m ≥ 0 }
II. { anbm | n = 2m }
III. { anbm | n ≠ m }
IV. { xcy | x, y ∈ {a, b}* }
  • (a)I and IV only
  • (b)I and III only
  • (c)I only
  • (d)IV only M1 THEORY OF COMPUTATION
GATE 2007 · MCQ · 2 marksQ71 · page 210 of the book
Which of the following languages is regular?
  • (a){ wwR | w ∈ {0,1}+ }
  • (b){ wwRx | x, w ∈ {0,1}+ }
  • (c){ wxwR | x, w ∈ {0,1}+ }
  • (d){ xwwR | x, w ∈ {0,1}+ }
GATE 2006 · MCQ · 2 marksQ75 · page 210 of the book
If s is a string over (0 + 1)* then let n0(s) denote the
number of 0's in s and n1(s) the number of 1's in s. Which
one of the following languages is not regular?
  • (a)L = { s ∈ (0 + 1)* | n0(s) is a 3-digit prime }
  • (b)L = {s ∈ (0 + 1)* | for every prefix s' of s, | n0(s') - n1(s') | ≤ 2 }
  • (c)L = { s ∈ (0 + 1)* | | n0(s) - n1(s) | ≤ 4 }
  • (d)L = {s ∈ (0 + 1)* | n0(s) mod 7= n1(s) mod 5= 0 }
GATE 2020 · MCQ · 2 marksQ17 · page 227 of the book
Consider the following languages.
L1 = {wxyx | w, x, y ε (0 + 1)+}
L2 = {xy | x, y ε(a + b)*, |x| = |y|, x !=y}
Which one of the following is TRUE?
  • (a)L1 is context-free but not regular and L2 is context-free.
  • (b)Neither L1 nor L2 is context-free.
  • (c)L1 is regular and L2 is context-free.
  • (d)L1 is context-free but L2 is not context-free.
year not read yetQ22 · page 244 of the book
Which of the following is true for the language
{ ap| p is prime} ?
  • (a)It is not accepted by a Turing Machine
  • (b)It is regular but not context-free
  • (c)It is context-free but not regular
  • (d)It is neither regular nor context-free, but accepted by a Turing machine

Self-check

Question 1

State the pumping lemma for regular languages. Which part of it does the adversary choose?

Show answer

Every regular LL has a pp such that each w∈Lw \in L with ∣w∣≥p|w| \ge p can be split w=xyzw = xyz with ∣y∣≥1|y| \ge 1, ∣xy∣≤p|xy| \le p and xyiz∈Lxy^iz \in L for all i≥0i \ge 0. The adversary chooses pp and the split; you choose ww and ii.

Question 2

A DFA has 77 states. What is a valid pumping length, and which test decides whether its language is infinite?

Show answer

p=7p = 7. The language is infinite iff the DFA accepts a string of length from 77 to 1313.

Question 3

Prove {an2}\{a^{n^2}\} is not regular by pumping.

Show answer

Take w=ap2w = a^{p^2}; the pumped part has length kk with 1≤k≤p1 \le k \le p; then ∣xy2z∣=p2+k|xy^2z| = p^2 + k lies strictly between p2p^2 and (p+1)2(p + 1)^2, so it is not a square.

Question 4

Is L={w∈{0,1}∗:#01(w)=#10(w)}L = \{w \in \{0,1\}^* : \#01(w) = \#10(w)\} regular?

Show answer

Yes: it is the set of strings that are empty or have equal first and last symbols, a regular language.

Question 5

How many different cuts xyzxyz with ∣xy∣≤4|xy| \le 4 and ∣y∣≥1|y| \ge 1 does a string of length at least 44 have?

Show answer

4⋅5/2=104 \cdot 5 / 2 = 10: choose ∣x∣+∣y∣≤4|x| + |y| \le 4 with ∣y∣≥1|y| \ge 1.

Revision card

One-screen revision

  • Necessary condition only; proves non-regularity, never regularity.
  • You pick ww and ii; the adversary picks pp and the cut with ∣xy∣≤p|xy| \le p, ∣y∣≥1|y| \ge 1.
  • Choose ww so the first pp symbols are one repeated symbol. Try i=0i = 0 and i=2i = 2.
  • DFA with nn states: p=np = n; empty iff nothing shorter than nn; infinite iff a string of length nn to 2n−12n - 1.
  • Non-regular: anbna^n b^n, wwww, primes, squares, palindromes; regular: equal 0101 and 1010 counts.

References

  • ·Sipser, Introduction to the Theory of Computation (3rd edition), Section 1.4 (Theorem 1.70, the pumping lemma for regular languages)
  • ·Hopcroft, Motwani, Ullman, Introduction to Automata Theory, Languages, and Computation (3rd edition), Section 4.1 (the pumping lemma for regular languages)

Verification

scripts/enum-checks/toc_regular.py: in 300 random DFAs with n states, EVERY accepted string of length at least n over {a, b} up to length 9 (125128 strings) has a cut xyz with |xy| <= n, |y| >= 1 such that x y^i z is accepted for i = 0..7; for six non-regular languages (a^n b^n, ww, a^(n^2), a^p with p prime, balanced parentheses, a^i b^j with i > j) and palindromes the chosen string has NO pumpable cut for p = 1..8; no complete 3-state DFA agrees with a^n b^n on all strings up to length 8 (all 5832 machines tried) and the strings a^0..a^6 have 7 different futures; the language {a^i b^j c^k : i = 1 implies j = k} satisfies the pumping condition with p = 3 on all strings up to length 9 (checked with i up to 6), yet its intersection with a b* c* is {a b^j c^j}; emptiness (a string shorter than n) and finiteness (a string of length between n and 2n - 1) tests agree with a graph search on 300 random DFAs; the number of strings with equally many occurrences of 01 and 10 was checked for all strings up to length 14. The pumping lemma statement and proof are standard; unary-language and other quoted facts are marked. Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (state counts, derivations, parse-tree and move counts, decomposition counts); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never