# Generating regular expression from Finite Automata

Prerequisite – Introduction of FA, Regular expressions, grammar and language, Designing FA from Regular Expression

There are two methods to convert FA to regular expression –

**1. State Elimination Method –**

**Step 1 –**

If the start state is an accepting state or has transitions in, add a new non-accepting start state and add an €-transition between the new start state and the former start state.**Step 2 –**

If there is more than one accepting state or if the single accepting state has transitions out, add a new accepting state, make all other states non-accepting, and add an €-transition from each former accepting state to the new accepting state.**Step 3 –**

For each non-start non-accepting state in turn, eliminate the state and update transitions accordingly.

** Example ** :-

__Solution __:-

__Step 1__

__Step 2__

__Step 3__

**2. Arden’s Theorem –** Let P and Q be 2 regular expressions. If P does not contain null string, then following equation in R, viz R = Q + RP, Has a unique solution by R = QP*

**Assumptions –**

- The transition diagram should not have €-moves.
- It must have only one initial state.

__Using Arden’s Theorem to find Regular Expression of Deterministic Finite automata –__

- For getting the regular expression for the automata we first create equations of the given form for all the states

**q**_{1}= q_{1}w_{11}+q_{2}w_{21}+…+q_{n}w_{n1}+€ (q_{1}is the initial state)

q_{2}= q_{1}w_{12}+q_{2}w_{22}+…+q_{n}w_{n2}

.

.

.

q_{n}= q_{1}w_{1n}+q_{2}w_{2n}+…+q_{n}w_{nn}

w_{ij}is the regular expression representing the set of labels of edges from q_{i}to q_{j}

**Note –**For parallel edges there will be that many expressions for that state in the expression. - Then we solve these equations to get the equation for q
_{i}in terms of w_{ij}and that expression is the required solution, where q_{i}is a final state.

__Example __:-

** Solution **:-

Here the initial state is q

_{2}and the final state is q

_{1}.

The equations for the three states q

_{1}, q

_{2}, and q

_{3}are as follows ?

q

_{1}= q

_{1}a + q

_{3}a + € ( € move is because q

_{1}is the initial state)

q

_{2}= q

_{1}b + q

_{2}b + q

_{3}b

q

_{3}= q

_{2}a

Now, we will solve these three equations ?

q

_{2}= q

_{1}b + q

_{2}b + q

_{3}b

= q

_{1}b + q

_{2}b + (q

_{2}a)b (Substituting value of q

_{3})

= q

_{1}b + q

_{2}(b + ab)

= q

_{1}b (b + ab)* (Applying Arden’s Theorem)

q

_{1}= q

_{1}a + q

_{3}a + €

= q

_{1}a + q

_{2}aa + € (Substituting value of q

_{3})

= q

_{1}a + q

_{1}b(b + ab*)aa + € (Substituting value of q

_{2})

= q

_{1}(a + b(b + ab)*aa) + €

= € (a+ b(b + ab)*aa)*

= (a + b(b + ab)*aa)*

Hence, the regular expression is (a + b(b + ab)*aa)*.

**GATE CS Corner Questions**

Practicing the following questions will help you test your knowledge. All questions have been asked in GATE in previous years or in GATE Mock Tests. It is highly recommended that you practice them.

- GATE CS 2008, Question 52
- GATE CS 2007, Question 74
- GATE CS 2014 (Set-1), Question 25
- GATE CS 2014 (Set-1), Question 65
- GATE IT 2006, Question 5
- GATE CS 2013, Question 33
- GATE CS 2012, Question 12

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Designing Finite Automata from Regular Expression (Set 4)
- Designing Finite Automata from Regular Expression (Set 3)
- Designing Finite Automata from Regular Expression (Set 1)
- Designing Finite Automata from Regular Expression (Set 2)
- Designing Finite Automata from Regular Expression (Set 7)
- Designing Finite Automata from Regular Expression (Set 6)
- Designing Finite Automata from Regular Expression (Set 5)
- Designing Finite Automata from Regular Expression (Set 8)
- Difference between Pushdown Automata and Finite Automata
- Star Height of Regular Expression and Regular Language
- Compound Finite Automata (FA)
- Finite Automata with Output (Set 5)
- Finite Automata with Output (Set 3)
- Finite Automata with Output (Set 6)
- Introduction of Finite Automata

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.