Skip to content
PostTheory Of Computation / Lecture

05-TOC-Pumping Lemma

2024-10-30
Back to Blog

TOC-Pumping Lemma ​

Pumping Lemma ​

[!abstract]+ Lemma (Pumping Lemma) If A is a regular language, then there is a positive constant p such that every string w∈A of length at least p can be written as w=xyz satisfying the following conditions

  • for each i≥0,xyiz∈A
  • |y|>0, and
  • |xy|≤p.

p is the pumping length.

Instead of proving the lemma formally, let's try to understand it.

Assume A is regular and N is the NFA which recognizes A. Let s be the number of states in N.

Assume w in A of length |w|>s.

To accept w, N goes through a sequence of states q0,q1,…,qf

By pigeonhole principle, some states qx appears multiple times in the sequence.

Assuming w=xyz, the sequence of states can be

q0,…,qx⏟to accept x,qx,…,qx⏟to accept y,…,qf⏟to accept z

Then, it's trivial that xz, xyz, xyyz, … are all in A.

The pumping lemma is used to prove a language is not regular.

Proof language is not regular ​

To prove the language A is not regular (ByContradiction)

  • Assume that A is regular.
  • Construct a string w∈A of length |w|≥p.
  • Show that no matter how w is split into xyz, there is always an i such that xyiz∉A (contradiction).

Proof Example ​

[!abstract]+ Theorem L={0n1n∣for some natural number n} is not regular.

Proof:

Assume L is regular. Let w=0p1p, which is a string in L. By the pumping lemma, w is in the form xyz. The substring y can be of the following three cases.

  1. If y only consists of 0's, then xyyz (pump y one more time) has more 0's than 1's. Thus, xyyz∉L.

  2. If y only consists of 1's, same as case 1.

  3. If y has both 0's and 1's, then 0's and 1's in xyyz are intersecting with each other, which is not in the form of 0⋯01⋯1. Thus, xyyz∉L.

In summary, w can never be split into xyz such that xyiz∈L for all i. Thus, L is not regular.

Here are some notes.

The pumping length p depends on the machine. Different machines have different pumping length.

But the pumping lemma is "machine independent". Thus you cannot assume the value of p.

The condition |y|>0 ensures that the substring being pumped is not empty. Otherwise, xyiz=xyz=xz

The condition |xy|≤p ensures that y always can be pumped by a machine.

The pumping lemma is only a necessary condition for regular. It is not a sufficient condition. In other words, if you cannot find a w or such w does not exist to accomplish the proof, you CANNOT say the language is regular.

Pumping lemma proof example ​

L3={an∣n≥2 and n is a prime number}. ​

Answer: Not regular.

Proof:

Assume L3 is regular. Consider the string ac, where c is a prime number and c>p (where p is a constant from the pumping lemma). By definition, ac∈L3.

Since prime numbers are infinite, ac is constructible for some c>p. (This point is crucial because, if ac were not constructible, the proof could not proceed.)

By the pumping lemma, ac can be split into three substrings xyz, where:

  • |xy|≤p,
  • |y|>0,
  • xyiz∈L3 for all i≥0.

Here, y consists of only a's. Assume |y|=d. Then |xz|=c−d.

Now, consider the string xyc+1z (i.e., xyiz for i=c+1):

  • Its length is |xz|+(c+1)|y|=(c−d)+(c+1)d=c(d+1).

However, c(d+1) is not a prime number, because c is a prime number, and d+1 (where d≥1) is an integer greater than 1.

Thus, xyc+1z∉L3, which contradicts the pumping lemma. Therefore, L3 is not regular.

L4={an∣n is not a prime number}. ​

Answer: Not regular.

Proof (Sketch):

We can express L4 as:

L4=Σ∗∖(L3∪{ε,a})=L3∪{ε,a}―.
  • Here, L3={an∣n is a prime number}, and {ε,a} are regular languages.
  • Regular languages are closed under set union and complement.

If L4 were regular, then L3 would also be regular (by closure properties of regular languages). However, this contradicts the result from L3, where L3 was proven to be not regular. Thus, L4 is not regular.

L6=L3∗. ​

Answer: Regular. In fact, L6=Σ∗∖{a}.

Proof:

Consider ai:

  1. Case 0: If i=0, then a0=ε∈L30.

  2. Case 1: If i is a prime, then ai∈L3.

  3. Case 2: If i is composite, then i=p1c1×p2c2×⋯×pncn, where each pj is a prime and cj is a positive integer constant.

    • Next, apjcj∈L3 for all j.
    • Thus, ai∈L3c1∘L3c2∘⋯∘L3cn.

Thus, ai∈Σ∗.

L7={anbn∣n≥1}∪{anbm∣n≥1,m≥1} ​

Answer: Regular.

  • {anbn∣n≥1}⊆{anbm∣n≥1,m≥1}.
  • {anbm∣n≥1,m≥1} is regular.

L8={anbn∣n≥1}∪{anbn+2∣n≥1} ​

Answer: Not regular.

Proof (Sketch):

  • Consider the string apbp.
  • Then, xy2z∉L8.

Pumping Lemma for Context-Free Language ​

[!abstract]+ Lemma (Pumping Lemma for Context-Free Language) If A is a context-free language, then there is a positive constant p (pumping length) such that every string s∈A of length at least p can be written as s=wvxyz satisfying the following conditions:

  • For each i≥0,wvixyiz∈A,
  • |vy|>0, and
  • |vxy|≤p

Assume L is context-free. Let s=0p1p#0p1p, which is a string in L. By the pumping lemma, s=wvxyz. Then, we have two cases.

|vy|>0|vxy|≤p

  • Case 1

vxy before the #

α=wv2xy2z has more symbols before # then after.

  • Case 2

vxy before the #

α=wv2xy2z=0..01...1#0...01...1

more 1's but less 0's