The Wayback Machine - https://web.archive.org/web/20240607080327/https://www.geeksforgeeks.org/conversion-from-nfa-to-dfa/
Open In App

Conversion from NFA to DFA

Last Updated : 20 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

An NFA can have zero, one or more than one move from a given state on a given input symbol. An NFA can also have NULL moves (moves without input symbol). On the other hand, DFA has one and only one move from a given state on a given input symbol. 

Steps for converting NFA to DFA:

Step 1: Convert the given NFA to its equivalent transition table
To convert the NFA to its equivalent transition table, we need to list all the states, input symbols, and the transition rules. The transition rules are represented in the form of a matrix, where the rows represent the current state, the columns represent the input symbol, and the cells represent the next state. 

Step 2: Create the DFA’s start state
The DFA’s start state is the set of all possible starting states in the NFA. This set is called the “epsilon closure” of the NFA’s start state. The epsilon closure is the set of all states that can be reached from the start state by following epsilon (?) transitions.

Step 3: Create the DFA’s transition table
The DFA’s transition table is similar to the NFA’s transition table, but instead of individual states, the rows and columns represent sets of states. For each input symbol, the corresponding cell in the transition table contains the epsilon closure of the set of states obtained by following the transition rules in the NFA’s transition table.

Step 4: Create the DFA’s final states
The DFA’s final states are the sets of states that contain at least one final state from the NFA.

Step 5: Simplify the DFA
The DFA obtained in the previous steps may contain unnecessary states and transitions. To simplify the DFA, we can use the following techniques:

  • Remove unreachable states: States that cannot be reached from the start state can be removed from the DFA.
  • Remove dead states: States that cannot lead to a final state can be removed from the DFA.
  • Merge equivalent states: States that have the same transition rules for all input symbols can be merged into a single state.

Step 6: Repeat steps 3-5 until no further simplification is possible
After simplifying the DFA, we repeat steps 3-5 until no further simplification is possible. The final DFA obtained is the minimized DFA equivalent to the given NFA.

Example: Consider the following NFA shown in Figure 1. 

 

Following are the various parameters for NFA. Q = { q0, q1, q2 } ? = ( a, b ) F = { q2 } ? (Transition Function of NFA) 

 

Step 1: Q’ = ? Step 2: Q’ = {q0} Step 3: For each state in Q’, find the states for each input symbol. Currently, state in Q’ is q0, find moves from q0 on input symbol a and b using transition function of NFA and update the transition table of DFA. ?’ (Transition Function of DFA) 

 

Now { q0, q1 } will be considered as a single state. As its entry is not in Q’, add it to Q’. So Q’ = { q0, { q0, q1 } } Now, moves from state { q0, q1 } on different input symbols are not present in transition table of DFA, we will calculate it like: ?’ ( { q0, q1 }, a ) = ? ( q0, a ) ? ? ( q1, a ) = { q0, q1 } ?’ ( { q0, q1 }, b ) = ? ( q0, b ) ? ? ( q1, b ) = { q0, q2 } Now we will update the transition table of DFA. ?’ (Transition Function of DFA) 

 

Now { q0, q2 } will be considered as a single state. As its entry is not in Q’, add it to Q’. So Q’ = { q0, { q0, q1 }, { q0, q2 } } Now, moves from state {q0, q2} on different input symbols are not present in transition table of DFA, we will calculate it like: ?’ ( { q0, q2 }, a ) = ? ( q0, a ) ? ? ( q2, a ) = { q0, q1 } ?’ ( { q0, q2 }, b ) = ? ( q0, b ) ? ? ( q2, b ) = { q0 } Now we will update the transition table of DFA. ?’ (Transition Function of DFA) 

 

As there is no new state generated, we are done with the conversion. Final state of DFA will be state which has q2 as its component i.e., { q0, q2 } Following are the various parameters for DFA. Q’ = { q0, { q0, q1 }, { q0, q2 } } ? = ( a, b ) F = { { q0, q2 } } and transition function ?’ as shown above. The final DFA for above NFA has been shown in Figure 2. 

 

Note : Sometimes, it is not easy to convert regular expression to DFA. First you can convert regular expression to NFA and then NFA to DFA.

Question : The number of states in the minimal deterministic finite automaton corresponding to the regular expression (0 + 1)* (10) is ____________. 

Solution : First, we will make an NFA for the above expression. To make an NFA for (0 + 1)*, NFA will be in same state q0 on input symbol 0 or 1. Then for concatenation, we will add two moves (q0 to q1 for 1 and q1 to q2 for 0) as shown in Figure 3. 

 

 

 

 

 

This article has been contributed by Sonal Tuteja.


Previous Article
Next Article

Similar Reads

State Elimination Method convert DFA/NFA/Ɛ-NFA into Regular Expression
State Elimination Method : Rules to convert a DFA/NFA//Ɛ-NFA into corresponding Regular Expression. Arden's Method is not capable of converting Ɛ-NFA. By state elimination method you can conveniently and quickly find RE without writing anything just by imagination. Rule-1 : If there are no incoming edges to the start state proceed further to check
3 min read
Conversion of Epsilon-NFA to NFA
Non-deterministic Finite Automata (NFA) is a finite automata having zero, one or more than one moves from a given state on a given input symbol. Epsilon NFA is the NFA which contains epsilon move(s)/Null move(s). To remove the epsilon move/Null move from epsilon-NFA and to convert it into NFA, we follow the steps mentioned below. Figure - Vertex v1
4 min read
Program to Implement NFA with epsilon move to DFA Conversion
Non-deterministic Finite Automata (NFA) : NFA is a finite automation where for some cases when a single input is given to a single state, the machine goes to more than 1 states, i.e. some of the moves cannot be uniquely determined by the present state and the present input symbol. An NFA can be represented as M = { Q, ?, ?, q0, F}Q ? Finite non-emp
10 min read
Difference between DFA and NFA
1. DFA: DFA refers to Deterministic Finite Automaton. A Finite Automata(FA) is said to be deterministic if corresponding to an input symbol, there is a single resultant state i.e. there is only one transition. A deterministic finite automata is set of five tuples represented as, Where, Q: A non-empty finite set of states in the finite control(qo, q
3 min read
Hypothesis (language regularity) and algorithm (L-graph to NFA) in TOC
Prerequisite - Finite automata, L-graphs and what they represent L-graphs can generate context sensitive languages, but it’s much harder to program a context sensitive language over programming a regular one. This is why I’ve came up with a hypothesis about what kind of L-graphs can generate a regular language. But first, I need to introduce you to
7 min read
Regular expression to ∈-NFA
Prerequisite - Finite Automata Introduction, Designing Finite Automata from Regular Expression (Set 1) ∈-NFA is similar to the NFA but have minor difference by epsilon move. This automaton replaces the transition function with the one that allows the empty string ∈ as a possible input. The transitions without consuming an input symbol are called ∈-
2 min read
NFA machines accepting all strings that ends or not ends with substring 'ab'
Prerequisite: Finite Automata Introduction Problem-1: Construction of a minimal NFA accepting a set of strings over {a, b} in which each string of the language ends with 'ab'. Explanation: The desired language will be like: L1 = {ab, abbab, abaab, ...........} Here as we can see that each string of the above language ends with 'ab' but the below la
2 min read
NFA to accept strings that has atleast one character occurring in a multiple of 3
Prerequisites: Finite AutomataGiven a string str consisting of characters a, b and c, check if the number of occurrences of any character in the string is a multiple of 3 or not. Examples: Input: str = bc Output: ACCEPTED Explanation: The string consists 0 a's and 3 * 0 = 0. Input: str = abccc Output: ACCEPTED Explanation: The string consists 3 c's
21 min read
NFA which accepts set of strings over an alphabet {0, 1, 2} such that the final digit has appeared before
Prerequisite - Introduction of Finite Automata C++ Program to construct an NFA which accepts the set of strings over an alphabet {0, 1, 2} such that the final digit has appeared before. Examples: Input : 01101 Output : Accepted Input : 012 Output : Not Accepted Input : 2 Output : Not Accepted Input : 0122 Output : Accepted Explanation: In the first
9 min read
NFA for language at least one of the symbols occurring an odd number of times
Problem Statement : Design and construct Non deterministic finite automata machine that accepts the string over input alphabets (a, b, c) with atleast one of the (input symbols) have odd number of presence in the string. Example : aaabbbb, ababac, ababababccc ... Approach: Construct Initial state with the mark –>. Make transitions from one state
2 min read
Article Tags :