DFA To Regular Expressions

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

1

<DFA to Regular
expressions>

<Anas Makki 301>


<Arslan 315>
<Abdul Rehman 334>
2

Conversion of DFA to Regular


Expressions

Mostly two methods are used to convert DFA to Regular Expressions.

1. Aiden’s Method(Theorem)

2. State Elimination Method


3

Converting DFA to REGULAR EXPRESSION


METHODS

Aiden’s Method State Elimination Method


4

Aiden’s methods(Theorem)
Arden’s theorem states that:
“If P and Q are two regular expressions over ∑ and P does
not contain any null string(ε), then the following equation in R
given by
has a unique solution i.e. “
It means, whenever we get any equation in the form of
, then we can directly replace it with
R = Q + RP
Can be written
as:
R = QP*
5

Proof:
R = Q + RP________(1)
R = Q + RP
R = Q + RP
Can be written
R = Q + (Q+RP)P
as:
R = Q + QP + RP2 R = QP*
R = Q + QP + (Q+RP)P2
R = Q + QP + QP2 +RP3
R = Q + QP + QP2 + QP3 +….+QPN+RPN+1
R = Q [^+P+P2+P3+….]
R = QP*________(2)
6

Problem-01. Convert following DFA into R.E. using Aiden’s method

Note:
Solution: R = Q + RP
q1 = ε _____(1) Can be written
as:
q2 = q1a + q2a _____(2) R = QP*
q3 = q1a + q3a _____(3)
Now, Put q1=ε in eq.2
q2 = ε.a+ q2(a)
7

q2= a + q2 (a) Can be written as q2 = a a*_____(4)

R=Q+R P R = Q P* Note:
R = Q + RP
Now, Put q1=ε in eq.3
Can be written
as:
q3 = q1(b) + q3(a)
R = QP*
q3 = ε.b + q3(a)
q3 = b + q3(a) Can be written as q3 = b a*_____(5)

R =Q+R P R = Q P*
As we have two final states that are q2 and q3 therefore , we have to join eq. (4) and eq. (5) to
generate final regular expression.

R.E. = eq(4) + eq(5)


R.E.= aa* + ba*
8

Problem-02. Convert following DFA into R.E. using Aiden’s method


Note:
R = Q + RP
Can be written
as:
R = QP*

Solution:
q1 = ε + q1a + q3a_____(1)
q2 = q1b + q2b + q3b_____(2)
q3 = q2a_____(3)
9

q2 = q1b + q2b + q3b_____(2) q1 = ε + q1a + q3a_____(1)


Put q3 = q2a in eq. 2 Put q3 = q2a in eq. 1

q2 = q1b + q2b + q2ab q1 = ε + q1a + (q2a)a


q 2 = q1 b + q 2 (b + ab) q1 = ε + q1a + q2aa_____(5)
It can be written in the form put eq.4 in eq.5

R = QP* q 1 = ε + q1a + q1b (b + ab)*aa


q2 = q1b (b + ab)*____(4) q1 = ε + q1 (a + b (b + ab)*aa)
this equation can be written as
Note: q1 = ε ( a+b (b+ab)*aa)*
R = Q + RP
R.E. = (a+b(b+ab)*aa)*
Can be written
as:
R.E. = (a+b(b+ab)*aa)*
R = QP*
10

Problem-03. Convert following DFA into R.E. using Aiden’s method

Solution:
q1 = ε + q1a + q2b_____(1)
q2 = q1a + q2b + q3a_____(2)
q3 = q2a_____(3)
11

q2 = q1a + q2b + q3a_____(2) q1 = ε + q1a + q2b_____(1)


Put q3 = q2a in eq.2 Put eq.4 in eq.1
q2 = q1a + q2b + (q2a)a q1 = ε + q1a + (q1a(b + aa)*)b
Taking q2 common Taking q1 common

q2 = q1a + q2(b + aa) q1 = ε + q1(a + a(b + aa)*b)


This equation can be written as This equation can be written as

q2 = q1a(b + aa)*____(4) q1 = (a + a(b + aa)*b)*___(5)

Note:
R = Q + RP
Can be written
as:
R = QP*
12

q2 = q1a(b + aa)*____(4)
q1 = (a + a(b + aa)*b)*___(5)
Put eq.1 in eq.4
q2 = (a + a(b + aa)*b)*a(b + aa)*____(6)
q3 = q2a_____(3)
Put eq.6 in eq.3
q3 = (a + a(b + aa)*b)*a(b + aa)*a
As q3 is final state , so it is our required regular expression.

R.E. = (a + a(b + aa)*b)*a(b + aa)*a


13

STATE ELIMINATION METHOD

State Elimination Methods is used to convert DFA into Regular Expression.


It consists of some steps in order to convert DFA to R.E.
14

Step-1
➢ Initial State of DFA must not have any incoming edge.
➢ If there is any incoming edge at initial state then, create a
new initial state have no incoming edge.
Example:
15

Step-02
➢ There must be one/single final state.
➢ If there exist multiple final states then, convert all final
states into non-final states and create a single final state.
Example:
16

Step-03
➢ The final state must not have any outgoing edge.
➢ If there exist any outgoing edge at final state then, make it
non-final and create new final state having no outgoing
edge.
Example:
17

Step-04
➢ Eliminate all intermediate(states between initial and final
state) states.
➢ These intermediate states can be eliminated in any order.
Example:
18

Problem 01. Convert this DFA into R.E. using State Elimination Method.

Solution:
Step-01:
 initial state has incoming edge .
 So, we create a new initial state.
19

Step-02:
 There are no multiple final state in the above diagram ( No action needed )
Step-03:
 Final state B has an outgoing edge .
 So , make state B non-final and add new final state.
20

Step-04 (Eliminating intermediate states)


First remove state A

Now , remove state B

R.E. = 0(10)*
21

Problem 02. Convert this DFA into R.E. using State Elimination Method.

Solution: In this diagram there is dummy/dead state , firstly we have to remove it


because we can’t reach final state through dummy state at all
So,
22

Step – 01 : Initial state has no incoming edge (No operation required)


Step – 02 : There are no multiple final states (No operation required)
Step – 03 : Final state has no outgoing edge/transition (No operation required)
Step – 04: There is no intermediate state exist between Initial and final state (No
operation required)
So,

R.E. = 0
23

Problem 03. Convert this DFA into R.E. using State Elimination Method.

Solution:
Step – 01:
Initial state has incoming edges.
So, we create new initial state.
24

Step – 02 : There are no multiple Final states exist(No operation required)


Step – 03: Final state q2 has outgoing edge
So, we make final state q2 non-final and create new final state.
25

Step – 04 (Elimination Intermediate States)


First Remove state q1
26

Now , Remove state q2

We have initial and final states now . Their cost is our required R.E.

R.E. = c*a (d + b c*a)*


27

Problem 04. Convert this DFA into R.E. using State Elimination Method.

Solution: Note that state D is a dummy/dead state because it can’t reach final state
anyway, so we have to remove it , in order to proceed.
28

Step – 01:
Initial state has incoming edge .
So, we create new initial state.
29

Step – 02:
There are multiple final state exists
Make them non-final and convert them into single state by creating a
new final state
30

Step – 03: There is no outgoing edge from final state(No action required)
Step – 04: (Removing intermediate states)
Eliminating State C;
31

No Eliminating state B
32

Now Eliminating state A:

We have initial and final states now . Their cost is our required R.E.

R.E. = 1* ( ε +00* (11* + ε )


33

Problem 05. Convert this DFA into R.E. using State Elimination Method.

Solution:
Step – 01:
Initial state has incoming edge
So, we create a new initial state
34

Step – 02: There are no multiple final states ( No operation required )


Step – 03: The final state A has outgoing edges.
So , we make state A non-final and create new final state.
35

Step – 04: Eliminating Intermediate states


First eliminating state B
36

Eliminating State C
37

Eliminating State A

R.E. = ( 01 + 1 (0 + 11))*
38

Problem 06. Convert this DFA into R.E. using State Elimination Method.

Solution:
Step – 01: There is no incoming edge at initial state(No process required)
Step – 02: There are multiple Final states that are B and C.
So, we make B and C non-final and create a new final state.
39

Step – 03 : There is no outgoing edge at final state qf. (No action required)
Step – 04 : Eliminating all intermediate states.
First eliminating state B
40

Eliminating state C

R.E. = aa* + ba*


41

You might also like