Discrete MathematicsLanguages, Automata and Computability4 of 6
Languages, Automata and Computability · 4

Limits of Finite Memory: Non Regular Languages

A finite automaton can check patterns like whether a string has an even number of zeros using only two circles, but we need a way to tell when a pattern requires more memory than any finite machine can hold.

Even\text{Even}
Odd\text{Odd}
11
11
00
00
0zeros\begin{gathered}0\\[-4pt]\text{zeros}\end{gathered}
1zero\begin{gathered}1\\[-4pt]\text{zero}\end{gathered}
2zeros\begin{gathered}2\\[-4pt]\text{zeros}\end{gathered}
3zeros\begin{gathered}3\\[-4pt]\text{zeros}\end{gathered}
00
00
0\htmlClass{lc-orange}{0}
0\htmlClass{lc-orange}{0}
?\htmlClass{lc-orange}{?}
Why Finite Memory Cannot Count Without Bound
0    1    1    0    0    10\;\;1\;\;1\;\;0\;\;0\;\;1
11
00
00
11
00
11
1

Proving a Language Is Not Regular: The Pumping Lemma

We want to prove that no finite automaton can recognize the language of strings made of some number of zeros followed by the exact same number of ones:

0n1n\displaystyle 0^n 1^n =00…0⏟n zeros11…1⏟n ones\displaystyle = \underbrace{00\dots 0}_{n\text{ zeros}}\underbrace{11\dots 1}_{n\text{ ones}}

Our tool relies on the Pigeonhole Principle: if a machine has pp circles in total, then reading pp characters takes pp steps and visits p+1\visits{p + 1} circles (counting the starting circle before the first step), which forces the path to land on at least one circle twice and form a loop.

Forcing a Loop: The Pumping Lemma
recognizes 0n1n\text{recognizes }0^n 1^n
0    0    0    0    1    1    1    10\;\;0\;\;0\;\;0\;\;1\;\;1\;\;1\;\;1
4 circles\text{4 circles}
x\htmlClass{lc-blue}{x}
y\htmlClass{lc-pink}{y}
z\htmlClass{lc-violet}{z}
Accept\text{Accept}

Whenever a machine with pp circles (where pp is called the pumping length) accepts a string ss of length at least pp, the first loop splits the string into three pieces:

s\displaystyle s =x⏟path to loop y⏟loop z⏟path to Accept\displaystyle = \xpart{\underbrace{x}_{\text{path to loop}}}\,\ypart{\underbrace{y}_{\text{loop}}}\,\zpart{\underbrace{z}_{\text{path to Accept}}}

This split always satisfies the three rules of the Pumping Lemma:

∣xy∣≤p(the loop happens withinthe first p steps)∣y∣>0(the loop contains atleast one character)xyiz is accepted for all i≥0(repeating or skipping theloop still ends on Accept)\displaystyle \begin{aligned} |\xpart{x}\ypart{y}| &\le p \\ &\quad \text{(the loop happens within} \\ &\quad \text{the first } p \text{ steps)} \\[8pt] |\ypart{y}| &> 0 \\ &\quad \text{(the loop contains at} \\ &\quad \text{least one character)} \\[8pt] \xpart{x}\ypart{y}^i \zpart{z} &\text{ is }\accepted{\text{accepted}}\text{ for all } i \ge 0 \\ &\quad \text{(repeating or skipping the} \\ &\quad \text{loop still ends on Accept)} \end{aligned}

Here is how those three rules write out the proof that 0n1n0^n 1^n is not regular. (Remember that you do not know how the machine is wired, so write the loop length as an unknown k\ypart{k} with 1≤k≤p1 \le \ypart{k} \le p rather than picking a fixed number like 22.)

Text solution
Pick a string of length at least ppFor a machine with pp circles, we feed it pp zeros followed by pp ones, which belongs to the language:
s\displaystyle s =0p1p\displaystyle = 0^p 1^p
Locate the loop within the first pp stepsSince ∣xy∣≤p|\xpart{x}\ypart{y}| \le p and ∣y∣>0|\ypart{y}| > 0, the loop y\ypart{y} happens entirely inside the first pp zeros and consists of k\ypart{k} zeros:
y\displaystyle \ypart{y} =0kfor some 1\displaystyle = \ypart{0^k} \quad \text{for some } 1 ≤k\displaystyle \le \ypart{k} ≤p\displaystyle \le p
Repeat the loop (i=2i = 2) to break the patternGoing around the loop twice adds k≥1\ypart{k} \ge 1 extra zeros without changing the pp ones, producing an accepted string with unequal counts:
xy2z\displaystyle \xpart{x}\ypart{y}^2 \zpart{z} =0p+k1p(p+k≠p)\displaystyle = 0^{p+\ypart{k}} 1^p \quad (\breaks{p + \ypart{k} \ne p})

In a 4-circle machine reading 0000111100001111, no matter which earlier circle the path loops back into during the first four steps, the loop y\ypart{y} contains only zeros, so skipping it (xz\xpart{x}\zpart{z}) or repeating it (xyyz\xpart{x}\ypart{y}\ypart{y}\zpart{z}) always changes the number of zeros while leaving the four ones untouched.

Wherever the loop falls in the first four steps, it holds only zeros, so skipping or repeating it changes the zero count while still landing on Accept.
2

Designing a Trap String with a Uniform Start

Now suppose the zeros and ones do not have to come in two neat blocks: we want to prove that the language of all strings with an equal number of 00s and 11s in any order is not regular. If our test string mixes 00s and 11s in its first pp characters, the machine can loop on a balanced piece like 01\ypart{01} and keep the counts equal.

Designing a Trap String with a Uniform Start
equal number of 0s and 1s, any order\text{equal number of }0\text{s and }1\text{s, any order}
0101 ✓1001 ✓0011 ✓0101\,\htmlClass{lc-green}{\checkmark}\qquad 1001\,\htmlClass{lc-green}{\checkmark}\qquad 0011\,\htmlClass{lc-green}{\checkmark}
p circlesp\ \text{circles}
length≥p\htmlClass{lc-mute}{\text{length}\ge p}

Even though the language allows any order, we are free to test the machine with a single valid string whose first pp characters are identical.

Text solution
Choose a valid string that starts with all the same symbolAgainst a pp-circle machine, we pick pp zeros followed by pp ones, which has equal counts and makes the first pp characters all zeros:
s\displaystyle s =0p1p\displaystyle = 0^p 1^p
Keep the loop to only zerosBecause ∣xy∣≤p|\xpart{x}\ypart{y}| \le p and ∣y∣>0|\ypart{y}| > 0, the loop y\ypart{y} falls entirely inside the first block of pp zeros:
y\displaystyle \ypart{y} =0kfor some 1\displaystyle = \ypart{0^k} \quad \text{for some } 1 ≤k\displaystyle \le \ypart{k} ≤p\displaystyle \le p
Repeat the loop (i=2i = 2)Repeating the loop once adds k≥1\ypart{k} \ge 1 zeros and no ones, forcing the machine to accept a string with more zeros than ones:
xy2z\displaystyle \xpart{x}\ypart{y}^2 \zpart{z} =0p+k1p(p+k>p)\displaystyle = 0^{p+\ypart{k}} 1^p \quad (\breaks{p + \ypart{k} > p})
3

Skipping the Loop to Break an Inequality

Next, consider a pattern defined by an inequality rather than an exact match:

{0i1j∣i>j}\displaystyle \{0^i 1^j \mid i > j\}

Repeating a loop of zeros only increases the number of zeros, which keeps i>ji > j true instead of breaking the machine.

Skipping the Loop to Break an Inequality
{ 0i1j∣i>j }\{\, 0^i 1^j \mid i > j \,\}

To break an inequality, we place our test string right on the boundary of the rule and skip the loop.

Text solution
Pick a boundary stringWe give the pp-circle machine a string where there is just one more zero than there are ones:
s\displaystyle s =0p+11p(p+1>p)\displaystyle = 0^{p+1} 1^p \quad (\holds{p + 1 > p})
Keep the loop in the leading zerosSince ∣xy∣≤p|\xpart{x}\ypart{y}| \le p and ∣y∣>0|\ypart{y}| > 0, the loop y\ypart{y} lies within the first pp zeros and contains at least one zero:
y\displaystyle \ypart{y} =0kfor some 1\displaystyle = \ypart{0^k} \quad \text{for some } 1 ≤k\displaystyle \le \ypart{k} ≤p\displaystyle \le p
Skip the loop (i=0i = 0)Removing y\ypart{y} deletes k≥1\ypart{k} \ge 1 zeros, leaving an accepted string that no longer has strictly more zeros than ones:
xz\displaystyle \xpart{x}\zpart{z} =0p+1−k1p(p+1−k≤p)\displaystyle = 0^{p+1-\ypart{k}} 1^p \quad (\breaks{p + 1 - \ypart{k} \le p})
4

Jumping into the Gap Between Valid Lengths

What if a language uses only one symbol, so there are no separate counts of 00s and 11s to compare? We want to prove that the language of strings of zeros whose length is a perfect square is not regular:

{0n2∣n≥1}={0,0000,000000000,… }\displaystyle \begin{aligned} &\{0^{n^2} \mid n \ge 1\} \\ &\quad = \{0, 0000, 000000000, \dots\} \end{aligned}
Jumping into the Gap Between Square Lengths
strings of zeros whose length\text{strings of zeros whose length}
is a perfect square\text{is a perfect square}
00
1\htmlClass{lc-green}{1}
00000000
4\htmlClass{lc-green}{4}
000000000000000000
9\htmlClass{lc-green}{9}
00000000000000000000000000000000
16\htmlClass{lc-green}{16}
⋮\vdots
0n20^{n^2}

Here, a finite loop of length at most pp is too short to reach the next square number.

Text solution
Pick a square length of at least ppWe test the pp-circle machine with a string of p2p^2 zeros:
s\displaystyle s =0p2\displaystyle = 0^{p^2}
Add one extra loopBecause ∣xy∣≤p|\xpart{x}\ypart{y}| \le p and ∣y∣>0|\ypart{y}| > 0, the loop y=0k\ypart{y} = \ypart{0^k} has length 1≤k≤p1 \le \ypart{k} \le p, and repeating it once adds k\ypart{k} zeros:
xy2z=0p2+kwith 1≤k≤p\displaystyle \begin{aligned} \xpart{x}\ypart{y}^2 \zpart{z} &= 0^{p^2 + \ypart{k}} \\ &\quad \text{with } 1 \le \ypart{k} \le p \end{aligned}
Trap the new length between consecutive squaresSince 1≤k≤p1 \le \ypart{k} \le p, the new length falls strictly between p2\valid{p^2} and (p+1)2\valid{(p + 1)^2}, so the machine accepts a string whose length cannot be a square:
p2<p2+k≤p2+p<p2+2p+1=(p+1)2\displaystyle \begin{aligned} \valid{p^2} &< p^2 + \ypart{k} \\ &\le p^2 + p \\ &< p^2 + 2p + 1 \\ &= \valid{(p + 1)^2} \end{aligned}

Even when the loop y\ypart{y} uses all pp of the first pp zeros, adding ∣y∣≤p|\ypart{y}| \le p to p2\valid{p^2} always lands strictly inside the 2p+12p + 1 gap before the next square number (p+1)2\valid{(p + 1)^2}.

Duplicating any loop in the front window pushes the string past the starting square, but never adds enough zeros to reach the next square.

Key concepts

Before reaching for the Pumping Lemma, check whether the pattern actually requires unlimited memory: if a rule only tracks a fixed finite count or a repeating cycle (such as whether a string has an even number of 00s), a finite automaton with a fixed number of circles recognizes it and the language is regular. When a pattern instead requires matching arbitrary counts or ever-growing gaps, we prove it is not regular by contradiction against a pp-circle machine. Any accepted string ss of length at least pp must loop within its first pp steps, splitting into s=xyzs = \xpart{x}\ypart{y}\zpart{z} with ∣xy∣≤p|\xpart{x}\ypart{y}| \le p, ∣y∣>0|\ypart{y}| > 0, and xyiz\xpart{x}\ypart{y}^i \zpart{z} accepted for all i≥0i \ge 0. Your proof comes down to three choices:

Uniform start for matching counts

(like 0n1n0^n 1^n or equal 00s and 11s in any order): Pick s=0p1ps = 0^p 1^p so the first pp characters are all 00s, forcing y=0k\ypart{y} = \ypart{0^k} (1≤k≤p1 \le \ypart{k} \le p), and repeat the loop (xy2z=0p+k1p\xpart{x}\ypart{y}^2 \zpart{z} = 0^{p+\ypart{k}} 1^p) to break the balance.

Boundary string and loop-skipping for inequalities

(like {0i1j∣i>j}\{0^i 1^j \mid i > j\}): Pick a string right on the boundary (s=0p+11ps = 0^{p+1} 1^p) and skip the loop (i=0i = 0, giving xz=0p+1−k1p\xpart{x}\zpart{z} = 0^{p+1-\ypart{k}} 1^p) so removing k≥1\ypart{k} \ge 1 zeros breaks the inequality.

Length bounding for widening gaps

(like 0n20^{n^2}): Pick s=0p2s = 0^{p^2} and repeat the loop once (xy2z=0p2+k\xpart{x}\ypart{y}^2 \zpart{z} = 0^{p^2 + \ypart{k}}), using 1≤k≤p1 \le \ypart{k} \le p to show p2<p2+k<(p+1)2\valid{p^2} < p^2 + \ypart{k} < \valid{(p + 1)^2}.

5

Practice

1

Suppose a 6-circle finite automaton claims to recognize {0n1n∣n≥0}\{0^n 1^n \mid n \ge 0\} and accepts the string 000000111111000000111111. (a) Explain why the machine must visit some circle twice while reading the first six 00s. (b) If the first loop splits the string into x=00\xpart{x} = \xpart{00}, y=000\ypart{y} = \ypart{000}, and z=0111111\zpart{z} = \zpart{0111111}, write the strings the machine accepts when skipping the loop (xz\xpart{x}\zpart{z}) and when going around the loop twice (xy2z\xpart{x}\ypart{y}^2 \zpart{z}), and explain why this proves the machine is broken.

(a) Count the starting circle before the first 00 is read, then add one circle for each step. (b) Replace y\ypart{y} with zero copies of 000\ypart{000} for xz\xpart{x}\zpart{z} and two copies of 000\ypart{000} for xy2z\xpart{x}\ypart{y}^2 \zpart{z}, keeping all of z\zpart{z} attached.

(a) Before reading any symbols, the machine starts on 11 circle, and reading the first six 00s takes 66 steps, so the machine visits 6+1=76 + 1 = \visits{7} circles in total (watch out for the common slip of counting only the 66 steps and saying 6\wrong{6} circles visited, which would not force a repeat in a 66-circle machine). By the Pigeonhole Principle, since the machine only has 66 circles, at least one circle must be visited twice during the first six 00s. (b) Skipping the loop gives xz=000111111\xpart{x}\zpart{z} = \xpart{00}\zpart{0111111} (three 00s and six 11s), and going around the loop twice gives xy2z=000000000111111\xpart{x}\ypart{y}^2 \zpart{z} = \xpart{00}\ypart{000000}\zpart{0111111} (nine 00s and six 11s). Be careful not to drop the leading 0\zpart{0} at the start of z=0111111\zpart{z} = \zpart{0111111}, which would wrongly give two 00s (00111111\wrong{00111111}) and eight 00s (00000000111111\wrong{00000000111111}). Because both paths end on the same Accept circle as xyz\xpart{x}\ypart{y}\zpart{z}, the machine accepts strings with unequal numbers of 00s and 11s, contradicting its claim.
2

Consider the language LL of all strings of 00s and 11s (in any order) that contain twice as many 11s as 00s. (a) Explain why (011)p(011)^p fails as a trap string against a pp-circle machine (for p≥3p \ge 3). (b) Prove that LL is not regular by choosing a trap string whose first pp characters are all 00s.

(a) Look at what 3-character block sits inside the first pp characters of 011011…011011011\dots 011. (b) If your string starts with 0p0^p, how many 11s must follow for the string to belong to LL?

(a) The first pp characters of (011)p\wrong{(011)^p} contain both 00s and 11s, so the machine's loop within the first pp steps could be y=011\ypart{y} = \ypart{011}. Repeating or skipping y=011\ypart{y} = \ypart{011} adds or removes one 00 and two 11s together, keeping the 2-to-1 ratio of 11s to 00s. (b) Suppose a machine with pp circles recognizes LL, and feed it s=0p12ps = 0^p 1^{2p}, which is in LL because it has pp zeros and 2p2p ones (do not pick 0p1p\wrong{0^p 1^p} out of habit, as it does not have twice as many 11s as 00s and is not in LL). Within the first pp steps, the machine visits p+1\visits{p + 1} circles and must hit a loop, splitting s=xyzs = \xpart{x}\ypart{y}\zpart{z} with ∣xy∣≤p|\xpart{x}\ypart{y}| \le p and ∣y∣>0|\ypart{y}| > 0. Because the first pp characters of ss are all 00s, the loop y\ypart{y} consists of k\ypart{k} zeros for some 1≤k≤p1 \le \ypart{k} \le p (avoid assuming k=1\ypart{k} = 1, which only checks 0p+112p0^{p+1} 1^{2p}). Going around the loop twice gives xy2z=0p+k12p\xpart{x}\ypart{y}^2 \zpart{z} = 0^{p+\ypart{k}} 1^{2p}, which has p+kp + \ypart{k} zeros and 2p2p ones. Since k≥1\ypart{k} \ge 1, twice the number of zeros is 2(p+k)>2p\breaks{2(p + \ypart{k}) > 2p}, so xy2z\xpart{x}\ypart{y}^2 \zpart{z} is not in LL, yet the machine accepts it, which is a contradiction.
3

Prove that the language L={0i1j∣i≥j}L = \{0^i 1^j \mid i \ge j\} (strings of 00s followed by 11s where there are at least as many 00s as 11s) is not regular, and explain why your proof must skip the loop (go around it 00 times) rather than repeat it.

Pick a boundary string where i=ji = j holds exactly, then check whether xz\xpart{x}\zpart{z} or xy2z\xpart{x}\ypart{y}^2 \zpart{z} breaks i≥ji \ge j.

Suppose a machine with pp circles recognizes LL, and feed it the boundary string s=0p1ps = 0^p 1^p, which is in LL since p≥p\holds{p \ge p} (if you instead pick 0p+11p\wrong{0^{p+1} 1^p}, skipping a loop of size k=1\ypart{k} = 1 leaves 0p1p0^p 1^p, which is still in LL). Within the first pp steps, the machine hits a loop, splitting s=xyzs = \xpart{x}\ypart{y}\zpart{z} with ∣xy∣≤p|\xpart{x}\ypart{y}| \le p and ∣y∣>0|\ypart{y}| > 0. Since the first pp characters are all 00s, y=0k\ypart{y} = \ypart{0^k} for some 1≤k≤p1 \le \ypart{k} \le p. Repeating the loop (going around it 22 or more times) adds more 00s, giving at least p+k>p\holds{p + \ypart{k} > p} zeros against pp ones, which stays in LL and fails to break the machine. Skipping the loop (going around it 00 times) gives xz=0p−k1p\xpart{x}\zpart{z} = 0^{p-\ypart{k}} 1^p. Since k≥1\ypart{k} \ge 1, the string xz\xpart{x}\zpart{z} has p−k≤p−1\breaks{p - \ypart{k} \le p - 1} zeros and pp ones, breaking i≥ji \ge j while still being accepted by the machine, which is a contradiction.
4

Prove that the language L={02n∣n≥0}L = \{0^{2^n} \mid n \ge 0\} (strings of 00s whose length is a power of 22) is not regular.

Feed a pp-circle machine s=02ps = 0^{2^p}, repeat the loop once, and compare the new length 2p+k2^p + \ypart{k} with the next power of 22, which is 2p+1=2p+2p\valid{2^{p+1}} = 2^p + 2^p.

Suppose a machine with pp circles (p≥1p \ge 1) recognizes LL, and feed it s=02ps = 0^{2^p}, which is in LL and has length 2p>p2^p > p. Within the first pp steps, the machine hits a loop y\ypart{y} of length k\ypart{k}, where 1≤k≤p1 \le \ypart{k} \le p. Going around the loop one extra time produces xy2z=02p+k\xpart{x}\ypart{y}^2 \zpart{z} = 0^{2^p + \ypart{k}}, which the machine must accept. Since 1≤k≤p1 \le \ypart{k} \le p and p<2pp < 2^p for every p≥1p \ge 1, the new length satisfies 2p<2p+k≤2p+p<2p+2p=2p+1\valid{2^p} < 2^p + \ypart{k} \le 2^p + p < 2^p + 2^p = \valid{2^{p+1}} (be careful to write the next power of 22 as 2p+1=2p+2p\valid{2^{p+1}} = 2^p + 2^p, not (2+1)p=3p\wrong{(2+1)^p = 3^p} or 2p+2\wrong{2^p + 2}). Because 2p+k2^p + \ypart{k} falls strictly in the gap between consecutive powers of 22, it cannot be a power of 22, which is a contradiction.
5

Prove that the language L={ww∣w is a string of 0s and 1s}L = \{ww \mid w \text{ is a string of } 0\text{s and } 1\text{s}\} (strings made of the same half repeated twice) is not regular. Explain why 0p0p0^p 0^p fails as a trap string and why 0p10p10^p 1 0^p 1 succeeds.

Check what happens to 0p0p=02p0^p 0^p = 0^{2p} when the loop has length k=2\ypart{k} = 2, then see how placing a 11 at the end of each half in 0p10p10^p 1 0^p 1 locks where the two halves must split.

If we test a pp-circle machine with 0p0p=02p\wrong{0^p 0^p} = 0^{2p} (a common wrong choice that assumes repeating the loop makes the length odd), a loop of even length (such as y=00\ypart{y} = \ypart{00} when p≥2p \ge 2) gives 02p+2=0p+10p+10^{2p+2} = 0^{p+1} 0^{p+1}, which keeps the total number of 00s even and stays in LL. Instead, feed the machine s=0p10p1s = 0^p 1 0^p 1, which is in LL with w=0p1w = 0^p 1. Within the first pp steps, the machine hits a loop, splitting s=xyzs = \xpart{x}\ypart{y}\zpart{z} with ∣xy∣≤p|\xpart{x}\ypart{y}| \le p and ∣y∣>0|\ypart{y}| > 0. Since the first pp characters are all 00s, the loop y=0k\ypart{y} = \ypart{0^k} (1≤k≤p1 \le \ypart{k} \le p) falls entirely inside the first block of 00s. Going around the loop twice gives xy2z=0p+k10p1\xpart{x}\ypart{y}^2 \zpart{z} = 0^{p+\ypart{k}} 1 0^p 1. Because this string contains only two 11s and ends with 11, any split into two identical halves wwww must end each half with 11, giving a first half of 0p+k1\breaks{0^{p+\ypart{k}} 1} and a second half of 0p1\breaks{0^p 1}. Since k≥1\ypart{k} \ge 1, the two halves do not match, so xy2z\xpart{x}\ypart{y}^2 \zpart{z} is not in LL, contradicting that the machine recognizes LL.