MIT 18.404 — Lecture 3

Regular Expressions, The Pumping Lemma

§ Generalized NFAs

Recall that we proved in Lecture 2 the following theorem by introducing a new kind of FA (the NFA).

Theorem. (Regularness) Every regular expression is a regular language.

In this lecture, we'll introduce another kind of FA—the generalized NFA (GNFA)—to prove the reverse direction.

Theorem. (Regularness) Every regular language is a regular expression.

Definition. A generalized NFA is an NFA in which edges may be labeled with any regular expression (not just atoms!). We say a GNFA is good if it satisfies the following properties:

Evidently, every GNFA can be easily translated into a good GNFA by adding ∅\emptyset edges and some additional states.

Expectedly, good GNFAs are equally as representative of regular languages as ordinary NFAs (or DFAs) are.

Theorem. (GNFA Implies NFA) A language AA is recognized by some NFA if and only if AA is recognized by some GNFA.

Proof: The NFA   ⟹  \implies GNFA direction is trivial, so we only show the GNFA   ⟹  \implies NFA direction.

Simply apply the fact from Lecture 2 that every regular expression is the language of some NFA. That is, for each edge with regular expression rr, replace that edge with the NFA whose language is rr, linking into and out of the NFA using ϵ\epsilon-edges.

If this new NFA accepts a string ss, then the original GNFA must also accept ss; to see why, consider breaking up the computation path of ss into sub-paths p→qp \to q where pp and qq are states of the original GNFA, and so…   ■\blacksquare

§ Regular Languages are Regular Expressions

We now prove the following theorem.

Theorem. (GNFA Regularness) Every good GNFA regular language is a regular expression.

Proof: Proof by induction on the number of states. The base case is trivial.

For the inductive step, remove a state XX from the GNFA GG that is not the start or accept state.

Furthermore, for every ordered pair of states (p,q)∈(Q∖{X})2(p, q) \in (Q \setminus \{X\})^2, update the label of the edge p→qp \to q as shown above. This leaves the language of GG unchanged, so we can induct down successfully.   ■\blacksquare

Remark. CYU: Where, in the proof, does the “goodness” of the GNFA get used?

And of course, by combining this theorem with the “GNFA Implies NFA” theorem preceding it, we get:

Theorem. (DFA Regularness) Every (DFA) regular language is a regular expression.

§ Proving Nonregularity

All of our work so far has gone toward proving the regularity of languages. What about nonregularity?

Example. Consider the following languages with alphabet Σ={0,1}\Sigma = \{\str{0}, \str{1}\}.

B:={w∣w has as many 0s as 1s}   ∥   C:={w∣w has as many 01s as 10s}B := \{w \mid w \text{ has as many } \str{0}\text{s} \text{ as } \str{1}\text{s}\} ~~~ \parallel ~~~ C := \{w \mid w \text{ has as many } \str{01}\text{s} \text{ as } \str{10}\text{s}\}

As we'll see, BB is nonregular. But CC is regular—consider a five-state FA that tracks (First Symbol,Last Symbol)(\textsc{First Symbol}, \textsc{Last Symbol}).

Theorem. (Pumping Lemma) For every regular language AA, there must be some pumping length pp such that:

For all s∈As \in A with ∣s∣≥p|s| \geq p, we may write s=xyzs = xyz with y≠ϵy \neq \epsilon such that ∣xy∣≤p|xy| \leq p and xyiz∈Axy^iz \in A for all i∈Ni \in \mathbb{N}.

Proof: The pumping length of A=L(M)A = L(M) is the number of states in the FA MM.

Consider some string s∈As \in A with ∣s∣≥p|s| \geq p. Suppose it begins at SS and ends at TT through some path P\mathcal{P}. Since ∣s∣≥p|s| \geq p, the path P\mathcal{P} must land on some state twice. Say this happens for the first time at state LL.

Then taking (x,y,z):=(S→L,L→L,L→T)(x, y, z) := (S \to L, L \to L, L \to T) just works.   ■\blacksquare

Remark. All finite languages are regular. And this is trivially consistent with the pumping lemma—why?

Problem. Prove that the language D:={0k1k∣k≥0}D := \{\str{0}^k \str{1}^k \mid k \geq 0\} is not regular.

Solution: Argue by contradiction. If the pumping length were pp, then s=0p1p∈Ds = \str{0}^p\str{1}^p \in D would contradict the pumping lemma.   ■\blacksquare

Problem. Prove that the language B:={w∣w has as many 0s as 1s}B := \{w \mid w \text{ has as many } \str{0}\text{s} \text{ as } \str{1}\text{s}\} is not regular.

Solution: The point is that if BB is regular, then so is D=B∩0∗1∗D = B \cap \str{0}^*\str{1}^* (by closure). Contradiction by the previous problem.   ■\blacksquare

Unfortunately, there exist nonregular languages that satisfy the pumping lemma; take 11∗D∪0∗1∗\str{1}\str{1}^*D \cup \str{0}^*\str{1}^* with pumping length 11, say.