NFA (Non-Deterministic Finite Automata) is a state machine used to validate an input string using state transitions. Regular expressions are a sequence of characters that are used to check if the given string follows the pattern or not. Both NFA and regular expressions can be used to define a regular language. In this article, we will see how to convert a given NFA to a regular expression.
Let there be an NFA with N states. Our task is to convert NFA to a regular expression. To do this, we will write equations for each state of the given NFA in the form:
q1 = q1a11 + q2a21 + q3a31 + … qnan1
q2 = q1a12 + q2a22 + q3a32 + … qnan2
…
qn = q1a1n + q2a2n + q3a3n + … qnann
In the above equations, qi is a state variable corresponding to the ith state, and aij is a set of input symbols.
After this, we try to represent qi in terms of aij using Arden’s Theorem and substituting the state variables. To convert the NFA to a regular expression, we then take the union of all the state variables corresponding to the final states.
Example: Consider the following NFA
The above NFA has 3 states - q1, q2, q3. To convert this NFA to a regular expression, we will first write the equations of all the states:
q1 = ϵ + q1a + q2b - (1)
q2 = q1a + q2b + q3b - (2)
q3 = q2a - (3)
Now we will simplify the above equations.
From (3):
q3 = q2a
= (q1a + q2b + q3b)a
= q1aa + q2ba + q3ba - (4)
From (2):
q2 = q1a + q2b + q3b
= q1a + q2b + (q2a)b
= q1a + q2(b + ab)
According to Arden’s theorem, whenever we have an equation of the form R = Q + RP, it can be written as R = QP*.
q2 = q1a + q2(b + ab) is of the form R = Q + RP, where R = q2, Q = q1a and P = (b + ab), therefore, we can write it as:
q2 = q1a(b + ab)* - (5)
From (1):
q1 = ϵ + q1a + q2b
Putting value of q2 from equation (5):
q1 = ϵ + q1a + (q1a(b + ab)*)b
q1 = ϵ + q1(a + (a(b + ab)*)b
Now, this equation is again of the form R = Q + RP, where R = q1, Q = ϵ and P = (a + (a(b + ab)*)b.
Therefore, according to Arden’s theorem, we can write it as:
q1 = ϵ(a + (a(b + ab)*)b)*
= (a + (a(b + ab)*)b)* [From identity eq.: ϵR = R] - (6)
The last step is to substitute these values in the final state. The final state for the given example is q3.
q3 = q2a
Substituting q2 from the equation (5):
q3 = q1a(b + ab)*a
Substituting q1 from the equation (6):
q3 = (a + (a(b + ab)*)b)* a(b + ab)*a
We can see that now the equation is represented as a combination of the input symbols a and b only. Thus we have converted the given NFA to a regular expression.
18 videos|69 docs|44 tests
|
|
Explore Courses for Computer Science Engineering (CSE) exam
|