Languages Automata (Slides)

74
Languages

description

Theory of Automata

Transcript of Languages Automata (Slides)

Languages

• A language is a set of strings/words

• A string is a finite sequence of symbols

Example: “cat”, “dog”, “house”, …defined over an alphabet:

Example: “aaaab”, “bbbbbb”, “ababababa”, …defined over an alphabet:

Languages

{ }a,b,c, ,zΣ =

{ }a,bΣ =2

Alphabets and Strings• We usually use small alphabets:

• Strings

u abv bbbaaaw abba

===

{ } { }a,b , 0,1Σ = Σ =

aababbababaaaabbbaabab

3

String Operations

1 2 n

1 2 m

w a a av b b b==

abbabbbaaa

1 2 n 1 2 mwv a a a b b b=

Concatenation

abbabbbaaa

4

Rn 2 1w a a a=

1 2 nw a a a= abbaabbb

Reverse

bbbaabba

String Operations

5

String Length

• Length:

• Examples:

1 2 nw a a a=

w n=

abba 4aa 2a 1

=

=

=

6

Length of Concatenation

• Example:

uv u v= +

u aab, u 3v abaab, v 5

uv aababaab 8uv u v 3 5 8

= =

= =

= =

= + = + =

7

Empty String• A string without symbols:

• Observations:

λ

0

w w w

abba abba ab ba abba

λ =

λ = λ =

λ = λ = λ =

8

Substring• A substring of a string is

a subsequence of consecutive symbols

String Substringababbabbabbab

ababba

b

babb

ab ababbababbab

9

Prefix and Suffix

Prefixes Suffixes

abbab

aababbabbaabbab

λ abbabbbabbababbλ

w vu=

prefix

suffix

10

Replication

• Example:

• Definition:

n

n

w ww w=

( )2abba abbaabba=

0w = λ

( )0abba = λ

11

* Operation• : the set of all possible strings from

alphabet

• Example:

∗Σ

{ }{ }*

a,b

,a,b,aa,ab,ba,bb,aaa,aab,

Σ =

Σ = λ

12

+ Operation• : the set of all possible strings from

alphabet except

• Example:

+ΣΣ

{ }{ }a,b

* ,a,b,aa,ab,ba,bb,aaa,aab,Σ =

Σ = λ

{ }+ ∗Σ = Σ − λ

{ }a,b,aa,ab,ba,bb,aaa,aab,+Σ =

13

Language• A language is any subset of

• Example:

• Languages:

∗Σ

{ }{ }*

a,b

,a,b,aa,ab,ba,bb,aaa,

Σ =

Σ = λ

{ }{ }a,aa,aab{ ,abba,baba,aa,ab,aaaaaa}

λ

λ14

Language

• An infinite language n nL {a b : n 0}= ≥

abL

aabbaaaaabbbbb

λ

∈ abb L∉

15

Operations on Languages• The usual set operations:

• Complement:

{ } { }{ } { }{ } { } { }

a,ab,aaaa bb,ab {a,ab,bb,aaaa}a,ab,aaaa bb,ab {ab}a,ab,aaaa bb,ab a,aaaa

∪ =

∩ =

− =

L L∗= Σ −

{ } { }a,ba ,b,aa,ab,bb,aaa,= λ

, ,∪ ∩ −

16

Reverse

• Definition:

• Examples:

R RL {w : w L}= ∈

{ } { }Rab,aab,baba ba,baa,abab=

n n

R n n

L {a b : n 0}

L {b a : n 0}

= ≥

= ≥

17

Concatenation

• Definition:

• Example:

{ }1 2 1 2L L xy : x L , y L= ∈ ∈

{ }{ } { }a,ab,ba b,aa ab,aaa,abb,abaa,bab,baaa=

18

Replication• Definition:

• Special case:

n

n

L LL L=

{ } { }{ }{ }{ }

3a,b a,b a,b a,baaa,aab,aba,abb,baa,bab,bba,bbb

= =

{ }

{ } { }

0

0

L

a,bba,aaa

= λ

= λ

19

Examples

n nL {a b : n 0}= ≥

2 n n m mL {a b a b : n,m 0}= ≥

2aabbaaabbb L∈

20

Star-Closure (Kleene *)

• Definition:

• Example:

0 1 2L L L L∗ = ∪ ∪

{ }

,a,bb,

a,bbaa,abb,bba,bbbb,aaa,aabb,abba,abbbb,

λ =

21

Positive Closure (Kleene +)

• Definition: { }1 2L L L L+ ∗= ∪ ∪ = − λ

{ }a, bb,

a, bb aa, abb, bba, bbbb,aaa, aabb, abba, abbbb,

+ =

22

Exercises

1. Let 𝐿𝐿 = 𝑎𝑎𝑎𝑎, 𝑎𝑎𝑎𝑎, 𝑎𝑎𝑎𝑎𝑎𝑎 . Which strings are in 𝐿𝐿2, 𝐿𝐿3, and

𝐿𝐿4?

2. Let Σ = 𝑎𝑎, 𝑎𝑎 and 𝐿𝐿 = 𝑎𝑎𝑎𝑎, 𝑎𝑎𝑎𝑎 . Use set notation to

describe �𝐿𝐿.

3. Prove that 𝑤𝑤𝑅𝑅 𝑅𝑅 = 𝑤𝑤 for all 𝑤𝑤 ∈ Σ∗.

4. Prove that 𝐿𝐿1𝐿𝐿2 𝑅𝑅 = 𝐿𝐿2𝑅𝑅𝐿𝐿1𝑅𝑅 for all languages 𝐿𝐿1 and 𝐿𝐿2.

23

Finite Automata

• An automaton is an abstract model of a digital computer.

Finite Automaton

Input

String

Output

String

FiniteAutomaton

25

• An automaton is an abstract model of a digital computer.

Finite Accepter

Input

“Accept”or

“Reject”

String

FiniteAutomaton

Output

26

Transition Graph

initialstate

finalstate

“accept”statetransition

Abba -Finite Accepter

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

5qa a bb

a,b

a,b

27

Initial Configuration

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

5qa a bb

a,b

a,b

Input Stringa b b a

28

Reading the Input

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

5qa a bb

a,b

a,b

Input Stringa b b a

29

Reading the Input

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

5qa a bb

a,b

a,b

Input Stringa b b a

30

Reading the Input

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

5qa a bb

a,b

a,b

Input Stringa b b a

31

Reading the Input

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

5qa a bb

a,b

a,b

Input Stringa b b a

32

Reading the Input

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

5qa a bb

a,b

a,b

Input Stringa b b a

Output: “accept”33

Rejection

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

5qa a bb

a,b

a,b

Input Stringa b a

34

Rejection

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

5qa a bb

a,b

a,b

Input Stringa b a

35

Rejection

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

5qa a bb

a,b

a,b

Input Stringa b a

36

Rejection

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

5qa a bb

a,b

a,b

Input Stringa b a

37

Rejection

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

5qa a bb

a,b

a,b

Input Stringa b a

Output: “reject”38

Another Example

0q 2q1qb

a,ba

a,b

Input Stringa a b

39

Another Example

0q 2q1qb

a,ba

a,b

Input Stringa a b

40

Another Example

0q 2q1qb

a,ba

a,b

Input Stringa a b

41

Another Example

0q 2q1qb

a,ba

a,b

Input Stringa a b

42

Another Example

0q 2q1qb

a,ba

a,b

Input Stringa a b

Output: “accept”43

Rejection

0q 2q1qb

a,ba

a,b

Input Stringb a b

44

Rejection

0q 2q1qb

a,ba

a,b

Input Stringb a b

45

Rejection

0q 2q1qb

a,ba

a,b

Input Stringb a b

46

Rejection

0q 2q1qb

a,ba

a,b

Input Stringb a b

47

Rejection

0q 2q1qb

a,ba

a,b

Input Stringb a b

Output: “reject”48

Formalities• Deterministic Finite Accepter (DFA)

( )0M Q, , ,q ,F= Σ δ

Q

Σ

δ

0q

F

: set of states

: input alphabet

: transition function

: initial state

: set of final states49

Input Alphabet Σ

{ }a,bΣ =

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

5qa a bb

a,b

a,b

50

Set of States Q

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

5qa a bb

a,b

a,b

{ }0 1 2 3 4 5Q q ,q ,q ,q ,q ,q=

51

Initial State 0q

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

5qa a bb

a,b

a,b

52

Set of Final States F

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

5qa a bb

a,b

a,b

{ }4F q=

53

Transition Function δ

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

5qa a bb

a,b

a,b

: Q Qδ ×Σ→

54

Transition Function δ

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

5qa a bb

a,b

a,b

( )0 1q ,a qδ =

55

Transition Function

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

5qa a bb

a,b

a,b

( )0 5q ,b qδ =

δ

56

Transition Function

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

5qa a bb

a,b

a,b

( )2 3q ,b qδ =

δ

57

Transition Function

δ a b0q1q2q3q4q

5q

1q 5q5q 2q2q 3q4q 5q

5q5q5q5q

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

5qa a bb

a,b

a,b

δ

58

Extended Transition Function ∗δ

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

5qa a bb

a,b

a,b

: Q Q∗ ∗δ ×Σ →

59

Extended Transition Function ∗δ

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

5qa a bb

a,b

a,b

0 2(q ,ab) q∗δ =

60

Extended Transition Function ∗δ

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

5qa a bb

a,b

a,b

0 4(q ,abba) q∗δ =

61

Extended Transition Function ∗δ

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

5qa a bb

a,b

a,b

0 5(q ,abbbaa) q∗δ =

62

Extended Transition Function ∗δ

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

5qa a bb

a,b

a,b

0 5(q ,abbbaa) q∗δ =

• Observation: there is a walk from to with label

0q 5qabbbaa.

63

Languages Accepted by DFAs• Take DFA

• Definition:

The language contains

all input strings accepted by

= { strings that drive to a final state}

M

( )L M

MM( )L M

64

Example

( ) { }L M abba=M

accept

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

5qa a bb

a,b

a,b

65

Example

( ) { }L M ,ab,abba= λM

accept

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

5qa a bb

a,b

a,b

acceptaccept66

Formally

• For a DFA

• Language accepted by :

( )0M Q, , ,q ,F= Σ δ

M

( ) ( ){ }0L M w : q ,w F∗ ∗= ∈Σ δ ∈

alphabet

transition function

initial state

final states

67

Observation• Language accepted by :

• Language rejected by :

( ) ( ){ }0L M w : q ,w F∗ ∗= ∈Σ δ ∈

M

( ) ( ){ }0L M w : q ,w F∗ ∗= ∈Σ δ ∉

M

68

More Examples

( ) nL M {a b : n 0}= ≥

accept trap state

0q 2q1qb

a,ba

a,b

69

( )L M = {all substrings with prefix }

More Examples

0q 2q1qa

a,b

a

a,b

3q

b

ab

b

70

( )L M = {all strings without substring }

More Examples

λ 0010

0

0,1

0

1

001

1

00

0

1

71

Regular Languages

• A language is regular if there is a DFA such that

• All regular languages form the family of regular languages.

L M

( )L L M .=

72

L {awa : w {a,b} }∗= ∈• The language

Example

0q 2q1qa

a

3q

bb

is regular.

a,b

ab

73

Exercises• For , construct DFAs that accept the

languages consisting of

a) all strings with exactly one 𝑎𝑎.

b) all strings with at least one 𝑎𝑎.

c) all strings with no more than three 𝑎𝑎’s.

d) all strings with at least one 𝑎𝑎 and exactly 2 𝑎𝑎’s.

e) all strings in which 𝑎𝑎𝑎𝑎 occurs at least twice.

{a,b}∑ =

74