Kca 201 Unit 2 1 Unit 1 Notes

You might also like

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

lOMoARcPSD|40000079

KCA 201 - UNIT-2-1 - Unit 1 notes

Theory of Formal Language and Automata (KIET Group of Institutions)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by kanhaiya Dhiman (kanhaiyadhiman5354@gmail.com)
lOMoARcPSD|40000079

UNIT-2: Introduction to Regular Expression and Language

Regular Expression:
Regular expressions are used for representing certain sets of strings(language)in an algebraic
fashion.

1. Any terminal symbols (i.e. an element of ∑), ∈ and Φ are regular expressions (Primitive
regular expressions)

2. The union of two regular expression is also a regular expression

3. The concatenation of two regular expression is also a regular expression

4. The clean closure of regular expression is also regular expression

Example 2:
Write the regular expression for the language accepting all the string which are starting with 1 and
ending with 0, over ∑ = {0, 1}.

Solution:

R = 1 (0+1)* 0

Page|2

Downloaded by kanhaiya Dhiman (kanhaiyadhiman5354@gmail.com)


lOMoARcPSD|40000079

Identities for REs:

The two regular expressions P and Q are equivalent (denoted as P=Q) if and only if P represents
the same set of strings as Q does. For showing this equivalence of regular expressions we need to
show some identities of regular expressions.
Let P, Q and R are regular expressions then the identity rules are as given below.

Examples of Regular Expression

Page|3

Downloaded by kanhaiya Dhiman (kanhaiyadhiman5354@gmail.com)


lOMoARcPSD|40000079

Example 01:
Find the regular expression for the set of all strings over {0, 1} beginning with 00.
Solution:
We have the input alphabets are ∑ = {0, 1}
L= {00, 001, 0001, 0011,……}
R.E= 00(0+1)*

Example 02:
Find the regular expression for the set of all strings over {0, 1} end with 00.
Solution:
We have the input alphabets are ∑ = {0, 1}
L = {00, 100, 1000, 10100,……}

R.E = (0+1)*00
Example 03:
Write a R.E for the Language over {0, 1} which are staring with 1 ending with 0.
Solution:
We have the input alphabets are ∑ = {0, 1}
The first symbol is 1
Last symbol is 0
So R. E = 1(0+1)*0

Page|4

Downloaded by kanhaiya Dhiman (kanhaiyadhiman5354@gmail.com)


lOMoARcPSD|40000079

Regular Expression (R.E) to Finite Automata (FA):


1. R.E to F.A
2. R.E. to NFA

Regular Expression (R.E) to FA:

Page|5

Downloaded by kanhaiya Dhiman (kanhaiyadhiman5354@gmail.com)


lOMoARcPSD|40000079

Some basic RA expressions are the following –

Example 1: Design a FA from given regular expression 10 + (0 + 11)0* 1.

Solution:
Step 1: the transition diagram for a given regular expression.

Step 2:

Step3:

Step 4:

Page|6

Downloaded by kanhaiya Dhiman (kanhaiyadhiman5354@gmail.com)


lOMoARcPSD|40000079

Step 5:

DFA to Regular Expression-

The two popular methods for converting a given DFA to its regular expression are-

Arden’s Theorem-
Arden’s Theorem is used to convert a given DFA to its regular expression.
Let P and Q be the two regular expressions over the input set ∑. The regular expression R is given
as
R= Q+ RP

Which has a unique solution as R= QP*

Rules:
1. Add ‘∈’ in the equation of initial state.
2. Incoming are used.
3. First solve non- final.
4. Finally solve final state.
5. Bring final state in the form R = Q + RP to get the required regular expression.

Page|7

Downloaded by kanhaiya Dhiman (kanhaiyadhiman5354@gmail.com)


lOMoARcPSD|40000079

Problem-01: Find regular expression for the following DFA using Arden’s Theorem.

Solution-
Step-01: Form a equation for each state-
A = ∈ + B.1 …… (1)
B = A.0 …… (2)
Step-02: Bring final state in the form R = Q + RP.

Using (1) in (2), we get-


B = (∈ + B.1).0
B = ∈.0 + B.1.0
B = 0 + B.(1.0) ……(3)
Using Arden’s Theorem in (3), we get-
B = 0.(1.0)*
Thus, Regular Expression for the given DFA = 0(10)*

Problem-02:
Find regular expression for the following DFA using Arden’s Theorem-

Solution-
Step-01: Form a equation for each state-

Page|8

Downloaded by kanhaiya Dhiman (kanhaiyadhiman5354@gmail.com)


lOMoARcPSD|40000079

q1 = ∈ ……(1)
q2 = q1.a ……(2)
q3 = q1.b + q2.a + q3.a …….(3)

Step-02:
Using (1) in (2), we get-
q2 = ∈.a
q2 = a …….(4)
Using (1) and (4) in (3), we get-
q3 = q1.b + q2.a + q3.a
q3 = ∈.b + a.a + q3.a
q3 = (b + a.a) + q3.a …….(5)
Using Arden’s Theorem in (5), we get-
q3 = (b + a.a)a*
Thus, Regular Expression for the given DFA = (b + aa) a*

Problem-03:
Find regular expression for the following DFA using Arden’s Theorem-
Solution-

Step-01: Form a equation for each state-


q1 = ∈ + q1.b + q2.a ……(1)
q2 = q1.a + q2.b ……(2)

Step-02: Using Arden’s Theorem in (2), we get-


q2 = q1.a.b* …….(3)
Using (3) in (1), we get-
q1 = ∈ + q1.b + q1.a.b*.a
q1 = ∈ + q1.(b + a.b*.a) …….(4)

Page|9

Downloaded by kanhaiya Dhiman (kanhaiyadhiman5354@gmail.com)


lOMoARcPSD|40000079

Using Arden’s Theorem in (4), we get-


q1 = ∈.(b + a.b*.a)*
q1 = (b + a.b*.a)*
Thus, Regular Expression for the given DFA = (b + a.b*.a)*

Problem-04:
Find regular expression for the following FA using Arden’s Theorem-

Solution-
Step-01: Form a equation for each state-
q1 = ∈ + q1.a + q3.a ……(1)
q2 = q1.b + q2.b + q3.b ……(2)
q3 = q2.a …….(3)

Step-02: Using (3) in (2), we get-


q2 = q1.b + q2.b + q2.a.b
q2 = q1.b + q2.(b + a.b) …….(4)
Using Arden’s Theorem in (4), we get-
q2 = q1.b.(b + a.b)* …….(5)

Using (5) in (3), we get-


q3 = q1.b.(b + a.b)*.a …….(6)
Using (6) in (1), we get-
q1 = ∈ + q1.a + q1.b.(b + a.b)*.a.a
q1 = ∈ + q1.(a + b.(b + a.b)*.a.a) …….(7)

P a g e | 10

Downloaded by kanhaiya Dhiman (kanhaiyadhiman5354@gmail.com)


lOMoARcPSD|40000079

Using Arden’s Theorem in (7), we get-


q1 = ∈.(a + b.(b + a.b)*.a.a)*
q1 = (a + b.(b + a.b)*.a.a)*

Thus, Regular Expression for the given DFA = (a + b(b + ab)*aa)*

Problem-05:
Find regular expression for the following FA using Arden’s Theorem-

Solution:
A = ε + Aa + Ba ................. (1)
B = Bb + Ab… ................... (2)
Which one is final state?
First solve non-final state
Using Arden’s theorem in equation (2)
B = Abb*
Now, Put value of B in equation (1)
So that,
A = ε + Aa + Abb*a

A = ε + A (a + bb*a)
Using Arden’s theorem
A = ε (a + bb*a)*
Ans

P a g e | 11

Downloaded by kanhaiya Dhiman (kanhaiyadhiman5354@gmail.com)


lOMoARcPSD|40000079

Problem-:

Find regular expression for the following FA using Arden’s Theorem-

Solution:
Here initial state and final state is q1
q1 = q1a + q3a + ε
q2 = q1b + q2b + q3b
q3 = q2 a
Now, we will solve these three equations
q2 = q1b + q2b + q3b
= q1b + q2b + (q2 a) b [substituting value of q3]
= q1b + q2 (b + a b)
Appling Arde’s Theorem
q2 = q1b (b + a b)*
Now,
q1 = q1a + q3a + ε

P a g e | 12

Downloaded by kanhaiya Dhiman (kanhaiyadhiman5354@gmail.com)


lOMoARcPSD|40000079

= q1a + q2 aa + ε [substituting value of q3]


= q1a + q1b (b + a b)*aa + ε [substituting value of q2]
= q1 (a + b (b + a b)*aa ) + ε
Appling Arde’s Theorem
q1 = ε (a + b (b + a b)*aa)*
q1 = (a + b (b + a b)*aa)*

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

State Elimination Method

This method involves the following steps in finding the regular expression for any given DFA

Step-01:
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:
If there exists any outgoing edge from the final state, then create a new final state having no
outgoing edge from it.
Example

P a g e | 13

Downloaded by kanhaiya Dhiman (kanhaiyadhiman5354@gmail.com)


lOMoARcPSD|40000079

Step-03:
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-04:
Eliminate all the intermediate states one by one except initial states and final state. eliminated in
any order.

Problem-01: Find regular expression for the following FA

Solution-
Step-01: Initial state A has an incoming edge.

Step-02: Final state B has an outgoing edge

Step-03:

P a g e | 14

Downloaded by kanhaiya Dhiman (kanhaiyadhiman5354@gmail.com)


lOMoARcPSD|40000079

Now, we start eliminating the intermediate states.

Step-04:
Now, let us eliminate state B.

Regular Expression = 0(10)*

Problem-02: Find regular expression for the following FA-


Solution-

Step-01: There exist multiple final states

Step-02:
Now, we start eliminating the intermediate states

P a g e | 15

Downloaded by kanhaiya Dhiman (kanhaiyadhiman5354@gmail.com)


lOMoARcPSD|40000079

Step-05: Now, eliminate state q2


From here,
Regular Expression = a(b+c+d)

Problem-03:
Find regular expression for the following FA

Solution-
Step-01: Initial state q1 has an incoming edge

Step-02:
Final state q2 has an outgoing edge

Step-03:

P a g e | 16

Downloaded by kanhaiya Dhiman (kanhaiyadhiman5354@gmail.com)


lOMoARcPSD|40000079

Now, we start eliminating the intermediate states.

Step-04: Now, eliminate state q2

From here,

Regular Expression = c*a(d+bc*a)*

Pumping Lemma for regular expression:


Pumping lemma is used to find out whether the language is regular or not.
Pumping lemma performs negative test.
Pumping Lemma:
If L is a regular language (infinite), there exists an integer ‘n'(pumping length) such that any string
w∈L, with |w|≥n.
then w can be divided into three strings, w= xyz satisfy following conditions
1. xyiz∈ L, for each i≥0
2. |y|>0
3. |xy|≤n

Problem 01:
Prove that L = {aibi | i ≥ 0} is not regular.

Solution:
L= {∈, ab, aabb, aaabbb, aaaaabbbbb,… ............ }

Step 01: Assume L is a regular, then we can make its FA. Suppose FA has n states.
Step 02: Take a string w =anbn
Step 03: Divide w= xyz, |w|≥n such that

P a g e | 17

Downloaded by kanhaiya Dhiman (kanhaiyadhiman5354@gmail.com)


lOMoARcPSD|40000079

Let n=4
Then w= aaaabbbb (X= a, y= aaa, z= bbbb)
Case 01:|xy|≤n

4≤ 4 true
Case 02:|y| > 0
3>0 true
Case 03:xyiz ∈ L, for each i≥0

aaaaaaabbbb ∉ L , i = 2
Thus, contradicts our assumption. Hence L is not regular

Problem 02:
Prove that L = {aibj | i ≤j} is not regular.

Solution:
L= {aabb, aabbb, aaabbbb,… ........... }
Step 01:Assume that L is a regular. then we can make its FA. Suppose FA has n states.
Step 02:Take a string w = anbn+1
Step 03: Divide w= xyz, |w|≥n such that
Let n = 2, then w = aabbb (X= a, y= a, z= bb)

Case 01:|xy| ≤ n
2≤ 2 true
Case 02: |y| > 0
1> 0 true
Case 03: xyiz∈L, for each i≥0
aaabbb ∈ L , i = 2
aaaabbb ∉ L, i = 3
Thus, contradicts our assumption. Hence L is not regular

P a g e | 18

Downloaded by kanhaiya Dhiman (kanhaiyadhiman5354@gmail.com)


lOMoARcPSD|40000079

Problem 03:
Prove that L = {ap | p is prime} is not regular.
Solution:
L= {a2, a3, a5, a7, a11 ............... }
Step 01: Assume that L is a regular, then we can make its FA. Suppose FA has n states.
Step 02: Take a string w = an
Step 03: Choose a string w= xyz, |w|≥n such that
Let n = 5,
then w = aaaaa(X= a, y= aa, z= aa)

Case 01:|xy| ≤ n
3≤ 5 true
Case 02: |y| > 0
2> 0 true
Case 03:xyiz∈L, for each i≥0
aaaaaaa∈L , i = 2
aaaaaaaaa∉ L , i = 3
Thus, contradicts our assumption. Hence L is not regular

Problem 04:
Prove that L = {ww | w ∈ (0, 1)*} is not regular.

Solution:
L= {∈, 00, 11, 0011, 0101, 1010, 010010 ................}
Step 01: Assume that L is a regular. then we can make its FA. Suppose FA has n states.
Step 02: Take a string S = 0n10n1
Step 03: Choose a string S= xyz, |S|≥n such that
Let n = 3
Then S = 00010001(X= 0, y= 00, z= 10001)

P a g e | 19

Downloaded by kanhaiya Dhiman (kanhaiyadhiman5354@gmail.com)


lOMoARcPSD|40000079

Case 01:|xy| ≤ n
3≤ 3 true
Case 02: |y| > 0
2> 0 true
Case 03:xyiz∈L, for each i≥0
00010001∈L, i = 1
0000010001∉ L, i = 2
Thus, contradicts our assumption. Hence L is not regular

Problem 05:
Prove that L={wwR| w ∈{a, b}*} is not regular by using Pumping Lemma.
Or
Show that L ={ wwR : w ∈ Σ*} is not regular by using Pumping Lemma.

Solution:
L= {∈, aa, bb, baab, abba, aabbaa, aaabbaaa ................. }
Step 01: Assume that L is a regular. then we can make its FA. Suppose FA has n states.
Step 02: Take a string S = anb ban
Step 03: Choose a string S= xyz, |S|≥n such that
Let n = 3
Then S =aaabbaaa(X= a, y= aa, z= bbaaa)
Case 01:|xy| ≤ n
3≤ 3 true

P a g e | 20

Downloaded by kanhaiya Dhiman (kanhaiyadhiman5354@gmail.com)


lOMoARcPSD|40000079

Case 02: |y| > 0


2>0 true
Case 03:xyiz∈L, for each i≥0
aaabbaaa∈L , i = 1
aaaaabbaaa∉ L , i = 2

Thus, contradicts our assumption. Hence L is not regular

Problem 06:
Prove that L= {an2|n ≥ 0} is not regular by using Pumping Lemma.

Solution:
L= {∈, a1,a4, a9, a16 ............ }
Step 01: Assume that L is a regular. then we can make its FA. Suppose FA has n states.
Step 02: Take a string w = ap2
Step 03: Choose a string w= xyz, |w|≥n
Let p = 2
Then w =aaaa(X= a, y= a, z= aa)

Case 01:|xy| ≤ n
2≤ 2 true
Case 02: |y| > 0

P a g e | 21

Downloaded by kanhaiya Dhiman (kanhaiyadhiman5354@gmail.com)


lOMoARcPSD|40000079

1> 0 true
Case 03:xyiz∈L, for each i≥0
aaaaa ∉ L , i = 2
Thus, contradicts our assumption. Hence L is not regular

P a g e | 22

Downloaded by kanhaiya Dhiman (kanhaiyadhiman5354@gmail.com)

You might also like