knowledge

Theory of Computation: Lecture 3

Non-regular languages, the pigeonhole argument, the pumping lemma as an adversary game, closure-based reductions, and a regular surprise.

The Edge of Finite Memory

Every NFA has an equivalent DFA, so DFAs, NFAs, and all the closure constructions draw one and the same boundary: the regular languages. What sits comfortably inside that boundary:

  • Pattern matching. Fixed substrings, such as strings containing 1011011: one waiting state per matched prefix.
  • Modular counting. Parity, divisibility, anything counted "modulo some number", such as strings whose number of 1 symbols is divisible by 5.
  • Bounded position information. Fixed prefixes, suffixes, or positions, such as strings whose second-to-last bit is 1.
  • Pattern shapes. Languages like 0110={0n110mn,m0}0^*110^*=\{0^n110^m\mid n,m\ge 0\}, and anything assembled from regular pieces by complement, union, intersection, and concatenation.

The common thread is that finitely many states suffice to summarize everything relevant about the prefix read so far. The limit appears when a language needs exact unbounded memory: a finite automaton can count modulo a fixed number, but it cannot remember an arbitrary number exactly.

Non-regular language. A language that no DFA, and equivalently no NFA, recognizes.

Proving non-regularity is a statement about every machine at once. It is not enough that the language feels hard or that one design fails; the proof must show that every possible finite automaton is wrong somewhere. Three test languages calibrate where the boundary runs: BB, the strings 0n1n0^n1^n; CC, the strings with as many 0s as 1s; and DD, the strings with as many occurrences of 01 as of 10 as substrings. Which of the three are regular? The intuition "anything that counts is non-regular" misleads for exactly one of them.

The First Non-Regular Language

Start with

B={0n1nn0}={ϵ,01,0011,000111,}.B=\{0^n1^n\mid n\ge 0\} =\{\epsilon,01,0011,000111,\ldots\}.

The number of 1 symbols must match exactly the number of earlier 0 symbols, and the number to remember is unbounded. That intuition becomes a proof through the pigeonhole principle: a DFA with pp states cannot keep p+1p+1 different prefixes apart.

Theorem. BB is not regular.

1. Assumption. Suppose, for contradiction, that a DFA MM with pp states recognizes BB.

2. Repeated state. Feed MM the p+1p+1 prefixes ϵ,0,00,,0p\epsilon,0,00,\ldots,0^p. They land in at most pp states, so by the pigeonhole principle there are i<ji<j such that 0i0^i and 0j0^j end in the same state.

3. Same suffix. A DFA is deterministic, so from one state the same suffix always produces the same verdict. Append 1i1^i to both prefixes: since 0i1iB0^i1^i\in B the machine accepts it, and therefore it must also accept 0j1i0^j1^i.

4. Contradiction. Because j>ij>i, the string 0j1i0^j1^i has more 0s than 1s, so it is not in BB. Thus MM accepts a string outside BB, contradicting L(M)=BL(M)=B.

Once two different prefixes share a state, the machine has forgotten their difference forever, and no suffix can restore the lost information.

The argument never used anything specific to BB except at the very end. Long accepted strings force a repeated state, and the input read between the two visits can be repeated or removed freely. Packaging that observation once and for all gives the central tool of the lecture.

The Pumping Lemma

Pumping lemma for regular languages. If AA is regular, then there exists a number pp, the pumping length, such that every string sAs\in A with sp|s|\ge p can be written as s=xyzs=xyz with:

  1. xyizAxy^iz\in A for every i0i\ge 0,
  2. y1|y|\ge 1,
  3. xyp|xy|\le p.
PartMeaning
xxinput read before entering the repeated state
yynonempty input read while going around the loop
zzremaining suffix after the loop
xyizxy^izthe string obtained by taking the loop ii times

With i=0i=0 the loop is removed and the string becomes xzxz; with i=2i=2 the loop is taken twice and the string becomes xyyzxyyz. A pumping length is not unique: if pp works, any larger number works. And if AA is finite, the lemma holds vacuously, because choosing pp longer than every string in AA leaves no string with sp|s|\ge p.

Proof. Choose a DFA M=(Q,Σ,δ,q0,F)M=(Q,\Sigma,\delta,q_0,F) with L(M)=AL(M)=A and let p=Qp=|Q|. Take any sAs\in A with sp|s|\ge p. Reading the first pp symbols visits p+1p+1 state positions, the start state plus one per symbol, so some state qq repeats; say its first two visits happen at times j<kpj<k\le p. Split ss into xx, the first jj symbols, yy, the symbols from j+1j+1 through kk, and zz, the rest. Then yy is nonempty and xy=kp|xy|=k\le p, and the accepting run has the shape

q0  x  q  y  q  z  rF.q_0 \xrightarrow{\;x\;} q \xrightarrow{\;y\;} q \xrightarrow{\;z\;} r\in F.

Reading yy returns the machine to qq, so the loop can be taken any number of times without changing where the run ends:

q0  x  q  yi  q  z  rF,q_0 \xrightarrow{\;x\;} q \xrightarrow{\;y^i\;} q \xrightarrow{\;z\;} r\in F,

so xyizAxy^iz\in A for every i0i\ge 0.

The pumping lemma is the pigeonhole principle applied to a DFA run, stated once so that each non-regularity proof does not have to reopen the machine.

Proving Non-Regularity

The lemma gives a necessary condition for regularity, so it is used by contradiction: exhibit a string that cannot be pumped. The proof is best played as a game against an adversary who defends the (impossible) DFA.

Recipe: non-regularity via the pumping lemma.

  1. Assume LL is regular. The adversary announces a pumping length pp. You do not know pp; treat it as a parameter.
  2. Choose a witness. Pick one string sLs\in L with sp|s|\ge p, phrased in terms of pp. This is the creative step.
  3. Face every split. The adversary picks the decomposition s=xyzs=xyz with xyp|xy|\le p and y1|y|\ge 1. Your argument must work for every such split. Use xyp|xy|\le p to pin down what yy can look like; with the right witness, the constraint forces yy into one uniform block.
  4. Pump. Choose one i0i\ge 0, often i=2i=2 or i=0i=0, and show xyizLxy^iz\notin L.
  5. Conclude. The lemma is violated, so LL is not regular. If instead some split survives every ii, the witness was too weak; return to step 2 with a stronger string.

You choose the string and the pump count; the adversary chooses the length and the split. A proof is a winning strategy against every split, not one lucky play.

Example 1. The copy language.

Let F={www{0,1}}F=\{ww\mid w\in\{0,1\}^*\}: strings made by writing the same string twice. In FF: ϵ\epsilon, 00, 0101, 001001. Not in FF: 01, 001, 0110.

Assume FF is regular with pumping length pp, and pick the witness

s=0p1p0p1p=ww for w=0p1p.s=0^p1^p0^p1^p=ww \text{ for } w=0^p1^p.

Every split s=xyzs=xyz with xyp|xy|\le p puts xx and yy inside the first block of zeros, so y=0ky=0^k with k>0k>0. Pump up once:

xy2z=0p+k1p0p1p.xy^2z=0^{p+k}1^p0^p1^p.

If kk is odd, the length is odd and the string cannot be wwww. If k=2hk=2h is even, the midpoint moves into the first block of 1s, so the first half starts with 0 and the second half starts with 1, and the halves differ. Either way xy2zFxy^2z\notin F, contradiction: FF is not regular.

The witness did the work. Choosing s=0p1p0p1ps=0^p1^p0^p1^p forces every legal yy into the first zero block, so pumping damages only the first copy.

Example 2. More zeros than ones.

Let E={0m1nm>n}E=\{0^m1^n\mid m>n\}.

Assume EE is regular with pumping length pp, and pick s=0p+11pEs=0^{p+1}1^p\in E. Every legal split has y=0ky=0^k with k>0k>0. Pumping up only adds zeros and stays in EE; the contradiction comes from pumping down with i=0i=0:

xy0z=xz=0p+1k1p.xy^0z=xz=0^{p+1-k}1^p.

Since k1k\ge 1, the string has at most pp zeros against pp ones, so xzExz\notin E. Contradiction: EE is not regular. The useful pump count is not always up.

Example 3. Equal zeros and ones.

Let C={w{0,1}w has as many 0s as 1s}C=\{w\in\{0,1\}^*\mid w\text{ has as many }0\text{s as }1\text{s}\}.

A first witness attempt fails instructively. The string s=(01)ps=(01)^p is in CC and long enough, but the adversary answers with the split x=ϵx=\epsilon, y=01y=01: then xyiz=(01)p+i1xy^iz=(01)^{p+i-1} stays in CC for every ii, and that single surviving split destroys the whole attempt. A witness must lose to no split.

Pick instead s=0p1ps=0^p1^p. Now every legal split has y=0ky=0^k with k>0k>0, and pumping up gives

xy2z=0p+k1pC.xy^2z=0^{p+k}1^p\notin C.

Contradiction: CC is not regular.

Non-Regularity by Closure

Knowing one non-regular language unlocks others without touching the pumping lemma again: reduce the new language to a known one through closure operations.

Containment alone proves nothing. BCB\subseteq C and BB non-regular does not make CC non-regular, just as B{0,1}B\subseteq\{0,1\}^* does not make {0,1}\{0,1\}^* non-regular. The operation must transform the whole language, and intersection with a regular filter is the workhorse.

Recipe: non-regularity by closure reduction.

  1. Assume LL is regular.
  2. Combine LL with known regular languages using operations under which regular languages are closed: intersection with a regular filter, complement, union.
  3. Identify the result as a language already known to be non-regular.
  4. Conclude. Closure would force that language to be regular, a contradiction, so LL is not regular.

For CC, filter with R=01={0m1nm,n0}R=0^*1^*=\{0^m1^n\mid m,n\ge 0\}, which is regular: a three-state DFA checks that no 0 follows a 1. If CC were regular then CRC\cap R would be regular, because regular languages are closed under intersection. But

CR={0n1nn0}=B,C\cap R=\{0^n1^n\mid n\ge 0\}=B,

and BB is not regular. Contradiction: CC is not regular, now proved a second way.

A closure reduction says: if this language were regular, a known non-regular language would become regular after a harmless operation. The filter strips away everything except the hard core.

A Regular Surprise

The third test language counts, yet it is regular:

D={w{0,1}w has as many occurrences of 01 as of 10 as substrings}.D=\{w\in\{0,1\}^*\mid w\text{ has as many occurrences of }01\text{ as of }10\text{ as substrings}\}.

An occurrence here is an adjacent pair of symbols, so the counts track changes from 0 to 1 and from 1 to 0, not symbol totals.

Boundary-change characterization. A binary string has equally many 01 and 10 substrings if and only if it is empty or its first and last symbols are equal.

Scanning left to right, the changes alternate: after a 0 to 1 change, the next change can only be 1 to 0, and conversely. The counts therefore differ by at most one, and they are equal exactly when the string ends on the same symbol value it started with. A string starting with 0 and ending with 1 has one extra 01; starting with 1 and ending with 0, one extra 10.

A DFA for DD needs to remember only the first symbol and the most recent symbol:

0101011010q0q1q3q2q4
A DFA for strings with equal numbers of 01 and 10 substrings.
StateMeaning
q0q_0no symbol has been read
q1q_1first symbol 0, last symbol 0
q3q_3first symbol 0, last symbol 1
q2q_2first symbol 1, last symbol 1
q4q_4first symbol 1, last symbol 0

The accepting states q0q_0, q1q_1, q2q_2 are exactly the cases where the string is empty or its first and last symbols agree.

Accepted: ϵ\epsilon, 0, 1, 00, 11, 010, 101. Rejected: 01, 10, 001, 110.

DD looks like it compares two unbounded counts, but the difference between the counts is fully determined by the first and last symbols, and that is two bits of memory. Regularity is about how much information must be carried, not about how the language is phrased; whenever a counting language is regular, some bounded quantity secretly carries the count. Past this boundary lies computation with unbounded memory, Turing machines, where both the powers and the limits change shape.