Nfa egs

88
1 Non Deterministic Automata

description

cd4

Transcript of Nfa egs

Page 1: Nfa egs

1

Non Deterministic Automata

Page 2: Nfa egs

2

1q 2q

3q

a

a

a

0q

}{aAlphabet =

Nondeterministic Finite Accepter (NFA)

Page 3: Nfa egs

3

1q 2q

3q

a

a

a

0q

Two choices

}{aAlphabet =

Nondeterministic Finite Accepter (NFA)

Page 4: Nfa egs

4

No transition

1q 2q

3q

a

a

a

0q

Two choices No transition

}{aAlphabet =

Nondeterministic Finite Accepter (NFA)

Page 5: Nfa egs

5

a a

0q

1q 2q

3q

a

a

First Choice

a

Page 6: Nfa egs

6

a a

0q

1q 2q

3q

a

a

a

First Choice

Page 7: Nfa egs

7

a a

0q

1q 2q

3q

a

a

First Choice

a

Page 8: Nfa egs

8

a a

0q

1q 2q

3q

a

a

a “accept”

First Choice

Page 9: Nfa egs

9

a a

0q

1q 2q

3q

a

a

Second Choice

a

Page 10: Nfa egs

10

a a

0q

1q 2qa

a

Second Choice

a

3q

Page 11: Nfa egs

11

a a

0q

1q 2qa

a

a

3q

Second Choice

No transition:the automaton hangs

Page 12: Nfa egs

12

a a

0q

1q 2qa

a

a

3q

Second Choice

“reject”

Page 13: Nfa egs

13

Observation

An NFA accepts a stringifthere is a computation of the NFAthat accepts the string

Page 14: Nfa egs

14

Example

aa is accepted by the NFA:

0q

1q 2q

3q

a

a

a

Page 15: Nfa egs

15

Lambda Transitions

1q 3qa0q

1q a

Page 16: Nfa egs

16

a a

1q 3qa0q

2q a

Page 17: Nfa egs

17

a a

1q 3qa0q

2q a

Page 18: Nfa egs

18

a a

1q 3qa0q

2q a

(read head doesn’t move)

Page 19: Nfa egs

19

a a

1q 3qa0q

2q a

Page 20: Nfa egs

20

a a

1q 3qa0q

2q a

“accept”

String is acceptedaa

Page 21: Nfa egs

21

Language accepted: }{aaL

1q 3qa0q

2q a

Page 22: Nfa egs

22

Another NFA Example

0q 1q 2qa b

3q

Page 23: Nfa egs

23

a b

0q 1q 2qa b

3q

Page 24: Nfa egs

24

0q 2qa b

3q

a b

1q

Page 25: Nfa egs

25

a b

0q 1qa b

3q2q

Page 26: Nfa egs

26

a b

0q 1qa b

3q2q

“accept”

Page 27: Nfa egs

27

0qa b

a b

Another String

a b

1q 2q 3q

Page 28: Nfa egs

28

0qa b

a b a b

1q 2q 3q

Page 29: Nfa egs

29

0qa b

a b a b

1q 2q 3q

Page 30: Nfa egs

30

0qa b

a b a b

1q 2q 3q

Page 31: Nfa egs

31

0qa b

a b a b

1q 2q 3q

Page 32: Nfa egs

32

0qa b

a b a b

1q 2q 3q

Page 33: Nfa egs

33

0qa b

a b a b

1q 2q 3q

Page 34: Nfa egs

34

a b a b

0qa b

1q 2q 3q

“accept”

Page 35: Nfa egs

35

ab

ababababababL ...,,,

Language accepted

0q 1q 2qa b

3q

Page 36: Nfa egs

36

Another NFA Example

0q 1q 2q0

11,0

Page 37: Nfa egs

37

*10

...,101010,1010,10,

L

0q 1q 2q0

11,0

Language accepted

Page 38: Nfa egs

38

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

Final states

Page 39: Nfa egs

39

10 1, qq

0

11,0

Transition Function

0q 1q 2q

Page 40: Nfa egs

40

0q

0

11,0

},{)0,( 201 qqq

1q 2q

Page 41: Nfa egs

41

0q

0

11,0

1q 2q

},{),( 200 qqq

Page 42: Nfa egs

42

0q

0

11,0

1q 2q

)1,( 2q

Page 43: Nfa egs

43

Extended Transition Function

*

0q

5q4q

3q2q1qa

aa

b

10 ,* qaq

Page 44: Nfa egs

44

540 ,,* qqaaq

0q

5q4q

3q2q1qa

aa

b

Page 45: Nfa egs

45

0320 ,,,* qqqabq

0q

5q4q

3q2q1qa

aa

b

Page 46: Nfa egs

46

Formally

wqq ij ,*It holds

if and only if

there is a walk from towith label

iq jqw

Page 47: Nfa egs

47

The Language of an NFA

0q

5q4q

3q2q1qa

aa

b

540 ,,* qqaaq

M

)(MLaa

50 ,qqF

Page 48: Nfa egs

48

0q

5q4q

3q2q1qa

aa

b

0320 ,,,* qqqabq MLab

50 ,qqF

Page 49: Nfa egs

49

0q

5q4q

3q2q1qa

aa

b

50 ,qqF

540 ,,* qqabaaq )(MLaaba

Page 50: Nfa egs

50

0q

5q4q

3q2q1qa

aa

b

50 ,qqF

10 ,* qabaq MLaba

Page 51: Nfa egs

51

0q

5q4q

3q2q1qa

aa

b

aaababaaML *

Page 52: Nfa egs

52

FormallyThe language accepted by NFA is:

where

and there is some

M

,...,, 321 wwwML

,...},{),(* 0 jim qqwq

Fqk (final state)

Page 53: Nfa egs

53

0q kq

w

w

w

),(* 0 wq MLw

Fqk

iq

jq

Page 54: Nfa egs

54

Equivalence of NFAs and DFAs

Page 55: Nfa egs

55

Equivalence of Machines

For DFAs or NFAs:

Machine is equivalent to machine

if

1M 2M

21 MLML

Page 56: Nfa egs

56

Example

0q 1q 2q

0

11,0

0q 1q 2q

0

11

0

1,0

NFA

DFA

*}10{1 ML

*}10{2 ML

1M

2M

Page 57: Nfa egs

57

Since

machines and are equivalent

*1021 MLML

1M 2M

0q 1q 2q

0

11,0

0q 1q 2q

0

11

0

1,0

DFA

NFA 1M

2M

Page 58: Nfa egs

58

Equivalence of NFAs and DFAs

Question: NFAs = DFAs ?

Same power?Accept the same languages?

Page 59: Nfa egs

59

Equivalence of NFAs and DFAs

Question: NFAs = DFAs ?

Same power?Accept the same languages?

YES!

Page 60: Nfa egs

60

We will prove:

Languages acceptedby NFAs

Languages acceptedby DFAs

Page 61: Nfa egs

61

We will prove:

Languages acceptedby NFAs

Languages acceptedby DFAs

NFAs and DFAs have the same computation power

Page 62: Nfa egs

62

Languages acceptedby NFAs

Languages acceptedby DFAs

Step 1

Page 63: Nfa egs

63

Languages acceptedby NFAs

Languages acceptedby DFAs

Step 1

Proof: Every DFA is also an NFA

Page 64: Nfa egs

64

Languages acceptedby NFAs

Languages acceptedby DFAs

Step 1

Proof: Every DFA is also an NFA

A language accepted by a DFAis also accepted by an NFA

Page 65: Nfa egs

65

Languages acceptedby NFAs

Languages acceptedby DFAs

Step 2

Page 66: Nfa egs

66

Languages acceptedby NFAs

Languages acceptedby DFAs

Step 2

Proof: Any NFA can be converted to anequivalent DFA

Page 67: Nfa egs

67

Languages acceptedby NFAs

Languages acceptedby DFAs

Step 2

Proof: Any NFA can be converted to anequivalent DFA

A language accepted by an NFAis also accepted by a DFA

Page 68: Nfa egs

68

NFA to DFA

a

b

a

0q 1q 2q

NFA

DFA

0q

Page 69: Nfa egs

69

NFA to DFA

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

Page 70: Nfa egs

70

NFA to DFA

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

Page 71: Nfa egs

71

NFA to DFA

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

a

Page 72: Nfa egs

72

NFA to DFA

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

Page 73: Nfa egs

73

NFA to DFA

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

ba,

Page 74: Nfa egs

74

NFA to DFA

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

ba,

Page 75: Nfa egs

75

NFA to DFA: Remarks

We are given an NFA

We want to convert it to an equivalent DFA

With

M

M

)(MLML

Page 76: Nfa egs

76

If the NFA has states

the DFA has states in the powerset

,...,, 210 qqq

,....,,,,,,, 7432110 qqqqqqq

Page 77: Nfa egs

77

Procedure NFA to DFA

1. Initial state of NFA:

Initial state of DFA:

0q

0q

Page 78: Nfa egs

78

Example

a

b

a

0q 1q 2q

NFA

DFA

0q

Page 79: Nfa egs

79

Procedure NFA to DFA

2. For every DFA’s state

Compute in the NFA

Add transition

},...,,{ mji qqq

...

,,*

,,*

aq

aq

j

i

},...,,{ mji qqq

},...,,{},,...,,{ mjimji qqqaqqq

Page 80: Nfa egs

80

Exampe

a

b

a

0q 1q 2q

NFA

0q 21,qqa

DFA

},{),(* 210 qqaq

210 ,, qqaq

Page 81: Nfa egs

81

Procedure NFA to DFA

Repeat Step 2 for all letters in alphabet,

untilno more transitions can be added.

Page 82: Nfa egs

82

Example

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

ba,

Page 83: Nfa egs

83

Procedure NFA to DFA

3. For any DFA state

If some is a final state in the NFA

Then, is a final state in the DFA

},...,,{ mji qqq

jq

},...,,{ mji qqq

Page 84: Nfa egs

84

Example

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

ba,

Fq 1

Fqq 21,

Page 85: Nfa egs

85

Theorem Take NFA M

Apply procedure to obtain DFA M

Then and are equivalent :M M

MLML

Page 86: Nfa egs

86

Finally

We have proven

Languages acceptedby NFAs

Languages acceptedby DFAs

Page 87: Nfa egs

87

Languages acceptedby NFAs

Languages acceptedby DFAs

We have proven

Regular Languages

Page 88: Nfa egs

88

Languages acceptedby NFAs

Languages acceptedby DFAs

We have proven

Regular LanguagesRegular Languages