Theory of Computation

44
Theory of Computation Fourth Year Students Department of Computer Science T.A . Rehab Emad El - Dein Sayed FCI-Minia University 1

Transcript of Theory of Computation

Theory of ComputationFourth Year Students

Department of Computer Science

T.A. Rehab Emad El-Dein Sayed

FCI-Minia University 1

Course Contents

I. PART 1

Introduction to Automata and Languages

II. PART 2

Regular Expressions and Languages

Properties of Regular Languages

Context-Free Grammars and Languages

Applications of Context-Free Grammars

FCI-Minia University 2

Aim of The Lecture

Regular Expression

Properties (Identities) of Regular Expression

Arden’s theorem

Designing Regular Expression

Conversion from FA to Regular Expression

Conversion from Regular Expression to Finite Automata

FCI-Minia University 3

Regular Expression

Regular Language is said to be a regular language if and only if some StateFinite Machine recognize it.

Regular Expressions are used to denote regular languages.

An expression is regular if:

1. ɸ is a regular expression for regular language ɸ.

2. ɛ is a regular expression for regular language {ɛ}.

3. If a ∈ Σ (Σ represents the input alphabet), a is regular expression with language{a}.

4. If a and b are regular expression, a + b (union of a and b) is also a regularexpression with language {a,b}.

5. If a and b are regular expression, ab (concatenation of a and b) is alsoregular.

6. If a is regular expression, a* (0 or more times a) (closure of a) is also regular.4

Aim of The Lecture

Regular Expression

Designing Regular Expression Properties (Identities) of Regular Expression

Arden’s theorem

Conversion from FA to Regular Expression

Conversion from Regular Expression to Finite Automata

FCI-Minia University 5

Regular Expression: Examples

FCI-Minia University 6

REGULAR

EXPRESSION

REGULAR

LANGUAGES

set of vowels ( a + e + i + o + u ) {a, e, i, o, u}

a followed by 0 or more b (a.b*) {a, ab, abb, abbb, abbbb, ..}

any no. of vowels followed by

any no. of consonants

v*.c*

( where v – vowels and c –

consonants)

{ ε , a ,aou, aiou, b, abcd ...}

where ε represent empty

string (in case 0 vowels and

o consonants )

Regular Expression: Examples

FCI-Minia University 7

REGULAR LANGUAGES REGULAR EXPRESSION

{0, 1, 2} R=(0+1+2)

{ʌ, ab} R=ʌab

{abb,a,b,bba} R=(abb+a+b+bba)

{ʌ, 0,00,000,....} R=0* (* zero or more 0)

{1, 11,111,1111,....} R=1+ (+ one or more 1)

Regular Expression: Examples

Design Regular Expression for the following Languages over ∑={a,b}

1. Languages accepting strings of length exactly 2

L1={aa,ab,ba,bb}

RE1=aa+ab+ba+bb

=a(a+b)+b(a+b)

=(a+b)(a+b)

2. Languages accepting strings of length at least 2

L2={aa,ab,ba,bb,aaa,aba,…}

RE2=(a+b)(a+b)(a+b)*

3. Languages accepting strings of length at most 2

L3={ε,a,b,aa,ab,ba,bb}

RE3=ε+a+b+aa+ab+ba+bb

=(ε+a+b)(ε+a+b) FCI-Minia University 8

Regular Languages:

Closure Properties

FCI-Minia University 9

Closure Properties of Regular Languages

Union : If L1 and If L2 are two regular languages, their union L1 ∪ L2 will also be regular.

For example,

L1 = {an | n ≥ 0} and L2 = {bn | n ≥ 0}

L3 = L1 ∪ L2 = {an ∪ bn | n ≥ 0} is also regular.

Intersection : If L1 and If L2 are two regular languages, their intersection L1 ∩ L2 will

also be regular.

For example,

L1= {am bn | n ≥ 0 and m ≥ 0} and L2= {am bn ∪ bn am | n ≥ 0 and m ≥ 0}

L3 = L1 ∩ L2 = {am bn | n ≥ 0 and m ≥ 0} is also regular.

Regular Languages:

Closure Properties

FCI-Minia University 10

Closure Properties of Regular Languages

Concatenation : If L1 and If L2 are two regular languages, their concatenation L1.L2 will

also be regular.

For example,

L1 = {an | n ≥ 0} and L2 = {bn | n ≥ 0}

L3 = L1.L2 = {am . bn | m ≥ 0 and n ≥ 0} is also regular.

Kleene Closure : If L1 is a regular language, its Kleene closure L1* will also be regular.

For example,

L1 = (a ∪ b)

L1* = (a ∪ b)*

Regular Languages:

Closure Properties

FCI-Minia University 11

Closure Properties of Regular Languages

Complement : If L(G) is regular language, its complement L’(G) will also be regular.

Complement of a language can be found by subtracting strings which are in L(G) from all

possible strings.

For example,

L(G) = {an | n > 3}

L’(G) = {an | n <= 3}

Aim of The Lecture

Regular Expression

Properties (Identities) of Regular Expression

Arden’s theorem

Designing Regular Expression

Conversion from FA to Regular Expression

Conversion from Regular Expression to Finite Automata

FCI-Minia University 12

Identities of Regular Expression

FCI-Minia University 13

Given R, P, L, Q as regular expressions, the following identities hold:

1. ϕ* = ε

2. ε* = ε

3. RR* = R*R

4. (R*)* = R*

5. (PQ)*P =P(QP)*

6. (a+b)* = (a*b*)* = (a*+b*)* = (a+b*)* = a*(ba*)*

7. R + ϕ = ϕ + R = R (The identity for union)

8. Rε = εR = R (The identity for concatenation)

9. ϕL=Lϕ

10. R+R=R

11. L(M+N)=LM+LN (Left distributive law)

12. (M+N)L=ML+N L (Right distributive law)

13. ε + RR* = ε + R*R = R*

FCI-Minia University 14

Identities of Regular Expression

Aim of The Lecture

Regular Expression

Designing Regular Expression

Properties (Identities) of Regular Expression

Arden’s Theorem

Conversion from FA to Regular Expression

Conversion from Regular Expression to Finite Automata

FCI-Minia University 15

Arden’s Theorem

Arden’s theorem state that:

“If P and Q are two regular expressions over ∑ , and if P does not contain ε , then the

following equation in R given by R = Q + RP has a unique solution i.e., R = QP*.”

That means, whenever we get any equation in the form of R = Q + RP, then we can

directly replaced by R = QP*.

So, here first we will prove that R = QP* is the solution of this equation and then we

will also prove that it is the unique solution of this equation.

FCI-Minia University 16

Arden’s Theorem: Prove 1

Let’s start by taking this equation as equation (i)

Now, replacing R by R = QP*, we get,

Taking Q as common,

(As we know that ε + R*R = R*). Hence proved.

FCI-Minia University 17

R = Q + RP ......(i)

R = Q + QP*P

R = Q(ε + P*P)

R = QP*Hence proved.

Thus, R = QP* is the

unique solution of the

equation R = Q + RP.

Arden’s Theorem: Prove 2

Now, we have to prove that this is the only solution of this equation. Let me take this

equation again:

Again, replace R by R = Q + RP:-

Now, replace R by R = Q + RP,

FCI-Minia University 18

R = Q + RP ......(i)

R = Q + (Q + RP)P

= Q + QP + R P2

R = Q + QP + (Q + RP) P2

= Q + QP + QP2+ RP3

= ...

= ...

= Q + QP + QP2

+ .. + QPn

+ RPn+1

Arden’s Theorem: Prove 2

Now, we have to prove that this is the only solution of this equation. Let me take this

equation again:

Now, replace R by R = QP*,

Taking Q as common,

19

R = Q + QP + QP2

+ .. + QPn

+ RPn+1

R = Q + QP + QP2

+ .. + QPn

+ QP*Pn+1

R = Q( ε+ P + P2

+ .. + Pn

+ P*Pn+1)

R=QP*

Hence proved.

Thus, R = QP* is the

unique solution of the

equation R = Q + RP.

Arden’s Theorem: Example

To understand this theorem, we will solve an example:Example #1

20

q1 = q1.0 +ε

q2 = q1.1 + q2.0

q3 = q2.1 + q3.0 + q3.1

q1 = ε + q1.0

q1 = ε.0* [By Arden's theorem R=Q+RP=QP*]

q1 = 0* [ε R *= R*]

q2 = 0*1 +q2.0 [By Arden's theorem R=Q+RP=QP*]

q2 = 0*10*

[Applying Arden’s theorem]. Hence, the value of q2 is 0*10*.

Arden’s Theorem: Example #2

FCI-Minia University 21

Example #2

Prove that (1+00*1)+(1+00*1)(0+10*1)*(0+10*1) is equal to 0*1(0+10*1)*

Solution

R= (1+00*1)+(1+00*1)(0+10*1)*(0+10*1)

By taking (1+00*1) common

R=(1+00*1) (ε+(0+10*1)*(0+10*1))

By using ε +RR*=R* , so (ε+(0+10*1)*(0+10*1))=(0+10*1)*

R=(1+00*1) (0+10*1)*

By taking 1 common

R=(ε +00*)1 (0+10*1)*

By using ε +RR*=R* , so (ε +00*)=0*

R=0*1 (0+10*1)*

Aim of The Lecture

Regular Expression

Designing Regular Expression

Properties (Identities) of Regular Expression

Arden’s Theorem

Conversion from FA to Regular Expression

Conversion from Regular Expression to Finite Automata

FCI-Minia University 22

EX #1: Find RE for the following NFA

Conversion from FA to Regular

Expression

FCI-Minia University 23

q3q1a

q2a

a

Write the equations of NFA states by checking the incoming transitions for each state.

Equations:

1. q3=q2a

2. q2=q1a+q2b+q3b

3. q1=ε+q1a+q2b

b

b b

24

Conversion from FA to Regular

Expression

Simplify equations:

Step1: q3=q2a

Putting value of q2 from eq2

q3=(q1a+q2b+q3b)a

q3=q1aa+q2ba+q3ba → eq4

Step2: q2=q1a+q2b+q3b

Putting value of q3 from eq1

q2=q1a+q2b+q2ab

Taking q2 as common

q2=q1a+q2(b+ab)

Using Arden’s theorem

q2=q1a(b+ab)* → eq5

Equations:

1. q3=q2a

2. q2=q1a+q2b+q3b

3. q1=ε+q1a+q2b

FCI-Minia University 25

Step3: q1=ε+q1a+q2b

Putting value of q2 from eq5

q1=ε+q1a+q1a(b+ab)*b

Taking q1 as common

q1=ε+q1(a+a(b+ab)*b)

Using Arden’s theorem

q1=ε(a+a(b+ab)*b)*

Using εR*=R*

q1=(a+a(b+ab)*b)* → eq6

To get final RE from RE of final state q3

q3=q2a

=q1a(b+ab)*a

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

Conversion from FA to Regular

ExpressionEquations:

1. q3=q2a

2. q2=q1a+q2b+q3b

3. q1=ε+q1a+q2b

4. q3=q1aa+q2ba+q3ba

5. q2=q1a(b+ab)*

Conversion from FA to Regular

Expression

EX #2: Find RE for the following NFA

FCI-Minia University 26

1 00 1

q1 q2

0,1

Equations:

1. q1= ε +q10

2. q2=q11+q21

3. q3=q20+q30+q31

q3

27

Conversion from FA to Regular

ExpressionSimplify equations:

Step1: q1= ε +q10

Using Arden’s theorem

q1= ε0*

= 0* → eq4

Step2: q2=q11+q21

Putting value of q1 from eq4

q2=0*1+q21

Using Arden’s theorem

q2=0*11* → eq5

Final RE is to union REs of all final states q1,q2

RE=0*+0*11*

=0*(ε+11*) =0*1* using ε+RR*=R*

Equations:

1. q1= ε +q10

2. q2=q11+q21

3. q3=q20+q30+q31

EX #3: Find RE for the following DFA

FCI-Minia University 28

Equations:

1. q1=ε+q2b+q3a

2. q2=q1a

3. q3=q1b

4. q4=q2a+q3b+q4a+q4b

a

q3

b

b

a

bq2

a

a,b

q1

Conversion from FA to Regular

Expression

q4

Simplify equations:

Step1 : q1=ε+q2b+q3a

Putting value of q2, q3 from eq2,3

q1= ε+q1ab+q1ba

Taking q1 as common

q1=ε+q1(ab+ba)

Using Arden’s Theorem

q1= ε(ab+ba)*

Using εR*=R*

q1=(ab+ba)* the final REFCI-Minia University 29

Conversion from FA to Regular

ExpressionEquations:

1. q1=ε+q2b+q3a

2. q2=q1a

3. q3=q1b

4. q4=q2a+q3b+q4a+q4b

EX #4: Find RE for the following DFA

30

Conversion from FA to Regular

Expression

Equations:

1. q0=ε+q0b

2. q1=q0a+q1a

3. q2=q1b+q2a+q2b

Simplify equations:

Step1: q0=ε+q0b

Using Arden’s Theorem

q0= εb*=b* → eq4

Step2: q1=q0a+q1a

Putting value of q0 from eq4

q1=b*a+q1a

Using Arden’s Theorem

q1=b*a a* → eq5

Step3: q2=q1b+q2a+q2b

Putting value of q1 from eq5

q2=b*a a* b+q2a+q2b

Taking q2 as common

q2=b*a a* b+q2(a+b)

Using Arden’s Theorem

q2=b*a a* b(a+b)* the final REFCI-Minia University 31

Conversion from FA to Regular

ExpressionEquations:

1. q0=ε+q0b

2. q1=q0a+q1a

3. q2=q1b+q2a+q2b

Aim of The Lecture

Regular Expression

Designing Regular Expression

Properties (Identities) of Regular Expression

Arden’s Theorem

Conversion from FA to Regular Expression

Conversion from Regular Expression to Finite Automata

FCI-Minia University 32

Conversion from Regular

Expression to Finite Automata

Case 1: For a regular expression 'a', we can construct the following FA:

Case 2: For a regular expression 'ab', we can construct the following FA:

FCI-Minia University 33

Conversion from Regular

Expression to Finite Automata

Case 3: For a regular expression (a+b), we can construct the following FA:

Case 4: For a regular expression (a+b)*, we can construct the following FA:

FCI-Minia University 34

Example #1 Construct the FA from the following RE

RE1=ba*b

The strings that are accepted by this RE start with b, following by

zero or more a and end with b

L1={bb, bab, baab, baaab,…}

35

Conversion from Regular

Expression to Finite Automata

A B Cb ba

Example #2 Construct the FA from the following RE

RE2=(a+b)c

The strings that are accepted by this RE start with a or b, and end

with a

L2={ac,bc}

36

Conversion from Regular Expression

to Finite Automata

A B Ca,b

c

Example #3 Construct the FA from the following RE

RE3=a(bc)*

The strings that are accepted by this RE start with a , and

followed by any number of bc.

L3={a, abc, abcbc, abcbcbc,….}

37

Conversion from Regular Expression

to Finite Automata

A Ca

bBc

Example #4 Construct the FA from the following RE

RE4=(a|b)*(abb|a+b)

The strings that are accepted by this RE start with any number of

a or b , and followed by string abb or one or more a then b.

L4={abb, aabb, babb, aab, bab,….}

38

Conversion from Regular Expression

to Finite Automata

q1 q3

ab

q4

a

q2

q5 q6

a, b

b

b

a

Example #5 Construct the FA from the following RE

RE5=10+ (0+11) 0*1

L5={10,01,111,001,1101,..}

39

Conversion from Regular Expression

to Finite Automata

q1

10

(0+11)0*1

q2

q1 q21 0 q3

q1 q11

0*

110

40

Conversion from Regular

Expression to Finite Automata

q1 q21 0 q3

q4 q51

0*

110

q1 q21 0 q3

q4

11

1

0

q5

0

Example #6 Construct the FA from the following RE

RE6=(aaa) *

L5={a3n| n>=0}

41

Conversion from Regular Expression

to Finite Automata

q2aq1 q3

a

a

Example #7 Construct the FA from the following RE

RE6=(111+11111)*

3, 5, 6,(111 twice), 9(111 three)

42

Conversion from Regular Expression

to Finite Automata

q21

q1 q31

q41

q8q7 1

1

1 q91

q51

q6

1

43

Assignments

Problem #1 Construct the FA from the following RE of string of binary number divisible by 3

RE#1=(0|1(01*0)*1)*

Problem #2 Construct the FA from the following RE RE#2=(ab+ba)*

Problem #3 Construct the FA from the following RE of string of even length

RE#3=((a+b)(a+b))*

Problem #4 Find RE for the following FA:

q1 q2a q3

q4

ab

abb

a,b

Thanks…

Next: Grammar

FCI-Minia University 44