lecture 5: finite automata

132
Lecture 5: Finite Automata 虞虞虞 虞虞虞虞虞虞虞 虞虞虞虞虞 虞虞虞

Upload: victor-moody

Post on 31-Dec-2015

54 views

Category:

Documents


7 download

DESCRIPTION

Lecture 5: Finite Automata. 虞台文. 大同大學資工所 智慧型多媒體研究室. Content. Alphabets and Languages Deterministic Finite Automata Nondeterministic Finite Automata Equivalence of DFA &NFA Properties of Languages Accepted by FA’s Finite Automata & Regular Expressions Languages Not Accepted by FA’s. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 5: Finite Automata

Lecture 5: Finite Automata

虞台文大同大學資工所智慧型多媒體研究室

Page 2: Lecture 5: Finite Automata

Content Alphabets and Languages Deterministic Finite Automata Nondeterministic Finite Automata Equivalence of DFA &NFA Properties of Languages Accepted by FA’s Finite Automata & Regular Expressions Languages Not Accepted by FA’s

Page 3: Lecture 5: Finite Automata

Lecture 5: Finite Automata

Alphabetsand

Languages

大同大學資工所智慧型多媒體研究室

Page 4: Lecture 5: Finite Automata

Alphabets

Alphabet a finite set of symbols Examples:

1 0,1 binary alphabet

3 , , ,a b z Roman alphabet

2 0,1, ,9 decimal alphabet

4 , , , Greek alphabet

5 , , , , , , , , a b c d x y z Z my alphabet

Page 5: Lecture 5: Finite Automata

Strings String over an alphabet () a finite

sequence of symbols from * the set of all strings over Examples:

1 0,1

3 , , ,a b z

2 0,1, ,9

4 , , ,

*1001100, 1000011110

*200799, 123789000

*3 ,taxi ixat

*4 ,

*

Page 6: Lecture 5: Finite Automata

More on Strings

String length # symbols in a string, e.g.,

A string, w, can be considered as a function , e.g.,

Some string operations:– Concatenation

– Reversal

– Duplication

00110| 0| 6 | | 0 :{1, ,| |}w w

" "w hello(1) ' ', (2) ' ', (3) (4) ' ', (5) ' '.w h w e w w l w o

" ", " "x Hello y World " "x y xy Hello World

" "w Hello " "Rw olleH

" "w Hello 3 " "w HelloHelloHello

Page 7: Lecture 5: Finite Automata

More on Strings

Substring Suffix Prefix

" "w HelloWorld

" "u World

" "t Hello

" "v oWo

a prefix of w and a substring of w.

a suffix of w and a substring of w.

a substring of w.

Page 8: Lecture 5: Finite Automata

Countability of *

* is countably infinite for any finite alphabet .

Example: {0,1}

0,1

00,01,10,11

000,001,010,011,100,101,110,111

* can be enumerated by:

Page 9: Lecture 5: Finite Automata

Languages

Language L over any subset of * i.e., L *

How many possible languages over ?

How to specify a language?

*

2

* : L w w P has property

uncountably infinite

Page 10: Lecture 5: Finite Automata

Operations of Languages

Complement

Union

Intersection

*{ : , }L w w w L

1 2L L L

1 2: or L w w L w L

1 2L L L

1 2: and L w w L w L

Page 11: Lecture 5: Finite Automata

Operations of Languages

Complement, Union and Intersection Concatenation

Closure or Kleene Star

Positive Closure

1 2 1 2L L L L L

1 2: ,L w xy x L y L

*1 2 1 2: 0, , ,k kL w w w w k w w w L

*1 2 1 2: 1, , ,k kL LL w w w w k w w w L

Page 12: Lecture 5: Finite Automata

Example: Kleene Star

01,1,100L

110001110011w

100011000010v

*L ?

*L ?

1909 – 1994

Page 13: Lecture 5: Finite Automata

Example: Kleene Star

01,1,100L

110001110011w

100011000010v

*L ?

*L ?

1909 – 1994

Page 14: Lecture 5: Finite Automata

Example: Kleene Star

01,1,100L

110001110011w *L ? *

1 2 1 2: 1, , ,k kL LL w w w w k w w w L

L ?

1909 – 1994

Page 15: Lecture 5: Finite Automata

Language Recognition Device

A machine or algorithm to answer the question

?w L

Page 16: Lecture 5: Finite Automata

Lecture 5: Finite Automata

Deterministic Finite Automata

大同大學資工所智慧型多媒體研究室

Page 17: Lecture 5: Finite Automata

Finite State Machine

Reading Head

Inputtape

q0q1

q3

q2

Finitecontrol

q5

q4

aa bb aa aa bb bb aa bb aa aa

Page 18: Lecture 5: Finite Automata

Definition Deterministic Finite Automaton

A deterministic finite automaton is a 5-tuple

(K, , , s, F)K : a finite set of states : a finite set of symbols

: the transition function K Ks K : the initial state

F K : the set of final states

Reading Head

I nputtape

q0q1

q3

q2

Finitecontrol

q5

q4

q0q1

q3

q2

Finitecontrol

q5

q4

aa bb aa aa bb bb aa bb aa aaaa bb aa aa bb bb aa bb aa aa

Page 19: Lecture 5: Finite Automata

Example

K={q0, q1, q2, q3} ={0, 1}

q0

q1

q2

q3

0 1q2 q1

q3 q0

q0 q3

q1 q2

symbol

state

s=q0 F= {q0}q0

q2

q1

q3

1

1

1

1

0

0

0

0

>

(K, , , s, F)

Page 20: Lecture 5: Finite Automata

Operations of a DFA

q0

q2

q1

q3

1

1

1

1

0

0

0

0

> 0 1 1 0 0 1 0 1

q0 q1

q3 q2

Finitecontrol

Reading Head

Inputtape

Page 21: Lecture 5: Finite Automata

Operations of a DFA

q0

q2

q1

q3

1

1

1

1

0

0

0

0

> 0 1 1 0 0 1 0 1

q0 q1

q3 q2

Finitecontrol

Reading Head

Inputtape

Strings are fed into the device by means of an input tape.

Strings are fed into the device by means of an input tape.

Main part of the machine• Initially,

• putting reading head at the leftmost• in state s = q0

• Sense input symbol• Change state based on • Move head right one cell• Give answer when head reaches end

Main part of the machine• Initially,

• putting reading head at the leftmost• in state s = q0

• Sense input symbol• Change state based on • Move head right one cell• Give answer when head reaches end

Page 22: Lecture 5: Finite Automata

DFA As a Language Acceptor

q0

q2

q1

q3

1

1

1

1

0

0

0

0

> 0 1 1 0 0 1 0 1

q0 q1

q3 q2

Finitecontrol

Reading Head

Inputtape

The input string is accepted by the machine if the reading head ends at a final state.

Otherwise, it is rejected.

Accepted

Rejected JFLAP

Page 23: Lecture 5: Finite Automata

DFA As a Language Acceptor

q0

q2

q1

q3

1

1

1

1

0

0

0

0

> 0 1 1 0 0 1 0 1

q0 q1

q3 q2

Finitecontrol

Reading Head

Inputtape

The input string is accepted by the machine if the reading head ends at a final state.

Otherwise, it is rejected.

What language the

machine accepts?What language the

machine accepts?

Page 24: Lecture 5: Finite Automata

DFA As a Language Acceptor

q0

q2

q1

q3

1

1

1

1

0

0

0

0

> 0 1 1 0 0 1 0 1

q0 q1

q3 q2

Finitecontrol

Reading Head

Inputtape

The input string is accepted by the machine if the reading head ends at a final state.

Otherwise, it is rejected.

What language the

machine accepts?What language the

machine accepts?

q3

Page 25: Lecture 5: Finite Automata

Memory Configuration of a DFA

( , )q uaw

M = (K, , , s, F)

K *

*

Reading Head

Inputtape

q0q1

q3

q2

Finitecontrol

q5

q4

aa bb aa aa bb bb aa bb aa aa

u w

Page 26: Lecture 5: Finite Automata

Memory Configuration of a DFA

( , )q uaw

M = (K, , , s, F)

K *

*

Reading Head

Inputtape

q0q1

q3

q2

Finitecontrol

q5

q4

aa bb aa aa bb bb aa bb aa aa

u wDoes not effect the result.

Does not effect the result.

Page 27: Lecture 5: Finite Automata

Memory Configuration of a DFA

( , )q uaw

M = (K, , , s, F)

( , )q aw

Page 28: Lecture 5: Finite Automata

Memory Configuration of a DFA

( , )q uaw

M = (K, , , s, F)

( , )q aw

Page 29: Lecture 5: Finite Automata

Memory Configuration of a DFA

Reading Head

Inputtape

q0q1

q3

q2

Finitecontrol

q5

q4

aa bb aa aa bb bb aa bb aa aa

u w

2( , )q abbabaa

Page 30: Lecture 5: Finite Automata

Yields in One Step M M

( , )q aw M M ( , )q w

if ( , )q a q

M M

Page 31: Lecture 5: Finite Automata

Yields

( , )q w ( , )q w

if M M

*

M M*

M M*

( , )q w M M M M ( , )q w

in some (including zero) steps

Page 32: Lecture 5: Finite Automata

String Acceptance by a DFA

M = (K, , , s, F)

w* is said to be accepted by M iff

( , )s w ( , )q M M* M M

*

such that q F

Page 33: Lecture 5: Finite Automata

Language Defined by a DFA

M = (K, , , s, F)

*( ) : , ( , ) ( , ),L M w w s w q q F M M*

M M*

Language L(M) denotes the set of all

strings accepted by M, i.e.,

Page 34: Lecture 5: Finite Automata

Example

q0

q2

q1

q3

1

1

1

1

0

0

0

0

>

w=0100100110

(q0, 0100100110)

M M(q2, 100100110)

M M(q3, 00100110)

M M(q1, 0100110)

M M(q3, 100110)

M M(q2, 00110)

M M(q0, 0110)

M M(q2, 110)

M M (q3, 10)

M M (q2, 0)

M M(q0, )

M

wL(M)?

0q F

Page 35: Lecture 5: Finite Automata

Example

q0

q2

q1

q3

1

1

1

1

0

0

0

0

>

w=0100100110

(q0, 0100100110)

M M(q2, 100100110)

M M(q3, 00100110)

M M(q1, 0100110)

M M(q3, 100110)

M M(q2, 00110)

M M(q0, 0110)

M M(q2, 110)

M M (q3, 10)

M M (q2, 0)

M M(q0, )

M

wL(M)?

0q F

( , )s w 0( , )q M M*

M M*

Page 36: Lecture 5: Finite Automata

Example

q0

q2

q1

q3

1

1

1

1

0

0

0

0

>

M *{0,1} and has even( )

numbers of 0's and 1's.

w wL M w

Page 37: Lecture 5: Finite Automata

Example

q0 q1 q2 q3> b b b

ba a

a

a

*{ , } and does not( )

contain three consecutive 's.

w a b wL M w

b

( ) ?L M

Page 38: Lecture 5: Finite Automata

Exercises

Describe informally the languages accepted by the deterministic finite automata shown below:

a

ba

a, b

a, b

b

> >

a

b

ab

a, b

a, b

a, b

(a) (b)

Page 39: Lecture 5: Finite Automata

Exercises

Construct deterministic finite automata accepting each of the following languages

a) {w{a, b}*: each a in w is immediately preceded and immediately followed by a b}.

b) {w{a, b}*: w has abab as a substring}.c) {w{a, b}*: w has neither aa nor bb as a substrin

g}.d) {w{a, b}*: w has both ab and ba as a substring}.

Page 40: Lecture 5: Finite Automata

Lecture 5: Finite Automata

Nondeterministic

Finite Automata

大同大學資工所智慧型多媒體研究室

Page 41: Lecture 5: Finite Automata

Review Deterministic Finite Automaton

A deterministic finite automaton is a 5-tuple

(K, , , s, F)K : a finite set of states : a finite set of symbols

: the transition function K Ks K : the initial state

F K : the set of final states

Page 42: Lecture 5: Finite Automata

: the transition function K K2K : the transition function K 2K

A deterministic finite automaton is a 5-tuple

(K, , , s, F)

non,

A nondeterministic finite automaton is a 5-tuple

(K, , , s, F)

Definition Nondeterministic Finite Automaton

K : a finite set of states : a finite set of symbols

s K : the initial state

F K : the set of final states

Page 43: Lecture 5: Finite Automata

Transition Functions of FA’s

: K 2K: K K

p

a

qK

p

a

Q K

Page 44: Lecture 5: Finite Automata

Transition Functions of FA’s

: K 2K: K K

p

a

qK

p

a

Q K

Change to state q

deterministically.

Change to state q

deterministically.

Change to one of state in Q nondeterministically.

Change to one of state in Q nondeterministically.

Page 45: Lecture 5: Finite Automata

Yields in One Step M M

( , )q aw M M ( , )q w

if ( , )q q a

M M

M = (K, , , s, F)

Page 46: Lecture 5: Finite Automata

Yields

( , )q w ( , )q w

M M*

M M*

M M*

in some (including zero) steps

if is possible.( , )q w M M M M ( , )q w ( , )q w M M M M ( , )q w

M = (K, , , s, F)

Page 47: Lecture 5: Finite Automata

String Acceptance by a NFA

M = (K, , , s, F)

w* is said to be accepted by M iff

( , )s w ( , )q M M* M M

*

such that q F

Page 48: Lecture 5: Finite Automata

Language Defined by an NFA

M = (K, , , s, F)

*( ) : , ( , ) ( , ),L M w w s w q q F M M*

M M*

Language L(M) denotes the set of all

strings accepted by M, i.e.,

Page 49: Lecture 5: Finite Automata

Example: L=(ab+aba)*

a

q0 q1 q2 q3a b a

b>

DFANFA

q4

abb

a,b

q1

a

q0>b

q2

ba

Page 50: Lecture 5: Finite Automata

Extension: -Move & Multi-Move

NFA

q1

a

q0>b

q2

ba

q1aq0>

ba

q2

L=(ab+aba)*

Page 51: Lecture 5: Finite Automata

Extension: -Move & Multi-Move

NFA

q1

a

q0>b

q2

ba

q1aq0>

b

q2

L=(ab+aba)*

a,

q1

ab

q0>a,

> q0

ab

aba

Page 52: Lecture 5: Finite Automata

: the transition function K* 2K

A nondeterministic finite automaton is a 5-tuple

(K, , , s, F)

Definition Nondeterministic Finite Automaton

K : a finite set of states : a finite set of symbols

s K : the initial state

F K : the set of final states

Page 53: Lecture 5: Finite Automata

Yields in One Step M M

( , )q w M M ( , )q w

* , ( , )u w uw q q u iff st. and

M M

M = (K, , , s, F)

Page 54: Lecture 5: Finite Automata

Yields

( , )q w ( , )q w

M M*

M M*

M M*

in some (including zero) steps

if is possible.( , )q w M M M M ( , )q w ( , )q w M M M M ( , )q w

M = (K, , , s, F)

Page 55: Lecture 5: Finite Automata

String Acceptance by a NFA

M = (K, , , s, F)

w* is said to be accepted by M iff

( , )s w ( , )q M M* M M

*

such that q F

Page 56: Lecture 5: Finite Automata

Language Defined by an NFA

M = (K, , , s, F)

*( ) : , ( , ) ( , ),L M w w s w q q F M M*

M M*

Language L(M) denotes the set of all

strings accepted by M, i.e.,

Page 57: Lecture 5: Finite Automata

Lecture 5: Finite Automata

Equivalence of

DFA &NFA

大同大學資工所智慧型多媒體研究室

Page 58: Lecture 5: Finite Automata

Equivalence of Finite Automata

Finite automata M1 and M2 are said to be equivalent iff

1 2( ) ( )L M L M

Page 59: Lecture 5: Finite Automata

Machine Simulation

NFA DFA

?

?

Of CourseOf Course

To be discussedTo be discussed

Page 60: Lecture 5: Finite Automata

Lemma

For each NFA, there is an equivalent NFA

finite automaton without multi-move on

the transition function.

q q’1 k

q p11 p2 pk1 q’2 k

Page 61: Lecture 5: Finite Automata

Example

q1

bab

q0>

a

q2ba

a

b

q1

b

q0>

a

q2b

a

b

p1 p2

p3a

a

b

In the following, we will assume the NFA being discussed is one without multi-move unless otherwise stated.

Page 62: Lecture 5: Finite Automata

-Closure

M = (K, , , s, F)

In the following, we will assume the NFA being discussed is one without multi-move unless otherwise stated.

( ) : ( , ) ( , )q p K q w p w M M* -Closure of state q

without consumeany input

Page 63: Lecture 5: Finite Automata

Example

q0

q1

q2

q3

q4

a

b

a

b

a

>

M ( ) ?L M * *( )a a b bb a

0 ?( )q

1 ?( )q

2 ?( )q

3 ?( )q

4 ?( )q

0 1 2 3, , ,q q q q

1 2 3, ,q q q

2q

3q

3 4,q q

To be used in the next example.

Page 64: Lecture 5: Finite Automata

Theorem

For each nondeterministic finite

automaton, there is an equivalent

deterministic finite automaton.

Page 65: Lecture 5: Finite Automata

Theorem

For each nondeterministic finite

automaton, there is an equivalent

deterministic finite automaton.

Pf) Let M = (K, , , s, F) be an NFA.

To prove the theorem, we must be able to build a DFA M’ = (K’, ’, , s’, F’) such that L(M)=L(M’).

?K ?

?s ?F

?

Page 66: Lecture 5: Finite Automata

Theorem

For each nondeterministic finite

automaton, there is an equivalent

deterministic finite automaton.

Pf) Let M = (K, , , s, F) be an NFA.

To prove the theorem, we must be able to build a DFA M’ = (K’, ’, , s’, F’) such that L(M)=L(M’).

?K ?

?s ?F

? 2K

( )s 2 :KQ Q F

Page 67: Lecture 5: Finite Automata

?

Theorem

For each nondeterministic finite

automaton, there is an equivalent

deterministic finite automaton.

Pf)

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

Next, we have to show that ( ) ( ).L M L M

Page 68: Lecture 5: Finite Automata

?

Theorem

For each nondeterministic finite

automaton, there is an equivalent

deterministic finite automaton.

Pf)

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

Next, we have to show that ( ) ( ).L M L M

Exercises:

1.Referring to the textbook to complete the proof in detail.

2.To present the proof using PowerPoint.Exercises:

1.Referring to the textbook to complete the proof in detail.

2.To present the proof using PowerPoint.

Page 69: Lecture 5: Finite Automata

Example ?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

q0

q1

q2

q3

q4

a

b

a

b

a

>

M ( ) ?L M * *( )a a b bb a

0 ?( )q

1 ?( )q

2 ?( )q

3 ?( )q

4 ?( )q

0 1 2 3, , ,q q q q

1 2 3, ,q q q

2q

3q

3 4,q q

?M

Page 70: Lecture 5: Finite Automata

Example ?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

q0

q1

q2

q3

q4

a

b

a

b

a

>

M 0 0 1 2 3( ) , , ,q q q q q 1 1 2 3( ) , ,q q q q

2 2( )q q 3 3( )q q 4 3 4( ) ,q q q

?K ?

?s ?F

?

Page 71: Lecture 5: Finite Automata

Example ?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

q0

q1

q2

q3

q4

a

b

a

b

a

>

M 0 0 1 2 3( ) , , ,q q q q q 1 1 2 3( ) , ,q q q q

2 2( )q q 3 3( )q q 4 3 4( ) ,q q q

?K ?

?s ?F

? ,a b0 4{ , , }2 q q

0 1 2 3, , ,q q q qThere are 32 possible subsets. However, most of them are useless.

There are 32 possible subsets. However, most of them are useless.

Page 72: Lecture 5: Finite Automata

Example ?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

q0

q1

q2

q3

q4

a

b

a

b

a

>

M 0 0 1 2 3( ) , , ,q q q q q 1 1 2 3( ) , ,q q q q

2 2( )q q 3 3( )q q 4 3 4( ) ,q q q

?K ?

?s ?F

? ,a b0 4{ , , }2 q q

0 1 2 3, , ,q q q q 4: :Q K q Q

Page 73: Lecture 5: Finite Automata

Example ?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

q0

q1

q2

q3

q4

a

b

a

b

a

>

M 0 0 1 2 3( ) , , ,q q q q q 1 1 2 3( ) , ,q q q q

2 2( )q q 3 3( )q q 4 3 4( ) ,q q q

?s 0 1 2 3, , ,q q q q

0 1 2 3, , ,q q q q>M’

Page 74: Lecture 5: Finite Automata

Example ?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

q0

q1

q2

q3

q4

a

b

a

b

a

>

M 0 0 1 2 3( ) , , ,q q q q q 1 1 2 3( ) , ,q q q q

2 2( )q q 3 3( )q q 4 3 4( ) ,q q q

0 1 2 3, , ,q q q q> 0 4( ) ( )q q a

b

2 4( ) ( )q q

M’

Page 75: Lecture 5: Finite Automata

Example ?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

?

2KK ( )s s 2 :KF Q Q F

( , )( , ) ( ) ,

q Q p qQ p Q K

( , , , , ) ( , , , , )M K s F M K s F

q0

q1

q2

q3

q4

a

b

a

b

a

>

M 0 0 1 2 3( ) , , ,q q q q q 1 1 2 3( ) , ,q q q q

2 2( )q q 3 3( )q q 4 3 4( ) ,q q q

0 1 2 3, , ,q q q q> 0 4( ) ( )q q a

b

2 4( ) ( )q q

0 1 2 3 4, , , ,q q q q q

2 3 4, ,q q q

b

a

3 4,q qa, b

a

b

a, b

M’

Page 76: Lecture 5: Finite Automata

Example

q0

q1

q2

q3

q4

a

b

a

b

a

>

M

0 1 2 3, , ,q q q q> 0 4( ) ( )q q a

b

2 4( ) ( )q q

0 1 2 3 4, , , ,q q q q q

2 3 4, ,q q q

b

a

3 4,q qa, b

a

b

a, b

M’

* *( ) ( )L M a a b bb a ( )L M

JFLAP

Page 77: Lecture 5: Finite Automata

Exercises Construct deterministic finite automata equivalent

to the nondeterministic automata shown below:

a, b

b

ab

b

a

a

>q0

q1

q2 q3

q4

>

bba

abaq0

(a) (b)

Page 78: Lecture 5: Finite Automata

Lecture 5: Finite Automata

Properties of

Languages Accepted by

Finite Automata

大同大學資工所智慧型多媒體研究室

Page 79: Lecture 5: Finite Automata

The Chomsky Hierarchy

Chomsky Hierarchy

Languages Grammars Automaton

Type 0 Recursively enumerable

unrestricted Turing Machine

Recursive unrestricted Decider

Type 1 Context-Sensitive Context-Sensitive

Linear-Bounded Automaton

Type 2 Context-Free Context-Free Push-Down Automaton

Type 3 Regular Regular NFA or DFA

Page 80: Lecture 5: Finite Automata

The Chomsky Hierarchy

Non-recursively enumerable

Recursively-enumerable

Recursive

Context-sensitive

Context-free

Regular

Page 81: Lecture 5: Finite Automata

Properties of Languages Accepted by FA’s

The class of languages accepted by finite automata

is

closed under:

a) union;

b) concatenation;

c) Keene star;

d) complementation;

e) intersection.

1L 2L

1L

2L1 2L L

1 2L L*1L

*2L

1 2L L

2 1L L

Page 82: Lecture 5: Finite Automata

Properties of Languages Accepted by FA’s

The class of languages accepted by finite automata

is

closed under:

a) union;

b) concatenation;

c) Keene star;

d) complementation;

e) intersection.

>M1 >M2

>

accepts

1 2( ) ( )L M L M

Page 83: Lecture 5: Finite Automata

Properties of Languages Accepted by FA’s

The class of languages accepted by finite automata

is

closed under:

a) union;

b) concatenation;

c) Keene star;

d) complementation;

e) intersection.

>M2

accepts 1 2( ) ( )L M L M

>M1

>

Page 84: Lecture 5: Finite Automata

Properties of Languages Accepted by FA’s

The class of languages accepted by finite automata

is

closed under:

a) union;

b) concatenation;

c) Keene star;

d) complementation;

e) intersection.

>M2

accepts *1( )L M

>M1

>

Page 85: Lecture 5: Finite Automata

Properties of Languages Accepted by FA’s

The class of languages accepted by finite automata

is

closed under:

a) union;

b) concatenation;

c) Keene star;

d) complementation;

e) intersection.

( , , , , )

( , , , , )

M K s F

M K s K F

( )M L M accepts

Page 86: Lecture 5: Finite Automata

Properties of Languages Accepted by FA’s

The class of languages accepted by finite automata

is

closed under:

a) union;

b) concatenation;

c) Keene star;

d) complementation;

e) intersection.

1 2L L 1 2L L

1 2L L

Page 87: Lecture 5: Finite Automata

Theorem

There are algorithms to answer the following

questions about finite automata:

a) w L(M)?

b) L(M) = ?

c) L(M) = *?

d) L(M1) L(M2) ?

e) L(M1) = L(M2) ?

Page 88: Lecture 5: Finite Automata

Theorem

There are algorithms to answer the following

questions about finite automata:

a) w L(M)?

b) L(M) = ?

c) L(M) = *?

d) L(M1) L(M2) ?

e) L(M1) = L(M2) ?

Feed w into M.

Check any final state is reachable Check L(M) = ?

Check L(M1) L(M2) = ? Check L(M1) L(M2) ? L(M2) L(M1) ?

Page 89: Lecture 5: Finite Automata

Exercises

Draw state diagrams for nondeterministic finite automata accepting these languages:

a) (ab)*(ba)*aa*

b) ((abaab)*a*)*

c) ((a*b*a*)*b)*

d) (bab)*(bba)*

Page 90: Lecture 5: Finite Automata

Lecture 5: Finite Automata

Finite Automata&

Regular Expressions

大同大學資工所智慧型多媒體研究室

Page 91: Lecture 5: Finite Automata

The Languages Accepted by FA’s

The languages accepted by FA’s are called regular sets.

How to describe a regular set?– Regular Expression

Page 92: Lecture 5: Finite Automata

Definition Regular Expressions

A regular expression is defined inductively over the alphabet { (, ), , , +, *} as follows:

, , and each is a regular expression If and are regular expressions, then

– ( + )

– ( )

– *

are regular expressions

basic

inductive

Page 93: Lecture 5: Finite Automata

Definition Regular Expressions

A regular expression is defined inductively over the alphabet { (, ), , , +, *} as follows:

, , and each is a regular expression If and are regular expressions, then

– ( + )

– ( )

– *

are regular expressions

basic

inductive

union, or concatenation

Kleene closure

Page 94: Lecture 5: Finite Automata

Writing a Regular Expression

For omitting parentheses, the precedence of ‘operators’ are assigned as follows:

* > • > +((0(1*))+0) 01*+0

What is this language?

What is this language?

Page 95: Lecture 5: Finite Automata

Examples

1. 00

2. (0+1)*

3. (0+1)* 00 (0+1)*

4. (0+) (1+10)*

5. (0+1)*011

6. 0*1*2*

7. 00*11*22*

What do the following regular expressions represent?

Page 96: Lecture 5: Finite Automata

More ExamplesIs the following language L an r.e. over =[a-z]?

L={what, which, when, where}.

Write L as an r.e.

L = what + which + when + where

L = (wh)(at + ich + en + ere)

L = (wh)(at + ich) + (whe) (n + re)

Page 97: Lecture 5: Finite Automata

Theorem

A language is regular if and only if it is accepted by a finite automaton.

“”

“”

Pf)

Given a regular expression, how to construct an FA to accept the language described by it?

Given an FA, what is its corresponding regular expression?

Page 98: Lecture 5: Finite Automata

Theorem

A language is regular if and only if it is accepted by a finite automaton.

“”Pf) Suppose that we are given a regular expression r, we can construct an NFA to accept the language described it inductively based on the number of `operators' it possesses as follows.

Basis (zero operators)Case 1: r =

Case 2: r =

Case 3: r = a

q0>

qfq0>

qfq0a>

Page 99: Lecture 5: Finite Automata

Theorem

A language is regular if and only if it is accepted by a finite automaton.

“”Pf) Suppose that we are given a regular expression r, we can construct an NFA to accept the language described it inductively based on the number of `operators' it possesses as follows.

Basis (zero operators)

trueHypothesis

Assumed true for r.e.’s few than m operatorsInduction

To be shown true for r.e.’s with m+1 operators

Case 1: r = r1+r2

Case 2: r = r1r2

Case 3: r = r1*

r : m+1 operatorsr1: m operatorsr2: m operators

Page 100: Lecture 5: Finite Automata

Theorem

A language is regular if and only if it is accepted by a finite automaton.

“”Pf) Suppose that we are given a regular expression r, we can construct an NFA to accept the language described it inductively based on the number of `operators' it possesses as follows.

Basis (zero operators)

trueHypothesis

Assumed true for r.e.’s few than m operatorsInduction

To be shown true for r.e.’s with m+1 operators

Case 1: r = r1+r2

Case 2: r = r1r2

Case 3: r = r1*

r : m+1 operatorsr1: m operatorsr2: m operators

The class of languages accepted by fi nite automata is

closed under:

a) union;

b) concatenation;

c) Keene star;

d) complementation;

e) intersection.

1L 2L

1L

2L1 2L L

1 2L L*1L

*2L

1 2L L

2 1L L

The class of languages accepted by fi nite automata is

closed under:

a) union;

b) concatenation;

c) Keene star;

d) complementation;

e) intersection.

1L 2L

1L

2L1 2L L

1 2L L*1L

*2L

1 2L L

2 1L L

Page 101: Lecture 5: Finite Automata

Theorem

A language is regular if and only if it is accepted by a finite automaton.

“”Pf) Suppose that we are given a DFA, we will determine its corresponding regular expression also inductively.

Consider DFA M = ({q1, q2,…,qn}, , , q1, F).

qi qj

ql

x

y

l k

1 1 1 1( )*k k k k kij ik kk kj ijR R R R R

0 { ( , ) }

{ ( , ) } { }i j

iji i

a | δ q a q i jR

a | δ q a q ε i j

*( , ) ( , ) ( , )

i l jkij

q x δ q y qR x

l k y x y

satisfi es if and

M M*

M M* M M

* M M

*

Define

Page 102: Lecture 5: Finite Automata

qi qj

ql

x

y

l k

Theorem

A language is regular if and only if it is accepted by a finite automaton.

“”Pf) Suppose that we are given a DFA, we will determine its corresponding regular expression also inductively.

Consider DFA M = ({q1, q2,…,qn}, , , q1, F).

1 1 1 1( )*k k k k kij ik kk kj ijR R R R R

0 { ( , ) }

{ ( , ) } { }i j

iji i

a | δ q a q i jR

a | δ q a q ε i j

*( , ) ( , ) ( , )

i l jkij

q x δ q y qR x

l k y x y

satisfi es if and

M M*

M M* M M

* M M

*

Define

kijR is regular for

any i, j, and k.

Page 103: Lecture 5: Finite Automata

Theorem

A language is regular if and only if it is accepted by a finite automaton.

“”Pf) Suppose that we are given a DFA, we will determine its corresponding regular expression also inductively.

Consider DFA M = ({q1, q2,…,qn}, , , q1, F).

1 1 1 1( )*k k k k kij ik kk kj ijR R R R R

0 { ( , ) }

{ ( , ) } { }i j

iji i

a | δ q a q i jR

a | δ q a q ε i j

*( , ) ( , ) ( , )

i l jkij

q x δ q y qR x

l k y x y

satisfi es if and

M M*

M M* M M

* M M

*

Define

kijR is regular for

any i, j, and k.1( ) n

ff FL M R

( )L M is reguar.

Page 104: Lecture 5: Finite Automata

Equivalence FA & RE

FA RE

Page 105: Lecture 5: Finite Automata

The Chomsky Hierarchy

Chomsky Hierarchy

Languages Grammars Automaton

Type 0Recursively enumerable

unrestricted Turing Machine

Recursive unrestricted Decider

Type 1 Context-SensitiveContext-Sensitive

Linear-Bounded Automaton

Type 2 Context-Free Context-Free Push-Down Automaton

Type 3 Regular Regular NFA or DFA

Page 106: Lecture 5: Finite Automata

FA

The Chomsky Hierarchy

Non-recursively enumerable

Recursively-enumerable

Recursive

Context-sensitive

Context-free

Regular

Page 107: Lecture 5: Finite Automata

Example

Find an r.e. to represent the following FA.

M: q1 q2 q3

1

10

0 0,1>

3 312 13( )L M R R 3 3

12 13r r

Page 108: Lecture 5: Finite Automata

ExampleM: q1 q2 q3

1

10

0 0,1> q1 q2 q3

1

10

0 0,1q1 q2 q3

1

10

0 0,1>

3 312 13( )L M R R 3 3

12 13r r

11kr

12kr

13kr

21kr

22kr

23kr

31kr

32kr

33kr

0

1

0

1

0+1

k = 0 k = 11 0 0 0 0

11 11 11 11 11( )*r r r r r 1 0 0 0 0

12 11 11 12 12( )* 0r r r r r 1 0 0 0 0

13 11 11 13 13( )* 1r r r r r 1 0 0 0 021 21 11 11 21( )*r r r r r 01 0 0 0 022 21 11 12 22( )* 00r r r r r 1 0 0 0 023 21 11 13 23( )* 1 01r r r r r 1 0 0 0 031 31 11 11 31( )*r r r r r 1 0 0 0 032 31 11 12 32( )* 0 1r r r r r 1 0 0 0 033 31 11 13 33( )*r r r r r

1 1 1 1( )*k k k k kij ik kk kj ijR R R R R

0 { ( , ) }

{ ( , ) } { }i j

iji i

a | δ q a q i jR

a | δ q a q ε i j

0( , )l i l jl

ij

a δ q a qr

otherwise

1 1 1 1( )*k k k k kij ik kk kj ijr r r r r

Page 109: Lecture 5: Finite Automata

ExampleM: q1 q2 q3

1

10

0 0,1> q1 q2 q3

1

10

0 0,1q1 q2 q3

1

10

0 0,1>

3 312 13( )L M R R 3 3

12 13r r

11kr

12kr

13kr

21kr

22kr

23kr

31kr

32kr

33kr

0

1

0

1

0+1

k = 0 k = 10

1

0

+00

1+01

0+1

2 1 1 1 111 12 22 21 11( )* 0( 00)*0r r r r r 2 1 1 1 1

12 12 22 22 12( )* 0( 00)*( 00) 0r r r r r 2 1 1 1 1

13 12 22 23 13( )* 0( 00)*(1 01) 1r r r r r 2 1 1 1 1

21 22 22 21 21( )* ( 00)( 00)*0 0r r r r r 2 1 1 1 1

22 22 22 22 22( )* ( 00)( 00)*( 00) ( 00)r r r r r 2 1 1 1 1

23 22 22 23 23( )* ( 00)( 00)*(1 01) (1 01)r r r r r 2 1 1 1 1

31 32 22 21 31( )* (0 1)( 00)*0r r r r r 2 1 1 1 1

32 32 22 22 32( )* (0 1)( 00)*( 00) (0 1)r r r r r 2 1 1 1 1

33 32 22 23 33( )* (0 1)( 00)*(1 01)r r r r r

k = 2(00)*

0(00)*

0*1

0(00)*

(00)*

0*1

(0+1)(00)*0

(0+1)(00)*

+(0+1)0*1

Page 110: Lecture 5: Finite Automata

ExampleM: q1 q2 q3

1

10

0 0,1> q1 q2 q3

1

10

0 0,1q1 q2 q3

1

10

0 0,1>

3 312 13( )L M R R 3 3

12 13r r

11kr

12kr

13kr

21kr

22kr

23kr

31kr

32kr

33kr

0

1

0

1

0+1

k = 0 k = 10

1

0

+00

1+01

0+1

k = 2(00)*

0(00)*

0*1

0(00)*

(00)*

0*1

(0+1)(00)*0

(0+1)(00)*

+(0+1)0*1

3 2 2 2 212 13 33 32 12( )*

0*1( (0 1)0*1)*(0 1)(00)* 0(00)*

0*1((0 1)0*1)*(0 1)(00)* 0(00)*

r r r r r

3 2 2 2 213 13 33 33 13( )*

0*1( (0 1)0*1)*( (0 1)0*1) 0*1

0*1((0 1)0*1)*

r r r r r

Page 111: Lecture 5: Finite Automata

11kr

12kr

13kr

21kr

22kr

23kr

31kr

32kr

33kr

0

1

0

1

0+1

k = 0 k = 10

1

0

+00

1+01

0+1

k = 2(00)*

0(00)*

0*1

0(00)*

(00)*

0*1

(0+1)(00)*0

(0+1)(00)*

+(0+1)0*1

ExampleM: q1 q2 q3

1

10

0 0,1> q1 q2 q3

1

10

0 0,1q1 q2 q3

1

10

0 0,1>

3 312 13( )L M R R 3 3

12 13r r

3 2 2 2 212 13 33 32 12( )*

0*1( (0 1)0*1)*(0 1)(00)* 0(00)*

0*1((0 1)0*1)*(0 1)(00)* 0(00)*

r r r r r

3 2 2 2 213 13 33 33 13( )*

0*1( (0 1)0*1)*( (0 1)0*1) 0*1

0*1((0 1)0*1)*

r r r r r

3 312 13 0*1((0 1)0*1)*( (0 1)(00)*) 0(00)*r r 3 3

12 13 0*1((0 1)0*1)*( (0 1)(00)*) 0(00)*r r

Page 112: Lecture 5: Finite Automata

Lecture 5: Finite Automata

Languages Not Accepted by

Finite Automata

大同大學資工所智慧型多媒體研究室

Page 113: Lecture 5: Finite Automata

What FA’s can? What FA’s cannot?

1 { | {0, ,9}* 2 | ( ) 3 | ( )}L w w val w val w st. and

2

{ , }* | |w a b wL w

w a

st. is and

contains number of

odd

even 's

3 0n nL a b n

4 nL a n is prime

Which of the following languages are regular?

Page 114: Lecture 5: Finite Automata

The Pumping Theory

Let L be an infinite regular set. Then there are strings u,v, and w st. |v|>0 and uvnwL for all n 0.

Pf)Let L is accepted by a DFA M = (K, , , q0 , F) with m states.

Consider input w = 1 2… l, l m.

1 20( , , , , )lq M M 21( , , , )lq M M M M 1( , )llq M M ( , )lq . . .

Then,

By pigeonhole, there exists j, k with 0 j < k l st. qj = qk.

qj = qk qmu=1… j

v= j+1… k

w= k+1… mq0>

Page 115: Lecture 5: Finite Automata

The Pumping Theory

Let L be an infinite regular set. Then there are strings u,v, and w st. |v|>0 and uvnwL for all n 0.

Pf)Let L is accepted by a DFA M = (K, , , q0 , F) with m states.

Consider input w = 1 2… l, l m.

1 20( , , , , )lq M M 21( , , , )lq M M M M 1( , )llq M M ( , )lq . . .

Then,

By pigeonhole, there exists j, k with 0 j < k l st. qj = qk.

v= j+1… k

qj = qk qmu=1… j w= k+1… mq0>

It is then seen that if qmF, then

uw L, uvw L, uv2w L, . . ., uvnwL for all n 0.

Page 116: Lecture 5: Finite Automata

ExampleShow that the language L={anbn | n0} is not regular.

Pf) Suppose that L is regular.By pumping theorem,

u v wCase 1: z = a a … a b b … b

uvmwL for all m 0

z = anbn =uvwL st. uvmwL for all m 0.

u v wCase 2: z = a a … a b b … b

u v wCase 3: z = a a … a b b … b

Page 117: Lecture 5: Finite Automata

The Chomsky Hierarchy

Non-recursively enumerable

Recursively-enumerable

Recursive

Context-sensitive

Context-free

Regular

L={anbn | n0}L={anbn | n0}

Page 118: Lecture 5: Finite Automata

Lecture 5: Finite Automata

Implementation of DFA

大同大學資工所智慧型多媒體研究室

Page 119: Lecture 5: Finite Automata

Example Input a 0/1 sting. If the numbers of 0 and 1 in the

string are both even, then it is legal; otherwise, it is illegal.– 0011001001(legal)– 11001001001(illegal)

Writing a C program to do so.

Page 120: Lecture 5: Finite Automata

q0 q1

q2 q3

0

0

0

0

1 1 1 1

Examples:

00100011<CR>

01101011<CR>

001010101<CR>

0010010101<CR>

Finite State Machine

Page 121: Lecture 5: Finite Automata

Finite State Machine

q0 q1

q2 q3

0

0

0

0

1 1 1 1

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>q1

q0

q3

q2

q2

q3

q0

q1

ok

err

err

err

The ParserThe Parser

Page 122: Lecture 5: Finite Automata

Implementation (I)

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>q1

q0

q3

q2

q2

q3

q0

q1

ok

err

err

err

The ParserThe Parser

#define q0 0#define q1 1#define q2 2#define q3 3#define fini4

#define q0 0#define q1 1#define q2 2#define q3 3#define fini4

Page 123: Lecture 5: Finite Automata

Implementation (I)

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>q1

q0

q3

q2

q2

q3

q0

q1

ok

err

err

err

The ParserThe Parser

int parser[4][3]={q1, q2, fini,q0, q3, fini,q3, q0, fini,q2, q1, fini

};

int state=q0;int event;char* str;

int parser[4][3]={q1, q2, fini,q0, q3, fini,q3, q0, fini,q2, q1, fini

};

int state=q0;int event;char* str;

Page 124: Lecture 5: Finite Automata

Implementation (I)

void ToEvent(char c){

if(c == ’0’) event = 0;else if(c == ’1’) event = 1;else event = 2;

}

void ToEvent(char c){

if(c == ’0’) event = 0;else if(c == ’1’) event = 1;else event = 2;

}

Event (or Message) EncodingEvent (or Message) Encoding

Page 125: Lecture 5: Finite Automata

Implementation (I)

void main(){

// Ask user to input a 0/1 string// Store the string into strstate = q0; //initializationwhile(state!=fini){

ToEvent(*str++);EventHandler(event);

}}

void main(){

// Ask user to input a 0/1 string// Store the string into strstate = q0; //initializationwhile(state!=fini){

ToEvent(*str++);EventHandler(event);

}} Event (or Message) LoopEvent (or Message) Loop

Page 126: Lecture 5: Finite Automata

Implementation (I)

void EventHandler(int event){

int next_state = parser[state][event];switch(next_state){ case fini:

printf(”%s\n”, state==q0 ? ”ok” : ”err”); default:

state = next_state; //change state}

}

void EventHandler(int event){

int next_state = parser[state][event];switch(next_state){ case fini:

printf(”%s\n”, state==q0 ? ”ok” : ”err”); default:

state = next_state; //change state}

}

Event HandlerEvent Handler

Page 127: Lecture 5: Finite Automata

Implementation (II)

q0 q1

q2 q3

0

0

0

0

1 1 1 1

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>pq1

pq0

pq3

pq2

pq2

pq3

pq0

pq1

ok

err

err

err

The ParserThe Parser

Page 128: Lecture 5: Finite Automata

Implementation (II)

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>pq1

pq0

pq3

pq2

pq2

pq3

pq0

pq1

ok

err

err

err

The ParserThe Parser

#define q0 0#define q1 1#define q2 2#define q3 3#define fini4

#define q0 0#define q1 1#define q2 2#define q3 3#define fini4

void pq0(), pq1(), pq2(), pq3();void ok(), err();void pq0(), pq1(), pq2(), pq3();void ok(), err();

Page 129: Lecture 5: Finite Automata

Implementation (II)

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>pq1

pq0

pq3

pq2

pq2

pq3

pq0

pq1

ok

err

err

err

The ParserThe Parser

void pq0(){

state = q0;}

void pq1(){

state = q1;}

void pq0(){

state = q0;}

void pq1(){

state = q1;}

void pq2(){

state = q2;}

void pq3(){

state = q3;}

void pq2(){

state = q2;}

void pq3(){

state = q3;}

Page 130: Lecture 5: Finite Automata

Implementation (II)

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>pq1

pq0

pq3

pq2

pq2

pq3

pq0

pq1

ok

err

err

err

The ParserThe Parser

void ok(){

printf(”ok\n”);state = fini;

}

void err(){

printf(”error\n”);state = fini;

}

void ok(){

printf(”ok\n”);state = fini;

}

void err(){

printf(”error\n”);state = fini;

}

Page 131: Lecture 5: Finite Automata

Implementation (II)

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>pq1

pq0

pq3

pq2

pq2

pq3

pq0

pq1

ok

err

err

err

The ParserThe Parser

typedef void (*FUNCTION)();

FUNCTION parser[4][3]={pq1, pq2, ok,pq0, pq3, err,pq3, pq0, err,pq2, pq1, err

};

typedef void (*FUNCTION)();

FUNCTION parser[4][3]={pq1, pq2, ok,pq0, pq3, err,pq3, pq0, err,pq2, pq1, err

};

Page 132: Lecture 5: Finite Automata

Implementation (II)

void main(){

// Ask user to input a 0/1 string// Store the string into strstate = q0; //initializationwhile(state!=fini){

ToEvent(*str++);(*parser[state][event])();

}}

void main(){

// Ask user to input a 0/1 string// Store the string into strstate = q0; //initializationwhile(state!=fini){

ToEvent(*str++);(*parser[state][event])();

}} Event (or Message) LoopEvent (or Message) Loop