# CREATE OWN LIBRARY

## DFA/NFA to Regular Expression Calculation

Procedure to find regular expression from a DFA or NFA:

1. Ignore any trap state
2. DFA/NFA treat the same way
3. Resolve the loop
4. If more than one state is final then write RE for each final state and add all.
5. Always take the longest path
6. Reduce the number of states if possible

Step1: Ignore any trap state at time of find regular expression from DFA/NFA

Here is an example where we ignore the trap state ${\mathbf{q}}_{\mathbit{t}}$.

Step2: Whatever the machine DFA or NFA we follow the same procedure to find regular expression.

Step3: Resolve Loop

We can resolve the loop in two ways –

a. Left hand side loop resolve and b. Right hand side loop resolve.

a. Left hand side loop resolve: (Here we calculate loop left to right)

We start from the initial state ‘A and again come back to left hand side state ‘A’ by traversing all states several times. And as we know we always take the longest path so, here it is (pq)*

Now we reach left hand side at state ‘A’ using the longest path (pq)*

After reached left hand side at state ‘A’ how we go to the final state ‘B’?

Simply we go to left hand side state ‘A’ to the final state ‘B’ by input p.

So, the final regular expression of machine M is RE1 = (pq)*p

b. Right hand side loop resolve: (Here we calculate loop right to left)

Unlike left hand loop resolve, we first go initial state ‘A’ to right hand final state ‘B’ simply by input p.

So, the final regular expression for machine M is RE2 = p(qp)*

So, machine M has two regular expressions one from left hand loop resolve and one from right hand side.

As we know one machine can have more than a regular expression but all are equivalent.

But another way we can say this two regular expression is equivalent because regular expression holds the associative property.

Step 4:  More than one final state

This machine has two final states ‘A’ and ‘B’.

So, we calculate regular expressions individually for the state ‘A’ and state ‘B’.

Regular expression of final state ‘A’= (pq)*

Regular expression of final state ‘B’ = (pq)*p (Left hand side loop resolve).

After calculating the individual regular expression of final states just add all.

So, final regular expression for this machine RE = (pq)* + (pq)*p = (pq)* (λ + p)

Step 5: Always take the longest path

We always try to choose the longest path to cover all possible states when we calculate regular expression.

Machine: i.e. Odd numbers of b’s

Now we calculate the regular expression of this machine by left hand side loop resolve.

As we know to calculate this regular expression by left hand side loop resolve we will reach the first left hand side state ${\mathbf{q}}_{\mathbf{0}}$ from initial same state by traversing all states of the machine i.e. we always consider the longest loop to calculate the path.

But this longest loop is not like (a*ba*b)*

To reach left hand side state ${\mathbf{q}}_{\mathbf{0}}$ from the initial same state by longest loop: (a + b a* b)*

If you do not understand follow the machine how many ways you reach left hand side state ${\mathbf{q}}_{\mathbf{0}}$ from the initial same state:

i. By input ‘a’ (loop: a*) we reach ${\mathbf{q}}_{\mathbf{0}}$ from the initial same state.

OR (+)

ii. By input ba*b we reach ${\mathbf{q}}_{\mathbf{0}}$ from the initial same state. As we know the same path we repeat several times to get ${\mathbf{q}}_{\mathbf{0}}$, so, longest path (b a* b)*.

OR (+)

iii. We can also get ${\mathbf{q}}_{\mathbf{0}}$ from the initial same state by a combination of a* and (b a* b)*.

So, the final path to ${\mathbf{q}}_{\mathbf{0}}$ is (a + b a* b)* i.e. either we can get ${\mathbf{q}}_{\mathbf{0}}$ by using a* OR it can get by (b a* b)*  OR combination of both.

Now, after we reached ${\mathbf{q}}_{\mathbf{0}}$ from initial same state, we go to the final state ${\mathbf{q}}_{\mathbf{1}}$ by input ba*

So, final regular expression of given machine = (a + b a* b)*ba* (Left hand side loop resolve)

So, the final regular expression of given machine = ba*(a + b a* b)* (Left hand side loop resolve)

Step 6: Reduce the number of states

Let take a machine as an example and reduce the number of machine.

Now we start from the last state ${\mathbf{q}}_{\mathbf{3}}$ to resolve it.

We can resolve ${\mathbf{q}}_{\mathbf{2}}$ now,

After more simplification of state ${\mathbf{q}}_{\mathbf{1}}$

So, the final regular expression RE = a(a + a(b + ba)*b)*
{"ft_lang_label":"__label__en","ft_lang_prob":0.87256193,"math_prob":0.99857193,"size":3716,"snap":"2023-14-2023-23","text_gpt3_token_len":909,"char_repetition_ratio":0.20851293,"word_repetition_ratio":0.10128388,"special_character_ratio":0.24919268,"punctuation_ratio":0.08961039,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9996179,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-06-07T12:31:31Z\",\"WARC-Record-ID\":\"<urn:uuid:5f510627-3694-4905-a8da-959af6ee4d8a>\",\"Content-Length\":\"126619\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:670f1726-1826-451b-a042-07bcdd8a2b34>\",\"WARC-Concurrent-To\":\"<urn:uuid:21048cd6-5755-418e-a2f8-b60e79334256>\",\"WARC-IP-Address\":\"15.206.97.216\",\"WARC-Target-URI\":\"https://mycareerwise.com/content/dfanfa-to-regular-expression-calculation/content/exam/nta-net/computer-science\",\"WARC-Payload-Digest\":\"sha1:V7RGE4DQR3VW6YNSYXIAMP5XMWL233DD\",\"WARC-Block-Digest\":\"sha1:FUPNVL5WFE5ETB2PGGGIU74ULTVNUFDI\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224653764.55_warc_CC-MAIN-20230607111017-20230607141017-00233.warc.gz\"}"}