Discrete MathematicsLanguages, Automata and Computability2 of 6
Languages, Automata and Computability · 2

Finite State Automata

We want to build a simple machine that reads a string of 00s and 11s one digit at a time and decides whether the string follows a given rule.

1

States as Finite Memory

Suppose we want to check whether a string of 00s and 11s contains an even number of 11s, without counting up into the millions as the digits stream by.

States as Finite Memory
00
11
11
00
11
00
00
11
…\dots
Even or odd number of 1s?\text{Even or odd number of }1\text{s?}
Even\text{Even}
Odd\text{Odd}
00
00
11
11
Text solution
States for what to rememberWe only track whether the count of 11s seen so far is even or odd, making Even\text{Even} both the start state (00 ones is even) and the double-circled accepting state:
Start⟶((Even))⏟accept(Odd)\displaystyle \text{Start} \longrightarrow \underbrace{((\text{Even}))}_{\accept{\text{accept}}} \qquad (\text{Odd})
Arrows for each input digitReading 00 leaves the count of 11s unchanged, while reading 11 flips the count between Even\text{Even} and Odd\text{Odd}:
Even→0Even,Odd→0Odd,Even⇄11Odd\displaystyle \begin{aligned} \text{Even} &\xrightarrow{0} \text{Even}, \\ \text{Odd} &\xrightarrow{0} \text{Odd}, \\ \text{Even} &\underset{1}{\overset{1}{\rightleftarrows}} \text{Odd} \end{aligned}
Trace 10111011 from the start stateFollow one arrow per digit starting at Even\text{Even}:
Even→1Odd→0Odd→1Even→1Odd\displaystyle \begin{aligned} \text{Even} &\walk{\xrightarrow{1}} \text{Odd} \walk{\xrightarrow{0}} \text{Odd} \\ &\walk{\xrightarrow{1}} \text{Even} \walk{\xrightarrow{1}} \text{Odd} \end{aligned}

So 10111011 ends on the single circle Odd\text{Odd} and is rejected, whereas 101101 and the empty string ε\varepsilon end on the double circle Even\text{Even} and are accepted.

2

Formal Notation and Transition Tables

To write a machine down in text instead of a drawing, we package its states and arrows into a five-part list or a transition table.

Formal Notation and Transition Tables
M=(Q, Σ, δ, q0, F)M = (Q,\ \Sigma,\ \htmlClass{lc-violet}{\delta},\ q_0,\ F)
Even\text{Even}
Odd\text{Odd}
00
00
11
11

A Deterministic Finite Automaton (DFA) is written as a 55-tuple listing the parts of the diagram, paired with a table that records where each arrow goes:

M=(Q⏟states,  Σ⏟alphabet,  δ⏟transitions (arrows),  q0⏟start state,  F⏟accepting states)\displaystyle \begin{aligned} M = \bigl(&\underbrace{Q}_{\text{states}},\; \underbrace{\Sigma}_{\text{alphabet}},\; \underbrace{\rules{\delta}}_{\text{transitions (arrows)}},\; \\ &\underbrace{q_0}_{\text{start state}},\; \underbrace{F}_{\text{accepting states}}\bigr) \end{aligned}
δ(Even,1)=Odd⟺δ01→∗ EvenEvenOddOddOddEven\displaystyle \rules{\delta}(\text{Even}, 1) = \text{Odd} \qquad\Longleftrightarrow\qquad \begin{array}{r|cc} \rules{\delta} & 0 & 1 \\ \hline \rightarrow *\,\text{Even} & \text{Even} & \text{Odd} \\ \text{Odd} & \text{Odd} & \text{Even} \end{array}

In a transition table, →\rightarrow marks the start state q0q_0 and ∗* marks an accepting state in FF (a double circle in the drawing). "Deterministic" means every row has a single entry in every column: from every state, there is one and only one arrow for each symbol in Σ\Sigma.

When a problem gives you a transition table or a list of δ\rules{\delta} equations, don't try to trace strings in your head from the text; take ten seconds to draw a circle for each state and sketch the arrows first.

3

Tracking Remainders and Binary Divisibility

Checking whether a count is even or odd is just checking its remainder modulo 22. When a rule asks about divisibility by 33, either for the number of 11s or for the value of the string read as a binary number, we use one state for each possible remainder modulo 33.

Tracking Remainders and Binary Divisibility
number of 1s≡0(mod3)\text{number of }1\text{s} \equiv 0 \pmod 3
rem 0\text{rem }0
rem 1\text{rem }1
rem 2\text{rem }2
Text solution
One state per remainder mod 33Track the remainder r∈{0,1,2}r \in \{0, 1, 2\} of the binary number NN read so far, with [rem 0][\text{rem } 0] as both the start state and the accepting state:
Q={[rem 0], [rem 1], [rem 2]},q0=[rem 0],F={[rem 0]}\displaystyle \begin{aligned} Q &= \{[\text{rem } 0],\, [\text{rem } 1],\, [\text{rem } 2]\}, \\ q_0 &= [\text{rem } 0], \\ F &= \{[\text{rem } 0]\} \end{aligned}
Update the remainder when a bit is added at the endPutting 00 on the right end of NN doubles its value (N→2N\rules{N \to 2N}), and putting 11 on the right end doubles it and adds 11 (N→2N+1\rules{N \to 2N + 1}). So from [rem r][\text{rem } r], the arrows go to 2r mod 3\rules{2r} \bmod 3 and (2r+1) mod 3\rules{(2r + 1)} \bmod 3:
[rem 0]:0→2(0)=0(mod3),1→2(0)+1=1(mod3)[rem 1]:0→2(1)=2(mod3),1→2(1)+1=3≡0(mod3)[rem 2]:0→2(2)=4≡1(mod3),1→2(2)+1=5≡2(mod3)\displaystyle \begin{aligned} [\text{rem } 0]: \\ \rules{0 \to 2}(0) &= 0 \pmod 3, \\ \rules{1 \to 2}(0) \rules{{}+ 1} &= 1 \pmod 3 \\ [\text{rem } 1]: \\ \rules{0 \to 2}(1) &= 2 \pmod 3, \\ \rules{1 \to 2}(1) \rules{{}+ 1} &= 3 \equiv 0 \pmod 3 \\ [\text{rem } 2]: \\ \rules{0 \to 2}(2) &= 4 \equiv 1 \pmod 3, \\ \rules{1 \to 2}(2) \rules{{}+ 1} &= 5 \equiv 2 \pmod 3 \end{aligned}
Trace 1102=6110_2 = 6Follow the arrows from [rem 0][\text{rem } 0]:
[rem 0]→1[rem 1]→1[rem 0]→0[rem 0]\displaystyle \begin{aligned} [\text{rem } 0] &\walk{\xrightarrow{1}} [\text{rem } 1] \\ &\walk{\xrightarrow{1}} [\text{rem } 0] \walk{\xrightarrow{0}} [\text{rem } 0] \end{aligned}

So 110110 lands on [rem 0][\text{rem } 0] and is accepted because 1102=6110_2 = 6 is divisible by 33.

Each new bit at the right end of a binary string applies 2r mod 3\rules{2r} \bmod 3 or (2r+1) mod 3\rules{(2r + 1)} \bmod 3 to the current state, so the active state always matches the full number's remainder mod 33 no matter how large NN grows.

Each new bit updates the remainder the same way it updates the number, so three states can track divisibility by 3 for any length.
4

Matching a Pattern at the End of a String

When a rule cares about the exact pattern at the end of a string rather than a remainder, such as accepting all strings in (0∪1)∗101\wild{(0 \cup 1)^*}101 that end with 101101, we never know when the string will stop, so our states must track how much of 101101 is currently built up at the end.

Matching a Pattern at the End of a String
DFA: strings that end with 101\text{DFA: strings that end with }101
(0∪1)∗101\htmlClass{lc-violet}{(0 \cup 1)^*}101
11
11
00
11
11
00
11
Text solution
States for each prefix of 101101Create one state for each prefix of the target pattern 101101, starting at [nothing][\text{nothing}] and accepting at [101][101]:
Start⟶[nothing]→1[1]→0[10]→1(([101]))\displaystyle \begin{aligned} \text{Start} \longrightarrow{}& [\text{nothing}] \xrightarrow{1} [1] \\ &\xrightarrow{0} [10] \xrightarrow{1} \bigl(\bigl([101]\bigr)\bigr) \end{aligned}
Fallback arrows from the longest reusable endingWhen the next bit breaks the pattern, attach that bit to the state's label and move to the state for the longest ending that is still a prefix of 101101:
[nothing]+0=0⟶[nothing][1]+1=11‾⟶[1][10]+0=100⟶[nothing][101]+0=1010‾⟶[10][101]+1=1011‾⟶[1]\displaystyle \begin{aligned} [\text{nothing}] + 0 &= \broken{0} \\ &\longrightarrow [\text{nothing}] \\ [1] + 1 &= 1\kept{\underline{1}} \\ &\longrightarrow [1] \\ [10] + 0 &= \broken{100} \\ &\longrightarrow [\text{nothing}] \\ [101] + 0 &= 10\kept{\underline{10}} \\ &\longrightarrow [10] \\ [101] + 1 &= 101\kept{\underline{1}} \\ &\longrightarrow [1] \end{aligned}
Trace 110101110101Follow the forward and fallback arrows from [nothing][\text{nothing}]:
[nothing]→1[1]→1[1]→0[10]→1[101]→0[10]→1[101]\displaystyle \begin{aligned} [\text{nothing}] &\walk{\xrightarrow{1}} [1] \walk{\xrightarrow{1}} [1] \\ &\walk{\xrightarrow{0}} [10] \walk{\xrightarrow{1}} [101] \\ &\walk{\xrightarrow{0}} [10] \walk{\xrightarrow{1}} [101] \end{aligned}

So 110101110101 ends on [101][101] and is accepted: when 00 arrives after 11011101, the last four bits 1010‾10\kept{\underline{10}} already end with 10\kept{10}, so the machine falls back to [10][10] instead of starting over at [nothing][\text{nothing}].

At every step of the string, the active state matches the longest ending of the string read so far that is also a prefix of 101101, so a bit that breaks the pattern falls back to the longest reusable ending rather than to [nothing][\text{nothing}].

At every step, the active state matches the longest ending of the bits read so far that starts `101`.
5

Trap States and Complements

Now suppose a pattern is forbidden anywhere in the string, such as accepting all strings that never contain 00\trap{00}. Once 00\trap{00} appears, the string is rejected forever, and because a DFA cannot leave out arrows, that transition goes into a non-accepting trap state.

Trap States and Complements
Accept strings that never contain 00\text{Accept strings that never contain }\htmlClass{lc-orange}{00}
Safe\text{Safe}
11
Never contains 00\text{Never contains }\htmlClass{lc-orange}{00}
Safe\text{Safe}
Just saw\text{Just saw}
one 0\text{one }0
Trap\htmlClass{lc-orange}{\text{Trap}}
11
00
11
00
0, 10,\,1
Contains 00\text{Contains }00
Safe\text{Safe}
Just saw\text{Just saw}
one 0\text{one }0
Victory\htmlClass{lc-green}{\text{Victory}}
Lock\htmlClass{lc-green}{\text{Lock}}
11
00
11
00
0, 10,\,1
Text solution
Track consecutive 00s into a trap stateBoth [Safe][\text{Safe}] (start) and [Just saw one 0][\text{Just saw one } 0] are accepting states, while a second 00 in a row enters [Trap]\trap{[\text{Trap}]} and loops there forever:
Start⟶(([Safe]))⇄01(([Just saw one 0]))→0[Trap]↺0,1\displaystyle \begin{aligned} \text{Start} \longrightarrow{}& \bigl(\bigl([\text{Safe}]\bigr)\bigr) \\ \underset{1}{\overset{0}{\rightleftarrows}}{}& \bigl(\bigl([\text{Just saw one } 0]\bigr)\bigr) \\ \xrightarrow{0}{}& \trap{[\text{Trap}]} \circlearrowleft 0,1 \end{aligned}
Swap single and double circles for the complementBecause a complete DFA sends every string to a single final state, keeping all states and arrows the same and flipping the accepting set from FF to Q∖FQ \setminus F accepts the exact opposite language (strings that do contain 0000):
Fnever 00={[Safe], [Just saw one 0]}→complementFcontains 00={[Trap]}\displaystyle \begin{aligned} F_{\text{never } 00} &= \{[\text{Safe}],\, \\ &\qquad [\text{Just saw one } 0]\} \\ &\xrightarrow{\text{complement}} \\ F_{\text{contains } 00} &= \{\accept{[\text{Trap}]}\} \end{aligned}
6

Combining Two Rules with a Grid

When a string must satisfy two rules at once, such as having an even number of 11s and ending with 00, we pair the states of the two individual machines in a grid.

Combining Two Rules with a Grid
even number of 1s AND ends with 0\htmlClass{lc-blue}{\text{even number of }1\text{s}}\text{ AND }\htmlClass{lc-pink}{\text{ends with }0}
left hand\htmlClass{lc-mute}{\text{left hand}}
Even 1s\htmlClass{lc-blue}{\text{Even }1\text{s}}
Odd 1s\htmlClass{lc-blue}{\text{Odd }1\text{s}}
right hand\htmlClass{lc-mute}{\text{right hand}}
Start or\htmlClass{lc-pink}{\text{Start or}}
ended\htmlClass{lc-pink}{\text{ended}}
in 1\htmlClass{lc-pink}{\text{in }1}
Ended\htmlClass{lc-pink}{\text{Ended}}
in 0\htmlClass{lc-pink}{\text{in }0}
Text solution
Form the 2×22 \times 2 grid of state pairsPair the two parity states {Even 1s, Odd 1s}\{\first{\text{Even } 1\text{s}},\, \first{\text{Odd } 1\text{s}}\} as rows with the two ending states {Not ending in 0, Ended in 0}\{\second{\text{Not ending in } 0},\, \second{\text{Ended in } 0}\} as columns, starting at (Even 1s,Not ending in 0)(\first{\text{Even } 1\text{s}}, \second{\text{Not ending in } 0}):
Not ending in 0Ended in 0Even 1s→(Even 1s,Not 0)(Even 1s,Ended in 0)Odd 1s(Odd 1s,Not 0)(Odd 1s,Ended in 0)\displaystyle \begin{array}{c|cc} & \second{\text{Not ending in } 0} & \second{\text{Ended in } 0} \\ \hline \first{\text{Even } 1\text{s}} & \rightarrow (\first{\text{Even } 1\text{s}}, \second{\text{Not } 0}) & (\first{\text{Even } 1\text{s}}, \second{\text{Ended in } 0}) \\ \first{\text{Odd } 1\text{s}} & (\first{\text{Odd } 1\text{s}}, \second{\text{Not } 0}) & (\first{\text{Odd } 1\text{s}}, \second{\text{Ended in } 0}) \end{array}
Update the row and column on each digitReading 11 flips the row (Even 1s↔Odd 1s\first{\text{Even } 1\text{s}} \leftrightarrow \first{\text{Odd } 1\text{s}}) and always lands in the left column (Not ending in 0\second{\text{Not ending in } 0}); reading 00 stays in the same row and always lands in the right column (Ended in 0\second{\text{Ended in } 0}).
Mark accepting states for AND versus ORThe states and arrows stay the same, while the accepting set FF selects one grid entry for AND or three for OR:
FAND={(Even 1s,Ended in 0)}FOR={(Even 1s,Not 0),  (Even 1s,Ended in 0),  (Odd 1s,Ended in 0)}\displaystyle \begin{aligned} F_{\text{AND}} &= \{(\first{\text{Even } 1\text{s}}, \second{\text{Ended in } 0})\} \\ F_{\text{OR}} &= \{(\first{\text{Even } 1\text{s}}, \second{\text{Not } 0}),\; \\ &\qquad (\first{\text{Even } 1\text{s}}, \second{\text{Ended in } 0}),\; \\ &\qquad (\first{\text{Odd } 1\text{s}}, \second{\text{Ended in } 0})\} \end{aligned}

So AND accepts only at the top-right state where both rules hold, while OR accepts at all three states where at least one rule holds.

The active state in the 2×22 \times 2 grid is always the pair made of the current row state (even or odd 11s) and column state (whether the last digit was 00), and switching between AND and OR only changes which pairs have double circles.

In the 2-by-2 grid, the active state is always where the row tracker and column tracker meet, while switching between AND and OR changes only which circles get double rings.
7

Decoding a Mystery DFA

When a problem goes the other way and hands us an unlabeled DFA to figure out, we find what each state remembers by spotting any trap states first, then checking how a string leaves the start state and what puts it on a double circle at the end.

Decoding a Mystery DFA
Describe the language this DFA accepts\text{Describe the language this DFA accepts}
q0q_0
q1q_1
q2q_2
00
11
11
00
0, 10,\,1
Text solution
Mystery DFA 1 (forward path with a trap)State q2\trap{q_2} is a single-circle trap (δ(q2,0)=δ(q2,1)=q2\rules{\delta}(\trap{q_2}, 0) = \rules{\delta}(\trap{q_2}, 1) = \trap{q_2}), reaching the double circle q1q_1 from q0q_0 requires reading a 11 after any number of 00s, and reading 00 from q1q_1 falls into the trap q2\trap{q_2}:
→q0↺0→1((q1))↺1→0q2↺0,1⏟trap⟹0∗11∗\displaystyle \begin{aligned} \rightarrow q_0 \circlearrowleft 0 &\xrightarrow{1} ((q_1)) \circlearrowleft 1 \\ &\xrightarrow{0} \underbrace{\trap{q_2} \circlearrowleft 0,1}_{\trap{\text{trap}}} \\ &\Longrightarrow \quad 0^*11^* \end{aligned}
So Mystery DFA 1 accepts all strings of zero or more 00s followed by one or more 11s (all strings containing at least one 11 and never having a 00 after a 11).
Mystery DFA 2 (first-digit test plus last-digit switch)From q0q_0, a first digit of 11 goes straight to the trap q3\trap{q_3}, while a first digit of 00 enters {((q1)),q2}\{((q_1)), q_2\} where every 00 lands on ((q1))((q_1)) and every 11 lands on q2q_2:
q1=[started with 0, last digit 0],q2=[started with 0, last digit 1]⟹0∪0(0∪1)∗0\displaystyle \begin{aligned} q_1 &= [\text{started with } 0,\text{ last digit } 0], \\ q_2 &= [\text{started with } 0,\text{ last digit } 1] \\ &\Longrightarrow \quad 0 \cup 0\wild{(0 \cup 1)^*}0 \end{aligned}

Because ((q1))((q_1)) is the only accepting state, Mystery DFA 2 accepts all strings that both start with 00 and end with 00 (including the single-digit string 00).

8

The Limits of Finite Memory

A DFA can check any rule that depends on a fixed number of possibilities, so where does a finite state machine finally run out of memory? Consider the language {0n1n∣n≥0}\{0^n 1^n \mid n \ge 0\} of nn zeros followed by the exact same number of ones.

The Limits of Finite Memory
nothing\text{nothing}
11
11
00
1010
11
101101
0\htmlClass{lc-mute}{0}
1\htmlClass{lc-mute}{1}
0\htmlClass{lc-mute}{0}
0\htmlClass{lc-mute}{0}
1\htmlClass{lc-mute}{1}

By the Pigeonhole Principle, if you place k+1k + 1 items into kk boxes, at least one box must hold two items. If a DFA has only kk states and reads k+1k + 1 zeros in a row (0k+10^{k+1}), it lands on a state after each of the k+1k + 1 zeros, so it must land on some state q\crowded{q} at least twice, forming a loop of length m≥1m \ge 1:

0k+1 and 0k+1+mreach the same state⟹0k+11k+1⏟in L and 0k+1+m1k+1⏟not in Lreach the same final state\displaystyle \begin{aligned} &\first{0^{k+1}} \text{ and } \second{0^{k+1+m}} \\ &\text{reach the same state} \\ \Longrightarrow \quad &\underbrace{\first{0^{k+1}}1^{k+1}}_{\accept{\text{in } L}} \text{ and } \underbrace{\second{0^{k+1+m}}1^{k+1}}_{\reject{\text{not in } L}} \\ &\text{reach the same final state} \end{aligned}

Because a DFA only remembers its current state q\crowded{q} and not how many times it went around the loop, no DFA can accept {0n1n∣n≥0}\{0^n 1^n \mid n \ge 0\}.

Key concepts

Building or reading a DFA M=(Q,Σ,δ,q0,F)M = (Q, \Sigma, \rules{\delta}, q_0, F) always starts with one question: what about the part of the string read so far do I need to remember?

Remainders and divisibility (nn states)

When a rule checks a digit count modulo nn, use states [rem 0],…,[rem n−1][\text{rem } 0], \dots, [\text{rem } n-1] where the counted digit steps +1 mod n\rules{+1} \bmod n and the other digit loops (such as number of 11s ≡0(mod3)\equiv 0 \pmod 3). When the string itself is a binary number modulo nn, keep the same nn remainder states and send [rem r][\text{rem } r] to 2r mod n\rules{2r} \bmod n on 00 and (2r+1) mod n\rules{(2r + 1)} \bmod n on 11.

Ending pattern vs. forbidden pattern

When the string must end with a pattern like 101101, do not use a trap state. A later 101101 can still succeed, so create prefix states [nothing][\text{nothing}], [1][1], [10][10], [101][101] and send each fallback arrow to the longest suffix of (state+new bit)(\text{state} + \text{new bit}) that matches a state name (for instance, [101]→0[10][101] \xrightarrow{0} [10] because 1010‾10\kept{\underline{10}} ends in 10\kept{10}). Use a trap state Trap↺0,1\trap{\text{Trap}} \circlearrowleft 0,1 only when a rule can never be fixed once broken (such as never containing 00\trap{00}, or failing to start with 00).

Complements and grid combinations

Once a DFA is complete (every state has a 00 arrow and a 11 arrow), you do not need a new diagram for the opposite rule: keep all arrows fixed and swap single and double circles (F→Q∖FF \to Q \setminus F). To combine two rules with m\first{m} and n\second{n} states, form the m×n\first{m} \times \second{n} grid of state pairs; changing AND to OR only changes which grid states get double circles.

When a DFA cannot work

If a language requires matching two counts that can grow without bound, such as {0n1n∣n≥0}\{0^n 1^n \mid n \ge 0\}, no finite number of states kk is enough, because 0k+10^{k+1} forces a loop by the Pigeonhole Principle and the machine forgets how many times it looped.

9

Practice

1

A DFA M=(Q,Σ,δ,q0,F)M = (Q, \Sigma, \delta, q_0, F) over Σ={0,1}\Sigma = \{0, 1\} has states Q={q0,q1,q2}Q = \{q_0, q_1, q_2\}, start state q0q_0, accepting set F={q2}F = \{q_2\}, and transitions δ(q0,0)=q1\rules{\delta}(q_0, 0) = q_1, δ(q0,1)=q0\rules{\delta}(q_0, 1) = q_0, δ(q1,0)=q1\rules{\delta}(q_1, 0) = q_1, δ(q1,1)=q2\rules{\delta}(q_1, 1) = q_2, δ(q2,0)=q1\rules{\delta}(q_2, 0) = q_1, and δ(q2,1)=q0\rules{\delta}(q_2, 1) = q_0. Trace the state sequence starting from q0q_0 for each of the strings ε\varepsilon, 0101, 10011001, and 011011, and state whether MM accepts or rejects each string.

Sketch the three circles first; for a string of length LL, how many arrow steps do you take from q0q_0, and which state must you end on when the string finishes?

For ε\varepsilon, no digits are read, so we stay at the start state q0∉Fq_0 \notin F:
q0(reject)\displaystyle q_0 \quad (\reject{\text{reject}})
For 0101, follow 00 then 11 from q0q_0:
q0→0q1→1q2(accept)\displaystyle q_0 \walk{\xrightarrow{0}} q_1 \walk{\xrightarrow{1}} q_2 \quad (\accept{\text{accept}})
For 10011001, follow one arrow per bit:
q0→1q0→0q1→0q1→1q2(accept)\displaystyle \begin{aligned} q_0 &\walk{\xrightarrow{1}} q_0 \walk{\xrightarrow{0}} q_1 \\ &\walk{\xrightarrow{0}} q_1 \walk{\xrightarrow{1}} q_2 \quad (\accept{\text{accept}}) \end{aligned}
For 011011, do not stop and accept as soon as the path visits q2q_2 after 0101. The machine must read the final 11, which sends δ(q2,1)=q0\rules{\delta}(q_2, 1) = q_0:
q0→0q1→1q2→1q0(reject)\displaystyle q_0 \walk{\xrightarrow{0}} q_1 \walk{\xrightarrow{1}} q_2 \walk{\xrightarrow{1}} q_0 \quad (\reject{\text{reject}})
2

Design a DFA over Σ={0,1}\Sigma = \{0, 1\} that accepts all strings in which the total number of 00s is congruent to 1(mod3)1 \pmod 3. Give the states, start state, accepting state(s), and transitions.

What are the possible remainders when the count of 00s is divided by 33, and which input digit changes that count?

Use three states Q={r0,r1,r2}Q = \{r_0, r_1, r_2\}, where rkr_k remembers that the number of 00s seen so far is congruent to k(mod3)k \pmod 3. Before reading any digits, the string has zero 00s, so the start state is q0=r0q_0 = r_0 and the only accepting state is r1r_1 with F={r1}F = \{r_1\} (a common mistake is starting at r1r_1 instead of r0r_0, which shifts every remainder by 11). Reading 11 leaves the count of 00s unchanged, while reading 00 adds 1(mod3)1 \pmod 3 (be careful not to loop on 00 and step on 11 out of habit):
δ(rk,1)=rk for k∈{0,1,2},δ(r0,0)=r1,δ(r1,0)=r2,δ(r2,0)=r0\displaystyle \begin{aligned} \rules{\delta}(r_k, 1) &= r_k \text{ for } k \in \{0, 1, 2\}, \\ \rules{\delta}(r_0, 0) &= r_1, \\ \rules{\delta}(r_1, 0) &= r_2, \\ \rules{\delta}(r_2, 0) &= r_0 \end{aligned}
3

Design a DFA over Σ={0,1}\Sigma = \{0, 1\} that accepts all strings ending with 010010, that is, the language of (0∪1)∗010\wild{(0 \cup 1)^*}010. Specify what each state remembers and give all transitions.

Label four states by the prefixes of 010010. When a new digit doesn't continue the match, attach it to the state label and ask: what is the longest ending that is still a prefix of 010010?

Use four states tracking the longest suffix of the string read so far that is a prefix of 010010:
Q={[nothing], [0], [01], [010]},q0=[nothing],F={[010]}\displaystyle \begin{aligned} Q &= \{[\text{nothing}],\, [0],\, [01],\, [010]\}, \\ q_0 &= [\text{nothing}], \\ F &= \{[010]\} \end{aligned}
On [0][0] reading 00, do not reset to [nothing][\text{nothing}] (since 00‾0\kept{\underline{0}} still ends in 00), and on [010][010] reading 11, do not reset to [nothing][\text{nothing}] (since 0101‾01\kept{\underline{01}} already has the two-digit prefix 0101 at its end). Checking each state plus the new bit gives all transitions:
δ([nothing],0)=[0],δ([nothing],1)=[nothing]δ([0],0)=[0],δ([0],1)=[01]δ([01],0)=[010],δ([01],1)=[nothing]δ([010],0)=[0],δ([010],1)=[01]\displaystyle \begin{aligned} \rules{\delta}([\text{nothing}], 0) &= [0], \\ \rules{\delta}([\text{nothing}], 1) &= [\text{nothing}] \\ \rules{\delta}([0], 0) &= [0], \\ \rules{\delta}([0], 1) &= [01] \\ \rules{\delta}([01], 0) &= [010], \\ \rules{\delta}([01], 1) &= [\text{nothing}] \\ \rules{\delta}([010], 0) &= [0], \\ \rules{\delta}([010], 1) &= [01] \end{aligned}
4

(a) Design a DFA over Σ={0,1}\Sigma = \{0, 1\} that accepts all strings that never contain 111\trap{111} (three 11s in a row). (b) Explain how to turn your machine from part (a) into a DFA that accepts all strings that do contain 111111 at least once.

(a) How many consecutive 11s at the end of the string are still safe before the rule is broken forever? (b) Once every state has both arrows, what happens when you swap single and double circles?

(a) Use four states Q={q0,q1,q2,q3}Q = \{q_0, q_1, q_2, q_3\}, where q0,q1,q2q_0, q_1, q_2 remember 00, 11, or 22 consecutive 11s at the end of a string that has not yet seen 111\trap{111}, and q3\trap{q_3} is a trap state once 111\trap{111} has occurred. The start state is q0q_0, and all three safe states must be accepting, F={q0,q1,q2}F = \{q_0, q_1, q_2\} (leaving q1q_1 or q2q_2 out of FF would wrongly reject valid strings like 11 or 1111 that end in one or two 11s). Reading 00 from any safe state resets the run of consecutive 11s all the way to q0q_0 (not one step back to qk−1q_{k-1}):
δ(qk,0)=q0 and δ(qk,1)=qk+1 for k∈{0,1,2},δ(q3,0)=q3,δ(q3,1)=q3\displaystyle \begin{aligned} \rules{\delta}(q_k, 0) &= q_0 \text{ and } \\ \rules{\delta}(q_k, 1) &= q_{k+1} \text{ for } k \in \{0, 1, 2\}, \\ \rules{\delta}(\trap{q_3}, 0) &= \trap{q_3}, \\ \rules{\delta}(\trap{q_3}, 1) &= \trap{q_3} \end{aligned}
(b) Keep the states QQ, start state q0q_0, and all transitions δ\rules{\delta} the same, and flip which states have double circles so that F={q3}F = \{\accept{q_3}\} is the only accepting state and q0,q1,q2q_0, q_1, q_2 are non-accepting.
5

Design a DFA over Σ={0,1}\Sigma = \{0, 1\} that accepts all strings with an even number of 00s and an odd number of 11s. Then state how the accepting states change if "and" is replaced by "or".

Set up a 2×22 \times 2 grid pairing (parity of 0s,parity of 1s)(\first{\text{parity of } 0\text{s}}, \second{\text{parity of } 1\text{s}}). Which part of the pair flips when you read 00, and which flips when you read 11?

Use a 2×22 \times 2 grid of four states pairing the parity of 00s with the parity of 11s:
Q={(Even 0s,Even 1s),  (Even 0s,Odd 1s),  (Odd 0s,Even 1s),  (Odd 0s,Odd 1s)}\displaystyle \begin{aligned} Q = \{&(\first{\text{Even } 0\text{s}}, \second{\text{Even } 1\text{s}}),\; \\ &(\first{\text{Even } 0\text{s}}, \second{\text{Odd } 1\text{s}}),\; \\ &(\first{\text{Odd } 0\text{s}}, \second{\text{Even } 1\text{s}}),\; \\ &(\first{\text{Odd } 0\text{s}}, \second{\text{Odd } 1\text{s}})\} \end{aligned}
Because ε\varepsilon has 00 zeros and 00 ones, the start state is q0=(Even 0s,Even 1s)q_0 = (\first{\text{Even } 0\text{s}}, \second{\text{Even } 1\text{s}}) (do not make the target state (Even 0s,Odd 1s)(\first{\text{Even } 0\text{s}}, \second{\text{Odd } 1\text{s}}) the start state). From any state, reading 00 flips the first entry (Even 0s↔Odd 0s\first{\text{Even } 0\text{s}} \leftrightarrow \first{\text{Odd } 0\text{s}}) while keeping the second fixed, and reading 11 flips the second entry (Even 1s↔Odd 1s\second{\text{Even } 1\text{s}} \leftrightarrow \second{\text{Odd } 1\text{s}}) while keeping the first fixed. For "and", the only accepting state is:
Fand\displaystyle F_{\text{and}} ={(Even 0s,Odd 1s)}\displaystyle = \{(\first{\text{Even } 0\text{s}}, \second{\text{Odd } 1\text{s}})\}
For "or", include all three states where at least one condition holds (don't leave out (Even 0s,Odd 1s)(\first{\text{Even } 0\text{s}}, \second{\text{Odd } 1\text{s}}) where both hold):
For={(Even 0s,Even 1s),  (Even 0s,Odd 1s),  (Odd 0s,Odd 1s)}\displaystyle \begin{aligned} F_{\text{or}} = \{&(\first{\text{Even } 0\text{s}}, \second{\text{Even } 1\text{s}}),\; \\ &(\first{\text{Even } 0\text{s}}, \second{\text{Odd } 1\text{s}}),\; \\ &(\first{\text{Odd } 0\text{s}}, \second{\text{Odd } 1\text{s}})\} \end{aligned}
6

Design a DFA over Σ={0,1}\Sigma = \{0, 1\} that accepts all strings that, when read left to right as a binary number (with ε\varepsilon having remainder 00), are divisible by 55. Give the states, start and accepting states, and transitions.

Use five remainder states r0r_0, r1r_1, r2r_2, r3r_3, r4(mod5)r_4 \pmod 5. When a bit is added to the right end of a binary number, how does the remainder rr change on 00 versus on 11?

Use five states Q={r0,r1,r2,r3,r4}Q = \{r_0, r_1, r_2, r_3, r_4\} for the remainder mod 55 of the binary number NN read so far, with r0r_0 as both the start state and the only accepting state (q0=r0q_0 = r_0, F={r0}F = \{r_0\}). Putting 00 on the right end sends r→2r(mod5)\rules{r \to 2r} \pmod 5 and putting 11 on the right end sends r→2r+1(mod5)\rules{r \to 2r + 1} \pmod 5 (do not just loop on 00 and add 11 on 11 as if counting the number of 11s):
δ(r0,0)=r0,δ(r0,1)=r1δ(r1,0)=r2,δ(r1,1)=r3δ(r2,0)=r4,δ(r2,1)=r0δ(r3,0)=r1,δ(r3,1)=r2δ(r4,0)=r3,δ(r4,1)=r4\displaystyle \begin{aligned} \rules{\delta}(r_0, 0) &= r_0, & \rules{\delta}(r_0, 1) &= r_1 \\ \rules{\delta}(r_1, 0) &= r_2, & \rules{\delta}(r_1, 1) &= r_3 \\ \rules{\delta}(r_2, 0) &= r_4, & \rules{\delta}(r_2, 1) &= r_0 \\ \rules{\delta}(r_3, 0) &= r_1, & \rules{\delta}(r_3, 1) &= r_2 \\ \rules{\delta}(r_4, 0) &= r_3, & \rules{\delta}(r_4, 1) &= r_4 \end{aligned}
7

A mystery DFA over Σ={0,1}\Sigma = \{0, 1\} has states {q0,q1,q2,q3}\{q_0, q_1, q_2, q_3\}, start state q0q_0, accepting states F={q1,q2}F = \{q_1, q_2\}, and transitions δ(q0,0)=q1\rules{\delta}(q_0, 0) = q_1, δ(q0,1)=q2\rules{\delta}(q_0, 1) = q_2, δ(q1,0)=q3\rules{\delta}(q_1, 0) = q_3, δ(q1,1)=q2\rules{\delta}(q_1, 1) = q_2, δ(q2,0)=q1\rules{\delta}(q_2, 0) = q_1, δ(q2,1)=q3\rules{\delta}(q_2, 1) = q_3, δ(q3,0)=q3\rules{\delta}(q_3, 0) = q_3, and δ(q3,1)=q3\rules{\delta}(q_3, 1) = q_3. Explain what each state remembers and describe in plain English the language accepted by this DFA.

Draw the four circles first: which state is a trap, what is the difference between landing on q1q_1 versus q2q_2, and what sends you from q1q_1 or q2q_2 into the trap?

State q3\trap{q_3} is a non-accepting trap state entered as soon as the string contains 00\trap{00} (from q1q_1 on 00) or 11\trap{11} (from q2q_2 on 11). State q0q_0 is the non-accepting start state for ε\varepsilon, q1q_1 remembers that the string has no two identical digits in a row so far and ended with 00, and q2q_2 remembers that the string has no two identical digits in a row so far and ended with 11. Because q0∉Fq_0 \notin F (don't forget to leave out the empty string ε\varepsilon), the DFA accepts all non-empty strings over {0,1}\{0, 1\} in which 00s and 11s strictly alternate (never containing 0000 or 1111).
8

Using the Pigeonhole Principle, explain why no DFA with a finite number of states kk can accept the language L={0n1n∣n≥0}L = \{0^n 1^n \mid n \ge 0\}.

Feed the machine k+1k + 1 zeros in a row (0k+10^{k+1}). How many times does the machine land on a state after a zero, and what happens if two of those landings are on the same state?

Suppose a DFA has kk states and reads k+1k + 1 zeros in a row (0k+10^{k+1}). Make sure to apply the Pigeonhole Principle strictly within the block of 00s (not across the whole string 0n1n0^n 1^n, where a repeated state across the 00s and 11s would mix digits): after each of the k+1k + 1 zeros, the machine lands on one of the kk states, so by the Pigeonhole Principle it must land on some state q\crowded{q} at least twice, forming a loop of length m≥1m \ge 1 made entirely of 00s. Because a DFA remembers only its current state q\crowded{q} and not how many times it went around the loop, 0k+1\first{0^{k+1}} and 0k+1+m\second{0^{k+1+m}} end at the exact same state. Following both with 1k+11^{k+1} leads to the exact same final state, so any machine that accepts 0k+11k+1∈L\first{0^{k+1}}1^{k+1} \accept{\in L} must also wrongly accept 0k+1+m1k+1∉L\second{0^{k+1+m}}1^{k+1} \reject{\notin L}.