Finite automata

Post on 14-Dec-2014

2.057 views 9 download

Tags:

description

 

Transcript of Finite automata

1

Theory of Automata &

Formal Languages

2

BOOKS

Theory of computer Science: K.L.P.Mishra &

N.Chandrasekharan

Intro to Automata theory, Formal languages and computation: Ullman,Hopcroft

Motwani

Elements of theory of computation Lewis & papadimitrou

3

Syllabus

Introduction

Deterministic and non deterministic Finite Automata, Regular Expression,Two way finite automata,Finite automata with output,properties of regular sets,pumping lemma, closure properties,Myhill nerode theorem

4

Context free Grammar: Derivation trees, Simplification forms

Pushdown automata: Def, Relationship between PDA and context free language,Properties, decision algorithms

Turing Machines: Turing machine model,Modification of turing machines,Church’s thesis,Undecidability,Recursive and recursively enumerable languages Post correspondence problems recursive functions

5

Chomsky Hierarchy: Regular grammars, unrestricted grammar, context sensitive language, relationship among languages

6

7

8

Finite AutomatonInput

String

Output

String

FiniteAutomaton

9

Finite Accepter

Input

“Accept” or“Reject”

String

FiniteAutomaton

Output

10

Transition Graph

initialstate

final state“accept”state

transition

Abba -Finite Accepter

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

5q

a a bb

ba,

ba,

11

Initial Configuration

1q 2q 3q 4qa b b a

5q

a a bb

ba,

Input Stringa b b a

ba,

0q

12

Reading the Input

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

5q

a a bb

ba,

a b b a

ba,

13

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

5q

a a bb

ba,

a b b a

ba,

14

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

5q

a a bb

ba,

a b b a

ba,

15

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

5q

a a bb

ba,

a b b a

ba,

16

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

Output: “accept”

5q

a a bb

ba,

a b b a

ba,

Input finished

17

Rejection

1q 2q 3q 4qa b b a

5q

a a bb

ba,

a b a

ba,

0q

18

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

5q

a a bb

ba,

a b a

ba,

19

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

5q

a a bb

ba,

a b a

ba,

20

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

5q

a a bb

ba,

a b a

ba,

21

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

5q

a a bb

ba,

Output:“reject”

a b a

ba,

Input finished

22

Another Example

a

b ba,

ba,

0q 1q 2q

a ba

23

a

b ba,

ba,

0q 1q 2q

a ba

24

a

b ba,

ba,

0q 1q 2q

a ba

25

a

b ba,

ba,

0q 1q 2q

a ba

26

a

b ba,

ba,

0q 1q 2q

a ba

Output: “accept”

Input finished

27

Rejection

a

b ba,

ba,

0q 1q 2q

ab b

28

a

b ba,

ba,

0q 1q 2q

ab b

29

a

b ba,

ba,

0q 1q 2q

ab b

30

a

b ba,

ba,

0q 1q 2q

ab b

31

a

b ba,

ba,

0q 1q 2q

ab b

Output: “reject”

Input finished

32

• Deterministic Finite Accepter (DFA)

FqQM ,,,, 0

Q

0q

F

: Finite set of states

: input alphabet

: transition function

: initial state is a member of Q

: set of final states

: Q X Q

33

Input Alphabet

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

5q

a a bb

ba,

ba,

ba,

34

Set of States

Q

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

5q

a a bb

ba,

543210 ,,,,, qqqqqqQ

ba,

35

Initial State

0q

1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,

0q

36

Set of Final States

F

0q 1q 2q 3qa b b a

5q

a a bb

ba,

4qF

ba,

4q

37

Transition Function

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

5q

a a bb

ba,

QQ :

ba,

38

10 , qaq

2q 3q 4qa b b a

5q

a a bb

ba,

ba,

0q 1q

39

50 , qbq

1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,

0q

40

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

5q

a a bb

ba,

ba,

32 , qbq

41

Transition Function

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

5q

a a bb

ba,

a b

0q

1q

2q

3q

4q

5q

1q 5q

5q 2q

2q 3q

4q 5q

ba,5q5q5q5q

42

Extended Transition Function

*

QQ *:*

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

5q

a a bb

ba,

ba,

43

20 ,* qabq

3q 4qa b b a

5q

a a bb

ba,

ba,

0q 1q 2q

44

40 ,* qabbaq

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

5q

a a bb

ba,

ba,

45

50 ,* qabbbaaq

1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,

0q

46

50 ,* qabbbaaq

1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,

0q

Observation: There is a walk from to with label

0qabbbaa

5q

47

Recursive Definition

)),,(*(,*

,*

awqwaq

qq

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

5q

a a bb

ba,

ba,

48

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

5q

a a bb

ba,

ba,

2

1

0

0

0

0

,

,,

,,,*

),,(*

,*

q

bq

baq

baq

baq

abq

49

Languages Accepted by DFAs• Take DFA

• Definition:

– The language contains – all input strings accepted by

– = { strings that drive to a final state}

M

MLM

M ML

50

Example

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

5q

a a bb

ba,

ba,

abbaML M

accept

51

Another Example

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

5q

a a bb

ba,

ba,

abbaabML ,, M

acceptacceptaccept

52

Formally

• For a DFA Language accepted by :

FqQM ,,,, 0

M

FwqwML ,*:* 0

alphabet transitionfunction

initialstate

finalstates

53

Observation

• Language accepted by :

• Language rejected by :

FwqwML ,*:* 0M

FwqwML ,*:* 0M

54

More Examples•

a

b ba,

ba,

0q 1q 2q

}0:{ nbaML n

accept trap state or dead state

55

ML = { all strings with prefix }ab

a b

ba,

0q 1q 2q

accept

ba,3q

ab

56

ML = { all strings without substring }001

0 00 001

1

0

1

10

0 1,0

57

Regular Languages

• A language is regular if there is

• a DFA such that

• All regular languages form a language family–

LM MLL

58

Example

• The language

• is regular:

*,: bawawaL

a

b

ba,

a

b

ba

0q 2q 3q

4q

59

Non Deterministic Automata

60

Non Deterministic Finite Accepter

FqQM ,,,, 0

: 2QQ

61

1q 2q

3q

a

a

a

0q

}{aAlphabet =

Nondeterministic Finite Accepter (NFA)

62

1q 2q

3q

a

a

a

0q

Two choices

}{aAlphabet =

Nondeterministic Finite Accepter (NFA)

63

No transition

1q 2q

3q

a

a

a

0q

Two choices No transition

}{aAlphabet =

Nondeterministic Finite Accepter (NFA)

64

a a

0q

1q 2q

3q

a

a

First Choice

a

65

a a

0q

1q 2q

3q

a

a

a

First Choice

66

a a

0q

1q 2q

3q

a

a

First Choice

a

67

a a

0q

1q 2q

3q

a

a

a “accept”

First Choice

All input is consumed

68

a a

0q

1q 2q

3q

a

a

Second Choice

a

69

a a

0q

1q 2qa

a

Second Choice

a

3q

70

a a

0q

1q 2qa

a

a

3q

Second Choice

No transition:the automaton hangs

71

a a

0q

1q 2qa

a

a

3q

Second Choice

“reject”

Input cannot be consumed

72

An NFA accepts a string:

when there is a computation of the NFAthat accepts the string

•All the input is consumed and the automaton is in a final state

73

An NFA rejects a string:

when there is no computation of the NFAthat accepts the string

• All the input is consumed and the automaton is in a non final state

• The input cannot be consumed

74

Example

aa is accepted by the NFA:

0q

1q 2q

3q

a

a

a

“accept”

0q

1q 2qa

a

a

3q “reject”

because this computationaccepts aa

75

a

0q

1q 2q

3q

a

a

Rejection example

a

76

a

0q

1q 2q

3q

a

a

a

First Choice

77

a

0q

1q 2q

3q

a

a

a

First Choice

“reject”

78

Second Choice

a

0q

1q 2q

3q

a

a

a

79

Second Choice

a

0q

1q 2qa

a

a

3q

80

Second Choice

a

0q

1q 2qa

a

a

3q “reject”

81

Examplea is rejected by the NFA:

0q

1q 2qa

a

a

3q “reject”

0q

1q 2qa

a

a

3q

“reject”

All possible computations lead to rejection

82

Rejection example

a a

0q

1q 2q

3q

a

a

a

a

83

a a

0q

1q 2q

3q

a

a

a

First Choice

a

84

a a

0q

1q 2q

3q

a

a

First Choice

a

a

No transition:the automaton hangs

85

a a

0q

1q 2q

3q

a

a

a “reject”

First Choice

a

Input cannot be consumed

86

a a

0q

1q 2q

3q

a

a

Second Choice

a

a

87

a a

0q

1q 2qa

a

Second Choice

a

3q

a

88

a a

0q

1q 2qa

a

a

3q

Second Choice

No transition:the automaton hangs

a

89

a a

0q

1q 2qa

a

a

3q

Second Choice

“reject”

a

Input cannot be consumed

90

aaais rejected by the NFA:

0q

1q 2q

3q

a

a

a

“reject”

0q

1q 2qa

a

a

3q “reject”

All possible computations lead to rejection

91

1q 2q

3q

a

a

a

0q

Language accepted: }{aaL

92

Lambda →Transitions

1q 3qa0q

2q a

93

a a

1q 3qa0q

2q a

94

a a

1q 3qa0q

2q a

95

a a

1q 3qa0q

2q a

(read head doesn’t move)

96

a a

1q 3qa0q

2q a

97

a a

1q 3qa0q

2q a

“accept”

String is acceptedaa

all input is consumed

98

a a

1q 3qa0q

2q a

Rejection Example

a

99

a a

1q 3qa0q

2q a

a

100

a a

1q 3qa0q

2q a

(read head doesn’t move)

a

101

a a

1q 3qa0q

2q a

a

No transition:the automaton hangs

102

a a

1q 3qa0q

2q a

“reject”

String is rejectedaaa

a

Input cannot be consumed

103

Language accepted: }{aaL

1q 3qa0q

2q a

104

Another NFA Example

0q 1q 2qa b

3q

105

a b

0q 1q 2qa b

3q

106

0q 2qa b

3q

a b

1q

107

a b

0q 1qa b

3q2q

108

a b

0q 1qa b

3q2q

“accept”

109

0qa b

a b

Another String

a b

1q 2q 3q

110

0qa b

a b a b

1q 2q 3q

111

0qa b

a b a b

1q 2q 3q

112

0qa b

a b a b

1q 2q 3q

113

0qa b

a b a b

1q 2q 3q

114

0qa b

a b a b

1q 2q 3q

115

0qa b

a b a b

1q 2q 3q

116

a b a b

0qa b

1q 2q 3q

“accept”

117

ab

ababababababL ...,,,

Language accepted

0q 1q 2qa b

3q

118

Another NFA Example

0q 1q 2q0

11,0

119

{ }{ } *10=

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

0q 1q 2q0

11,0

Language accepted

120

Remarks:

•The symbol never appears on the input tape

0q2M

0q1M

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

•Extreme automata:

121

0q

2q

1qa

0q 1qa

}{=)( 1 aML

2M1M

}{=)( 2 aML

NFA DFA

•NFAs are interesting because we can express languages easier than DFAs

ba,b

a,b

122

Formal Definition of NFAs

FqQM ,,,, 0

:Q

::0q

:F

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

: Input alphabet, i.e. ba,

Transition function

Initial state

Final states

123

10 1, qq

0

11,0

Transition Function

0q 1q 2q

124

0q

0

11,0

},{)0,( 201 qqq

1q 2q

125

0q

0

11,0

1q 2q

},{),( 200 qqq

126

0q

0

11,0

1q 2q

)1,( 2q

127

Extended Transition Function

*

0q

5q4q

3q2q1qa

aa

b

10 ,* qaq

128

540 ,,* qqaaq

0q

5q4q

3q2q1qa

aa

b

129

0320 ,,,* qqqabq

0q

5q4q

3q2q1qa

aa

b

130

Formally

wqq ij ,*

It holdsif and only if

there is a walk from towith label

iq jqw

131

The Language of an NFA

0q

5q4q

3q2q1qa

aa

b

540 ,,* qqaaq

M

)(MLaa

50 ,qqF

132

0q

5q4q

3q2q1qa

aa

b

0320 ,,,* qqqabq MLab

50 ,qqF

133

0q

5q4q

3q2q1qa

aa

b

50 ,qqF

540 ,,* qqabaaq )(MLaaba

134

0q

5q4q

3q2q1qa

aa

b

50 ,qqF

10 ,* qabaq MLaba

135

0q

5q4q

3q2q1qa

aa

b

aaababaaML *

136

Formally

• The language accepted by NFA is:

• where

• and there is some

M

,...,, 321 wwwML

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

Fqk (final state)

137

0q kq

w

w

w

),(* 0 wq MLw

Fqk

iq

jq

138

Equivalence of NFAs and DFAs

139

Equivalence of Machines

• For DFAs or NFAs:

• Machine is equivalent to machine

• if

1M 2M

21 MLML

140

• 0q 1q 2q

0

11,0

0q 1q 2q

0

11

0

1,0

NFA

DFA

*}10{1 ML

*}10{2 ML

1M

2M

141

• 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

142

Equivalence of NFAs and DFAs

Question: NFAs = DFAs ?

Same power?Accept the same languages?

143

Equivalence of NFAs and DFAs

Question: NFAs = DFAs ?

Same power?Accept the same languages?

YES!

144

We will prove:

Languages acceptedby NFAs

Languages acceptedby DFAs

NFAs and DFAs have the same computation power

145

Languages acceptedby NFAs

Languages acceptedby DFAs

Step 1

Proof: Every DFA is trivially an NFA

A language accepted by a DFAis also accepted by an NFA

146

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

147

NFA to DFA

a

b

a

0q 1q 2q

NFA

DFA

0q

M

M

148

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

M

M

149

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

M

M

150

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

a

M

M

151

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

M

M

152

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

ba,

M

M

153

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

ba,

M

M

)(MLML

154

NFA to DFA: Remarks

• We are given an NFA

• We want to convert it to an equivalent DFA

M

M

)(MLML

155

• If the NFA has states

• the DFA has states in the power set

,...,, 210 qqq

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

156

Procedure NFA to DFA

• 1. Initial state of NFA:

• Initial state of DFA:

0q

0q

157

a

b

a

0q 1q 2q

NFA

DFA

0q

M

M

158

Procedure NFA to DFA

• 2. For every DFA’s state

• Compute in the NFA

• Add transition to DFA

},...,,{ mji qqq

...

,,*

,,*

aq

aq

j

i

},...,,{ mji qqq

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

159

a

b

a

0q 1q 2q

NFA

0q 21,qqa

DFA

},{),(* 210 qqaq

210 ,, qqaq

M

M

},{),(* 210 qqaq

160

Procedure NFA to DFA

• Repeat Step 2 for all letters in alphabet,

• until

• no more transitions can be added.

161

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

ba,

M

M

162

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

163

a

b

a

0q 1q 2q

NFA

DFA

0q 21,qqa

b

ab

ba,

Fq 1

Fqq 21,

M

M

164

Theorem

• Take NFA M

Apply procedure to obtain DFA M

Then and are equivalent :M M

MLML

165

FinallyWe have proven

Languages acceptedby NFAs

Languages acceptedby DFAs

166

Languages acceptedby NFAs

Languages acceptedby DFAs

We have proven

Regular Languages

167

Languages acceptedby NFAs

Languages acceptedby DFAs

We have proven

Regular LanguagesRegular Languages

168

Languages acceptedby NFAs

Languages acceptedby DFAs

We have proven

Regular LanguagesRegular Languages

Thus, NFAs accept the regular languages

169

Single Final State for NFAs and DFAs

170

Observation

• Any Finite Automaton (NFA or DFA)

• can be converted to an equivalent NFA

• with a single final state

171

a

b

b

aNFA

Equivalent NFA

a

b

b

a

Example

172

NFAIn General

Equivalent NFA

Singlefinal state

173

Extreme Case

NFA without final state

Add a final stateWithout transitions

174

Some Properties of Regular Languages

175

1L2L

21LLConcatenation:

*1LStar:

21 LL Union:

Are regularLanguages

For regular languages and we will prove that:

properties

176

We Say:Regular languages are closed under

21LLConcatenation:

*1LStar:

21 LL Union:

177

Properties

1L 2L

21LLConcatenation:

*1LStar:

21 LL Union:

Are regularLanguages

For regular languages and we will prove that:

178

1LRegular language

11 LML

1M

Single final state

NFA 2M

2L

Single final state

22 LML

Regular language

NFA

179

Example

}{1 baL na

b 1M

baL 2ab

2M

180

Union

• NFA for 1M

2M

21 LL

181

• a

b

ab

}{1 baL n

}{2 baL

}{}{21 babaLL n NFA for

182

Concatenation

• NFA for

21LL

1M 2M

183

Example

• NFA for

a

b ab

}{1 baL n}{2 baL

}{}}{{21 bbaababaLL nn

184

Star Operation• NFA for *1L

1M

*1L

185

Example

• NFA for

*}{*1 baL n

a

b

}{1 baL n

N>= 0

186

Procedure: NFA to DFA

1 Create a graph with vertex {q0}.Identify this vertex as initial vertex of DFA.

2 Repeat the following steps until no more edges are missing.Take any vertex {qi,qj,….qk} of G that has no outgoing edge for some symbol a of the alphabet.Compute (qi, a), (qj, a)…. (qk, a)Then form the union of all these yielding the set {ql, qm, …qn}.Create a vertex for G labeled {ql, qm,…qn} if it does not already exist.Add to G an edge from {qi, qj,…qk} to {ql,qm…qn} and label it with a.

3 Every state of G whose label contains any qf of F is identified as a final vertex of DFA.

4 If NFA accepts then vertex {q0} in G is also made a final vertex.

**

*

*

187

q0 q2q10,1 0,1

10

188

q0

{q0,q1}

q1

q0,q1,q2q1,q2

q2

0

01

1

0

0,1

0,1

1

1

0,1

Equivalent DFA

Start

0