NFA to DFA Conversion 

WhatsApp Channel Join Now
Program to Implement NFA with epsilon move to DFA Conversion - GeeksforGeeks

In Automata Theory, NFA to DFA conversion is one of the most important topics. Every Non-Deterministic Finite Automaton (NFA) can be converted into an equivalent Deterministic Finite Automaton (DFA) using the Subset Construction Method.
This conversion helps in designing compilers, pattern matchers, lexical analyzers, and many formal language applications.

What is NFA?

NFA (Non-Deterministic Finite Automaton) is an automaton where:

  • For one input symbol, multiple transitions are allowed.
  • ε-transitions (empty transitions) may exist.
  • It is easier to design, but harder to execute directly.

Formal Definition of NFA

An NFA is a 5-tuple:
N = (Q, Σ, δ, q₀, F)
Where:

  • Q = Set of states
  • Σ = Input symbols
  • δ = Transition function (Q × Σ → 2^Q)
  • q₀ = Start state
  • F = Final states

What is DFA?

DFA (Deterministic Finite Automaton) is an automaton where:

  • For each symbol, only one unique transition exists.
  • No ε-transition is allowed.
  • It is fast, predictable, and used in real systems.

Formal Definition of DFA

DFA is also a 5-tuple:
D = (Q, Σ, δ, q₀, F)
But here,
δ: Q × Σ → Q (single unique transition)

Why Do We Convert NFA to DFA?

Converting NFA → DFA is important because:

  • DFA is required by lexical analyzers and compilers.
  • DFA is faster and more reliable than NFA.
  • NFA is easy to design, DFA is easy to execute.
  • Every NFA has an equivalent DFA (proved by theory).

NFA to DFA Conversion Method

This method is known as the Subset Construction Algorithm.

Steps of Subset Construction

  1. Start State of DFA = ε-closure(start state of NFA).
    If no ε, then simply δ(q₀).
  2. Create new DFA states by combining sets of NFA states.
    Example: {q0, q1}
  3. For each newly created DFA state:
    • Apply input symbols (0,1 or a,b etc.)
    • Find all possible transitions.
    • Take ε-closure if needed.
    • Create new DFA states until no new states appear.
  4. Final States of DFA = Any DFA state containing at least one NFA final state.

NFA to DFA Conversion Example

Step 01: Draw NFA Graph

The following is the NFA graph that needs to be converted to a DFA.

Step 02: Draw the NFA Transition Table.

The following is the NFA transition table, which is derived from the given NFA.

Note: All transitions are made according to the NFA transition table to get the DFA transition table.

Step 03: Conversion of NFA to DFA Transition Table

Let’s start the conversion of the NFA transition table to the DFA transition Table.

Step 3.1: Select the first two rows of the NFA transition table, which will become the first two rows of the DFA transition table given below.

In the above DFA Table

  • State Column Contains: “q0”
  • Input columns contains: “q0,q1”, “q1”

Step 3.2: States “q0q1” and “q1” are newly added because these states appear in the input columns of the DFA but do not exist in the state column. Include “q0q1” and “q1” in the state column along with their transitions.

 Transition calculations for state “q0q1

For input “a”

                      δ([q0q1], a) = δ(q0, a) ∪ δ(q1, a)  

                                        = {q0q1} ∪ {ϕ}  

                                      = {q0q1}

For input “b”

                   δ([q0q1], b) = δ(q0, b) ∪ δ(q1, b)  

                                      = {q1} ∪ {ϕ}  

                                     = {q1}

 Transition calculations for state “q1

For input “a”

                    δ([q1], a) = {ϕ}

For input “b”

                    δ([q1], b) = {ϕ}

The updated DFA table is given below.

In the above DFA Table

  • State Column Contains: “q0”, “q0q1”, “q1”
  • Input columns contain: “q0”, “q0q1”, “q1”

Repeat Step 3.2: No new state exists in the DFA table, except ϕ. Transition at  “ϕ” against all inputs will always stay at  “ϕ”. The updated DFA Table is given below

At this stage, no new state remains for the state column. The DFA table is one step away from being complete.

Step 3.3: State “q1” was the final state in the NFA Table. That’s why all those states will be the final states where “q1” is present in the DFA state column. Simply mark with “*” to represent the final state. 

The following is the updated DFA table with all  its final states

Step 4: Now draw the DFA according to the DFA transition table

The following is the converted DFA from the given NFA.

Convert NFA to DFA - DFA (Converted From Give NFA)

Advantages of Converting NFA to DFA

  • DFA has no ambiguity
  • Faster execution
  • Used in regex engines, compilers, tokenizers
  • Easier to implement in software/hardware

Similar Posts