135
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 3 School of Innovation, Design and Engineering Mälardalen University 2012

CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 3

  • Upload
    ova

  • View
    57

  • Download
    2

Embed Size (px)

DESCRIPTION

CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 3 School of Innovation, Design and Engineering Mälardalen University 2012. 1. Content Finite Automata, FA Deterministic Finite Automata, DFA Nondeterministic Automata NFA NFA  DFA Equivalence. - PowerPoint PPT Presentation

Citation preview

Page 1: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

11

CDT314

FABER

Formal Languages, Automata and Models of Computation

Lecture 3

School of Innovation, Design and Engineering Mälardalen University

2012

Page 2: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

2

Content

Finite Automata, FADeterministic Finite Automata, DFANondeterministic Automata NFANFA DFA Equivalence

Page 3: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

3

Finite Automata FA(Finite State Machines)

Based on C Busch, RPI, Models of Computation

Page 4: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

4

There is no formal general definition for "automaton". Instead, there are various kinds of automata, each with it's own formal definition.

• has some form of input• has some form of output• has internal states • may or may not have some form of storage• is hard-wired rather than programmable

Generally, an automaton

Page 5: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

5

Finite Automaton

Input

String

Output

String

FiniteAutomaton

Page 6: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

6

Finite Accepter

Input

“Accept” or“Reject”

String

FiniteAutomaton

Output

Page 7: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

7

Nodes = States Edges = Transitions

An edge with several symbols is a short-hand for several edges:

Finite Automaton as Directed Graph

1qa

0q

1qba,0q1q

a

0qb

Page 8: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

8

Deterministic Finite Automata DFA

Page 9: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

9

• Deterministic there is no element of choice• Finite only a finite number of states and arcs • Acceptors produce only a yes/no answer

DFA

Page 10: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

10

Transition Graph

initialstate

final state“accept”state

transition

abba -Finite Acceptor

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

5q

a a bb

ba,

ba,

},{ baAlphabet =

Page 11: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

11

Formal DefinitionDeterministic Finite Accepter (DFA)

FqQM ,,,, 0

Q

0q

F

: set of states

: input alphabet

: transition function

: initial state

: set of final states

Page 12: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

12

Set of States

Q

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

5q

a a bb

ba,

543210 ,,,,, qqqqqqQ

ba,

Page 13: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

13

Input Alphabet

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

5q

a a bb

ba,

ba,

ba,

Page 14: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

14

Initial State

0q

1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,0q

Page 15: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

15

Set of Final States

F

0q 1q 2q 3qa b b a

5q

a a bb

ba, 4qF

ba,

4q

Page 16: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

16

Transition Function

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

5q

a a bb

ba,

QQ :

ba,

Page 17: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

17

10 , qaq

2q 3q 4qa b b a

5q

a a bb

ba,

ba,0q 1q

Page 18: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

18

50 , qbq

1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,0q

Page 19: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

19

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

5q

a a bb

ba,

ba,

32 , qbq

Page 20: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

20

Transition Function

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

5q

a a bb

ba,

ba,

a b0q

2q

5q

1q 5q

5q5q

3q

4q

1q 5q 2q5q 3q4q 5q5q 5q

Page 21: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

21

Extended Transition Function

*

QQ *:*

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

5q

a a bb

ba,

ba,

Page 22: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

22

20 ,* qabq

3q 4qa b b a

5q

a a bb

ba,

ba,0q 1q 2q

Page 23: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

23

40 ,* qabbaq

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

5q

a a bb

ba,

ba,

Page 24: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

24

50 ,* qabbbaaq

1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,0q

Observation: There is a walk from to with the label

0q 5qabbbaa

Page 25: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

25

Recursive Definition

)),,(*(,*

,*awqwaq

qq

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

5q

a a bb

ba,

ba,

Page 26: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

26

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

5q

a a bb

ba,

ba,

21

0

0

0

0

,,,

,,,*),,(*

,*

qbqbaq

baqbaq

abq

Page 27: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

27

String Acceptance

Definition:A string w is accepted by DFA M if w

drives M to a final state from the initial state.

Formally: M accepts w iff

Fwq ,* 0

Page 28: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

28

50 ,* qabbbaaq

1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,0q

abbbaa is NOT accepted

Page 29: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

29

40 ,* qabbaq

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

5q

a a bb

ba,

ba,

abba is accepted

Page 30: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

30

Language Accepted by DFA

Take a DFA

Definition:The language contains all input strings accepted by

= strings that drive to a final

state

M

MLM

ML M

Page 31: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

31

Example

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

5q

a a bb

ba,

ba,

abbaML M

accept

},{ baAlphabet =

Page 32: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

32

Another Example

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

5q

a a bb

ba,

ba,

abbaabML ,, M

acceptacceptaccept

},{ baAlphabet =

Page 33: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

33

Formally

For a DFA

Language accepted by :

FqQM ,,,, 0

M

FwqwML ,*:* 0

alphabet transitionfunction

initialstate

finalstates

Page 34: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

34

Observation

Language accepted by

FwqwML ,*:* 0

M

FwqwML ,*:* 0

MLanguage rejected by

Page 35: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

35

More Examples

a

b ba,

ba,

0q 1q 2q

}0:{ nbaML n

accept trap state},{ baAlphabet =

Page 36: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

36

ML = { all strings with prefix }ab

a b

ba,

0q 1q 2q

accept

ba,3q

ab

},{ baAlphabet =

Page 37: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

37

ML = { all strings without substring }001

0 00 001

1

0

110

0 1,0

}1,0{Alphabet =

Page 38: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

38

Regular Languages

All regular languages form a language family

LM MLL

A language is regular if there is a DFA such that

Page 39: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

39

Example

*,: bawawaL

a

b

ba,

a

b

ba

0q 2q 3q

4q

is regular

The language

},{ baAlphabet =

Page 40: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

40

Nondeterministic Finite Automata NFA

Page 41: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

41

• Nondeterministic there is an element of choice: in a given

state NFA can act on a given string in different ways. Several start/final states are allowed. -transitions are allowed.

• Finite only a finite number of states and arcs • Acceptors produce only a yes/no answer

NFA

Page 42: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

42

1q 2q

3q

a

a

a

0q

Two choices

}{aAlphabet =

Nondeterministic Finite Accepter (NFA)

Page 43: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

43

a a

0q

1q 2q

3q

a

a

First Choice

a

Page 44: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

44

a a

0q

1q 2q

3q

a

a

a

First Choice

Page 45: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

45

a a

0q

1q 2q

3q

a

a

First Choice

a

Page 46: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

46

a a

0q

1q 2q

3q

a

a

a “accept”

First Choice

Page 47: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

47

a a

0q

1q 2q

3q

a

a

Second Choice

a

Page 48: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

48

a a

0q

1q 2qa

a

Second Choice

a

3q

Page 49: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

49

a a

0q

1q 2qa

a

a

3q

Second Choice

No transition:the automaton hangs

Page 50: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

50

a a

0q

1q 2qa

a

a

3q

Second Choice

“reject”

Page 51: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

51

Observation

An NFA accepts a string ifthere is at least one computation of the NFA that accepts the string

Page 52: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

52

Exampleaa is accepted by the NFA:

0q

1q 2q

3q

a

a

a

Page 53: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

53

Lambda Transitions

1q 3qa0q 2q a

Page 54: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

54

a a

1q 3qa0q 2q a

Page 55: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

55

a a

1q 3qa0q 2q a

Page 56: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

56

a a

1q 3qa0q 2q a

(read head doesn’t move)

Page 57: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

57

a a

1q 3qa0q 2q a

Page 58: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

58

a a

1q 3qa0q 2q a

“accept”

String is acceptedaa

Page 59: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

59

Language accepted: }{aaL

1q 3qa0q 2q a

Page 60: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

60

Another NFA Example

0q 1q 2qa b

3q

},{ baAlphabet =

Page 61: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

61

a b

0q 1q 2qa b

3q

Page 62: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

62

a b

0q 2qa b

3q1q

Page 63: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

63

a b

0q 1qa b

3q2q

Page 64: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

64

a b

0q 1qa b

3q2q“accept”

Page 65: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

65

a b

Another String

a b

0q a b

1q 2q 3q

},{ baAlphabet =

Page 66: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

66

a b a b

0q a b

1q 2q 3q

Page 67: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

67

a b a b

0q a b

1q 2q 3q

Page 68: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

68

a b a b

0q a b

1q 2q 3q

Page 69: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

69

a b a b

0q a b

1q 2q 3q

Page 70: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

70

a b a b

0q a b

1q 2q 3q

Page 71: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

71

a b a b

0q a b

1q 2q 3q

Page 72: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

72

a b a b

0q a b

1q 2q 3q

“accept”

Page 73: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

73

ab

ababababababL ...,,,

Language accepted

0q 1q 2qa b

3q

},{ baAlphabet =

Page 74: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

74

Another NFA Example

0q 1q 2q0

11,0

}1,0{Alphabet =

Page 75: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

75

*10

...,101010,1010,10, L

0q 1q 2q0

11,0

Language accepted

Page 76: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

76

Formal Definition of NFA FqQM ,,,, 0

:Q

::0q

:F

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

: Input alphabet, i.e. ba,Transition function

Initial state

Final states

Page 77: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

77

10 1, qq

Transition Function

0

11,0

0q 1q 2q

Page 78: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

78

},{)0,( 201 qqq

0q0

11,0

1q 2q

Page 79: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

79

0q0

11,0

1q 2q

},{),( 200 qqq

Page 80: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

80

0q0

11,0

1q 2q

)1,( 2q

Page 81: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

81

Extended Transition Function

*

10 ,* qaq

0q

5q4q

3q2q1qaaa

b

(Utvidgad övergångsfunktion)

Page 82: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

82

540 ,,* qqaaq

0q

5q4q

3q2q1qaaa

b

Page 83: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

83

0320 ,,,* qqqabq

0q

5q4q

3q2q1qaaa

b

Page 84: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

84

Formally

wqq ij ,*

if and only if

there is a walk from towith label

iq jqw

Page 85: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

85

The Language of an NFA

540 ,,* qqaaq

0q

5q4q

3q2q1qaaa

b

M

)(MLaa

50 ,qqF

},{ baAlphabet =

Page 86: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

86

0320 ,,,* qqqabq

0q

5q4q

3q2q1qaaa

b

MLab

50 ,qqF

Page 87: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

87

50 ,qqF

540 ,,* qqabaaq )(MLabaa

0q

5q4q

3q2q1qaaa

b

Page 88: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

88

10 ,* qabaq MLaba

0q

5q4q

3q2q1qaaa

b

50 ,qqF

Page 89: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

89

0q

5q4q

3q2q1qaaa

b

aaababaaML *

Page 90: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

90

Formally

The language accepted by NFA M

,...,, 321 wwwML

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

Fqk (final state)

where

and there is some (at least one)

is:

Page 91: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

91

MLw

0q kq

w

w

w

),(* 0 wq

Fqk

iq

jq

Page 92: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

92

NFA DFA Equivalence

Page 93: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

93

Equivalence of NFAs and DFAs

Accept the same languages?

YES!

NFAs DFAs ?

The same power?

Page 94: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

94

We will prove:

Languages acceptedby NFAs

Languages acceptedby DFAs

NFAs and DFAs have the same computation power!

Page 95: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

95

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 96: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

96

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 97: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

97

Procedure NFA to DFA Idea: Define new states in DFA that collect the states

with the same transition

0

1a

3a,b

{0}start

{1,3}a

{3}b

start

Page 98: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

98

Procedure NFA to DFA

1. Initial state of NFA:

Initial state of DFA:

0q

0q

Page 99: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

99

Example

a

b

a

0q 1q 2q

NFA

DFA 0q

Step 1

Page 100: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

100

Procedure NFA to DFA

2. For every DFA’s state

Compute in the NFA

},...,,{ mji qqq

...

,,*,,*

aqaq

j

i

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

},...,,{ mji qqq

Add transition

Page 101: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

101

Example NFA

DFA

a

b

a

0q 1q 2q

},{),(* 210 qqaq

0q 21,qqa

210 ,, qqaq

Step 2

},{ baAlphabet =

Page 102: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

102

Procedure NFA to DFA

Repeat Step 2 for all letters in alphabet,

until no more transitions can be added.

Page 103: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

103

Example

a

b

a

0q 1q 2q

NFA

DFA 0q 21,qqa

b

ab

ba,

Step 3

},{ baAlphabet =

Page 104: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

104

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 105: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

105

Example

a

b

a0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

ba,

Fq 1

Fqq 21,

Step 4

},{ baAlphabet =

Page 106: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

106

Theorem

Take NFA M

Apply the procedure to obtain DFA M

Then and are equivalent :M M

MLML

Page 107: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

107

Languages acceptedby NFAs

Languages acceptedby DFAs

We have proven (proof by construction):

Regular Languages

END OF PROOF

Page 108: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

108

Nondeterministic vs.

Deterministic Automata

Page 109: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

109

Formal Definition of NFA

FqQM ,,,, 0

:Q Set of states, i.e. 210 ,, qqq: Input alphabet, i.e. ba,: Transition function:0q Initial state

:F Final (accepting) states

NFA is a mathematical model defined as a quintuple:

Page 110: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

110

Deterministic Finite Automata

A deterministic finite automaton (DFA) is a special case of a nondeterministic finite automaton (NFA) in which1. no state has an -transition, i.e. a transition

on input , and2. for each state q and input symbol a, there

is at most one edge labeled a leaving q.

Page 111: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

111

STATEINPUT SYMBOL

a b

012

{0, 1}--

{0}{2}{3}

Transition table for the finite automaton above

A nondeterministic finite automaton

b

0start 1a 2b b3

a

Example: NFA

Page 112: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

112

NFA accepting aa* + bb*

0start

1

a2

a

3

b 4

b

Example

Page 113: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

113

NFA accepting (a+b)*abb

0start 1a 2b b

b

a a

a

b

3

a

Example

Page 114: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

114NFA recognizing three different patterns.

(a) NFA for a, abb, and a*b+.

(b) Combined NFA.

Example

4

1

start

a2

3a

65b b

7b

8

ba

0

4

1start a

2

3start a

65b b

7start b

8

ba

Page 115: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

115

Ways to Think of Nondeterminism

• always make the correct guess• “backtracking” (systematically try all possibilities)

For a particular string, imagine a tree of possiblestate transitions:

q4a

q0

q3

q0q2

q1a

a

a

b

a

Page 116: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

116

Advantages of Nondeterminism

• An NFA can be smaller, easier to construct and easier to understand than a DFA that accepts the same language

• NFAs are useful for proving some theorems• NFAs are good introduction to nondeterminism

in more powerful computational models, where nondeterminism plays an important role

Page 117: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

117

Space and time taken to recognize regular expressions:- NFA more compact but take time to backtrack all choices- DFA take place, but save time

Time-Space Tradeoffs Goal: Given reg. exp. r and input string x, determine whether x is in L(r) Method #1: Build NFA N from r using Thompson's construction, then run previous algorithm construct NFA in O(|r|) time. N has at most twice as many states as |r|, and at most two transitions from each state, so transition table is O(|r|) space. Previous algorithm accepts or rejects x in O(|r|×|x|) time

AUTOMATON SPACE TIME

NFADFA

O(|r|)O(2|r|)

O(|r||x|)O(|x|)

DFA vs. NFA Space and Time Complexity Tradeoffs

(Where r is regular expression, and x is input string)

Page 118: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

118

Equivalent automata

Two finite automata M1 and M2 are equivalent if

L(M1) = L(M2)

that is, if they both accept the same language.

Page 119: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

119

Equivalence of NFAs and DFAs

To show that NFAs and DFAs accept the same class of languages, we show two things:

– Any language accepted by a DFA can also be accepted by some NFA (As DFA is a special case of NFA)

– Any language accepted by a NFA can also be accepted by some (corresponding, specially constructed) DFA

Page 120: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

120

Proof Strategy

To show that any language accepted by a NFA is also accepted by some DFA, we describe an algorithm that takes any NFA and converts it into a DFA that accepts the same language.

The algorithm is called the “subset construction algorithm”.

We can use mathematical induction (on the length of a string accepted by the automaton) to prove the DFA that is constructed accepts the same language as the NFA.

Page 121: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

121

Converting NFA to DFA by Subset Construction

http://www.math.uu.se/~salling/Movies/SubsetConstruction.mov

Page 122: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

122

Subset construction

Given a NFA construct a DFA that accepts the same language.

The equivalent DFA simulates the NFA by keeping track of the possible states it could be in. Each state of the DFA is a subset of the set of states of the NFA -hence, the name of the algorithm.

If the NFA has n states, the DFA can have as many as 2n states, although it usually has many less.

Page 123: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

123

Steps of subset construction

The initial state of the DFA is the set of all states the NFA can be in without reading any input.

For any state {qi,qj,…,qk} of the DFA and any input a, the next state of the DFA is the set of all states of the NFA that can result as next states if the NFA is in any of the states qi,qj,…,qk when it reads a. This includes states that can be reached by reading a, followed by any number of -moves. Use this rule to keep adding new states and transitions until it is no longer possible to do so.

The accepting states of the DFA are those states that contain an accepting state of the NFA.

Page 124: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

124

Example

Here is a NFA that we want to convert to an equivalent DFA.

a

bb

01

2

b

a

a

b

},{ baAlphabet =

Page 125: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

125

{0,1}

The start state of the DFA is the set of states the NFA can be in before reading any input. This includes the start state of the NFA and any states that can be reached by a -transition.

a

bb

b

a

a

01

2

b

NFA

DFA

},{ baAlphabet =

Page 126: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

126

{0,1}

a

b

{2}

For start state {0,1}, make transitions for each possible input, here a and b. Reading b from start {0,1}, we reach state {2}. Means from either {0}, or {1} we reach {2}.

a

bb

b

a

a

01

2

b

NFADFA

Page 127: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

127

For state {2}, we create a transition for each possible input, a and b. From {2}, with b we are either back to {2} (loop) or we reach {1}- see the little framed original NFA. So from {2}, with b we end in state {1, 2}. Reading a leads us from {2} to {0} in the original NFA, which means state {0, 1} in the new DFA.

ba

a

b{0,1}{1,2}

{2}

a

bb

b

a

a

01

2

b

NFADFA

Page 128: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

128

For state {1, 2}, we make again transition for eachpossible input, a and b. From {2} a leads us to {0}. From {1} with a we are back to {1}. So, we reach {0, 1} with a from {1,2}. With b we are back to {1,2}.

At this point, a transition is defined for every state-input pair.

ba

a

b{0,1}

{1,2}

{2}

b

a

DFA a

bb

b

a

a

01

2

b

NFA

Page 129: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

129

The last step is to mark the final states of the DFA.As {1} was the accepting state in NFA, all states containing {1} in DFA will be accepting states: ({0, 1} and {1, 2}).

ba

a

b{0,1} {1,2}

{2}

b

a

DFA a

bb

b

a

a

01

2

b

NFA

Page 130: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

130

Subset Construction Algorithm

Page 131: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

131

Subset Construction

States of nondeterministic M´ will correspond to sets of states of deterministic M

Where q0 is start state of M, use {q0} as start state of M´.

Accepting states of M´ will be those state-sets containing at least one accepting state of M.

Page 132: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

132

Subset Construction (cont.)

For each state-set S and for each s in alphabet of M, we draw an arc labeled s from state S to that state-set consisting of all and only the s-successors of members of S.

Eliminate any state-set, as well as all arcs incident upon it, such that there is no path leading to it from {q0}.

Page 133: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

133

The power set of a finite set, Q, consists of 2|Q| elements

The DFA corresponding to a given NFA with Q states have a finite number of states, 2|Q|.

If |Q| = 0 then Q is the empty set, | P(Q)| = 1 = 20.

If |Q| = N and N 1, we construct subset of a given set so that for each element of the initial set there are two alternatives, either is the element member of a subset or not. So we have

2 · 2 · 2 · 2 · 2 · 2 · 2…. ·2 = 2N N times

Page 134: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

134

From an NFA to a DFASubset ConstructionOperation Description

- closure(s)

- closure(T)

Move(T,a)

Set of NFA states reachable from an NFA state s on -transitions

Set of NFA states reachable from some NFA state s in T on -transitions

Set of NFA states reachable from some NFA state set with a transition on input symbol a

Page 135: CDT314  FABER Formal Languages, Automata  and Models of Computation Lecture 3

135

From an NFA to a DFA

Subset Construction

Initially, -closure (s0) is the only states in D and it is unmarked

while there is an unmarked state T in D do mark T; for each input symbol a do U:= -closure(move(T,a)); if U is not in D then add U as an unmarked state to D Dtran[T,a]:=U; end(for) end(while)