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
a b a b b Input: Read Head Finite Control (States)

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"

q₀ q₁ q₂ q1 --> 0 q2 --> 1 q0 (self loop) --> 1 q1 (self loop) --> 0 q0 --> 1 q1 --> 0

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

Pattern 1: Modulo/Divisibility

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₂
Pattern 2: Substring Matching

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)
Pattern 3: Counting Parity

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)
Example

NFA for strings ending with "01" or "10"

In NFA, we can "guess" which pattern we're looking for:

q₀ q₁ q₂ q₃ q₄ 0,1 0 1 1 0

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}

Example

ε-NFA for (a+b)*ab

q₀ q₁ q₂ q₃ ε a b a,b

ε-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)

i ε f

RE: a (single symbol)

i a f

Inductive Cases

Union: R₁ + R₂

Create new start and final states. Use ε-transitions to branch to both NFAs and merge back.

i NFA for R₁ NFA for R₂ f ε ε ε ε

Concatenation: R₁R₂

Connect final of R₁ to start of R₂ with ε-transition.

NFA for R₁ NFA for R₂ ε

Kleene Star: R*

Add new start/final with ε-loops for repetition and skip.

i NFA for R f ε ε ε (skip) ε (repeat)

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.

Complete Example

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)

A B C B --> a C --> b A (self loop) --> b B (self loop) --> a B --> a A --> b

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.

Example

Minimize DFA with states {A, B, C, D, E}

Where A is start, C and E are final.

ABCD
B
CXX
DX
EXXX

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
Exam Pattern Example

Design Moore/Mealy to convert "abb" to "aba"

Strategy:

  1. Track the pattern "abb" as we read input
  2. Output each character as-is until we complete "abb"
  3. 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:

q₀ q₁ q₂ q₃ q1 --> a/a q2 --> b/b q3 (OUTPUT CHANGED!) --> b/a b/b q1 --> a/a q1 --> a/a a/a b/b

Mealy Machine for converting "abb" to "aba"

Trace for input "aabbabb":

Inputaabbabb
Stateq₀→q₁q₁→q₁q₁→q₂q₂→q₃q₃→q₁q₁→q₂q₂→q₃
Outputaabaaba

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.

Example

Moore to Mealy

Moore Machine:

StateOutputδ(0)δ(1)
q₀0q₁q₀
q₁1q₀q₁

Mealy Machine:

State01
q₀q₁/1q₀/0
q₁q₀/0q₁/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

What is Finite Automata? List its limitations. 5 Marks

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:

  1. Finite Memory: Can only remember current state, not history
  2. Cannot Count: Cannot count unbounded occurrences (e.g., aⁿbⁿ)
  3. No Stack: Cannot handle nested structures
  4. Limited Language Class: Can only recognize regular languages
  5. Cannot Compare: Cannot compare arbitrary substrings
Distinguish between NFA and DFA 5 Marks
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)
Differentiate between Moore and Mealy Machines 5 Marks
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

Design DFA for binary numbers divisible by 5 (excluding leading zeros) 10 Marks

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):

State01
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₀ ✓
Design NFA for strings containing "000" or "010" and convert to DFA 10 Marks

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 StateNFA States01
→A{q₀}BA
B{q₀,q₁,q₃}CD
C{q₀,q₁,q₂,q₃}ED
D{q₀,q₄}FA
*E{q₀,q₁,q₂,q₃,q₆}EG
*F{q₀,q₁,q₃,q₆}EG
*G{q₀,q₄,q₆}FH
*H{q₀,q₆}FH

Final states: E, F, G, H (all contain q₆)

Design Moore and Mealy machines to convert "aaa" to "bbb" 10 Marks

Strategy:

  1. Track consecutive 'a's seen
  2. Output 'a' or 'b' normally until "aaa" pattern
  3. 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:

Statea (next/out)b (next/out)
q₀q₁/aq₀/b
q₁q₂/aq₀/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.

Design Moore machine for vowel sequence: output same except 'ei' → 'u' 10 Marks

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"