Finite Automata
Exam Importance: Very High
Finite Automata questions appear in 100% of papers. Expect 10-mark design questions on DFA, NFA to DFA conversion, and Moore/Mealy machines. This is the most frequently tested module!
Introduction to Finite Automata
What is a Finite Automaton?
A Finite Automaton (FA) is the simplest model of computation. It consists of a finite number of states and transitions between them, used to recognize patterns in input strings.
FA as a Language Acceptor
A finite automaton reads an input string symbol by symbol, transitioning between states. After reading the entire input:
- If it ends in an accepting state → String is ACCEPTED
- If it ends in a non-accepting state → String is REJECTED
Structure of a Finite Automaton: Input tape and finite control
Types of Finite Automata
| Type | Description | Key Property |
|---|---|---|
| DFA | Deterministic Finite Automaton | Exactly one transition per symbol from each state |
| NFA | Non-deterministic Finite Automaton | Multiple transitions possible, including none |
| ε-NFA | NFA with epsilon transitions | Can move without reading input |
Deterministic Finite Automaton (DFA)
Formal Definition
A DFA is a 5-tuple: M = (Q, Σ, δ, q₀, F) where:
- Q = Finite set of states
- Σ = Input alphabet (finite set of symbols)
- δ = Transition function: Q × Σ → Q
- q₀ = Initial/Start state (q₀ ∈ Q)
- F = Set of final/accepting states (F ⊆ Q)
Key Property of DFA
For every state q and every symbol a, there is exactly one transition δ(q, a). No ambiguity!
Example: DFA for strings ending with "01"
DFA accepting strings over {0,1} that end with "01"
Transition Table
| State | 0 | 1 |
|---|---|---|
| → q₀ | q₁ | q₀ |
| q₁ | q₁ | q₂ |
| *q₂ | q₁ | q₀ |
Note: → indicates start state, * indicates final state
DFA Design Patterns
Binary numbers divisible by N
Key Insight: Use N states representing remainders 0 to N-1.
Transition Rule: New remainder = (2 × current + input bit) mod N
Example: For divisibility by 3:
- States: q₀ (remainder 0), q₁ (remainder 1), q₂ (remainder 2)
- q₀ is both start and final state
- δ(q₀, 0) = q₀, δ(q₀, 1) = q₁
- δ(q₁, 0) = q₂, δ(q₁, 1) = q₀
- δ(q₂, 0) = q₁, δ(q₂, 1) = q₂
Strings containing a specific pattern
Key Insight: States track how much of the pattern has been seen.
For pattern "011":
- q₀: Nothing matched
- q₁: Seen "0"
- q₂: Seen "01"
- q₃: Seen "011" (final - stay here)
Even/Odd count of symbols
For even number of 1s:
- 2 states: Even (q₀, final), Odd (q₁)
- On 1: Toggle between states
- On 0: Stay in same state
Non-deterministic Finite Automaton (NFA)
Formal Definition
An NFA is a 5-tuple: M = (Q, Σ, δ, q₀, F) where:
- Q = Finite set of states
- Σ = Input alphabet
- δ = Transition function: Q × Σ → P(Q) (power set of Q)
- q₀ = Initial state
- F = Set of final states
DFA vs NFA
| Aspect | DFA | NFA |
|---|---|---|
| Transitions per symbol | Exactly one | Zero, one, or many |
| δ return type | Single state (Q) | Set of states (P(Q)) |
| Acceptance | End in final state | At least one path ends in final |
| Implementation | Easier to implement | Easier to design |
| Expressive Power | Equivalent! (Same languages) | |
NFA for strings ending with "01" or "10"
In NFA, we can "guess" which pattern we're looking for:
The NFA non-deterministically chooses to look for "01" or "10" at any point.
NFA with ε-Transitions (ε-NFA)
ε-Transition
An ε-transition (epsilon transition) allows the automaton to change state without reading any input symbol. The machine can "spontaneously" move to another state.
ε-Closure
The ε-closure of a state q is the set of all states reachable from q using only ε-transitions (including q itself).
ε-closure(q) = {q} ∪ {all states reachable via ε from q}
ε-NFA for (a+b)*ab
ε-closure(q₀) = {q₀, q₁}
Regular Expression to NFA (Thompson's Construction)
Thompson's Construction provides a systematic way to convert any RE to an equivalent ε-NFA.
Base Cases
RE: ε (empty string)
RE: a (single symbol)
Inductive Cases
Union: R₁ + R₂
Create new start and final states. Use ε-transitions to branch to both NFAs and merge back.
Concatenation: R₁R₂
Connect final of R₁ to start of R₂ with ε-transition.
Kleene Star: R*
Add new start/final with ε-loops for repetition and skip.
NFA to DFA Conversion (Subset Construction)
Exam Importance
This algorithm appears frequently! The question often asks to design an NFA and then convert it to DFA. Practice this thoroughly.
Subset Construction Algorithm
Each DFA state represents a set of NFA states. The DFA simulates being in multiple NFA states simultaneously.
Algorithm Steps
Initialize
DFA start state = ε-closure(NFA start state)
For each unmarked DFA state S:
For each input symbol a:
- Find all NFA states reachable from S on input a
- Take ε-closure of the result
- This becomes a new DFA state (if not already exists)
Mark Final States
Any DFA state containing an NFA final state becomes a DFA final state.
Convert NFA for "(a+b)*ab" to DFA
NFA States: q₀ (start), q₁, q₂ (final)
Transitions:
- δ(q₀, a) = {q₀, q₁}
- δ(q₀, b) = {q₀}
- δ(q₁, b) = {q₂}
Subset Construction:
| DFA State | NFA States | a | b |
|---|---|---|---|
| → A | {q₀} | B | A |
| B | {q₀, q₁} | B | C |
| *C | {q₀, q₂} | B | A |
State C is final because it contains q₂ (NFA final state)
Resulting DFA after subset construction
DFA Minimization (Reduced DFA)
Minimum State DFA
A minimal DFA is one with the fewest states that accepts the same language. Any two minimal DFAs for the same language are isomorphic (same structure, different labels).
Table-Filling Algorithm (Myhill-Nerode)
Remove Unreachable States
Remove any state not reachable from the start state.
Create Distinguishability Table
Create a table with all pairs of states (i, j) where i < j.
Mark Obviously Distinguishable Pairs
Mark all pairs where one state is final and other is non-final.
Iteratively Mark More Pairs
For unmarked pair (p, q): if δ(p, a) and δ(q, a) are already marked for some input a, then mark (p, q).
Repeat until no more pairs can be marked.
Merge Unmarked Pairs
All unmarked pairs represent equivalent states. Merge them into one state.
Minimize DFA with states {A, B, C, D, E}
Where A is start, C and E are final.
| A | B | C | D | |
| B | ||||
| C | X | X | ||
| D | X | |||
| E | X | X | X |
X marks distinguishable pairs. Unmarked: (A,B), (A,D), (B,D), (C,E)
Equivalence classes: {A,B,D}, {C,E}
Finite Automaton to Regular Expression
State Elimination Method
Add new start and final states
Add new start state s with ε-transition to old start. Add new final state f with ε-transitions from all old final states.
Eliminate states one by one
For each state to eliminate, update the transitions between remaining states to include the paths through the eliminated state as regular expressions.
Final RE
When only s and f remain, the label on the edge s→f is the RE.
State Elimination Formula
When eliminating state q with:
• Self-loop: R (transition from q to q)
• Incoming edge: R₁ (from p to q)
• Outgoing edge: R₂ (from q to r)
New edge p→r: R₁ · R* · R₂
Moore and Mealy Machines
Exam Importance: Very High
Moore/Mealy design questions appear in every paper (100% frequency). Common patterns: sequence transformation (abb→aba, aaa→bbb), pattern detection, and inter-conversion.
Unlike DFA/NFA which only accept/reject strings, Transducers produce output. They are Finite State Machines with Output.
Moore Machine
M = (Q, Σ, Δ, δ, λ, q₀)
- Q = Finite set of states
- Σ = Input alphabet
- Δ = Output alphabet
- δ = Transition function: Q × Σ → Q
- λ = Output function: Q → Δ
- q₀ = Initial state
Output depends on STATE only!
Mealy Machine
M = (Q, Σ, Δ, δ, λ, q₀)
- Q = Finite set of states
- Σ = Input alphabet
- Δ = Output alphabet
- δ = Transition function: Q × Σ → Q
- λ = Output function: Q × Σ → Δ
- q₀ = Initial state
Output depends on STATE and INPUT!
Key Differences
| Aspect | Moore Machine | Mealy Machine |
|---|---|---|
| Output associated with | States | Transitions |
| Output timing | After entering state | During transition |
| Output length | |output| = |input| + 1 | |output| = |input| |
| Number of states | Generally more | Generally fewer |
| Diagram notation | Output inside state circle | Output on transition edge |
Design Moore/Mealy to convert "abb" to "aba"
Strategy:
- Track the pattern "abb" as we read input
- Output each character as-is until we complete "abb"
- When "abb" is complete, output 'a' instead of 'b'
States needed:
- q₀: Nothing matched (start)
- q₁: Seen 'a'
- q₂: Seen 'ab'
- q₃: Seen 'abb' (trigger replacement)
Mealy Machine:
Mealy Machine for converting "abb" to "aba"
Trace for input "aabbabb":
| Input | a | a | b | b | a | b | b |
|---|---|---|---|---|---|---|---|
| State | q₀→q₁ | q₁→q₁ | q₁→q₂ | q₂→q₃ | q₃→q₁ | q₁→q₂ | q₂→q₃ |
| Output | a | a | b | a | a | b | a |
Output: "aabaaaba" (each "abb" replaced with "aba")
Moore-Mealy Conversion
Mealy to Moore Conversion
Identify Output Variations
For each Mealy state, check if incoming transitions have different outputs.
Split States
If a state is entered with different outputs, split it into multiple states (one per unique output).
Assign Output to States
Each new state gets the output from its incoming transitions.
Update Transitions
Redirect transitions to the appropriate split state.
Moore to Mealy Conversion
Move Output to Transitions
For each transition into state q, attach q's output to that transition.
Simplify
The Moore machine's output function is no longer needed. Result is usually simpler.
Moore to Mealy
Moore Machine:
| State | Output | δ(0) | δ(1) |
|---|---|---|---|
| q₀ | 0 | q₁ | q₀ |
| q₁ | 1 | q₀ | q₁ |
Mealy Machine:
| State | 0 | 1 |
|---|---|---|
| q₀ | q₁/1 | q₀/0 |
| q₁ | q₀/0 | q₁/1 |
Format: next_state/output
Limitations of Finite Automata
Why FA is Limited
Finite automata have finite memory (only the current state). They cannot:
- Count unbounded quantities
- Compare arbitrary-length substrings
- Remember unbounded history
Languages NOT Recognizable by FA
| Language | Why FA Cannot Recognize |
|---|---|
| L = {aⁿbⁿ | n ≥ 0} | Needs to count a's and match with b's (unbounded counter) |
| L = {ww | w ∈ Σ*} | Needs to remember entire first half |
| Palindromes | Needs to compare first and second half |
| Balanced parentheses | Needs to count nesting depth |
| L = {aⁿbⁿcⁿ | n ≥ 0} | Needs multiple counters |
Common Mistake
FA CAN recognize bounded counting (e.g., "exactly 3 a's"). It CANNOT recognize unbounded counting (e.g., "equal number of a's and b's for any length").
Practice Questions
5 Mark Questions
Finite Automata (FA): A finite automaton is a mathematical model of computation consisting of:
- A finite set of states
- An input alphabet
- A transition function
- A start state
- A set of accepting states
FA reads input symbol by symbol and changes state according to transition function. If it ends in an accepting state, the string is accepted.
Limitations:
- Finite Memory: Can only remember current state, not history
- Cannot Count: Cannot count unbounded occurrences (e.g., aⁿbⁿ)
- No Stack: Cannot handle nested structures
- Limited Language Class: Can only recognize regular languages
- Cannot Compare: Cannot compare arbitrary substrings
| Aspect | DFA | NFA |
|---|---|---|
| Definition | δ: Q × Σ → Q | δ: Q × Σ → P(Q) |
| Transitions | Exactly one per symbol | Zero, one, or many |
| ε-transitions | Not allowed | Allowed |
| Acceptance | Single path to final state | Any path to final state |
| Implementation | Easier (direct simulation) | Harder (backtracking/subset) |
| Design | More complex | Often simpler |
| Power | Equivalent (recognize same languages) | |
| Aspect | Moore Machine | Mealy Machine |
|---|---|---|
| Output function | λ: Q → Δ | λ: Q × Σ → Δ |
| Output depends on | Current state only | Current state AND input |
| Output associated with | States | Transitions |
| Output length | n + 1 for input length n | n for input length n |
| States required | Generally more | Generally fewer |
| Output timing | Synchronous (after state change) | Asynchronous (during transition) |
Diagram representation:
- Moore: Output written inside state circle (e.g., q₀/0)
- Mealy: Output written on transition (e.g., a/0)
10 Mark Questions
Logic:
- Use 5 states for remainders 0, 1, 2, 3, 4
- Transition rule: new_remainder = (2 × current + input) mod 5
- Handle leading zeros specially
States:
- q₀: Start state (remainder 0, also handles "0" case)
- q₁: Remainder 1
- q₂: Remainder 2
- q₃: Remainder 3
- q₄: Remainder 4
- qₑ: Dead state for invalid leading zeros
Transitions (for valid numbers):
| State | 0 | 1 |
|---|---|---|
| q₀ | q₀ | q₁ |
| q₁ | q₂ | q₃ |
| q₂ | q₄ | q₀ |
| q₃ | q₁ | q₂ |
| q₄ | q₃ | q₄ |
Final State: q₀ (remainder 0 = divisible by 5)
For excluding leading zeros:
Add dead state qₑ where "0" at start goes to qₑ, and qₑ is a trap.
The string "0" can be accepted as special case (0 is divisible by 5).
Verification:
- 101 (5): q₀ →¹ q₁ →⁰ q₂ →¹ q₀ ✓
- 1010 (10): q₀ →¹ q₁ →⁰ q₂ →¹ q₀ →⁰ q₀ ✓
- 1111 (15): q₀ →¹ q₁ →¹ q₃ →¹ q₂ →¹ q₀ ✓
NFA Design:
States: q₀ (start), q₁, q₂, q₃, q₄, q₅, q₆ (final)
Transitions:
- q₀: Self-loop on 0,1 (waiting)
- Path for "000": q₀ →⁰ q₁ →⁰ q₂ →⁰ q₆
- Path for "010": q₀ →⁰ q₃ →¹ q₄ →⁰ q₆
- q₆: Self-loop on 0,1 (stay in final)
Subset Construction:
| DFA State | NFA States | 0 | 1 |
|---|---|---|---|
| →A | {q₀} | B | A |
| B | {q₀,q₁,q₃} | C | D |
| C | {q₀,q₁,q₂,q₃} | E | D |
| D | {q₀,q₄} | F | A |
| *E | {q₀,q₁,q₂,q₃,q₆} | E | G |
| *F | {q₀,q₁,q₃,q₆} | E | G |
| *G | {q₀,q₄,q₆} | F | H |
| *H | {q₀,q₆} | F | H |
Final states: E, F, G, H (all contain q₆)
Strategy:
- Track consecutive 'a's seen
- Output 'a' or 'b' normally until "aaa" pattern
- When third 'a' seen, output 'b' and reset
States:
- q₀: No consecutive a's
- q₁: One 'a' seen
- q₂: Two 'a's seen
Mealy Machine:
| State | a (next/out) | b (next/out) |
|---|---|---|
| q₀ | q₁/a | q₀/b |
| q₁ | q₂/a | q₀/b |
| q₂ | q₀/b (replacement!) | q₀/b |
Trace for "aaaab":
a: q₀→q₁, out=a
a: q₁→q₂, out=a
a: q₂→q₀, out=b
a: q₀→q₁, out=a
b: q₁→q₀, out=b
Output: "aabab"
Moore Machine:
Need to split states based on output. States become: q₀(a), q₀(b), q₁(a), q₁(b), q₂(a), q₂(b)
Or simpler: use states to encode both position AND pending output.
This is from May 2025 paper - complex vowel problem!
Alphabet: Σ = {a, e, i, o, u}
Rule: Output each vowel as-is, except when 'i' follows 'e', output 'u' instead
States:
- q₀: Start/normal state, output nothing initially
- qₐ: Just saw 'a', output 'a'
- qₑ: Just saw 'e', output 'e' (CRITICAL - watch for 'i')
- qᵢ: Just saw 'i' (not after 'e'), output 'i'
- qₒ: Just saw 'o', output 'o'
- qᵤ: Just saw 'u', output 'u'
- qₑᵢ: Just saw 'i' after 'e', output 'u' (REPLACEMENT)
Key Transitions:
- From qₑ on input 'i' → go to qₑᵢ (output 'u')
- From qₑ on any other vowel → go to appropriate state
- From all other states on 'i' → go to qᵢ (output 'i')
Trace for "aeiou":
- a: q₀→qₐ, Moore outputs: a
- e: qₐ→qₑ, Moore outputs: e
- i: qₑ→qₑᵢ, Moore outputs: u
- o: qₑᵢ→qₒ, Moore outputs: o
- u: qₒ→qᵤ, Moore outputs: u
Final output: "aeuou"