4.6.7 · D2Theory of Computation

Visual walkthrough — Pumping lemma for regular languages — proof and using to show non-regularity

2,426 words11 min readBack to topic

We assume nothing. If you have never seen a DFA, the first two steps build one for you.


Step 1 — What a machine that "reads" a string even is

WHAT. Picture a tiny robot that reads a word one letter at a time, left to right. At any moment the robot is standing in exactly one room. When it reads a letter, an arrow tells it which room to walk to next. The set of rooms is fixed and finite — the robot cannot build new rooms while it reads.

WHY. This "finite set of rooms" is the whole reason the lemma exists. The robot's only memory is which room it is standing in. If there are, say, 4 rooms, the robot can remember at most 4 different situations. It literally cannot remember "I have counted 500 letters" — that would need at least 501 different rooms. Hold onto this: finite rooms = finite memory = cannot count without bound.

PICTURE. Below: three rooms (circles), letters on the arrows. Reading the word a a b walks the robot along the highlighted path. One room is a start (thick incoming arrow); a double circle means "if you stop here, the word is accepted."

Figure — Pumping lemma for regular languages — proof and using to show non-regularity

Step 2 — Recording the robot's walk as a list of rooms

WHAT. Feed the robot a word (so is its length, the number of letters). As it reads, write down the room it stands in before the first letter, then after each letter. That gives a list of rooms.

Reading term by term:

  • — the room before reading anything; it is the start room .
  • — the -th letter of the word.
  • — the room we were in just before reading .
  • — follow the arrow labelled out of that room; where it lands is .

WHY. We want to see the walk, not the word. The proof will be about this list of rooms, because rooms are finite but the word can be as long as we like. Turning "a word" into "a sequence of rooms" is the move that lets pigeonhole strike.

PICTURE. The word a a b a written along the bottom; above each gap between letters sits the room the robot is in. Reading letters produces rooms: .

Figure — Pumping lemma for regular languages — proof and using to show non-regularity

Step 3 — More rooms visited than rooms exist (Pigeonhole)

WHAT. Let = the number of rooms, . Now take a word that is at least letters long, . Look only at the first rooms of the walk: That is entries in the list, but there are only distinct rooms in the whole building.

WHY this tool — the Pigeonhole Principle. We reach for pigeonhole because it is the exact tool that answers "if I place more items than boxes, must a box get two items?" — yes, always. Here the pigeons are the rooms in our list; the holes are the actual rooms. pigeons into holes forces two list-entries to be the same room. No other tool is needed: it is a pure counting fact.

  • — the position of the first visit to the repeated room.
  • — the position of the second (later) visit; so the two visits are genuinely different moments.
  • — both visits happen inside the first steps.

PICTURE. Nine numbered slots (the list) with only three colours available (the rooms). By slot count alone, one colour must appear twice — circled in red. That red pair is .

Figure — Pumping lemma for regular languages — proof and using to show non-regularity

Step 4 — The revisited room is a loop; name the three chunks

WHAT. Between the two visits to the same room , the robot read the letters and came right back to where it started. That is a loop. Cut the word at these two moments into three pieces:

  • — everything read before the first visit; it drives the robot .
  • — the letters read between the two visits; it drives : a loop back to the same room.
  • — everything read after the second visit; it drives (the finish).

WHY. We split exactly at the repeated room because that is where the loop lives. Naming the loop lets us talk about running it a different number of times.

PICTURE. The walk drawn as a path with a self-loop drawn around room . The segment leads in, the segment is the loop, the segment leads out to an accepting room.

Figure — Pumping lemma for regular languages — proof and using to show non-regularity

Two facts fall out immediately:

  • — the loop reads at least one letter, because . (This is condition 1.)
  • — the loop finishes within the first letters, because . (This is condition 2.)

Note also that and are allowed to be empty. If then (the loop is the very first thing read); and if then (the loop runs right up to the last letter, so there is nothing left after it). Both are perfectly legal — only is forbidden from being empty. We revisit these corners with pictures in Step 6.


WHAT. Because starts and ends at the same room , we can splice in as many copies of as we like and the robot still ends at the same finish room : So for every :

  • — the loop-word written times in a row.
  • — running the loop zero times (skip it entirely); the robot goes then straight to , still legal because it is standing at either way.
  • — the original word .
  • — extra laps of the loop.
  • — the language of accepted words from Step 1; landing in is exactly what puts into .

WHY. This is the payoff. A loop is "free repetition." Since every lap returns to , the room the robot stops in never changes — so acceptance never changes. This is exactly condition 3, and it completes the lemma.

PICTURE. Three filmstrip frames: (loop skipped), (one lap), (two laps). In all three the path enters , does its laps, exits via , and lands on the same double-circle finish.

Figure — Pumping lemma for regular languages — proof and using to show non-regularity

Step 6 — The degenerate cases (nothing is swept under the rug)

WHAT & WHY. A careful walkthrough must survive every corner case. Here they are, each pictured.

Case A — at the very front (). Then (empty) and the loop is the first thing read. Perfectly allowed: means the loop touches the start room. still holds.

Case B — the loop runs to the very end (). Then (empty) — there is nothing left after the second visit, so the finish room is the repeated room, . Still fine: gives , and pumping up appends more laps at the tail. Empty is allowed exactly like empty .

Case C — pumping down, . The robot skips the loop. We must confirm it is at to hand over to — and it is, because already brought it to . So too. Beginners forget this case; it is often the sharpest weapon (see Example 2 in the parent).

Case D — could the first rooms be all distinct? No — that would need distinct rooms, but only exist. Pigeonhole forbids this. The repeat is guaranteed, never optional.

PICTURE. Four mini-panels, one per case, each highlighting what changes (empty , empty with , skipped loop, the impossible all-distinct list crossed out in red).

Figure — Pumping lemma for regular languages — proof and using to show non-regularity

The one-picture summary

Everything above compressed into a single diagram: a long word along the bottom, its walk as a path that necessarily loops (pigeonhole), the three chunks coloured, and the loop shown running , , times — all landing on the same accepting room.

Figure — Pumping lemma for regular languages — proof and using to show non-regularity

has a DFA

read word longer than p

pigeonhole

between visits

run it i times

find a word that breaks this

Regular language

Finite rooms p

p plus 1 rooms visited

a room repeats

a loop y

x y power i z stays in L

language not regular

Recall Feynman retelling — say the whole proof in plain words

A regular language runs on a machine with a fixed number of rooms, call it ; the machine's only memory is which room it stands in. The language is just "the words this machine says yes to." Now feed it a word longer than letters. As it reads, it visits a room after each letter — more visits than rooms exist, so by simple counting (pigeonhole) it must step into some room twice. The letters it read between those two visits form a loop that starts and ends in the same room. Because a loop returns you to where you began, the machine can run that loop zero, one, or a hundred times and always finish in the very same room — so all those pumped words are accepted too. That is the whole lemma. To prove a language is not regular, you find one word where adding or removing a loop must break membership (like unbalancing the 's and 's in ); since the lemma promised the loop couldn't break anything, you have a contradiction, and no such machine can exist.

Recall In one line: which two facts about the DFA make the loop unavoidable?

The rooms are finite ( of them) and a long word forces visits — more visits than rooms, so a room repeats.

Related tools that reach the same conclusion differently: Myhill–Nerode Theorem (counts distinguishable prefixes), and note that pumping also has a cousin for grammars, the Pumping Lemma for Context-Free Languages. For what regular languages are closed under, see Closure Properties of Regular Languages.