regular languages, regular expressions, and pumping lemma · 2014-02-14 · regular languages,...

97
2/14/14 Costas Busch - RPI 1 Regular Languages, Regular Expressions, and Pumping Lemma

Upload: others

Post on 09-Jul-2020

18 views

Category:

Documents


1 download

TRANSCRIPT

2/14/14 Costas Busch - RPI 1

Regular Languages, Regular Expressions, and Pumping Lemma

2/14/14 Costas Busch - RPI 2

Non Turing-Acceptable

Turing-Acceptable

decidable

Context-sensitive

Context-free

Regular

The Chomsky Hierarchy

2/14/14 Costas Busch - RPI 3

Standard Representations of Regular Languages

Regular Languages

DFAs

NFAs Regular Expressions

2/14/14 Costas Busch - RPI 4

When we say: We are given a Regular Language

We mean:

L

Language is in a standard representation

L

(DFA, NFA, or Regular Expression)

2/14/14 Costas Busch - RPI 5

1L 2L

21LLConcatenation:

*1LStar:

21 LL ∪Union:

Are regular Languages

For regular languages and we will prove that:

1L

21 LL ∩

Complement:

Intersection:

RL1Reversal:

2/14/14 Costas Busch - RPI 6

We say: Regular languages are closed under

21LLConcatenation:

*1LStar:

21 LL ∪Union:

1L

21 LL ∩

Complement:

Intersection:

RL1Reversal:

2/14/14 Costas Busch - RPI 7

1LRegular language

( ) 11 LML =

1M

Single accepting state

NFA 2M

2L

Single accepting state

( ) 22 LML =

Regular language

NFA

Take two languages

2/14/14 Costas Busch - RPI 8

}{1 baL n=a

b

1M

{ }baL =2ab2M

0≥n

Example

2/14/14 Costas Busch - RPI 9

Union NFA for

1M

2M

21 LL ∪

λ

λ

2/14/14 Costas Busch - RPI 10

ab

ab

λ

λ

}{1 baL n=

}{2 baL =

}{}{21 babaLL n ∪=∪NFA for

Example

2/14/14 Costas Busch - RPI 11

Concatenation NFA for 21LL

1M 2Mλ

2/14/14 Costas Busch - RPI 12

NFA for

ab ab

}{1 baL n=}{2 baL =

}{}}{{21 bbaababaLL nn ==

λ

Example

2/14/14 Costas Busch - RPI 13

Star Operation NFA for *1L

1M

λ

λ

*1L∈λλ λ

1

21

Lwwwww

i

k

=

2/14/14 Costas Busch - RPI 14

NFA for *}{*1 baL n=

ab

}{1 baL n=

λ

λ

λ

λ

Example

2/14/14 Costas Busch - RPI 15

Reverse RL1

1MNFA for

ʹ′1M

1. Reverse all transitions

2. Make initial state accepting state and vice versa

1L

2/14/14 Costas Busch - RPI 16

}{1 baL n=a

b

1M

}{1nR baL =

ab

ʹ′1M

Example

2/14/14 Costas Busch - RPI 17

Complement

1. Take the DFA that accepts 1L

1M1L ʹ′1M1L

2. Make accepting states non-final, and vice-versa

2/14/14 Costas Busch - RPI 18

}{1 baL n=

ab

1M

ba,

ba,

}{*},{1 babaL n−= a

b

ʹ′1M

ba,

ba,

Example

2/14/14 Costas Busch - RPI 19

Intersection

1L regular

2L regular We show 21 LL ∩

regular

2/14/14 Costas Busch - RPI 20

DeMorgan’s Law: 2121 LLLL ∪=∩

21 , LL regular

21 , LL regular

21 LL ∪ regular

21 LL ∪ regular

21 LL ∩ regular

2/14/14 Costas Busch - RPI 21

Example

}{1 baL n=

},{2 baabL =

regular

regular }{21 abLL =∩

regular

2/14/14 Costas Busch - RPI 22

1Lfor for 2LDFA 1M

DFA

2M

Construct a new DFA that accepts

Machine Machine

M 21 LL ∩

M simulates in parallel and 1M 2M

Another Proof for Intersection Closure

2/14/14 Costas Busch - RPI 23

States in M

ji pq ,

1M 2MState in State in

2/14/14 Costas Busch - RPI 24

1M 2M

1q 2qa

transition 1p 2pa

transition

DFA DFA

11, pq a

New transition

MDFA

22, pq

2/14/14 Costas Busch - RPI 25

0q

initial state 0p

initial state

New initial state

00, pq

1M 2MDFA DFA

MDFA

2/14/14 Costas Busch - RPI 26

iq

accept state

jp

accept states

New accept states

ji pq ,

kp

ki pq ,

1M 2MDFA DFA

MDFA

Both constituents must be accepting states

2/14/14 Costas Busch - RPI 27

Example:

}{1 baL n=

ab

1M

0≥n

}{2mabL =

b

b

2M

0q 1q 0p 1p

0≥m

2q 2pa

a

ba, ba,

ba,

2/14/14 Costas Busch - RPI 28

00, pq

Automaton for intersection

}{}{}{ ababbaL nn =∩=

10, pqa

21, pq

b

ab 11, pq

20, pq

a

12, pq

22, pq

b

ba,

a

b

ba,

b

a

2/14/14 Costas Busch - RPI 29

M simulates in parallel and 1M 2M

M accepts string w if and only if:

accepts string w1Mand accepts string w2M

)()()( 21 MLMLML ∩=

2/14/14 Costas Busch - RPI 30

Regular Languages and Regular Expressions

2/14/14 Costas Busch - RPI 31

Regular Expressions Regular expressions describe regular languages Example: describes the language

*)( cba ⋅+

{ } { },...,,,,,*, bcaabcaabcabca λ=

2/14/14 Costas Busch - RPI 32

Recursive Definition αλ,,∅

( )11

21

21

*rrrrrr

+

Are regular expressions

Primitive regular expressions:

2r1rGiven regular expressions and

2/14/14 Costas Busch - RPI 33

Examples

( ) )(* ∅+⋅⋅+ ccbaA regular expression:

( )++ baNot a regular expression:

2/14/14 Costas Busch - RPI 34

Languages of Regular Expressions : language of regular expression Example

( )rL r

( ) { },...,,,,,*)( bcaabcaabcacbaL λ=⋅+

2/14/14 Costas Busch - RPI 35

Definition For primitive regular expressions:

( )

( ) { }

( ) { }aaL

L

L

=

=

∅=∅

λλ

2/14/14 Costas Busch - RPI 36

Definition (continued) For regular expressions and

1r 2r

( ) ( ) ( )2121 rLrLrrL ∪=+

( ) ( ) ( )2121 rLrLrrL =⋅

( ) ( )( )** 11 rLrL =

( )( ) ( )11 rLrL =

2/14/14 Costas Busch - RPI 37

Example Regular expression: ( ) *aba ⋅+

( )( )*abaL ⋅+ ( )( ) ( )*aLbaL +=

( ) ( )*aLbaL +=

( ) ( )( ) ( )( )*aLbLaL ∪=

{ } { }( ) { }( )*aba ∪=

{ }{ },...,,,, aaaaaaba λ=

{ },...,,,...,,, baababaaaaaa=

2/14/14 Costas Busch - RPI 38

Example Regular expression

( ) ( )bbabar ++= *

( ) { },...,,,,, bbbbaabbaabbarL =

2/14/14 Costas Busch - RPI 39

Example Regular expression ( ) ( ) bbbaar **=

( ) }0,:{ 22 ≥= mnbbarL mn

2/14/14 Costas Busch - RPI 40

Example Regular expression *)10(00*)10( ++=r

)(rL = { all strings containing substring 00 }

2/14/14 Costas Busch - RPI 41

Example Regular expression )0(*)011( λ++=r

)(rL = { all strings without substring 00 }

2/14/14 Costas Busch - RPI 42

Equivalent Regular Expressions Definition: Regular expressions and are equivalent if

1r 2r

)()( 21 rLrL =

2/14/14 Costas Busch - RPI 43

Example L = { all strings without substring 00 }

)0(*)011(1 λ++=r

)0(*1)0(**)011*1(2 λλ +++=r

LrLrL == )()( 211r 2rand

are equivalent regular expressions

2/14/14 Costas Busch - RPI 44

Regular Expressions and

Regular Languages

2/14/14 Costas Busch - RPI 45

Theorem

Languages Generated by Regular Expressions

Regular Languages =

2/14/14 Costas Busch - RPI 46

Languages Generated by Regular Expressions

Regular Languages

Languages Generated by Regular Expressions

Regular Languages

Proof:

2/14/14 Costas Busch - RPI 47

Proof - Part 1

r)(rL

For any regular expression the language is regular

Languages Generated by Regular Expressions

Regular Languages

Proof by induction on the size of r

2/14/14 Costas Busch - RPI 48

Induction Basis Primitive Regular Expressions: αλ,,∅Corresponding NFAs

)()( 1 ∅=∅= LML

)(}{)( 2 λλ LML ==

)(}{)( 3 aLaML ==

regular languages

a

2/14/14 Costas Busch - RPI 49

Inductive Hypothesis Suppose that for regular expressions and , and are regular languages

1r 2r)( 1rL )( 2rL

2/14/14 Costas Busch - RPI 50

Inductive Step We will prove: ( )

( )

( )

( )( )1

1

21

21

*

rL

rL

rrL

rrL

+

Are regular Languages

2/14/14 Costas Busch - RPI 51

By definition of regular expressions: ( ) ( ) ( )

( ) ( ) ( )

( ) ( )( )

( )( ) ( )11

11

2121

2121

**

rLrL

rLrL

rLrLrrL

rLrLrrL

=

=

=⋅

∪=+

2/14/14 Costas Busch - RPI 52

)( 1rL )( 2rLBy inductive hypothesis we know: and are regular languages

Regular languages are closed under: ( ) ( )( ) ( )( )( )*1

21

21

rLrLrLrLrL ∪Union

Concatenation

Star

We also know:

2/14/14 Costas Busch - RPI 53

Therefore:

( ) ( ) ( )

( ) ( ) ( )

( ) ( )( )** 11

2121

2121

rLrL

rLrLrrL

rLrLrrL

=

=⋅

∪=+

Are regular languages

)())(( 11 rLrL = is trivially a regular language (by induction hypothesis)

End of Proof-Part 1

2/14/14 Costas Busch - RPI 54

Using the regular closure of these operations, we can construct recursively the NFA that accepts

M)()( rLML =

Example: 21 rrr +=)()( 11 rLML =

)()( 22 rLML =

)()( rLML =

λ

λ

2/14/14 Costas Busch - RPI 55

For any regular language there is a regular expression with

Proof - Part 2

Languages Generated by Regular Expressions

Regular Languages

Lr LrL =)(

We will convert an NFA that accepts to a regular expression

L

2/14/14 Costas Busch - RPI 56

Since is regular, there is a NFA that accepts it

LM

LML =)(

Take it with a single final state

2/14/14 Costas Busch - RPI 57

From construct the equivalent Generalized Transition Graph in which transition labels are regular expressions

M

Example:

aba,

cM

a

ba +

c

Corresponding Generalized transition graph

2/14/14 Costas Busch - RPI 58

Another Example:

ba +a

b

b0q 1q 2q

ba,a

b

b0q 1q 2q

b

bTransition labels are regular expressions

2/14/14 Costas Busch - RPI 59

Reducing the states: ba +

ab

b0q 1q 2q

b

0q 2q

babb*

)(* babb +

Transition labels are regular expressions

2/14/14 Costas Busch - RPI 60

Resulting Regular Expression:

0q 2q

babb*

)(* babb +

*)(**)*( bbabbabbr +=

LMLrL == )()(

2/14/14 Costas Busch - RPI 61

In General Removing a state:

iq q jqa b

cde

iq jq

dae* bce*dce*

bae*

2/14/14 Costas Busch - RPI 62

0q fq

1r

2r

3r4r

*)*(* 213421 rrrrrrr +=

LMLrL == )()(

The resulting regular expression:

By repeating the process until two states are left, the resulting graph is

Initial graph Resulting graph

End of Proof-Part 2

2/14/14 Costas Busch - RPI 63

Non-regular languages

(Pumping Lemma)

2/14/14 Costas Busch - RPI 64

Regular languages ba* acb +*

...etc*)( bacb ++

Non-regular languages }0:{ ≥nba nn

}*},{:{ bavvvR ∈

2/14/14 Costas Busch - RPI 65

Non Turing-Acceptable

Turing-Acceptable

decidable

Context-sensitive

Context-free

Regular

The Chomsky Hierarchy

2/14/14 Costas Busch - RPI 66

How can we prove that a language is not regular?

L

Prove that there is no DFA or NFA or RE that accepts L

Difficulty: this is not easy to prove (since there is an infinite number of them)

Solution: use the Pumping Lemma !!!

2/14/14 Costas Busch - RPI 67

The Pumping Lemma

2/14/14 Costas Busch - RPI 68

Take an infinite regular language L

There exists a DFA that accepts L

mstates

(contains an infinite number of strings)

2/14/14 Costas Busch - RPI 69

mw ≥||(number of states of DFA)

then, at least one state is repeated in the walk of w

q...... ...... 1σ 2σ kσ

Take string with Lw∈

kw σσσ 21=Walk in DFA of

Repeated state in DFA

2/14/14 Costas Busch - RPI 70

Take to be the first state repeated q

q....

w

There could be many states repeated

q.... ....

Second

occurrence First

occurrence

Unique states

One dimensional projection of walk :

1σ 2σ kσiσ jσ1+iσ 1+jσ

2/14/14 Costas Busch - RPI 71

q.... q.... ....

Second

occurrence First

occurrence 1σ 2σ kσiσ jσ1+iσ 1+jσ

wOne dimensional projection of walk :

ix σσ 1= jiy σσ 1+= kjz σσ 1+=

xyzw =We can write

2/14/14 Costas Busch - RPI 72

zyxw =

q... ...

x

y

z

In DFA:

...

...

1σ kσiσ1+iσjσ

1+jσ

contains only first occurrence of q

2/14/14 Costas Busch - RPI 73

Observation: myx ≤||length number of states of DFA

Since, in no state is repeated (except q)

xy

Unique States

q...

x

y

...

1σ iσ1+iσjσ

2/14/14 Costas Busch - RPI 74

Observation: 1|| ≥ylength

Since there is at least one transition in loop

q

y

...

1+iσjσ

2/14/14 Costas Busch - RPI 75

We do not care about the form of string z

q...

x

y

z

...

z may actually overlap with the paths of and x y

2/14/14 Costas Busch - RPI 76

The string is accepted

zxAdditional string:

q... ...

x z

...

Do not follow loop y

...

1σ kσiσ1+iσjσ

1+jσ

2/14/14 Costas Busch - RPI 77

The string is accepted

zyyx

q... ... ...

x z

Follow loop 2 times

Additional string:

y

...

1σ kσiσ1+iσjσ

1+jσ

2/14/14 Costas Busch - RPI 78

The string is accepted

zyyyx

q... ... ...

x z

Follow loop 3 times

Additional string:

y

...

1σ kσiσ1+iσjσ

1+jσ

2/14/14 Costas Busch - RPI 79

The string is accepted

zyx iIn General:

...,2,1,0=i

q... ... ...

x z

Follow loop times i

y

...

1σ kσiσ1+iσjσ

1+jσ

2/14/14 Costas Busch - RPI 80

Lzyx i ∈Therefore: ...,2,1,0=i

Language accepted by the DFA

q... ... ...

x z

y

...

1σ kσiσ1+iσjσ

1+jσ

2/14/14 Costas Busch - RPI 81

In other words, we described:

The Pumping Lemma !!!

2/14/14 Costas Busch - RPI 82

The Pumping Lemma: •  Given a infinite regular language L

•  there exists an integer m

•  for any string with length Lw∈ mw ≥||

•  we can write zyxw =

•  with and myx ≤|| 1|| ≥y

•  such that: Lzyx i ∈ ...,2,1,0=i

(critical length)

2/14/14 Costas Busch - RPI 83

In the book:

Critical length = Pumping length m p

2/14/14 Costas Busch - RPI 84

Applications of

the Pumping Lemma

2/14/14 Costas Busch - RPI 85

Observation: Every language of finite size has to be regular

Therefore, every non-regular language has to be of infinite size (contains an infinite number of strings)

(we can easily construct an NFA that accepts every string in the language)

2/14/14 Costas Busch - RPI 86

Suppose you want to prove that An infinite language is not regular

1. Assume the opposite: is regular

2. The pumping lemma should hold for

3. Use the pumping lemma to obtain a contradiction

L

L

L

4. Therefore, is not regular L

2/14/14 Costas Busch - RPI 87

Explanation of Step 3: How to get a contradiction

2. Choose a particular string which satisfies the length condition

Lw∈

3. Write xyzw =4. Show that Lzxyw i ∉=ʹ′ for some 1≠i

5. This gives a contradiction, since from pumping lemma Lzxyw i ∈=ʹ′

mw ≥||

1. Let be the critical length for m L

2/14/14 Costas Busch - RPI 88

Note: It suffices to show that only one string gives a contradiction

Lw∈

You don’t need to obtain contradiction for every Lw∈

2/14/14 Costas Busch - RPI 89

Theorem: The language }0:{ ≥= nbaL nn

is not regular

Proof: Use the Pumping Lemma

Example of Pumping Lemma application

2/14/14 Costas Busch - RPI 90

Assume for contradiction that is a regular language L

Since is infinite we can apply the Pumping Lemma

L

}0:{ ≥= nbaL nn

2/14/14 Costas Busch - RPI 91

Let be the critical length for

Pick a string such that: w Lw ∈

mw ≥||and length

mmbaw =We pick

m

}0:{ ≥= nbaL nn

L

2/14/14 Costas Busch - RPI 92

with lengths

From the Pumping Lemma:

1||,|| ≥≤ ymyx

babaaaaabaxyz mm ............==

mkay k ≤≤= 1,

x y z

m m

we can write zyxbaw mm ==

Thus:

=w

2/14/14 Costas Busch - RPI 93

From the Pumping Lemma: Lzyx i ∈

...,2,1,0=i

Thus:

mmbazyx =

Lzyx ∈2

mkay k ≤≤= 1,

2/14/14 Costas Busch - RPI 94

From the Pumping Lemma:

Lbabaaaaaaazxy ∈= ...............2

x y z

km + m

Thus:

Lzyx ∈2

mmbazyx =

y

Lba mkm ∈+

mkay k ≤≤= 1,

2/14/14 Costas Busch - RPI 95

Lba mkm ∈+

}0:{ ≥= nbaL nnBUT:

Lba mkm ∉+

CONTRADICTION!!!

1≥k

2/14/14 Costas Busch - RPI 96

Our assumption that is a regular language is not true

L

Conclusion: L is not a regular language

Therefore:

END OF PROOF

2/14/14 Costas Busch - RPI 97

Regular languages

Non-regular language }0:{ ≥nba nn

)( **baL