1 languages and finite automata or how to talk to machines

55
1 Languages and Finite Automata or how to talk to machines...

Post on 20-Dec-2015

229 views

Category:

Documents


0 download

TRANSCRIPT

1

Languages and Finite Automata

orhow to talk to machines...

2

A language is a set of strings

String:A sequence of letters (a word)

Examples: “cat”, “dog”, “house”, …

Defined over an alphabet:set of symbols (letters)

Languages

zcba ,,,,

3

Alphabets and StringsWe will use small alphabets

Strings

abbaw

bbbaaav

abu

ba,

baaabbbaaba

baba

abba

ab

a

4

String Operations

Concatenation

Reverse

abbaw

aaaw n

21

bbbaaav

bbbv m

21

abbabbbaaawv

bbbaaawv mn

2121

aaabbbv

bbbvR

mR

12

5

String Length

Length:

Examples:

naaaw 21

nw

1

2

4

a

aa

abba

6

Recursive Definition of LengthFor any letter:

For any string :

Example:

1a

1wwawa

4

1111

111

11

1

a

ab

abbabba

7

Length of Concatenation

Example:

vuuv

853

8

5,

3,

vuuv

aababaabuv

vabaabv

uaabu

8

Proof of Concatenation LengthClaim:

Proof:By induction on the length

Induction basis:

is only one symbol

From definition of length:

vuuv

v

1v

vuuuv 1

v

9

Inductive hypothesis:

for all with

Inductive step: we will prove

for

vuuv

v nv ,,2,1

1nv

vuuv

10

Inductive StepWrite , where

From definition of length:

From inductive hypothesis:

Thus:

wav 1, anw

1

1

wwa

uwuwauv

wuuw

vuwauwuuv 1

11

Empty StringA string with no letters:

Observations:

abbaabbaabba

www

0

12

SubstringDefinition:

A substring of a string is any sequence of consecutive characters

Example: Substrings

w

abbab

bbab

b

abba

ab

13

Prefix and Suffix

Prefixes Suffixes

abbab

abbab

abba

abb

ab

a

b

ab

bab

bbab

abbabuvw

prefix

suffix

14

Another Operation

Example:

Definition for any :

n

n wwww

abbaabbaabba 2

w 0w

0abba

15

The * Operation : the set of all possible strings from alphabet

Example:

*

,,,,,,,,,*

,

aabaaabbbaabaaba

ba

*

,,,,,,,, aabaaabbbaabaaba

16

LanguageA language is any subset of

Examples:

A string is called “sentence”

*

,,,,,,,,,*

,

aabaaabbbaabaaba

ba

},,,,,{

,,,

aaaaaaabaababaabba

aabaaa

17

Another ExampleAn infinite language

0: nbaL nn

aaaaabbbbb

aabb

ab

L Labb

18

Operations on LanguagesThe usual set operations

Complement:

aaaaaabbbaaaaaba

ababbbaaaaaba

aaaabbabaabbbaaaaaba

,,,,

}{,,,

},,,{,,,

LL *

,,,,,,, aaabbabaabbaa

19

Reverse

Definition:

Examples:

LwwL RR :

ababbaabababaaabab R ,,,,

0:

0:

nabL

nbaL

nnR

nn

20

Concatenation

Definition:

Example:

2121 ,: LyLxxyLL

baaabababaaabbaaaab

aabbaaba

,,,,,

,,,

21

Another OperationDefinition:

Example:

Special case:

n

n LLLL

bbbbbababbaaabbabaaabaaa

babababa

,,,,,,,

,,,, 3

0

0

,, aaabbaa

L

22

Example

0: nbaL nn

0,:2 mnbabaL mmnn

2Laabbaaabbb

23

Star-Closure (Kleene *)

Definition:

Example:

210* LLLL

,,,,

,,,,

,,

,

*,

abbbbabbaaabbaaa

bbbbbbaabbaa

bbabba

24

Positive Closure

Definition:

*

21

L

LLL

,,,,

,,,,

,,

,

abbbbabbaaabbaaa

bbbbbbaabbaa

bba

bba

25

Finite Automata

Input

String

Output

String

FiniteAutomaton

26

Finite Accepter

Input

“Accept” or“Reject”

String

FiniteAutomaton

Output

27

Transition Graph

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

initialstate

final state“accept”state

transition

5q

a a bb

ba,Abba -Finite Accepter

ba,

28

Initial Configuration

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

5q

a a bb

ba,

Input Stringa b b a

ba,

29

Reading the Input

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

5q

a a bb

ba,

a b b a

ba,

30

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

5q

a a bb

ba,

a b b a

ba,

31

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

5q

a a bb

ba,

a b b a

ba,

32

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

Output: “accept”

5q

a a bb

ba,

a b b a

ba,

33

Rejection

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

5q

a a bb

ba,

a b a

ba,

34

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

5q

a a bb

ba,

a b a

ba,

35

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

5q

a a bb

ba,

a b a

ba,

36

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

5q

a a bb

ba,

Output:“reject”

a b a

ba,

37

FormalitiesDeterministic Finite Accepter (DFA)

FqQM ,,,, 0

Q

0q

F

: set of states

: input alphabet

: transition function

: initial state

: set of final states

38

Input Aplhabet

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

5q

a a bb

ba,

ba,

ba,

39

Set of States

Q

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

5q

a a bb

ba,

543210 ,,,,, qqqqqqQ

ba,

40

Initial State

0q

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

5q

a a bb

ba,

ba,

41

Set of Final States

F

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

5q

a a bb

ba,

4qF

ba,

42

Transition Function

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

5q

a a bb

ba,

QQ :

50

10

,

,

qbq

qaq

ba,

43

Transition Function

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

5q

a a bb

ba,

a b

0q

1q

2q

3q

4q

5q

1q 5q

5q 2q

2q 3q

4q 5q

ba,5q5q5q5q

44

Extended Transition Function

*

QQ *:*

50

40

20

,*

,*

,*

qabbaaq

qabbaq

qabq

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

5q

a a bb

ba,

ba,

45

Recursive Definition

)),,(*(,*

,*

awqwaq

qq

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

5q

a a bb

ba,

ba,

2

1

0

0

0

0

,

,,

,,,*

),,(*

,*

q

bq

baq

baq

baq

abq

46

Languages Accepted by DFAsTake DFA

Definition:The language accepted by contains all input strings accepted by

In other words: = { strings that drive to a final

state}

M

ML MM

M ML

47

Example

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

5q

a a bb

ba,

ba,

abbaML M

accept

48

Another Example

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

5q

a a bb

ba,

ba,

abbaabML ,, M

acceptacceptaccept

49

Formally

For a DFA

Language accepted by :

FqQM ,,,, 0

M

FwqwML ,*:* 0

alphabet transitionfunction

initialstate

finalstates

50

Observation Language accepted by :

Language rejected by :

FwqwML ,*:* 0

M

FwqwML ,*:* 0

M

51

More Examples

a

b ba,

ba,

0q 1q 2q

0: nbaML n

accept trap state

52

ML = { all substrings with prefix }ab

a b

ba,

0q 1q 2q

accept

ba,3q

ab

53

ML = { all strings without substring }001

0 00 001

1

0

1

10

0 1,0

54

Regular LanguagesDefinition:

A language is regularif there is a DFA such that

All the regular languages form a family

LM

MLL

55

ExampleThe languageis regular:

*,: bawawaL

a

b

ba,

a

b

ba

0q 2q 3q

4q