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 0s and 1s 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 0s and 1s contains an even number of 1s, without counting up into the millions as the digits stream by.
States as Finite Memory
Text solution
States for what to rememberWe only track whether the count of 1s seen so far is even or odd, making Even both the start state (0 ones is even) and the double-circled accepting state:
Start⟶accept((Even))(Odd)
Arrows for each input digitReading 0 leaves the count of 1s unchanged, while reading 1 flips the count between Even and Odd:
EvenOddEven0Even,0Odd,1⇄1Odd
Trace 1011 from the start stateFollow one arrow per digit starting at Even:
Even1Odd0Odd1Even1Odd
So 1011 ends on the single circle Odd and is rejected, whereas 101 and the empty string ε end on the double circle 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
A Deterministic Finite Automaton (DFA) is written as a 5-tuple listing the parts of the diagram, paired with a table that records where each arrow goes:
In a transition table, → marks the start state q0 and ∗ marks an accepting state in F (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 Σ.
When a problem gives you a transition table or a list of δ 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 2. When a rule asks about divisibility by 3, either for the number of 1s or for the value of the string read as a binary number, we use one state for each possible remainder modulo 3.
Tracking Remainders and Binary Divisibility
Text solution
One state per remainder mod 3Track the remainder r∈{0,1,2} of the binary number N read so far, with [rem 0] as both the start state and the accepting state:
Update the remainder when a bit is added at the endPutting 0 on the right end of N doubles its value (N→2N), and putting 1 on the right end doubles it and adds 1(N→2N+1). So from [rem r], the arrows go to 2rmod3 and (2r+1)mod3:
So 110 lands on [rem 0] and is accepted because 1102=6 is divisible by 3.
Each new bit at the right end of a binary string applies 2rmod3 or (2r+1)mod3 to the current state, so the active state always matches the full number's remainder mod 3 no matter how large N 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 that end with 101, we never know when the string will stop, so our states must track how much of 101 is currently built up at the end.
Matching a Pattern at the End of a String
Text solution
States for each prefix of 101Create one state for each prefix of the target pattern 101, starting at [nothing] and accepting at [101]:
Start⟶[nothing]1[1]0[10]1(([101]))
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 101:
Trace 110101Follow the forward and fallback arrows from [nothing]:
[nothing]1[1]1[1]0[10]1[101]0[10]1[101]
So 110101 ends on [101] and is accepted: when 0 arrives after 1101, the last four bits 1010 already end with 10, so the machine falls back to [10] instead of starting over at [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 101, so a bit that breaks the pattern falls back to the longest reusable ending rather than to [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. Once 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
Text solution
Track consecutive 0s into a trap stateBoth [Safe] (start) and [Just saw one 0] are accepting states, while a second 0 in a row enters [Trap] and loops there forever:
Start⟶1⇄00(([Safe]))(([Just saw one 0]))[Trap]↺0,1
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 F to Q∖F accepts the exact opposite language (strings that do contain 00):
Fnever 00Fcontains 00={[Safe],[Just saw one 0]}complement={[Trap]}
6
Combining Two Rules with a Grid
When a string must satisfy two rules at once, such as having an even number of 1s and ending with 0, we pair the states of the two individual machines in a grid.
Combining Two Rules with a Grid
Text solution
Form the 2×2 grid of state pairsPair the two parity states {Even 1s,Odd 1s} as rows with the two ending states {Not ending in 0,Ended in 0} as columns, starting at (Even 1s,Not ending in 0):
Even 1sOdd 1sNot ending in 0→(Even 1s,Not 0)(Odd 1s,Not 0)Ended in 0(Even 1s,Ended in 0)(Odd 1s,Ended in 0)
Update the row and column on each digitReading 1 flips the row (Even 1s↔Odd 1s) and always lands in the left column (Not ending in 0); reading 0 stays in the same row and always lands in the right column (Ended in 0).
Mark accepting states for AND versus ORThe states and arrows stay the same, while the accepting set F selects one grid entry for AND or three for OR:
FANDFOR={(Even 1s,Ended in 0)}={(Even 1s,Not 0),(Even 1s,Ended in 0),(Odd 1s,Ended in 0)}
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×2 grid is always the pair made of the current row state (even or odd 1s) and column state (whether the last digit was 0), 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
Text solution
Mystery DFA 1 (forward path with a trap)State q2 is a single-circle trap (δ(q2,0)=δ(q2,1)=q2), reaching the double circle q1 from q0 requires reading a 1 after any number of 0s, and reading 0 from q1 falls into the trap q2:
→q0↺01((q1))↺10trapq2↺0,1⟹0∗11∗
So Mystery DFA 1 accepts all strings of zero or more 0s followed by one or more 1s (all strings containing at least one 1 and never having a 0 after a 1).
Mystery DFA 2 (first-digit test plus last-digit switch)From q0, a first digit of 1 goes straight to the trap q3, while a first digit of 0 enters {((q1)),q2} where every 0 lands on ((q1)) and every 1 lands on q2:
q1q2=[started with 0, last digit 0],=[started with 0, last digit 1]⟹0∪0(0∪1)∗0
Because ((q1)) is the only accepting state, Mystery DFA 2 accepts all strings that both start with 0 and end with 0 (including the single-digit string 0).
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} of n zeros followed by the exact same number of ones.
The Limits of Finite Memory
By the Pigeonhole Principle, if you place k+1 items into k boxes, at least one box must hold two items. If a DFA has only k states and reads k+1 zeros in a row (0k+1), it lands on a state after each of the k+1 zeros, so it must land on some state q at least twice, forming a loop of length m≥1:
⟹0k+1 and 0k+1+mreach the same statein L0k+11k+1 and not in L0k+1+m1k+1reach the same final state
Because a DFA only remembers its current state q and not how many times it went around the loop, no DFA can accept {0n1n∣n≥0}.
Key concepts
Building or reading a DFA M=(Q,Σ,δ,q0,F) always starts with one question: what about the part of the string read so far do I need to remember?
Remainders and divisibility (n states)
When a rule checks a digit count modulo n, use states [rem 0],…,[rem n−1] where the counted digit steps +1modn and the other digit loops (such as number of 1s ≡0(mod3)). When the string itself is a binary number modulo n, keep the same n remainder states and send [rem r] to 2rmodn on 0 and (2r+1)modn on 1.
Ending pattern vs. forbidden pattern
When the string must end with a pattern like 101, do not use a trap state. A later 101 can still succeed, so create prefix states [nothing],[1],[10],[101] and send each fallback arrow to the longest suffix of (state+new bit) that matches a state name (for instance, [101]0[10] because 1010 ends in 10). Use a trap stateTrap↺0,1 only when a rule can never be fixed once broken (such as never containing 00, or failing to start with 0).
Complements and grid combinations
Once a DFA is complete (every state has a 0 arrow and a 1 arrow), you do not need a new diagram for the opposite rule: keep all arrows fixed and swap single and double circles (F→Q∖F). To combine two rules with m and n states, form the m×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}, no finite number of states k is enough, because 0k+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) over Σ={0,1} has states Q={q0,q1,q2}, start state q0, accepting set F={q2}, and transitions δ(q0,0)=q1,δ(q0,1)=q0,δ(q1,0)=q1,δ(q1,1)=q2,δ(q2,0)=q1, and δ(q2,1)=q0. Trace the state sequence starting from q0 for each of the strings ε,01,1001, and 011, and state whether M accepts or rejects each string.
Sketch the three circles first; for a string of length L, how many arrow steps do you take from q0, and which state must you end on when the string finishes?
For ε, no digits are read, so we stay at the start state q0∈/F:
q0(reject)
For 01, follow 0 then 1 from q0:
q00q11q2(accept)
For 1001, follow one arrow per bit:
q01q00q10q11q2(accept)
For 011, do not stop and accept as soon as the path visits q2 after 01. The machine must read the final 1, which sends δ(q2,1)=q0:
q00q11q21q0(reject)
2
Design a DFA over Σ={0,1} that accepts all strings in which the total number of 0s is congruent to 1(mod3). Give the states, start state, accepting state(s), and transitions.
What are the possible remainders when the count of 0s is divided by 3, and which input digit changes that count?
Use three states Q={r0,r1,r2}, where rk remembers that the number of 0s seen so far is congruent to k(mod3). Before reading any digits, the string has zero 0s, so the start state is q0=r0 and the only accepting state is r1 with F={r1} (a common mistake is starting at r1 instead of r0, which shifts every remainder by 1). Reading 1 leaves the count of 0s unchanged, while reading 0 adds 1(mod3) (be careful not to loop on 0 and step on 1 out of habit):
δ(rk,1)δ(r0,0)δ(r1,0)δ(r2,0)=rk for k∈{0,1,2},=r1,=r2,=r0
3
Design a DFA over Σ={0,1} that accepts all strings ending with 010, that is, the language of (0∪1)∗010. Specify what each state remembers and give all transitions.
Label four states by the prefixes of 010. 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 010?
Use four states tracking the longest suffix of the string read so far that is a prefix of 010:
On [0] reading 0, do not reset to [nothing] (since 00 still ends in 0), and on [010] reading 1, do not reset to [nothing] (since 0101 already has the two-digit prefix 01 at its end). Checking each state plus the new bit gives all transitions:
(a) Design a DFA over Σ={0,1} that accepts all strings that never contain 111 (three 1s in a row). (b) Explain how to turn your machine from part (a) into a DFA that accepts all strings that do contain 111 at least once.
(a) How many consecutive 1s 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}, where q0,q1,q2 remember 0,1, or 2 consecutive 1s at the end of a string that has not yet seen 111, and q3 is a trap state once 111 has occurred. The start state is q0, and all three safe states must be accepting, F={q0,q1,q2} (leaving q1 or q2 out of F would wrongly reject valid strings like 1 or 11 that end in one or two 1s). Reading 0 from any safe state resets the run of consecutive 1s all the way to q0 (not one step back to qk−1):
δ(qk,0)δ(qk,1)δ(q3,0)δ(q3,1)=q0 and =qk+1 for k∈{0,1,2},=q3,=q3
(b) Keep the states Q, start state q0, and all transitions δ the same, and flip which states have double circles so that F={q3} is the only accepting state and q0,q1,q2 are non-accepting.
5
Design a DFA over Σ={0,1} that accepts all strings with an even number of 0s and an odd number of 1s. Then state how the accepting states change if "and" is replaced by "or".
Set up a 2×2 grid pairing (parity of 0s,parity of 1s). Which part of the pair flips when you read 0, and which flips when you read 1?
Use a 2×2 grid of four states pairing the parity of 0s with the parity of 1s:
Because ε has 0 zeros and 0 ones, the start state is q0=(Even 0s,Even 1s) (do not make the target state (Even 0s,Odd 1s) the start state). From any state, reading 0 flips the first entry (Even 0s↔Odd 0s) while keeping the second fixed, and reading 1 flips the second entry (Even 1s↔Odd 1s) while keeping the first fixed. For "and", the only accepting state is:
Fand={(Even 0s,Odd 1s)}
For "or", include all three states where at least one condition holds (don't leave out (Even 0s,Odd 1s) where both hold):
Design a DFA over Σ={0,1} that accepts all strings that, when read left to right as a binary number (with ε having remainder 0), are divisible by 5. Give the states, start and accepting states, and transitions.
Use five remainder states r0,r1,r2,r3,r4(mod5). When a bit is added to the right end of a binary number, how does the remainder r change on 0 versus on 1?
Use five states Q={r0,r1,r2,r3,r4} for the remainder mod 5 of the binary number N read so far, with r0 as both the start state and the only accepting state (q0=r0,F={r0}). Putting 0 on the right end sends r→2r(mod5) and putting 1 on the right end sends r→2r+1(mod5) (do not just loop on 0 and add 1 on 1 as if counting the number of 1s):
A mystery DFA over Σ={0,1} has states {q0,q1,q2,q3}, start state q0, accepting states F={q1,q2}, and transitions δ(q0,0)=q1,δ(q0,1)=q2,δ(q1,0)=q3,δ(q1,1)=q2,δ(q2,0)=q1,δ(q2,1)=q3,δ(q3,0)=q3, and δ(q3,1)=q3. 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 q1 versus q2, and what sends you from q1 or q2 into the trap?
State q3 is a non-accepting trap state entered as soon as the string contains 00 (from q1 on 0) or 11 (from q2 on 1). State q0 is the non-accepting start state for ε,q1 remembers that the string has no two identical digits in a row so far and ended with 0, and q2 remembers that the string has no two identical digits in a row so far and ended with 1. Because q0∈/F (don't forget to leave out the empty string ε), the DFA accepts all non-empty strings over {0,1} in which 0s and 1s strictly alternate (never containing 00 or 11).
8
Using the Pigeonhole Principle, explain why no DFA with a finite number of states k can accept the language L={0n1n∣n≥0}.
Feed the machine k+1 zeros in a row (0k+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 k states and reads k+1 zeros in a row (0k+1). Make sure to apply the Pigeonhole Principle strictly within the block of 0s (not across the whole string 0n1n, where a repeated state across the 0s and 1s would mix digits): after each of the k+1 zeros, the machine lands on one of the k states, so by the Pigeonhole Principle it must land on some state q at least twice, forming a loop of length m≥1 made entirely of 0s. Because a DFA remembers only its current state q and not how many times it went around the loop, 0k+1 and 0k+1+m end at the exact same state. Following both with 1k+1 leads to the exact same final state, so any machine that accepts 0k+11k+1∈L must also wrongly accept 0k+1+m1k+1∈/L.