Toc U2
Toc U2
Toc U2
Regular Expression
o The language accepted by finite automata can be easily described by simple expressions
called Regular Expressions. It is the most effective way to represent any language.
o The languages accepted by some regular expression are referred to as Regular languages.
o A regular expression can also be described as a sequence of pattern that defines a string.
o Regular expressions are used to match character combinations in strings. String searching
algorithm used this pattern to find the operations on a string.
Regular Expressions
Regular Expressions are used to denote regular languages. An expression is
regular if:
o ɸ is a regular expression for regular language ɸ.
o ɛ is a regular expression for regular language {ɛ}.
o If a ∈ Σ (Σ represents the input alphabet), a is regular expression with
language {a}.
o If a and b are regular expression, a + b is also a regular expression with
language {a,b}.
o If a and b are regular expression, ab (concatenation of a and b) is also
regular.
o If a is regular expression, a* (0 or more times a) is also regular.
o
Write the regular expression for the language accepting all combinations of a's, over the set ∑ = {a}
Solution:
All combinations of a's means a may be zero, single, double and so on. If a is appearing zero
times, that means a null string. That is we expect the set of {ε, a, aa, aaa, ....}. So we give a
regular expression for this as:
1. R = a*
Write the regular expression for the language accepting all combinations of a's except the null string, over
the set ∑ = {a}
1. L = {a, aa, aaa, ....}
This set indicates that there is no null string. So we can denote regular expression as:
R = a+
Write the regular expression for the language accepting all the string containing any number of a's
and b's.
1. r.e. = (a + b)*
This will give the set as L = {ε, a, aa, b, bb, ab, ba, aba, bab, .....}, any combination of a and b.
The (a + b)* shows any combination with a and b even a null string.
Write the regular expression for the language accepting all the string which are starting with 1 and ending
with 0, over ∑ = {0, 1}.
Solution:
In a regular expression, the first symbol should be 1, and the last symbol should be 0. The r.e. is as
follows:
1. R = 1 (0+1)* 0
Example 2:
Write the regular expression for the language starting and ending with a and having any having any
combination of b's in between.
Solution:
1. R = a b* a
Example 3:
Write the regular expression for the language starting with a but not having consecutive b's.
1. L = {a, aba, aab, aba, aaa, abab, .....}
The regular expression for the above language is:
1. R = {a + ab}*
Example 4:
Write the regular expression for the language accepting all the string in which any number of a's is
followed by any number of b's is followed by any number of c's.
Solution: As we know, any number of a's means a* any number of b's means b*, any number of c's
means c*. Since as given in problem statement, b's appear after a's and c's appear after b's. So the
regular expression could be:
1. R = a* b* c*
Example 5:
Write the regular expression for the language over ∑ = {0} having even length of the string.
Solution:
1. L = {ε, 00, 0000, 000000, ......}
The regular expression for the above language is:
1. R = (00)*
Example 6:
Write the regular expression for the language having a string which should have atleast one 0 and
alteast one 1.
Solution:
1. R = [(0 + 1)* 0 (0 + 1)* 1 (0 + 1)*] + [(0 + 1)* 1 (0 + 1)* 0 (0 + 1)*]
Example 7:
Describe the language denoted by following regular expression
1. r.e. = (b* (aaa)* b*)*
Solution:
The language can be predicted from the regular expression by finding the meaning of it. We will
first split the regular expression as:
L = {The language consists of the string in which a's appear triples, there is no restriction on the
number of b's}
Example 8:
Write the regular expression for the language L over ∑ = {0, 1} such that all the string do not contain the
substring 01.
Solution:
1. L = {ε, 0, 1, 00, 11, 10, 100, .....}
The regular expression for the above language is as follows:
1. R = (1* 0*)
Example 9:
Write the regular expression for the language containing the string in which every 0 is immediately
followed by 11.
Solution:
1. R = (011 + 1)*
Given R, P, L, Q as regular expressions, the following identities hold −
● ∅* = ε
● ε* = ε
● RR* = R*R
● R*R* = R*
● (R*)* = R*
● RR* = R*R
● (PQ)*P =P(QP)*
● (a+b)* = (a*b*)* = (a*+b*)* = (a+b*)* = a*(ba*)*
● R + ∅ = ∅ + R = R (The identity for union)
● R ε = ε R = R (The identity for concatenation)
● ∅ L = L ∅ = ∅ (The annihilator for concatenation)
● R + R = R (Idempotent law)
● L (M + N) = LM + LN (Left distributive law)
● (M + N) L = ML + NL (Right distributive law)
● ε + RR* = ε + R*R = R*.
●
DFA to Regular Expression-
The two popular methods for converting a given DFA to its regular expression are-
1. Arden’s Method
2. State Elimination Method
Arden’s Theorem-
Arden’s Theorem is popularly used to convert a given DFA to its regular expression.
It states that-
Conditions-
Steps-
To convert a given DFA to its regular expression using Arden’s Theorem, following steps are followed-
Step-01:
● Form a equation for each state considering the transitions which comes towards that state.
Step-02:
Bring final state in the form R = Q + RP to get the required regular expression.
Important Notes-
Note-01:
Arden’s Theorem can be used to find a regular expression for both DFA and NFA.
Note-02:
● Add all the regular expressions to get the final regular expression.
Problem-01:
Find regular expression for the following DFA using Arden’s Theorem-
Solution-
Step-01:
● A = ∈ + B.1 ……(1)
● B = A.0 ……(2)
Step-02:
B = (∈ + B.1).0
B = ∈.0 + B.1.0
B = 0 + B.(1.0) ……(3)
B = 0.(1.0)*
Problem-02:
Find regular expression for the following DFA using Arden’s Theorem-
Solution-
Step-01:
● q1 = ∈ ……(1)
● q2 = q1.a ……(2)
Step-02:
q2 = ∈.a
q2 = a …….(4)
q3 = (b + a.a)a*
Step-01:
Thumb Rule
The initial state of the DFA must not have any incoming edge.
● If there exists any incoming edge to the initial state, then create a new initial state having no incoming edge to it.
Example-
Step-02:
Thumb Rule
There must exist only one final state in the DFA.
● If there exists multiple final states in the DFA, then convert all the final states into non-final states and create a new single final state.
Example-
Step-03:
Thumb Rule
The final state of the DFA must not have any outgoing edge.
● If there exists any outgoing edge from the final state, then create a new final state having no outgoing edge from it.
Example-
Step-04:
In the end,
NOTE
The state elimination method can be applied to any finite automata.
Problem-01:
Solution-
Step-01:
Step-02:
Step-03:
● So, after eliminating state A, we put a direct path from state qi to state B having cost ∈.0 = 0
● So, after eliminating state A, we put a direct loop on state B having cost 1.0 = 10.
Step-04:
● So, after eliminating state B, we put a direct path from state qi to state qf having cost 0.(10)*.∈ = 0(10)*
From here,
L' denotes the language that does not contain strings that begin and end with a. This implies L'
contains strings that
The DFA for L' is obtained by flipping the final states of DFA(L) to non-final states and vice-versa.
The DFA for L' is given below.
● q0 ensures ε is
accepted
Important Note: While specifying the DFA for L, we have also included the dead state q 3. It is
important to include the dead state(s) if we are going to derive the complement DFA since, the
dead state(s) too would become final in the complementation. If we didn't add the dead state(s)
originally, the complement will not accept all strings supposed to be accepted.
In the above example, if we didn't include q 3 originally, the complement will not accept strings
starting with b. It will only accept strings that begin with a and end with b which is only a subset of
the complement.
CONCLUSION: REGULAR LANGUAGES ARE CLOSED UNDER COMPLEMENTATION.
Union
If L1 and L2 are regular, then L1 ∪ L2 is regular.
This is easier proved using regular expressions. If L 1 is regular, there exists a regular expression R1 to
describe it. Similarly, if L2 is regular, there exists a regular expression R2 to describe it. R1 + R2
denotes the regular expression that describe L1 ∪ L2. Therefore, L1 ∪ L2 is regular.
Intersection
If L1 and L2 are regular, then L1 ∩ L2 is regular.
Concatenation
If L1 and L2 are regular, then L1 . L2 is regular
Kleene star
If L is regular, then L* is regular.
Difference
If L1 and L2 are regular, then L1 - L2 is regular.
Reverse
If L is regular, then LR is regular.
How To Minimize DFA?
The two popular methods for minimizing a DFA are-
In this article, we will discuss Minimization of DFA Using Equivalence Theorem.
Step-01:
● Eliminate all the dead states and inaccessible states from the given DFA (if any).
Dead State
All those non-final states which transit to itself for all input symbols in ∑ are called as dead states.
Inaccessible State
All those states which can never be reached from the initial state are called as inaccessible states.
Step-02:
● Draw a state transition table for the given DFA.
● Transition table shows the transition of all states on all input symbols in Σ.
Step-03:
Now, start applying equivalence theorem.
● Take a counter variable k and initialize it with value 0.
● Divide Q (set of states) into two sets such that one set contains all the non-final states and
other set contains all the final states.
● This partition is called P0.
Step-04:
● Increment k by 1.
● Find Pk by partitioning the different sets of P k-1 .
● In each set of Pk-1 , consider all the possible pair of states within each set and if the two
states are distinguishable, partition the set into different sets in P k.
Two states q1 and q2 are distinguishable in partition Pk for any input symbol ‘a’,
if δ (q1, a) and δ (q2, a) are in different sets in partition P k-1.
Step-05:
● Repeat step-04 until no change in partition occurs.
● In other words, when you find Pk = Pk-1, stop.
Step-06:
● All those states which belong to the same set are equivalent.
● The equivalent states are merged to form a single state in the minimal DFA.
Problem-01:
Minimize the given DFA-
Solution-
Step-01:
The given DFA contains no dead states and inaccessible states.
Step-02:
Draw a state transition table-
a b
→q0 q1 q2
q1 q1 q3
q2 q1 q2
q3 q1 *q4
*q4 q1 q2
Step-03:
Now using Equivalence Theorem, we have-
P0 = { q0 , q1 , q2 , q3 } { q4 }
P1 = { q0 , q1 , q2 } { q3 } { q4 }
P2 = { q0 , q2 } { q1 } { q3 } { q4 }
P3 = { q0 , q2 } { q1 } { q3 } { q4 }
Since P3 = P2, so we stop.
From P3, we infer that states q0 and q2 are equivalent and can be merged together.
So, Our minimal DFA is-
Problem-02:
Minimize the given DFA-
Solution-
Step-01:
Step-02:
Draw a state transition table-
a b
→q0 *q1 q0
Step-03:
Now using Equivalence Theorem, we have-
P0 = { q0 } { q1 , q2 }
P1 = { q0 } { q1 , q2 }
Since P1 = P0, so we stop.
From P1, we infer that states q1 and q2 are equivalent and can be merged together.
So, Our minimal DFA is-