Discrete MathematicsLanguages, Automata and Computability3 of 6
Languages, Automata and Computability · 3
Nondeterminism and Kleene's Theorem
Building a DFA requires wiring a backup arrow for every input that breaks a pattern, even when the pattern itself is just a few characters long.
1
Nondeterministic Finite Automata and Clones
A Nondeterministic Finite Automaton (NFA) removes those backup arrows by allowing a state to have multiple arrows for the same input, or no arrow for an input at all.
Nondeterministic Finite Automata and Clones
An ε-arrow is a transition a token crosses for free: the instant a token lands on a state with an outgoing ε-arrow, it immediately spawns a clone across that arrow before the next character is read, while the original token stays put.
To see whether the ends-in-101 NFA accepts 1101, we track the set of states holding surviving clones after each character.
Text solution
Split on the first 1Starting from a single token on A, the first 1 follows both the loop on A and the arrow to B.
{A}1{A,B}
Advance and vanish on 10On the second 1, the token on A splits into A and B while the old token on B has no 1-arrow and vanishes; on 0,A stays on A and B moves to C.
{A,B}1{A,B}0{A,C}
Check the final group on the last 1The final 1 splits A into A and B and moves C into D, so a surviving token stands on the finish state and the machine accepts 1101.
{A,C}1{A,B,finishD}
In the ends-in-101 NFA below, the token looping on A acts as a permanent safety net that tests every 1 as a possible start of the final 101, while failed attempts disappear on their own without backward backup arrows.
The token looping on A sends out a fresh clone on every 1 while failed clones vanish, catching a final 101 without any backward arrows.
2
Subset Construction: Converting an NFA to a DFA
Even with parallel clones and free ε-arrows, an NFA can only hold tokens on a subset of its states at any moment. Subset Construction converts an NFA into an equivalent DFA by making each state in the DFA represent the exact subset of NFA states currently holding tokens.
Subset Construction: Converting an NFA to a DFA
Text solution
Follow ε-arrows for the starting subsetPlacing a token on state 1 immediately spawns a clone across 1ε3 before any character is read, forming the DFA's start state.
Start={1,3}
Pool the surviving tokens on a and bFrom each subset, combine the destinations of all its tokens on a and on b (along with any ε-arrows they trigger), drawing a new DFA state whenever a new subset appears.
Send dead ends to EmptyFrom {3}, reading a goes to 1 and crosses the ε-arrow to {1,3}, whereas reading b makes the last token vanish and goes to Empty, which loops on a,b forever.
{3}a{1,3},{3}bEmptya,bEmpty
Mark every subset containing 3 as a finish stateBecause the NFA accepts whenever at least one clone stands on 3, every DFA state that includes 3 becomes a double circle.
Finish states={1,3},{2,3},{3},{1,2,3}
Stepping through the 3-state NFA alongside its subset DFA shows that every group of tokens on the NFA is a single state in the DFA, landing on a double circle right when at least one NFA token sits on state 3.
At every step, one pointer on the DFA tracks the whole group of tokens on the NFA, and both machines accept at the same time.
3
Power-Set Blowup and the Complement Trap
Because an n-state NFA has 2n possible subsets of states, converting a small NFA into a DFA can cause an exponential blowup in the number of states. Tracking subsets of clones also shows why flipping finish states directly on an NFA fails to give the complement language.
Power-Set Blowup and the Complement Trap
For the language of binary strings whose k-th character from the end is 1, an NFA only needs a safety-net start state plus a k-state chain, while a DFA never knows when the string will end and must remember all 2k possible k-bit endings:
⟶NFA: Smallest DFA: k+1 states2k states
When you want the complement of an NFA's language (accepting every string the NFA rejects, and rejecting every string it accepts), swapping single and double circles directly on the NFA can fail, because a surviving clone on a state the swap turned into a finish state, like the safety-net clone on A, still triggers acceptance. You must convert the NFA to a DFA first so that each state represents the entire group of clones at once:
NFASubset ConstructionDFAswap single anddouble circlesComplement DFA
4
Kleene's Theorem and Regular Expressions to NFAs
Kleene's Theorem states that DFAs, NFAs, and Regular Expressions all recognize the exact same class of languages: every regular expression can be wired into a machine, and every machine can be turned into a regular expression.
Kleene's Theorem and Regular Expressions to NFAs
Using one-entrance, one-exit boxes connected by ε-arrows, we can assemble an NFA for (ab+a)∗ directly from its pieces.
Text solution
Chain a followed by bBuild 2-state boxes for a and b, and connect the exit of a to the entrance of b with an ε-arrow.
[◯a◯]ε[◯b◯]
Split and merge for the choice ab+aAdd a new start state with ε-arrows into both the ab box and a separate a box, and run ε-arrows from their exits into one new finish state.
◯ε[ab]εε[a]ε◯
Wrap with a backward loop and bypass for (ab+a)∗Add an outer start and finish state connected by ε-arrows, a backward ε-arrow from the inner exit to the inner entrance to repeat the pattern, and a bypass ε-arrow from outer start to outer finish for zero repetitions.
Because every box has one entrance and one exit wired solely by free ε-arrows, the three box patterns below assemble complex regular expressions out of smaller machines without changing any arrows inside the boxes.
Each regular expression operation connects smaller machines from the outside with epsilon-arrows, without changing a single state or arrow inside the boxes.
5
State Elimination: Machines to Regular Expressions
To turn a finite automaton back into a regular expression, State Elimination removes states one by one, replacing every incoming-loop-outgoing path through the removed state with a single regular-expression arrow.
State Elimination: Machines to Regular Expressions
Whenever removing a state Middle (with self-loop c) replaces the path InaMiddlebOut alongside an existing direct arrow IndOut, the two parallel routes merge with a + sign:
Ind+ac∗bOut
We can apply this rule to reduce a 2-state machine, where start state 1 has loop a and arrow 1b2, and finish state 2 has loop b and arrow 2a1, into a single regular expression.
Text solution
Add isolated start and finish states S and FBecause state 1 has an incoming arrow and state 2 has outgoing arrows, connect a new start Sε1 and a new finish 2εF.
Sε1,2εF
Eliminate state 1 and record the round trip from 2State 1 is entered from S on ε and from 2 on a, loops with a∗, and leaves to 2 on b, producing a forward arrow from S to 2 and a round-trip loop aa∗b on 2 that merges with 2's existing b loop.
Sa∗b2,loop on 2:b+aa∗b
Eliminate state 2 to connect S directly to FChaining the incoming arrow a∗b, the merged self-loop (b+aa∗b)∗, and the outgoing ε-arrow leaves a single regular expression from S to F.
Sa∗b(b+aa∗b)∗F
As you remove each state between S and F below, every path through it becomes a direct regular-expression arrow, including round trips that leave a neighbor and come right back as a new loop.
Removing a state turns every path through it, including round trips back to a neighbor, into a direct regular-expression arrow until only one arrow from start to finish remains.
Key concepts
DFAs, NFAs, and Regular Expressions all define the exact same class of languages, and you can move between them depending on what a problem asks for:
Tracing or converting an NFA to a DFA (Subset Construction)
Track the subset of states holding tokens at the same time. Whenever a token starts on or moves to a state with an ε-arrow like 1ε3, include state 3 in that subset immediately. If all tokens vanish on an input, send that transition to an Empty state that loops on every letter, and mark any subset containing an old finish state as a finish state.
Finding the complement of a machine
If the machine is already a DFA, simply swap single and double circles; do not run Subset Construction on a machine that is already deterministic. If the machine is an NFA (such as A looping on 0,1 with A1B), never flip its states directly; convert the NFA to a DFA first, and then swap single and double circles.
Regular Expression to NFA (ε-box wiring)
Build 2-state boxes for single letters and wire them with ε-arrows: chain AεB for concatenation AB, split and merge with four ε-arrows for choice A+B, and use four ε-arrows (entry, exit, backward repeat, and outer bypass) for A∗.
Machine to Regular Expression (State Elimination)
First add Sεstart and finishεF so no arrows enter S or leave F. When erasing a state with loop c, replace every pair of incoming arrow a and outgoing arrow b (including round trips from a neighbor back to itself) with ac∗b, and merge parallel arrows between the same two states using d+ac∗b.
6
Practice
1
Consider the NFA with states {1,2,3}, start state 1, finish state 3, and transitions 1b2,1ε3,2a2,2a,b3, and 3a1. Trace the set of states holding tokens before any input is read and after each character of the string baba. Does the NFA accept baba?
Before reading the first b, check whether the starting token on 1 can cross any arrow for free.
Start (before reading any input)A token starts on 1 and immediately spawns a clone across 1ε3, giving {1,3}.(Watch out: forgetting the initial ε-arrow starts the trace at {1}, and forgetting it again on the final step leaves {1} instead of {1,3}, which falsely rejects the string.)
After each character of baba
{1,3}b{2}a{2,3}b{3}a{1,3}
DecisionBecause the final subset {1,3} contains the finish state 3, the NFA accepts baba.
2
An NFA over {0,1} has states {A,B,C}, start state A, finish state C, and transitions A0A,A0,1B,B1C, and no outgoing arrows from C. Convert this NFA into a DFA using Subset Construction: give the start state, the transitions on 0 and 1 for every reachable subset state, the finish states, and list which subsets of {A,B,C} are unreachable.
Start from {A} and pool where the tokens go on 0 and on 1; what DFA state do you draw when every token in a subset vanishes?
(Watch out: if you drop dead-end transitions instead of sending {B},{B,C}, and {C} on 0 to Empty, the resulting machine is missing arrows and is not a valid DFA.)
Finish states{B,C} and {C}(every reachable subset containing C; do not mark only {C} and forget {B,C}).
Unreachable subsetsOut of the 23=8 subsets, the 2 unreachable subsets are {A,C} and {A,B,C} (being in A after a step requires the last bit read to be 0, whereas being in C requires the last bit read to be 1).
3
Consider the language of all binary strings over {0,1} whose 4th character from the end is 1. Describe how to construct an NFA for this language, give its number of states, and find how many states the smallest DFA for this language requires.
Counting only the 4 states in the chain and forgetting the initial safety-net state A gives the wrong answer 4.
Once a clone leaves the start state on a 1, how many more characters must it step through to verify that this 1 was the 4th character from the end?
NFA constructionBuild a start state A that loops on 0,1 and has a 1-arrow to the first state of a 4-state chain, where each of the 3 steps along the chain moves forward on 0,1 and the 4th state in the chain is the sole finish state (with no outgoing arrows).
Number of NFA states5 states(1 start state plus the 4 states in the chain). (Watch out: counting only the 4 states in the chain and forgetting the initial safety-net state A gives the wrong answer 4.)
Smallest DFA state countBecause a DFA must remember the exact last 4 bits at every step, the smallest DFA requires 24=16 states(not 25=32, which comes from plugging in the NFA's 5 states instead of the 4 remembered bits).
4
Consider the 2-state NFA over {0,1} for strings ending in 1: start state A has a loop on 0,1 and a 1-arrow to finish state B, and B has no outgoing arrows. (a) If you swap the finish and non-finish states directly on this NFA (making A the finish state and B non-finish), what happens on the input string 1? (b) Construct a valid DFA for the complement language (strings that do not end in 1).
In part (a), track all clones produced when reading 1 from A, not just the one that moves to B.
(a)On input 1, the token on A splits onto both A and B, ending on {A,B}. Because a surviving clone sits on the new finish state A, the flipped NFA accepts 1 even though the original NFA also accepts 1 (in fact, because a clone stays on A forever, the flipped NFA accepts every binary string). (Watch out: looking only at the clone that reaches B leads to the wrong conclusion that the flipped NFA rejects 1.)
(b)Convert the NFA to a DFA first, which gives two states: start state {A} (where 0→{A} and 1→{A,B}) and finish state {A,B} (where 0→{A} and 1→{A,B}). Swapping finish and non-finish states on this DFA makes {A} the sole finish state and {A,B} non-finish.
5
Using the entrance-and-exit box method with ε-arrows, describe how to build an NFA for the regular expression a(a+b)∗. How many ε-arrows does this construction use, and what does each one do?
Leaving out the bypass ε-arrow gives 8 instead of 9 and forces at least one repetition of a+b.
Count the ε-arrows inside the choice box a+b, then the star wrapper around (a+b)∗, and finally the link from the leading a.
Box construction and ε-arrow breakdownBuild three basic 2-state boxes (one for the leading a, and one each for the inner a and b):
Choice a+b(4ε-arrows)Add a choice-start state with 2ε-arrows splitting into the inner a and b boxes, and 2ε-arrows merging their exits into a choice-finish state.
Star (a+b)∗(4ε-arrows)Add a star-start state with 1ε-arrow into the choice-start, 1ε-arrow from the choice-finish into a star-finish state, 1 backward ε-arrow from the choice-finish to the choice-start for repetition, and 1 bypass ε-arrow from the star-start to the star-finish for zero repetitions. (Watch out: leaving out the bypass ε-arrow gives 8 instead of 9 and forces at least one repetition of a+b.)
Concatenation a(a+b)∗(1ε-arrow)Run 1ε-arrow from the exit of the leading a box into the star-start state.
Total9ε-arrows.
6
Convert the following 3-state machine into a regular expression using State Elimination: state 1 is the start state with loop 1a1, arrow 1b2, and arrow 1c3; state 2 has loop 2b2 and arrow 2a3; state 3 is the sole finish state with arrow 3a1. Eliminate the states in the order 2, then 1, then 3.
When you eliminate state 1, check both states that have arrows entering 1:S and 3.
Add S and FAdd new start state S with Sε1 and new finish state F with 3εF.
Eliminate state 2The path 1b2 (with loop b∗)a3 becomes 1bb∗a3, which merges with 1c3 into:
1c+bb∗a3
Eliminate state 1State 1 is entered from S on ε and from 3 on a, loops with a∗, and leaves to 3 on c+bb∗a. This creates the forward arrow from S to 3 and a round-trip self-loop on 3:
Sa∗(c+bb∗a)3,loop on 3:aa∗(c+bb∗a)
(Watch out: forgetting the round-trip path 3a1c+bb∗a3 drops the self-loop on 3 and produces the wrong answer a∗(c+bb∗a).)
Eliminate state 3Connecting S through 3's loop into F gives the final regular expression: