Computer Science and IT‹›⌂ 0:00 studied

Module 7 · 14 min read · Free chapter

Regular languages and the pumping lemma

In short

Regular languages are closed under union, intersection, complement, concatenation, star and reversal, but not under subsets. The pumping lemma says every long string in a regular language has a short loop that can be repeated. It can only prove that a language such as aⁿbⁿ is not regular.

What are you preparing for? We show the practice and previous year questions of your exam.

Start from zero: counting with five fingers

A dabba delivery man picks up tiffins from homes in the morning and must hand over exactly the same number at the office. Suppose he is allowed to remember things only on the five fingers of one hand. Up to five tiffins he can keep count, but what happens at the sixth?

He has to reuse a finger, so two different counts now look the same to him. If he picked up 6 tiffins and his finger says "1", he cannot tell 6 from 1. A dishonest office boy could then take one tiffin away and he would never notice.

A finite automaton is exactly this man. It has a fixed number of states, so it cannot keep an unbounded count, and languages that need such a count, such as "the same number of aa's and then bb's", are not regular. This chapter gives you two kinds of tools: closure properties to build regular languages, and the pumping lemma to prove that a language is not regular.

Why it matters. Deciding "regular or not?" is one of the most common GATE questions in this subject. If you know the tools, you can settle it in under a minute instead of guessing.

Closure properties: building new regular languages

A language is regular if some DFA, NFA or regular expression describes it. Every finite language is regular, since a regular expression can simply list its strings joined by ++. A family of languages is closed under an operation if applying the operation to members of the family always gives a member again.

Regular languages are closed under almost every operation you meet. Union, concatenation and star come free from regular expressions. Complement is just as easy: take a complete DFA and swap accepting and non-accepting states, so every string that was accepted is now rejected and the other way round.

For intersection we run two DFAs at the same time. If M1M_1 has mm states and M2M_2 has nn states, the product DFA has the pairs (p,q)(p, q) as states, which is m×nm \times n states. It moves both parts together, δ((p,q),a)=(δ1(p,a),δ2(q,a))\delta((p, q), a) = (\delta_1(p, a), \delta_2(q, a)), and it accepts when both parts accept.

Operation Closed for regular languages?
Union, intersection, difference Yes
Complement Yes, by swapping final states
Concatenation, Kleene star, reversal Yes
Homomorphism and inverse homomorphism Yes
Subset of a regular language No
Union of infinitely many regular languages No

For example, the strings with an even number of aa's need a 2-state DFA, and so do the strings with an even number of bb's. Their intersection, even aa's and even bb's, therefore has a product DFA of 2×2=42 \times 2 = 4 states, and here all 4 are needed.

Why it matters. Closure lets you prove a language regular without drawing its automaton, by building it out of simple regular pieces. It also gives a quick estimate of the number of states, which GATE often asks.

Where closure stops, and how to use it against a language

Closure does not pass down to subsets. The language a∗b∗a^* b^* is regular, but its subset {anbn∣n≥0}\{a^n b^n \mid n \ge 0\} is not. So "this language sits inside a regular one" proves nothing at all.

Closure also fails for infinite unions. Every language, even a very hard one, is the union of its single strings, and each single-string language is regular. If infinite unions kept regularity, every language would be regular, which is false.

The most useful trick runs closure backwards. Suppose you suspect that L={w∣na(w)=nb(w)}L = \{w \mid n_a(w) = n_b(w)\}, the strings with equally many aa's and bb's, is not regular. If LL were regular, then L∩a∗b∗L \cap a^* b^* would be regular too, because a∗b∗a^* b^* is regular and intersection is closed.

But that intersection is exactly {anbn∣n≥0}\{a^n b^n \mid n \ge 0\}, which we know is not regular. So LL cannot be regular. The same trick works for {ambn∣m≠n}\{a^m b^n \mid m \ne n\}, since removing it from a∗b∗a^* b^* would leave anbna^n b^n.

Why it matters. A closure argument is often a two-line proof where the pumping lemma would take a page. In multiple-choice questions it is the fastest way to reject a wrong option.

Decision properties: questions a machine can answer

A decision property is a yes-or-no question about a language, given as a DFA or a regular expression. For regular languages, every natural question has an algorithm that always finishes. This is a big contrast with context-free languages and Turing machines, where many such questions have no algorithm at all.

Membership asks whether w∈Lw \in L, and you simply run the DFA on ww in O(∣w∣)O(\lvert w \rvert) steps. Emptiness asks whether L=∅L = \emptyset, and you check whether any accepting state is reachable from the start state by a graph search. Equivalently, a DFA with nn states accepts something if and only if it accepts some string shorter than nn.

Finiteness asks whether LL is finite. The language is infinite exactly when some cycle lies on a path from the start state to an accepting state, because that cycle can be repeated. In terms of lengths, a DFA with nn states accepts infinitely many strings if and only if it accepts some ww with n≤∣w∣<2nn \le \lvert w \rvert < 2n.

Equivalence asks whether L1=L2L_1 = L_2. One way is to minimise both DFAs and compare them, since the minimal DFA is unique. Another way is to test whether (L1−L2)∪(L2−L1)(L_1 - L_2) \cup (L_2 - L_1) is empty, using closure and the emptiness test.

Why it matters. Tools that check hardware designs and network rules depend on these algorithms, because they can prove two descriptions identical. In GATE, "which of these problems is decidable" questions usually have every regular-language problem marked as decidable.

The Myhill–Nerode idea: how many things must be remembered?

Go back to the delivery man and his fingers. The real question is: how many different situations must a machine keep apart? Two inputs xx and yy are the same situation for a language LL if no future can tell them apart, that is, for every string zz, xz∈Lxz \in L exactly when yz∈Lyz \in L.

We write this as x≡Lyx \equiv_L y, and it splits all strings into classes. The Myhill–Nerode theorem says that LL is regular if and only if there are finitely many classes. Moreover, the number of classes equals the number of states in the minimal DFA for LL.

For strings over {a,b}\{a, b\} ending in abab, there are exactly three classes: strings ending in aa, strings ending in abab, and all the rest. That matches the three-state minimal DFA. Now take L={anbn∣n≥0}L = \{a^n b^n \mid n \ge 0\} and the strings aia^i and aja^j with i≠ji \ne j.

The future z=biz = b^i puts aibia^i b^i in LL but leaves ajbia^j b^i outside it, so aia^i and aja^j are in different classes. Since this holds for every pair, there are infinitely many classes, and LL is not regular. This is the cleanest proof of all, and it also explains why the language fails: the machine would need a new state for every count.

Why it matters. Myhill–Nerode gives a lower bound on states: if you find kk strings that are pairwise distinguishable, every DFA needs at least kk states. GATE state-counting questions can be checked this way.

The pumping lemma and two full proofs

Suppose a DFA with pp states reads a string of length at least pp. While reading the first pp symbols it visits p+1p + 1 states, so by the pigeonhole principle some state repeats. The symbols read between the two visits form a loop, and a loop can be taken zero times, once, twice or any number of times.

That observation is the pumping lemma. If LL is regular, there is a number p≥1p \ge 1 such that every w∈Lw \in L with ∣w∣≥p\lvert w \rvert \ge p can be split as w=xyzw = xyz, where:

∣xy∣≤p,∣y∣≥1,xyiz∈L for every i≥0\lvert xy \rvert \le p, \qquad \lvert y \rvert \ge 1, \qquad xy^i z \in L \text{ for every } i \ge 0

The lemma is a necessary condition, not a sufficient one. Some non-regular languages also satisfy it, so it can never prove that a language is regular. We use it only in contrapositive form: if no split works for some long string, the language is not regular.

Treat a proof as a game against an opponent. The opponent picks pp, you pick a string w∈Lw \in L with ∣w∣≥p\lvert w \rvert \ge p, the opponent picks any split xyzxyz that obeys the rules, and you pick an ii that throws the string out of LL. You must win against every split, not just one.

Proof 1: {anbn∣n≥0}\{a^n b^n \mid n \ge 0\} is not regular. Assume it is regular with pumping length pp. Choose w=apbpw = a^p b^p, which is in LL and has length 2p≥p2p \ge p. Because ∣xy∣≤p\lvert xy \rvert \le p, both xx and yy lie inside the block of aa's:

x=aj,y=ak,z=ap−j−kbp,k≥1xy2z=aja2kap−j−kbp=ap+kbp\begin{aligned} x &= a^j, \quad y = a^k, \quad z = a^{p-j-k} b^p, \quad k \ge 1 \\ xy^2 z &= a^{j} a^{2k} a^{p-j-k} b^p = a^{p+k} b^p \end{aligned}

Since k≥1k \ge 1, the pumped string has more aa's than bb's, so xy2z∉Lxy^2 z \notin L. This contradicts the lemma, so LL is not regular.

Proof 2: {an2∣n≥0}\{a^{n^2} \mid n \ge 0\} is not regular. Assume it is regular with pumping length pp and choose w=ap2w = a^{p^2}. Any split has y=aky = a^k with 1≤k≤p1 \le k \le p, so pumping once more gives length p2+kp^2 + k:

p2<p2+k≤p2+p<p2+2p+1=(p+1)2p^2 < p^2 + k \le p^2 + p < p^2 + 2p + 1 = (p + 1)^2

The new length lies strictly between two neighbouring perfect squares, so it is not a perfect square, and xy2z∉Lxy^2 z \notin L. Hence the language is not regular.

Why it matters. Descriptive papers ask for exactly these proofs, and marks go to the correct choice of ww and the handling of every split. In GATE, the same reasoning helps you spot quickly which options need unbounded counting.

Regular or not: the common traps

The quick test is this: does the machine need to compare two unbounded quantities? If yes, the language is usually not regular. If it only needs a count modulo a fixed number, a fixed-length window, or independent pieces, it is regular.

Language Regular? Reason
{ambn∣m,n≥0}\{a^m b^n \mid m, n \ge 0\} Yes It is just a∗b∗a^* b^*, with no matching.
{anbn∣n≥0}\{a^n b^n \mid n \ge 0\} No The two counts must match without bound.
{anbn∣n≤100}\{a^n b^n \mid n \le 100\} Yes It is a finite language.
{a2n∣n≥0}\{a^{2n} \mid n \ge 0\} Yes Only the parity of the length matters.
{an2∣n≥0}\{a^{n^2} \mid n \ge 0\} No Gaps between squares keep growing.
{ap∣p prime}\{a^p \mid p \text{ prime}\} No Pumping always reaches a composite length.
{ww∣w∈{a,b}∗}\{ww \mid w \in \{a, b\}^*\} No The second half must copy the first.
{wwR∣w∈{a,b}∗}\{ww^R \mid w \in \{a, b\}^*\} No Even palindromes need unbounded memory.
{w∣na(w)≡nb(w)(mod3)}\{w \mid n_a(w) \equiv n_b(w) \pmod{3}\} Yes The difference is tracked modulo 3.
Equal numbers of abab and baba substrings Yes This holds exactly when the first and last symbols agree.
{xwxR∣x,w∈{a,b}+}\{xwx^R \mid x, w \in \{a, b\}^+\} Yes Taking xx as one symbol, it is strings of length at least 3 with equal ends.
{ambn∣m≠n}\{a^m b^n \mid m \ne n\} No Its complement inside a∗b∗a^* b^* is anbna^n b^n.

The last two regular rows are favourite traps. The language xwxRxwx^R looks like a palindrome, but because xx may be a single symbol and ww may be anything, it only checks that the first and last symbols agree. Likewise, "as many abab as baba" sounds like counting, yet each switch from aa to bb must be followed by a switch back unless the string ends in a different symbol.

Why it matters. GATE options are designed to look non-regular while being regular, and the other way round. Asking "what exactly must be remembered?" before answering protects you from both.

Descriptive lens: how written papers ask this

Written papers ask about regular languages in four ways:

  1. State the pumping lemma precisely and explain why it cannot prove that a language is regular.
  2. Prove that a given language is not regular, by the pumping lemma, by Myhill–Nerode or by closure.
  3. Construct an automaton for a combination of languages, such as an intersection, and give the number of states.
  4. Explain an algorithm for a decision property, such as emptiness or finiteness.

Practice questions (our own, not previous-year questions).

  1. State the pumping lemma for regular languages. Use it to prove that {anbn∣n≥0}\{a^n b^n \mid n \ge 0\} is not regular. (10 marks)
  2. Show that regular languages are closed under intersection and complement. Hence prove that {w∣na(w)=nb(w)}\{w \mid n_a(w) = n_b(w)\} is not regular. (10 marks)
  3. State the Myhill–Nerode theorem and use it to find the number of states in the minimal DFA for strings over {a,b}\{a, b\} that end in abab. (15 marks)

Answer plan for question 1.

  • State the lemma with all three conditions, ∣xy∣≤p\lvert xy \rvert \le p, ∣y∣≥1\lvert y \rvert \ge 1 and xyiz∈Lxy^i z \in L for every i≥0i \ge 0.
  • Assume for contradiction that the language is regular, and let pp be its pumping length.
  • Choose w=apbpw = a^p b^p, and note that it is in the language and has length 2p2p.
  • Argue that every allowed split puts y=aky = a^k with k≥1k \ge 1 inside the first pp symbols.
  • Pump with i=2i = 2 to get ap+kbpa^{p+k} b^p, and show that it is outside the language.
  • Conclude that the assumption is false, so the language is not regular.

Quick revision

  • Every finite language is regular.
  • Regular languages are closed under union, intersection, complement, difference, concatenation, star, reversal and homomorphisms.
  • The product DFA for intersection has m×nm \times n states, and complement swaps final states of a complete DFA.
  • Subsets and infinite unions of regular languages need not be regular.
  • To prove LL non-regular by closure, intersect it with a regular language and reach a known non-regular one.
  • Membership, emptiness, finiteness and equivalence are all decidable for regular languages.
  • A DFA with nn states accepts an infinite language exactly when it accepts some ww with n≤∣w∣<2nn \le \lvert w \rvert < 2n.
  • Myhill–Nerode classes equal the states of the minimal DFA, and infinitely many classes mean non-regular.
  • The pumping lemma needs ∣xy∣≤p\lvert xy \rvert \le p and ∣y∣≥1\lvert y \rvert \ge 1, and it can only prove non-regularity.
  • anbna^n b^n, wwww, palindromes, an2a^{n^2} and apa^p with pp prime are not regular.

Check yourself

Exam-style questions on this chapter. Pick an answer to see the explanation. 0 of 15 answered

  1. Which statement is TRUE?

  2. DFAs M1M_1 and M2M_2 have 3 and 4 states. The product DFA for L(M1)∩L(M2)L(M_1) \cap L(M_2) has at most:

  3. Regular languages are NOT closed under which operation?

  4. If L∩a∗b∗={anbn∣n≥0}L \cap a^* b^* = \{a^n b^n \mid n \ge 0\}, then LL is:

  5. A DFA has 5 states. Its language is infinite if and only if it accepts some string ww with:

  6. Which problem is decidable for regular languages given as DFAs?

  7. By Myhill–Nerode, the number of equivalence classes of ≡L\equiv_L for a regular LL equals:

  8. For L={anbn∣n≥0}L = \{a^n b^n \mid n \ge 0\}, which suffix distinguishes a2a^2 from a3a^3?

  9. In the pumping lemma for regular languages, which condition is required?

  10. A language satisfies the pumping lemma for regular languages. Then it is:

  11. To show {anbn∣n≥0}\{a^n b^n \mid n \ge 0\} is not regular, with w=apbpw = a^p b^p, pumping i=2i = 2 gives:

  12. Which language is regular?

  13. L={xwxR∣x,w∈{a,b}+}L = \{xwx^R \mid x, w \in \{a, b\}^+\} is:

  14. Which language is NOT regular?

  15. In the GATE CS syllabus, the pumping lemma is listed with:

Previous year questions

Previous year questions for UPSC CSE will appear here once each one is checked word for word against the official question paper.

Learn with AI

A tutor that asks instead of telling, a partner that argues the other side, and this chapter told through your own district.

Tell the tutor what you are stuck on. It will guide you with questions and give the answer after a few tries.

🙋 Raise a doubt or feedback

Stuck somewhere, or spotted a mistake? Write to the ExamHike team. We usually reply within a day, and you will see the reply on My progress.

Log in to ask a doubt or send feedback →

Frequently asked questions

Can the pumping lemma prove that a language is regular?

No. It is a necessary condition only, so it is used to prove that a language is not regular.

Why is aⁿbⁿ not regular?

A finite automaton cannot count the a's without bound, and pumping aᵖbᵖ adds a's without adding b's.

Is a subset of a regular language always regular?

No. aⁿbⁿ is a subset of the regular language a*b*, but it is not regular.

Which problems are decidable for regular languages?

Membership, emptiness, finiteness, equivalence and subset are all decidable for regular languages.

More in this module: Theory of computation

Official sources

Facts in this chapter were checked against these official sources.

  1. GATE 2026 Computer Science and Information Technology (CS) syllabus, IIT Guwahati
  2. NPTEL Theory of Computation (IIT Kanpur, 106104148): closure and decision properties, Myhill-Nerode theorem, pumping lemma