Deterministic Finite Automata And Regular Languagesvganesh/TEACHING/S2014/ECE351/lectures/DFA... ·...

Post on 12-Mar-2020

1 views 0 download

transcript

1

Deterministic Finite Automata And

Regular Languages

Costa Busch - LSU 2

Deterministic Finite Automaton (DFA)

Input Tape

“Accept” or “Reject”

String

Finite Automaton

Output

Costa Busch - LSU 3

Transition Graph

initial state

accepting state

state transition

0q 1q 2q 3q 4qa b b a

5qa a bb

ba,

ba,

Costa Busch - LSU 4

0q 1q 2q 3q 4qa b b a

5qa a bb

ba,

ba,

},{ ba=Σ

For every state, there is a transition for every symbol in the alphabet

Alphabet

Costa Busch - LSU 5

Initial Configuration

1q 2q 3q 4qa b b a

5qa a bb

ba,Input String

a b b a

ba,

0q

Initial state

Input Tape head

Costa Busch - LSU 6

Scanning the Input

0q 1q 2q 3q 4qa b b a

5qa a bb

ba,

a b b a

ba,

Costa Busch - LSU 7

0q 1q 2q 3q 4qa b b a

5qa a bb

ba,

a b b a

ba,

Costa Busch - LSU 8

0q 1q 2q 3q 4qa b b a

5qa a bb

ba,

a b b a

ba,

Costa Busch - LSU 9

0q 1q 2q 3q 4qa b b a accept

5qa a bb

ba,

a b b a

ba,

Input finished

Last state determines the outcome

Costa Busch - LSU 10

1q 2q 3q 4qa b b a

5qa a bb

ba,

a b a

ba,

0q

A Rejection Case

Input String

Costa Busch - LSU 11

0q 1q 2q 3q 4qa b b a

5qa a bb

ba,

a b a

ba,

Costa Busch - LSU 12

0q 1q 2q 3q 4qa b b a

5qa a bb

ba,

a b a

ba,

Costa Busch - LSU 13

0q 1q 2q 3q 4qa b b a

5qa a bb

ba,

reject

a b a

ba,

Input finished

Last state determines the outcome

Costa Busch - LSU 14

1q 2q 3q 4qa b b a

5qa a bb

ba,

ba,

0q

)(λ

Another Rejection Case

Tape is empty

reject

Input Finished (no symbol read)

Costa Busch - LSU 15

Language Accepted: { }abbaL =

0q 1q 2q 3q 4qa b b a

5qa a bb

ba,

ba,

This automaton accepts only one string

Costa Busch - LSU 16

To accept a string: all the input string is scanned and the last state is accepting

To reject a string:

all the input string is scanned and the last state is non-accepting

Costa Busch - LSU 17

Another Example

0q 1q 2q 3q 4qa b b a

5qa a bb

ba,

ba,

{ }abbaabL ,,λ=

Accept state

Accept state

Accept state

Costa Busch - LSU 18

)(λEmpty Tape

0q 1q 2q 3q 4qa b b a

5qa a bb

ba,

ba,

accept

Input Finished

Costa Busch - LSU 19

Another Example

a

b ba,

ba,

0q 1q 2q

Accept state

trap state

Costa Busch - LSU 20

a

b ba,

ba,

0q 1q 2q

a baInput String

Costa Busch - LSU 21

a

b ba,

ba,

0q 1q 2q

a ba

Costa Busch - LSU 22

a

b ba,

ba,

0q 1q 2q

a ba

Costa Busch - LSU 23

a

b ba,

ba,

0q 1q 2q

a ba

accept

Input finished

Costa Busch - LSU 24

a

b ba,

ba,

0q 1q 2q

ab b

A rejection case

Input String

Costa Busch - LSU 25

a

b ba,

ba,

0q 1q 2q

ab b

Costa Busch - LSU 26

a

b ba,

ba,

0q 1q 2q

ab b

Costa Busch - LSU 27

a

b ba,

ba,

0q 1q 2q

ab b

reject

Input finished

Costa Busch - LSU 28

Language Accepted: }0:{ ≥= nbaL n

a

b ba,

ba,

0q 1q 2q

Costa Busch - LSU 29

Another Example

0q 1q

1

1

}1{=ΣAlphabet:

Language Accepted:

even} is and :{ * xxxEVEN Σ∈=

},111111,1111,11,{ …λ=

Costa Busch - LSU 30

Formal Definition Deterministic Finite Automaton (DFA)

( )FqQM ,,,, 0δΣ=

δ

0q

F

: set of states

: input alphabet

: transition function

: initial state

: set of accepting states

Σ∉λ

Costa Busch - LSU 31

Set of States

Q

0q 1q 2q 3q 4qa b b a

5qa a bb

ba,{ }543210 ,,,,, qqqqqqQ =

ba,

Example

Costa Busch - LSU 32

Input Alphabet

Σ

0q 1q 2q 3q 4qa b b a

5qa a bb

ba,{ }ba,=Σ

ba,

:the input alphabet never contains λΣ∉λ

Example

Costa Busch - LSU 33

Initial State

0q

1q 2q 3q 4qa b b a

5qa a bb

ba,

ba,

0q

Example

Costa Busch - LSU 34

Set of Accepting States

QF ⊆

0q 1q 2q 3qa b b a

5qa a bb

ba,{ }4qF =

ba,

4q

Example

Costa Busch - LSU 35

Transition Function

QQ →Σ×:δ

q q ʹ′x

qxq ʹ′=),(δ

Describes the result of a transition from state with symbol q x

Costa Busch - LSU 36

2q 3q 4qa b b a

5qa a bb

ba,

ba,

0q 1q

( ) 10, qaq =δExample:

Costa Busch - LSU 37

( ) 50, qbq =δ

1q 2q 3q 4qa b b a

5qa a bb

ba,

ba,

0q

Costa Busch - LSU 38

0q 1q 2q 3q 4qa b b a

5qa a bb

ba,

ba,

( ) 32, qbq =δ

Costa Busch - LSU 39

δ a b0q1q2q3q4q5q

1q 5q5q 2q5q 3q4q 5q

0q 1q 2q 3q 4qa b b a

5qa a bb

ba,

ba,5q5q5q5q

Transition Table for st

ates

symbols

δ

Costa Busch - LSU 40

Extended Transition Function QQ →Σ× ** :δ

qwq ʹ′=),(*δ

Describes the resulting state after scanning string from state w q

Costa Busch - LSU 41

( ) 20* , qabq =δ

3q 4qa b b a

5qa a bb

ba,

ba,

0q 1q 2q

Example:

Costa Busch - LSU 42

( ) 50* , qabbbaaq =δ

1q 2q 3q 4qa b b a

5qa a bb

ba,

ba,

0q

Costa Busch - LSU 43

( ) 41* , qbbaq =δ

0q 1q 2q 3q 4qa b b a

5qa a bb

ba,

ba,

Costa Busch - LSU 44

Special case:

( ) qq =λδ ,*

for any state q

Costa Busch - LSU 45

( ) qwq ʹ′=,*δ

q qʹ′w

q qʹ′kw σσσ 21=

1σ 2σ kσ

states may be repeated

In general:

implies that there is a walk of transitions

Costa Busch - LSU 46

Language accepted by DFA :

Language Accepted by DFA

it is denoted as and contains all the strings accepted by

( )MLM

M

( )MLWe also say that recognizes M

Costa Busch - LSU 47

For a DFA Language accepted by :

( )FqQM ,,,, 0δΣ=

M( ) ( ){ }FwqwML ∈Σ∈= ,: 0

** δ

0q qʹ′w Fq ∈ʹ′

Costa Busch - LSU 48

Language rejected by :

( ) ( ){ }FwqwML ∉Σ∈= ,: 0** δ

M

0q qʹ′w Fq ∉ʹ′

Costa Busch - LSU 49

0q

More DFA Examples

ba,

},{ ba=Σ

*)( Σ=ML

0q

ba,

}{)( =MLEmpty language All strings

Costa Busch - LSU 50

0q

ba,

},{ ba=Σ

0q ba,

}{)( λ=MLLanguage of the empty string

Costa Busch - LSU 51

( )ML = { all strings with prefix } ab

a b

ba,

0q 1q 2q

accept

ba,3q

ab

},{ ba=Σ

Costa Busch - LSU 52

( )ML = { all binary strings containing substring } 001

λ 0 00 001

1

0

1

10

0 1,0

Costa Busch - LSU 53

( )ML = { all binary strings without substring } 001

λ 0 00 001

1

0

110

0 1,0

Costa Busch - LSU 54

{ }{ }*,:)( bawawaML ∈=

a

b

ba,

a

bb

a

0q 2q 3q

4q

Costa Busch - LSU 55

Regular Languages Definition: A language is regular if there is a DFA that accepts it ( )

The languages accepted by all DFAs form the family of regular languages

LM LML =)(

Costa Busch - LSU 56

{ }abba { }abbaab,,λ}0:{ ≥nban

{ all strings in {a,b}* with prefix } ab{ all binary strings without substring } 001

Example regular languages:

There exist DFAs that accept these languages (see previous slides).

{ }{ }*,: bawawa ∈

even} is and }1{:{ * xxx ∈

}{ }{λ *},{ ba

Costa Busch - LSU 57

There exist languages which are not Regular:

}0:{ ≥= nbaL nn

There are no DFAs that accept these languages

(we will prove this in a later class)

}n ,1z,1y,1x :{ mn

kmzyxADDITION k

=+

====+=

Properties of Regular Languages

Costa Busch - LSU 59

1L 2L

21LLConcatenation:

*1LStar:

21 LL ∪Union:

Are regular Languages

For regular languages and we will prove that:

1L

21 LL ∩

Complement:

Intersection:

RL1Reversal:

Costa Busch - LSU 60

We say: Regular languages are closed under

21LLConcatenation:

*1LStar:

21 LL ∪Union:

1L

21 LL ∩

Complement:

Intersection:

RL1Reversal:

Costa Busch - LSU 61

ab

ba

NFA

Equivalent NFA

λλ

ab

ba

A useful transformation: use one accept state

2 accept states

1 accept state

Costa Busch - LSU 62

NFA

Equivalent NFA

Single accepting state

λλλ

In General

Costa Busch - LSU 63

NFA without accepting state

Add an accepting state without transitions

Extreme case

Costa Busch - LSU 64

1LRegular language

( ) 11 LML =

1M

Single accepting state

NFA 2M

2L

Single accepting state

( ) 22 LML =

Regular language

NFA

Take two languages

Costa Busch - LSU 65

}{1 baL n=a

b

1M

{ }baL =2ab2M

0≥n

Example

Costa Busch - LSU 66

Union NFA for

1M

2M

21 LL ∪

λ

λ

Costa Busch - LSU 67

ab

ab

λ

λ

}{1 baL n=

}{2 baL =

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

Example

Costa Busch - LSU 68

Concatenation NFA for 21LL

1M 2Mλ

Costa Busch - LSU 69

NFA for

ab ab

}{1 baL n=}{2 baL =

}{}}{{21 bbaababaLL nn ==

λ

Example

Costa Busch - LSU 70

Star Operation NFA for *1L

1M

λ

λ

*1L∈λλ λ

1

21

Lwwwww

i

k

=

Costa Busch - LSU 71

NFA for *}{*1 baL n=

ab

}{1 baL n=

λ

λ

λ

λ

Example

Costa Busch - LSU 72

Reverse RL1

1MNFA for

ʹ′1M

1. Reverse all transitions

2. Make initial state accepting state and vice versa

1L

Costa Busch - LSU 73

}{1 baL n=a

b

1M

}{1nR baL =

ab

ʹ′1M

Example

Costa Busch - LSU 74

Complement

1. Take the DFA that accepts 1L

1M1L ʹ′1M1L

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

Costa Busch - LSU 75

}{1 baL n=

ab

1M

ba,

ba,

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

b

ʹ′1M

ba,

ba,

Example

Costa Busch - LSU 76

Intersection

1L regular

2L regular We show 21 LL ∩

regular

Costa Busch - LSU 77

DeMorgan’s Law: 2121 LLLL ∪=∩

21 , LL regular

21 , LL regular

21 LL ∪ regular

21 LL ∪ regular

21 LL ∩ regular

Costa Busch - LSU 78

Example

}{1 baL n=

},{2 baabL =

regular

regular }{21 abLL =∩

regular

Costa Busch - LSU 79

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

Costa Busch - LSU 80

States in M

ji pq ,

1M 2MState in State in

Costa Busch - LSU 81

1M 2M

1q 2qa

transition 1p 2pa

transition

DFA DFA

11, pq a

New transition

MDFA

22, pq

Costa Busch - LSU 82

0q

initial state 0p

initial state

New initial state

00, pq

1M 2MDFA DFA

MDFA

Costa Busch - LSU 83

iq

accept state

jp

accept states

New accept states

ji pq ,

kp

ki pq ,

1M 2MDFA DFA

MDFA

Both constituents must be accepting states

Costa Busch - LSU 84

Example:

}{1 baL n=

ab

1M

0≥n

}{2mabL =

b

b

2M

0q 1q 0p 1p

0≥m

2q 2pa

a

ba, ba,

ba,

Costa Busch - LSU 85

00, pq

Automaton for intersection

}{}{}{ ababbaL mn =∩=

10, pqa

21, pq

b

ab 11, pq

20, pq

a

12, pq

22, pq

b

ba,

a

b

ba,

b

a

Costa Busch - LSU 86

M simulates in parallel and 1M 2M

M accepts string w if and only if:

accepts string w1Mand accepts string w2M

)()()( 21 MLMLML ∩=

Regular Expressions

Costa Busch - LSU 88

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

*)( cba ⋅+

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

Costa Busch - LSU 89

Recursive Definition αλ,,∅

( )11

21

21

*rrrrrr

+

Are regular expressions

Primitive regular expressions:

2r1rGiven regular expressions and

Costa Busch - LSU 90

Examples

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

( )++ baNot a regular expression:

Costa Busch - LSU 91

Languages of Regular Expressions : language of regular expression Example

( )rL r

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

Costa Busch - LSU 92

Definition For primitive regular expressions:

( )

( ) { }

( ) { }aaL

L

L

=

=

∅=∅

λλ

Costa Busch - LSU 93

Definition (continued) For regular expressions and

1r 2r

( ) ( ) ( )2121 rLrLrrL ∪=+

( ) ( ) ( )2121 rLrLrrL =⋅

( ) ( )( )** 11 rLrL =

( )( ) ( )11 rLrL =

Costa Busch - LSU 94

Example Regular expression: ( ) *aba ⋅+

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

( ) ( )*aLbaL +=

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

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

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

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

Costa Busch - LSU 95

Example Regular expression

( ) ( )bbabar ++= *

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

Costa Busch - LSU 96

Example Regular expression ( ) ( ) bbbaar **=

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

Costa Busch - LSU 97

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

)(rL = { all strings containing substring 00 }

Costa Busch - LSU 98

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

)(rL = { all strings without substring 00 }

Costa Busch - LSU 99

Equivalent Regular Expressions Definition: Regular expressions and are equivalent if

1r 2r

)()( 21 rLrL =

Costa Busch - LSU 100

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

Regular Expressions and

Regular Languages

Costa Busch - LSU 102

Theorem

Languages Generated by Regular Expressions

Regular Languages =

Costa Busch - LSU 103

Languages Generated by Regular Expressions

Regular Languages

Languages Generated by Regular Expressions

Regular Languages

Proof:

Costa Busch - LSU 104

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

Costa Busch - LSU 105

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

)()( 1 ∅=∅= LML

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

)(}{)( 3 aLaML ==

regular languages

a

Costa Busch - LSU 106

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

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

Costa Busch - LSU 107

Inductive Step We will prove: ( )

( )

( )

( )( )1

1

21

21

*

rL

rL

rrL

rrL

+

Are regular Languages

Costa Busch - LSU 108

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

( ) ( ) ( )

( ) ( )( )

( )( ) ( )11

11

2121

2121

**

rLrL

rLrL

rLrLrrL

rLrLrrL

=

=

=⋅

∪=+

Costa Busch - LSU 109

)( 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:

Costa Busch - LSU 110

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

Costa Busch - LSU 111

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 =

λ

λ

Costa Busch - LSU 112

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

Costa Busch - LSU 113

Since is regular, there is a NFA that accepts it

LM

LML =)(

Take it with a single accept state

Costa Busch - LSU 114

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

Costa Busch - LSU 115

Another Example:

ba +a

b

b0q 1q 2q

ba,a

b

b0q 1q 2q

b

bTransition labels are regular expressions

Costa Busch - LSU 116

Reducing the states: ba +

ab

b0q 1q 2q

b

0q 2q

babb*

)(* babb +

Transition labels are regular expressions

Costa Busch - LSU 117

Resulting Regular Expression:

0q 2q

babb*

)(* babb +

*)(**)*( bbabbabbr ⋅+⋅=

LMLrL == )()(

Costa Busch - LSU 118

In General Removing a state:

iq q jqa b

cde

iq jq

dae* bce*dce*

bae*

Costa Busch - LSU 119

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

Costa Busch - LSU 120

Standard Representations of Regular Languages

Regular Languages

DFAs

NFAs Regular Expressions

Costa Busch - LSU 121

When we say: We are given a Regular Language

We mean:

L

Language is in a standard representation

L

(DFA, NFA, or Regular Expression)