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