Non-Deterministic Finite Automata - uranchimeg.comuranchimeg.com/ec303/ec303Lec14.pdf · Non-Deterministic Finite Automata EC303, Lecture - 14 . ... when there is a computation of

Post on 27-Mar-2019

285 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

1

Non-Deterministic Finite Automata

EC303, Lecture - 14

http://users.comlab.ox.ac.uk/luke.ong/teaching/moc/nfa2up.pdf

2

1q 2q

3q

a

a

a

0q

}{aAlphabet =

NondeterministicFinite Automaton (NFA)

EC303, Lecture - 14

3

1q 2q

3q

a

a

a

0q

Two choices

}{aAlphabet =

EC303, Lecture - 14

4

No transition

1q 2q

3q

a

a

a

0q

Two choicesNo transition

}{aAlphabet =

EC303, Lecture - 14

5

a a

0q

1q 2q

3q

a

a

First Choice

a

EC303, Lecture - 14

6

a a

0q

1q 2q

3q

a

a

a

First Choice

EC303, Lecture - 14

7

a a

0q

1q 2q

3q

a

a

First Choice

a

EC303, Lecture - 14

8

a a

0q

1q 2q

3q

a

a

a “accept”

First Choice

All input is consumed

EC303, Lecture - 14

9

a a

0q

1q 2q

3q

a

a

Second Choice

a

EC303, Lecture - 14

10

a a

0q

1q 2qa

a

Second Choice

a

3q

EC303, Lecture - 14

11

a a

0q

1q 2qa

a

a

3q

Second Choice

No transition:

the automaton hangs

EC303, Lecture - 14

12

a a

0q

1q 2qa

a

a

3q

Second Choice

“reject”

Input cannot be consumed

EC303, Lecture - 14

13

An NFA accepts a string:

when there is a computation of the NFA

that accepts the string

all the input is consumed and the automaton

is in an accepting state

There is a computation:

EC303, Lecture - 14

14

Example

aa is accepted by the NFA:

0q

1q 2q

3q

a

a

a

“accept”

0q

1q 2qa

a

a

3q “reject”because this

computation

accepts aaEC303, Lecture - 14

15

a

0q

1q 2q

3q

a

a

Rejection example

a

EC303, Lecture - 14

16

a

0q

1q 2q

3q

a

a

a

First Choice

EC303, Lecture - 14

17

a

0q

1q 2q

3q

a

a

a

First Choice

“reject”

EC303, Lecture - 14

18

Second Choice

a

0q

1q 2q

3q

a

a

a

EC303, Lecture - 14

19

Second Choice

a

0q

1q 2qa

a

a

3q

EC303, Lecture - 14

20

Second Choice

a

0q

1q 2qa

a

a

3q “reject”

EC303, Lecture - 14

21

An NFA rejects a string:

when there is no computation of the NFA

that accepts the string.

• All the input is consumed and the

automaton is in a non final state

• The input cannot be consumed

OR

For each computation:

EC303, Lecture - 14

22

Example

a is rejected by the NFA:

0q

1q 2qa

a

a

3q “reject”

0q

1q 2qa

a

a

3q

“reject”

All possible computations lead to rejectionEC303, Lecture - 14

23

Rejection example

a a

0q

1q 2q

3q

a

a

a

a

EC303, Lecture - 14

24

a a

0q

1q 2q

3q

a

a

a

First Choice

a

EC303, Lecture - 14

25

a a

0q

1q 2q

3q

a

a

First Choice

a

a

No transition:

the automaton hangs

EC303, Lecture - 14

26

a a

0q

1q 2q

3q

a

a

a “reject”

First Choice

a

Input cannot be consumed

EC303, Lecture - 14

27

a a

0q

1q 2q

3q

a

a

Second Choice

a

a

EC303, Lecture - 14

28

a a

0q

1q 2qa

a

Second Choice

a

3q

a

EC303, Lecture - 14

29

a a

0q

1q 2qa

a

a

3q

Second Choice

No transition:

the automaton hangs

a

EC303, Lecture - 14

30

a a

0q

1q 2qa

a

a

3q

Second Choice

“reject”

a

Input cannot be consumed

EC303, Lecture - 14

31

aaa is rejected by the NFA:

0q

1q 2q

3q

a

a

a

“reject”

0q

1q 2qa

a

a

3q “reject”

All possible computations lead to rejectionEC303, Lecture - 14

32

1q 2q

3q

a

a

a

0q

Language accepted: }{aaL

EC303, Lecture - 14

33

Lambda Transitions

1q 3qa0q 2q a

EC303, Lecture - 14

34

a a

1q 3qa0q 2q a

EC303, Lecture - 14

35

a a

1q 3qa0q 2q a

EC303, Lecture - 14

36

a a

1q 3qa0q 2q a

(read head does not move)

EC303, Lecture - 14

37

a a

1q 3qa0q 2q a

EC303, Lecture - 14

38

a a

1q 3qa0q 2q a

“accept”

String is acceptedaa

all input is consumed

EC303, Lecture - 14

39

a a

1q 3qa0q 2q a

Rejection Example

a

EC303, Lecture - 14

40

a a

1q 3qa0q 2q a

a

EC303, Lecture - 14

41

a a

1q 3qa0q 2q a

(read head doesn’t move)

a

EC303, Lecture - 14

42

a a

1q 3qa0q 2q a

a

No transition:

the automaton hangsEC303, Lecture - 14

43

a a

1q 3qa0q 2q a

“reject”

String is rejectedaaa

a

Input cannot be consumed

EC303, Lecture - 14

44

Language accepted: }{aaL

1q 3qa0q 2q a

EC303, Lecture - 14

45

Another NFA Example

0q 1q 2qa b3q

EC303, Lecture - 14

46

a b

0q 1q 2qa b3q

EC303, Lecture - 14

47

0q 2qa b3q

a b

1q

EC303, Lecture - 14

48

a b

0q 1qa b3q2q

EC303, Lecture - 14

49

a b

0q 1qa b3q2q

“accept”

EC303, Lecture - 14

50

0q a b

a b

Another String

a b

1q 2q 3q

EC303, Lecture - 14

51

0q a b

a b a b

1q 2q 3q

EC303, Lecture - 14

52

0q a b

a b a b

1q 2q 3q

EC303, Lecture - 14

53

0q a b

a b a b

1q 2q 3q

EC303, Lecture - 14

54

0q a b

a b a b

1q 2q 3q

EC303, Lecture - 14

55

0q a b

a b a b

1q 2q 3q

EC303, Lecture - 14

56

0q a b

a b a b

1q 2q 3q

EC303, Lecture - 14

57

a b a b

0q a b1q 2q 3q

“accept”

EC303, Lecture - 14

58

ab

ababababababL ...,,,

Language accepted

0q 1q 2qa b3q

EC303, Lecture - 14

59

Another NFA Example

0q 1q 2q

0

11,0

EC303, Lecture - 14

60

{ }

{ }*10=

...,101010,1010,10,λ=)(ML

0q 1q 2q

0

11,0

Language accepted

(redundant

state)

EC303, Lecture - 14

61

Remarks:

•The symbol never appears on the

input tape

0q

2M

0q

1M

{}=)M(L 1 }λ{=)M(L 2

•Simple automata:

EC303, Lecture - 14

62

0q

2q

1qa

a

a

0q 1qa

}{=)( 1 aML

2M1M

}{=)( 2 aML

NFA FA

•NFAs are interesting because we can

express languages easier than FAs

EC303, Lecture - 14

63

Formal Definition of NFAs

FqQM ,,,, 0

:Q

:

:0q

:F

Set of states, i.e. 210 ,, qqq

: Input aplhabet, i.e. ba,

Transition function

Initial state

Accepting statesEC303, Lecture - 14

64

10 1, qq

0

11,0

Transition Function

0q 1q 2q

EC303, Lecture - 14

65

0q

0

11,0

},{)0,( 201 qqq

1q 2q

EC303, Lecture - 14

66

0q

0

11,0

1q 2q

},{),( 200 qqq

EC303, Lecture - 14

67

0q

0

11,0

1q 2q

)1,( 2q

EC303, Lecture - 14

68

Extended Transition Function *

0q

5q4q

3q2q1qa

aa

b

10 ,* qaq

EC303, Lecture - 14

69

540 ,,* qqaaq

0q

5q4q

3q2q1qa

aa

b

EC303, Lecture - 14

70

0320 ,,,* qqqabq

0q

5q4q

3q2q1qa

aa

b

EC303, Lecture - 14

71

Formally

wqq ij ,* : there is a walk from to

with label iq jq

w

iq jq

w

kw 21

1 2 k

iq jq

EC303, Lecture - 14

72

The Language of an NFA

0q

5q4q

3q2q1qa

aa

b

540 ,,* qqaaq

M

)(MLaa

50,qqF

FEC303, Lecture - 14

73

0q

5q4q

3q2q1qa

aa

b

0320 ,,,* qqqabq MLab

50,qqF

FEC303, Lecture - 14

74

0q

5q4q

3q2q1qa

aa

b

50,qqF

540 ,,* qqabaaq )(MLaaba

FEC303, Lecture - 14

75

0q

5q4q

3q2q1qa

aa

b

50,qqF

10,* qabaq MLaba

FEC303, Lecture - 14

76

0q

5q4q

3q2q1qa

aa

b

}{* aaabML

EC303, Lecture - 14

77

Formally

The language accepted by NFA is:

where

and there is some

M

,...,, 321 wwwML

},,...,,{),(* 0 kjim qqqwq

Fqk (accepting state)

EC303, Lecture - 14

78

0q kq

w

w

w

),(* 0 wqMLw

Fqk

iq

jq

EC303, Lecture - 14

top related