Skip to main content
⚡ Calmops

Finite Automata: DFA and NFA

Introduction

Finite automata are fundamental computational models that form the basis for understanding regular languages and pattern matching. They are used extensively in:

  • Lexical analysis in compilers
  • Pattern matching and searching
  • Protocol verification
  • Hardware design
  • Input validation

In this article, we’ll explore finite automata in depth, including both deterministic and non-deterministic variants.

Finite Automaton Basics

Definition

A finite automaton is a mathematical model consisting of:

  1. States (Q): A finite set of states
  2. Alphabet (Σ): A finite set of input symbols
  3. Transition Function (δ): Rules for state transitions
  4. Start State (q₀): Initial state
  5. Accept States (F): Set of accepting states

Formal Definition:

M = (Q, Σ, δ, q₀, F)

State Diagram Representation

States are represented as circles, transitions as labeled arrows.

Example:

Language: {a*b*}

    a
q0 ←→ q0
    ↓ b
    q1 ←→ q1
         b

q0: start state (double circle)
q1: accept state (double circle)

Deterministic Finite Automata (DFA)

Definition

A DFA has exactly one transition for each state-symbol pair.

Transition Function:

δ: Q × Σ → Q
For each (q, a), there is exactly one δ(q, a)

DFA Construction

Step 1: Identify states needed Step 2: Define transitions Step 3: Mark accept states Step 4: Verify with test strings

Example 1: Binary Strings Ending in 01

Language: {w | w ends with 01}

DFA:

States: q0, q1, q2
Alphabet: {0, 1}
Start: q0
Accept: q2

Transitions:
δ(q0, 0) = q1
δ(q0, 1) = q0
δ(q1, 0) = q1
δ(q1, 1) = q2
δ(q2, 0) = q1
δ(q2, 1) = q0

Test Strings:

"01" → q0 → q1 → q2 ✓ (accept)
"101" → q0 → q0 → q1 → q2 ✓ (accept)
"10" → q0 → q0 → q1 ✗ (reject)

Example 2: Strings with Even Number of 0’s

Language: {w | w has even number of 0’s}

DFA:

States: q_even, q_odd
Alphabet: {0, 1}
Start: q_even
Accept: q_even

Transitions:
δ(q_even, 0) = q_odd
δ(q_even, 1) = q_even
δ(q_odd, 0) = q_even
δ(q_odd, 1) = q_odd

Test Strings:

"" → q_even ✓ (accept, 0 zeros)
"0" → q_even → q_odd ✗ (reject, 1 zero)
"00" → q_even → q_odd → q_even ✓ (accept, 2 zeros)
"010" → q_even → q_even → q_odd → q_even ✓ (accept, 2 zeros)

Non-Deterministic Finite Automata (NFA)

Definition

An NFA can have zero, one, or multiple transitions for each state-symbol pair.

Transition Function:

δ: Q × Σ → P(Q)
δ(q, a) = {q₁, q₂, ...} (set of possible next states)

Epsilon Transitions

NFAs can have epsilon (ε) transitions that don’t consume input.

Extended Transition Function:

δ(q, ε) = set of states reachable via ε-transitions

Example 1: Strings Containing “01” or “10”

Language: {w | w contains “01” or “10”}

NFA:

States: q0, q1, q2, q3, q4
Alphabet: {0, 1}
Start: q0
Accept: q3, q4

Transitions:
δ(q0, 0) = {q0, q1}
δ(q0, 1) = {q0, q2}
δ(q1, 1) = {q3}
δ(q2, 0) = {q4}
δ(q3, 0) = {q3}
δ(q3, 1) = {q3}
δ(q4, 0) = {q4}
δ(q4, 1) = {q4}

Example 2: Strings Ending in “01” or “10”

Language: {w | w ends with “01” or “10”}

NFA with ε-transitions:

States: q0, q1, q2, q3, q4, q5
Alphabet: {0, 1}
Start: q0
Accept: q3, q5

Transitions:
δ(q0, 0) = {q0, q1}
δ(q0, 1) = {q0, q2}
δ(q1, 1) = {q3}
δ(q2, 0) = {q5}
δ(q3, ε) = {q0}
δ(q5, ε) = {q0}

NFA to DFA Conversion

Subset Construction Algorithm

Algorithm:

1. Start with DFA state {q₀}
2. For each DFA state S and symbol a:
   a. Compute δ_NFA(q, a) for all q in S
   b. Union all results
   c. Include ε-closure
   d. This is the new DFA state
3. Accept states are DFA states containing NFA accept states

Example: Converting NFA to DFA

Original NFA:

States: q0, q1, q2
Alphabet: {a, b}
Start: q0
Accept: q2

Transitions:
δ(q0, a) = {q0, q1}
δ(q0, b) = {q0}
δ(q1, b) = {q2}

Conversion Process:

DFA State {q0}:
  On 'a': {q0, q1}
  On 'b': {q0}

DFA State {q0, q1}:
  On 'a': {q0, q1}
  On 'b': {q0, q2}

DFA State {q0, q2}:
  On 'a': {q0, q1}
  On 'b': {q0}

DFA State {q0, q1, q2}:
  On 'a': {q0, q1}
  On 'b': {q0, q2}

Resulting DFA:

States: {q0}, {q0,q1}, {q0,q2}, {q0,q1,q2}
Start: {q0}
Accept: {q0,q2}, {q0,q1,q2}

DFA Minimization

Equivalence of States

Two states are equivalent if:

  1. Both are accept or both are non-accept
  2. For all symbols, transitions lead to equivalent states

Minimization Algorithm

Step 1: Remove unreachable states Step 2: Partition states into accept/non-accept Step 3: Refine partitions until stable Step 4: Merge equivalent states

Example: Minimizing a DFA

Original DFA:

States: q0, q1, q2, q3, q4
Transitions:
δ(q0, a) = q1
δ(q0, b) = q2
δ(q1, a) = q3
δ(q1, b) = q4
δ(q2, a) = q3
δ(q2, b) = q4
δ(q3, a) = q3
δ(q3, b) = q3
δ(q4, a) = q4
δ(q4, b) = q4
Accept: q3, q4

Minimization:

Initial partition: {q0, q1, q2}, {q3, q4}

Refine on 'a':
  q0 → q1 (non-accept)
  q1 → q3 (accept)
  q2 → q3 (accept)
  q3 → q3 (accept)
  q4 → q4 (accept)
  
New partition: {q0}, {q1, q2}, {q3, q4}

Refine on 'b':
  q0 → q2 (non-accept)
  q1 → q4 (accept)
  q2 → q4 (accept)
  q3 → q3 (accept)
  q4 → q4 (accept)
  
Final partition: {q0}, {q1, q2}, {q3, q4}

Minimized DFA:

States: q0, q12, q34
Transitions:
δ(q0, a) = q12
δ(q0, b) = q12
δ(q12, a) = q34
δ(q12, b) = q34
δ(q34, a) = q34
δ(q34, b) = q34
Accept: q34

Complexity Analysis

Time Complexity

Operation DFA NFA
Recognition O(n) O(n²)
Construction O(n) O(n)
Minimization O(n²) N/A
Conversion N/A O(2ⁿ)

Space Complexity

Aspect DFA NFA
States O(2ⁿ) O(n)
Transitions O(n) O(n)

Glossary

  • Finite automaton: Machine with finite states
  • DFA: Deterministic finite automaton
  • NFA: Non-deterministic finite automaton
  • Transition function: Rules for state changes
  • Accept state: State indicating acceptance
  • Epsilon transition: Transition without consuming input
  • Subset construction: Converting NFA to DFA
  • Minimization: Reducing number of states
  • Equivalence: States with same behavior
  • Reachable state: State reachable from start state

Practice Problems

Problem 1: DFA Design

Design a DFA for strings over {0,1} with even number of 1’s.

Solution:

States: q_even, q_odd
Start: q_even
Accept: q_even

Transitions:
δ(q_even, 0) = q_even
δ(q_even, 1) = q_odd
δ(q_odd, 0) = q_odd
δ(q_odd, 1) = q_even

Problem 2: NFA Design

Design an NFA for strings ending in “00” or “11”.

Solution:

States: q0, q1, q2, q3, q4
Start: q0
Accept: q2, q4

Transitions:
δ(q0, 0) = {q0, q1}
δ(q0, 1) = {q0, q3}
δ(q1, 0) = {q2}
δ(q3, 1) = {q4}

Problem 3: NFA to DFA

Convert the NFA from Problem 2 to a DFA.

Solution:

DFA States: {q0}, {q0,q1}, {q0,q3}, {q0,q1,q3}, {q2}, {q4}, ...
Start: {q0}
Accept: {q2}, {q4}, {q0,q1,q2}, {q0,q3,q4}, ...

Problem 4: Minimization

Minimize the DFA from Problem 1.

Solution:

Already minimal (2 states, both needed)

Online Platforms

Interactive Tools

  • “Introduction to Automata Theory, Languages, and Computation” by Hopcroft, Motwani, Ullman
  • “Theory of Computation” by Sipser
  • “Elements of the Theory of Computation” by Lewis & Papadimitriou
  • “Formal Languages and Their Relation to Automata” by Hopcroft & Ullman
  • “Computability and Complexity” by Papadimitriou

Academic Journals

Software Tools

Conclusion

Finite automata are fundamental computational models:

  • DFAs provide efficient recognition with O(n) time
  • NFAs are easier to construct but require conversion
  • Minimization reduces complexity
  • Equivalence between DFA and NFA is proven

Understanding finite automata is essential for compiler design, pattern matching, and formal language theory.

In the next article, we’ll explore deterministic vs non-deterministic automata in more depth.


Next Article: Deterministic vs Non-Deterministic Automata

Comments