From NFA to DFA
Opens chatgpt.com in a new tab with a ready-made prompt for this topic.
The question as asked
Construct a DFA equivalent to the NFA M=({P,Q,R,S},{0,1},δ,P,{Q,S}) where δ is:
The table is empty. Press Next step to fill it one row at a time (4 steps).
| State | 0 | 1 |
|---|---|---|
In Nov 2023 the states were named a, b, c, d with finals b and d. Rename a→P, b→Q, c→R, d→S and the answer is identical.
The method, one row at a time
The rule: start from {P}. For each subset and each symbol, take the union of the rows of its states. A new subset joins the queue. Stop when the queue is empty. Subsets containing Q or S are final.
Every subset, with the union written out
This is the whole construction in one table. Each row takes one subset, and for each symbol writes the union of the NFA rows of its members. Any result not seen before goes into the last column and gets its own row later.
Check: NFA and DFA give the same answer
Run a string on the NFA, keeping the set of states it could be in after each symbol. Each set is exactly the DFA state reached by the same prefix, so this doubles as a trace of the DFA.
Write this in the booklet
Let the DFA be MD=(QD,{0,1},δD,[P],FD). Using δD(S,a)=⋃p∈SδN(p,a) and starting from [P], the reachable subsets are:
| DFA state | 0 | 1 |
|---|---|---|
| → [P] | [Q,S] | [Q] |
| * [Q,S] | [R] | [P,Q,R] |
| * [Q] | [R] | [Q,R] |
| [R] | [S] | [P] |
| * [P,Q,R] | [Q,R,S] | [P,Q,R] |
| * [Q,R] | [R,S] | [P,Q,R] |
| * [S] | ∅ | [P] |
| * [Q,R,S] | [R,S] | [P,Q,R] |
| * [R,S] | [S] | [P] |
| ∅ (dead) | ∅ | ∅ |
QD={[P],[Q,S],[Q],[R],[P,Q,R],[Q,R],[S],[Q,R,S],[R,S],∅}, start state [P], and FD={[Q,S],[Q],[P,Q,R],[Q,R],[S],[Q,R,S],[R,S]}, the subsets containing Q or S.
Draw the transition diagram from this table: one circle per row, double circles for the starred rows, and an arrow into [P].
NFA p, q, r, s (final s) to DFA
Construct a DFA equivalent to the NFA ({p,q,r,s},{0,1},δ,p,{s}) with the transition table:
| State | 0 | 1 |
|---|---|---|
| → p | {p, q} | {p} |
| q | {r} | {r} |
| r | {s} | ∅ |
| * s | {s} | {s} |
Read the NFA before converting
The start state is p and the only final state is s.
Build each subset's moves
For each subset and each symbol, take the union of the rows of its members. A subset seen for the first time is added to the list to process.
The DFA table and its final states
Collect the results into the DFA table. Each row is one subset.
Check on one string
Run 0100 on both machines. They must agree.
Booklet answer · NFA p, q, r, s (final s) to DFA
Given: M=({p,q,r,s},{0,1},δ,p,{s}).
Subset construction. Start with [p]. For each new subset S and symbol a, compute δD(S,a)=⋃x∈Sδ(x,a).
| DFA state | On 0 | On 1 |
|---|---|---|
| → [p] | [p,q] | [p] |
| [p,q] | [p,q,r] | [p,r] |
| [p,q,r] | [p,q,r,s] | [p,r] |
| [p,r] | [p,q,s] | [p] |
| * [p,q,r,s] | [p,q,r,s] | [p,r,s] |
| * [p,q,s] | [p,q,r,s] | [p,r,s] |
| * [p,r,s] | [p,q,s] | [p,s] |
| * [p,s] | [p,q,s] | [p,s] |
Final states: every subset containing s, that is [p,q,r,s], [p,q,s], [p,r,s], [p,s].
Result: the DFA has 8 states, start state [p], and no dead state because p is in every subset. Draw the diagram from the table, with double circles on the final states.
NFA for second-last symbol 1, to DFA, with tests
Construct an NFA that accepts all strings over {0,1} whose second-last symbol is 1, convert it to a DFA, and test the strings 100010 and 100000.
Design the NFA by guessing
An NFA can guess. q0 reads any prefix and loops on 0 and 1.
Subset construction
Name the subsets A, B, C, D in the order they are found.
The DFA
| DFA state | Subset of NFA states | On 0 | On 1 |
|---|---|---|---|
| → A | {q0} | A | B |
| B | {q0, q1} | C | D |
| * C | {q0, q2} | A | B |
| * D | {q0, q1, q2} | C | D |
Test 100010
The table is empty. Press Next step to fill it one row at a time (7 steps).
| Step | State | Read | Next state | Meaning of the next state |
|---|---|---|---|---|
It ends in C, a final state, so 100010 is accepted. Its second-last symbol is indeed 1.
Test 100000
The table is empty. Press Next step to fill it one row at a time (7 steps).
| Step | State | Read | Next state | Meaning of the next state |
|---|---|---|---|---|
It ends in A, which is not final, so 100000 is rejected. Its second-last symbol is 0.
Booklet answer · NFA for second-last symbol 1, to DFA, with tests
NFA: M=({q0,q1,q2},{0,1},δ,q0,{q2}) with δ(q0,0)={q0}, δ(q0,1)={q0,q1}, δ(q1,0)=δ(q1,1)={q2}, and no moves from q2.
Equivalent DFA by subset construction:
| DFA state | Subset of NFA states | On 0 | On 1 |
|---|---|---|---|
| → A | {q0} | A | B |
| B | {q0, q1} | C | D |
| * C | {q0, q2} | A | B |
| * D | {q0, q1, q2} | C | D |
Tests. 100010: A → B → C → A → A → B → C, ends in a final state, accepted. 100000: A → B → C → A → A → A → A, ends in A, rejected.
Finite automata with output
| Point | Moore | Mealy |
|---|---|---|
| Output depends on | present state only | present state and present input |
| Output written | inside the state, q/0 | on the edge, a/0 |
| Length of output | n + 1 for input of length n | n |
| Number of states | usually more | usually fewer |
| Reaction to input | output changes one clock later | output changes immediately |
| Conversion | Moore → Mealy keeps the state count | Mealy → Moore may split states |
Opens chatgpt.com in a new tab with a ready-made prompt for this topic.
The question as asked
Convert the following Mealy machine to a Moore machine.
Split states by incoming output
Step 1: transition table of the Mealy machine.
The split, state by state
Step 2 in a table. List every arrow that enters each state, with the output printed on it. A Moore state can print only one output, so a state that receives two different outputs has to be copied, once per output.
Check: both machines print the same thing
Feed the same input to both machines. The Moore machine prints one extra symbol at the start (the output of its start state); after that, each Moore output must equal the Mealy output of the same step.
Write this in the booklet
Incoming outputs: q1 receives only 1, q2 receives 0 and 1, q3 receives 0 and 1, q4 receives only 1. So q2 splits into q20,q21 and q3 splits into q30,q31.
| Moore state | a | b | Output λ |
|---|---|---|---|
| → q1 | q1 | q20 | 1 |
| q20 | q4 | q4 | 0 |
| q21 | q4 | q4 | 1 |
| q30 | q21 | q31 | 0 |
| q31 | q21 | q31 | 1 |
| q4 | q30 | q1 | 1 |
The Moore machine is ({q1,q20,q21,q30,q31,q4},{a,b},{0,1},δ,λ,q1) with the table above. Draw each state as a circle labelled state/output, for example q20/0.
Moore machine for 'ends in 10 / 11', then convert to Mealy
Design a Moore machine over {0,1} that outputs a if the input ends in 10, b if it ends in 11, and c otherwise. Convert it to a Mealy machine.
Decide what each state remembers
The output depends only on the last two symbols, so the states remember just enough of them.
| State | Meaning | Output |
|---|---|---|
| q0 | start, or ends in 0 but not in 10 | c |
| q1 | ends in 1 but not in 11 | c |
| q2 | ends in 10 | a |
| q3 | ends in 11 | b |
Fill in the moves
From q1 (last symbol 1): a 0 makes the ending 10, so go to q2. A 1 makes it 11, so go to q3.
Check the Moore machine
Run 1011. The last output should be b, because 1011 ends in 11.
Convert to Mealy
Rule: the output on an arrow into state q is λ(q), the output of the state it enters.
Booklet answer · Moore machine for 'ends in 10 / 11', then convert to Mealy
Moore machine (Q,Σ,Δ,δ,λ,q0) with Σ={0,1}, Δ={a,b,c}:
| State | 0 | 1 | λ |
|---|---|---|---|
| → q0 | q0 | q1 | c |
| q1 | q2 | q3 | c |
| q2 | q0 | q1 | a |
| q3 | q2 | q3 | b |
Conversion to Mealy: each arrow into q gets output λ(q). Rows of q0 and q2 coincide, as do q1 and q3, giving the 2-state Mealy machine:
| State | 0 | 1 |
|---|---|---|
| → A | A, c | B, c |
| B | A, a | B, b |
Check with 1011: Moore prints c c a c b, Mealy prints c a c b, the same once the start output is dropped.
Moore machine for divisibility by 3
Compare Mealy and Moore machines, and construct a Moore machine that computes divisibility of a binary number by 3.
Mealy versus Moore
| Point | Mealy machine | Moore machine |
|---|---|---|
| Output depends on | the state and the input symbol | the state only |
| Output is written | on the arrows (a/x) | inside the states (q/x) |
| Outputs for an input of length n | n | n + 1 (the start state also prints) |
| Number of states | usually fewer | usually more |
| Reaction to input | immediate, in the same step | one step later, on entering the state |
Each type can be converted into the other, so they compute the same functions.
Remember the remainder
Read the binary number from the most significant bit. Keep only the remainder r of the number read so far, divided by 3.
Compute every move
The table is empty. Press Next step to fill it one row at a time (3 steps).
| State | On 0 | On 1 | Output λ |
|---|---|---|---|
The number is divisible by 3 exactly when the machine ends in q0, that is when the last output is 0.
Check on 110 and 1011
110 is 6, which is divisible by 3:
Booklet answer · Moore machine for divisibility by 3
Mealy vs Moore: in a Mealy machine the output depends on the state and the input and is written on the arrows; in a Moore machine it depends on the state only and is written in the states. A Moore machine gives n + 1 outputs for n inputs, a Mealy machine n. Each can be converted to the other.
Divisibility by 3: state qr means the number read so far leaves remainder r. Reading bit b gives r′=(2r+b)mod3.
| State | On 0 | On 1 | Output λ |
|---|---|---|---|
| → q0 (r = 0) | (2·0 + 0) mod 3 = 0 → q0 | (2·0 + 1) mod 3 = 1 → q1 | 0 |
| q1 (r = 1) | (2·1 + 0) mod 3 = 2 → q2 | (2·1 + 1) mod 3 = 0 → q0 | 1 |
| q2 (r = 2) | (2·2 + 0) mod 3 = 1 → q1 | (2·2 + 1) mod 3 = 2 → q2 | 2 |
The output of the last state is the remainder; the number is divisible by 3 if and only if it ends in q0. Example: 110 (six) goes q0 → q1 → q0 → q0, output 0.
Mealy machine for the 2's complement
Construct a Mealy machine that gives the 2's complement of any binary input (the last carry is neglected).
The rule, read from the right
Read the number from the least significant bit, which is the right end.
Two states
q0 means no 1 has been seen yet. q1 means a 1 has been seen.
Check on 01100
01100 is 12. Read it from the right: 0, 0, 1, 1, 0.
Booklet answer · Mealy machine for the 2's complement
Reading from the least significant bit, the 2's complement copies bits up to and including the first 1 and inverts the rest. States: q0 (no 1 seen) and q1 (1 seen).
| State | 0 | 1 |
|---|---|---|
| → q0 | q0, 0 | q1, 1 |
| q1 | q1, 1 | q1, 0 |
Example: 01100 read from the right gives 0 0 1 0 1, i.e. 10100, and 01100 + 10100 = 100000 (carry dropped).
Outputs of a Moore machine for aabab and ababb
For the Moore machine q0/0 (a → q1, b → q0), q1/0 (a → q1, b → q2), q2/1 (a → q1, b → q0), find the outputs for aabab and ababb.
How a Moore machine prints
The machine prints the output of the start state first.
Run aabab
| Read | (start) | a | a | b | a | b |
|---|---|---|---|---|---|---|
| State | q0 | q1 | q1 | q2 | q1 | q2 |
| Output | 0 | 0 | 0 | 1 | 0 | 1 |
The output for aabab is 000101.
Run ababb
| Read | (start) | a | b | a | b | b |
|---|---|---|---|---|---|---|
| State | q0 | q1 | q2 | q1 | q2 | q0 |
| Output | 0 | 0 | 1 | 0 | 1 | 0 |
The output for ababb is 001010.
Booklet answer · Outputs of a Moore machine for aabab and ababb
A Moore machine prints λ(q0) first, then the output of every state entered.
| Read | (start) | a | a | b | a | b |
|---|---|---|---|---|---|---|
| State | q0 | q1 | q1 | q2 | q1 | q2 |
| Output | 0 | 0 | 0 | 1 | 0 | 1 |
| Read | (start) | a | b | a | b | b |
|---|---|---|---|---|---|---|
| State | q0 | q1 | q2 | q1 | q2 | q0 |
| Output | 0 | 0 | 1 | 0 | 1 | 0 |
Outputs: aabab → 000101, ababb → 001010.
Design a DFA from a description
A reliable recipe for every design question:
- Decide what the machine must remember (a parity, a remainder, how much of a pattern has been seen).
- Make one state for each possible value of that memory, and write in words what each state means.
- Fill the transition for every state on every symbol, adding a dead state if some inputs can never lead to acceptance.
- Mark the final states and test two accepted and two rejected strings.
Opens chatgpt.com in a new tab with a ready-made prompt for this topic.
The question as asked
Two parities, four states
The machine must remember two things: whether the number of 0s seen so far is even or odd, and the same for 1s. That gives four combinations, so four states. Reading a 0 flips only the 0-parity; reading a 1 flips only the 1-parity.
Filling the table, one cell at a time
Every cell follows one rule: a 0 flips the first letter, a 1 flips the second letter.
Write this in the booklet
Let the states record (parity of 0s, parity of 1s): qEE,qEO,qOE,qOO. Then M=({qEE,qEO,qOE,qOO},{0,1},δ,qEE,{qEO}), where:
| State | 0 | 1 |
|---|---|---|
| → qEE | qOE | qEO |
| * qEO | qOO | qEE |
| qOE | qEE | qOO |
| qOO | qEO | qOE |
Check. 1: qEE →1 qEO, accepted (zero 0s, one 1). 001: qEE →0 qOE →0 qEE →1 qEO, accepted. 01: ends in qOO, rejected. 11: ends in qEE, rejected.
Formal description of DFA and NFA; DFA that never contains xxy
(1) Illustrate the formal description of a DFA and an NFA with suitable examples. (2) Construct a DFA that accepts all strings over {x,y} that never contain the substring xxy.
Part 1: the two 5-tuples
Part 1: one example of each
DFA example: the machine built in part 2 below, where every state has exactly one arrow on x and one on y.
Part 2: track how much of xxy has been read
q0 means no part of xxy is pending. q1 means we just read one x. q2 means we just read two or more x's.
Part 2: the diagram and two tests
Booklet answer · Formal description of DFA and NFA; DFA that never contains xxy
(1) DFA: M=(Q,Σ,δ,q0,F) with δ:Q×Σ→Q. NFA: the same tuple with δ:Q×Σ→2Q. Example DFA: the machine in (2). Example NFA: strings whose second-last symbol is 1, where δ(q0,1)={q0,q1}.
(2) States record the longest pending prefix of xxy.
| State | On x | On y | What the state remembers |
|---|---|---|---|
| → * q0 | q1 | q0 | no part of xxy pending |
| * q1 | q2 | q0 | just read one x |
| * q2 | q2 | q3 | just read xx or more x's |
| q3 | q3 | q3 | xxy has appeared: dead |
F = {q0, q1, q2}; q3 is the trap state reached exactly when xxy occurs.
Minimal DFA for aⁿbᵐ, n ≥ 2, m ≥ 1
Construct a minimal DFA for L={anbm:n≥2, m≥1}.
What the machine must check
At least two a's come first.
One state per stage
| State | On a | On b | What the state remembers |
|---|---|---|---|
| → q0 | q1 | qd | nothing read |
| q1 | q2 | qd | one a |
| q2 | q2 | q3 | two or more a's |
| * q3 | qd | q3 | a's then at least one b (final) |
| qd | qd | qd | dead: pattern broken |
Test two strings
The table is empty. Press Next step to fill it one row at a time (5 steps).
| Step | State | Read | Next state | Meaning of the next state |
|---|---|---|---|---|
Why no state can be removed
A DFA is minimal when every pair of states can be told apart by some string. For each pair, here is the shortest such string:
Booklet answer · Minimal DFA for aⁿbᵐ, n ≥ 2, m ≥ 1
States: q0 (start), q1 (one a), q2 (two or more a's), q3 (a's followed by at least one b, final) and qd (dead).
| State | On a | On b |
|---|---|---|
| → q0 | q1 | qd |
| q1 | q2 | qd |
| q2 | q2 | q3 |
| * q3 | qd | q3 |
| qd | qd | qd |
Minimality: every pair of states is distinguishable (for example q2 and q1 by the string b, q3 and qd by ε), so no two states can be merged. The minimal DFA has 5 states.
NFA with ε-moves
Conversion to a DFA: the start state is ECLOSE(q0), and δD(S,a)=ECLOSE(⋃p∈Sδ(p,a)). A DFA state is final if it contains an ε-NFA final state.
Opens chatgpt.com in a new tab with a ready-made prompt for this topic.
The question as asked
For the ε-NFA below, (1) compute the ε-closure of each state and (2) convert it to a DFA.
The table is empty. Press Next step to fill it one row at a time (3 steps).
| State | ε | a | b | c |
|---|---|---|---|---|
Closure, move, closure, step by step
Part 1, ε-closures. Start at the state, then follow ε-arrows as far as they go. Include the state itself, because taking zero ε-moves is allowed.
Worked answer
(1) ECLOSE(p)={p,q,r}, ECLOSE(q)={q}, ECLOSE(r)={r}.
(2) Start state A=ECLOSE(p)={p,q,r}.
δD(A,a)=ECLOSE(∅∪{p}∪∅)=ECLOSE(p)=A.
δD(A,b)=ECLOSE({q}∪{r})={q,r}=B.
δD(A,c)=ECLOSE({r}∪{p,q})={p,q,r}=A.
δD(B,a)=ECLOSE({p})=A, δD(B,b)=ECLOSE({r})={r}=C, δD(B,c)=ECLOSE({p,q})=A.
δD(C,x)=∅=D for every x, and D is a dead state.
| DFA state | a | b | c |
|---|---|---|---|
| →* A = {p,q,r} | A | B | A |
| * B = {q,r} | A | C | A |
| * C = {r} | D | D | D |
| D = ∅ | D | D | D |
A, B and C contain the final state r, so all three are final.
Minimising a DFA
Table-filling (Myhill–Nerode) algorithm:
- Remove states that cannot be reached from the start state.
- Draw a triangle of all pairs. Mark every pair with one final and one non-final state (round 0).
- For each unmarked pair (p, q) and each symbol a, if the pair (δ(p,a),δ(q,a)) is already marked, mark (p, q).
- Repeat step 3 until a full round marks nothing. Unmarked pairs are equivalent; merge them.
Opens chatgpt.com in a new tab with a ready-made prompt for this topic.
The question as asked
Use the table-filling algorithm to minimise the DFA below (start q0, final q2).
The table is empty. Press Next step to fill it one row at a time (8 steps).
| State | 0 | 1 |
|---|---|---|
The paper gives this as a diagram; this table is read directly from it.
Round by round, every pair checked
Below, every unmarked pair is checked in every round, including the ones that stay unmarked, so you can see exactly why each pair is or is not marked.
Building the minimal DFA from the classes
Name the classes A = {q0, q4}, B = {q1, q7}, C = {q2}, D = {q5}, E = {q6}. Fill each row using any one member of the class:
Write this in the booklet
q3 is unreachable and is deleted. After round 0 (pairs with q2), round 1 (11 pairs) and round 2 ((q0,q6) and (q4,q6)), the unmarked pairs are (q0,q4) and (q1,q7). So q0≡q4 and q1≡q7.
| Minimal state | 0 | 1 |
|---|---|---|
| → A = {q0,q4} | B | D |
| B = {q1,q7} | E | C |
| * C = {q2} | A | C |
| D = {q5} | C | E |
| E = {q6} | E | A |
The minimal DFA has 5 states instead of 8.
Mathematical induction
Opens chatgpt.com in a new tab with a ready-made prompt for this topic.
The question as asked
Every line of the algebra explained
First, check small cases so you trust the formula: n = 2 gives 1 + 4 = 5 and 62⋅3⋅5=5; n = 3 gives 1 + 4 + 9 = 14 and 63⋅4⋅7=14.
Write this in the booklet
Let P(n):∑i=1ni2=6n(n+1)(2n+1).
Basis (n = 1). LHS =12=1. RHS =61⋅2⋅3=1. So P(1) is true.
Inductive hypothesis. Assume P(k) is true for some k≥1: ∑i=1ki2=6k(k+1)(2k+1).
Inductive step. Then
which is P(k + 1). By the principle of mathematical induction, P(n) holds for all positive integers n.
Part A quick check
| Asked | Answer | Why |
|---|---|---|
| DFA transition function | δ:Q×Σ→Q | one next state for each (state, symbol) |
| A DFA cannot be represented as | an undirected graph | transitions have a direction |
| From each DFA state, on each symbol | exactly one transition | that is what deterministic means |
| NFA with N states → DFA has at most | 2N | one DFA state per subset |
| NFA, ε-NFA and DFA | accept the same class (regular languages) | each converts to a DFA |
| A language is regular iff | some DFA accepts it | definition used in this syllabus |
| NOT a property of a finite automaton | it has a stack | a stack belongs to a PDA |
| Main limitation of an FA | no unbounded memory (cannot count) | only finitely many states |
| States needed to simulate 3 words × 8 bits | 224 | every bit pattern is a state |
| Moore machine | FA with output attached to states | λ: Q → Δ |
| Mealy machine output depends on | present state and input | λ: Q × Σ → Δ |
| Compiler phase that uses finite automata | lexical analysis | tokens are regular |
| Min DFA (with dead state): 3rd symbol from the left is 1 | 5 states | q0, q1, q2, accept, dead |
| Min DFA: no two consecutive a's over {a,b} | 3 states | last not a, last a, dead |
| Reverse of a DFA | reverse arrows, swap start and final | the result is in general an NFA |
| ε-closure of a state always contains | the state itself | zero ε-moves is allowed |
| Subset construction: DFA final states | subsets containing an NFA final state | acceptance needs one accepting path |
Opens chatgpt.com in a new tab with a ready-made prompt for this topic.
Unit 1 checklist
Before you move on, can you do each of these without looking?
0 of 7 topics complete
Not sure about one? Go back to it in the topic list and press Next step through the worked question once more. Ticks are saved in this browser. Final checklist for the subject →
Unit complete. Up next: Unit 2, Regular Expressions & Context-Free Grammars.
All units