1 Theory of Automata & Formal Languages
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
CPU
input memory
output memory
Program memory
temporary memory
7
CPU
input memory
output memoryProgram memory
temporary memory3)( xxf
compute xx
compute xx 2
2x
42*2 z82*)( zxf
8
Automaton
CPU
input memory
output memory
Program memory
temporary memory
Automaton
9
input memory
output memory
temporary memory
Finite
Automaton
10
Different Kinds of Automata
Automata are distinguished by the temporary memory
• Finite Automata: no temporary memory
• Pushdown Automata: stack
• Turing Machines: random access memory
11
input memory
output memory
Stack
Pushdown
Automaton
Pushdown Automaton
Programming Languages (medium computing power)
Push, Pop
12
input memory
output memory
Random Access Memory
Turing
Machine
Turing Machine
Algorithms (highest computing power)
13
Finite
Automata
Pushdown
Automata
Turing
Machine
Power of Automata
14
Power sets
A power set is a set of sets
Powerset of S = the set of all the subsets of S
S = { a, b, c }
2S = { , {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c} }
Observation: | 2S | = 2|S| ( 8 = 23 )
15
Cartesian ProductA = { 2, 4 } B = { 2, 3, 5 }
A X B = { (2, 2), (2, 3), (2, 5), ( 4, 2), (4, 3), (4, 4) }
|A X B| = |A| |B|
Generalizes to more than two sets
A X B X … X Z
16
RELATIONS R = {(x1, y1), (x2, y2), (x3, y3), …}
xi R yi
e. g. if R = ‘>’: 2 > 1, 3 > 2, 3 > 1
In relations xi can be repeated
17
Equivalence Relations
• Reflexive: x R x
• Symmetric: x R y y R x
• Transitive: x R Y and y R z x R z
Example: R = ‘=‘
• x = x
• x = y y = x
• x = y and y = z x = z
18
Equivalence ClassesFor equivalence relation R
equivalence class of x = {y : x R y}
Example:
R = { (1, 1), (2, 2), (1, 2), (2, 1),
(3, 3), (4, 4), (3, 4), (4, 3) }
Equivalence class of 1 = {1, 2}
Equivalence class of 3 = {3, 4}
19
Example of Equivalence relationLet Z = set of integers
R be defined on it as:
R= {(x,y)| x Z, y Z and
(x - y)is divisible by 5}
This relation is known as
” congruent modulo 5”
20
The equivalence classes are
[0]R= {…-10, -5, 0, 5,10,…}
[1]R = {…..,-9, -4, 1, 6, 11, 16….}
[2]R= {….-8, -3,2,7,12,17…..}
[3]R = {….-7, -2, 3, 8 ,13,…}
[4]R = {….-6,-1,4,9,14,19,….}
Z/R ={[0]R, [1]R, [2]R, [3]R, [4]R}
21
PROOF TECHNIQUES
• Proof by induction
• Proof by contradiction
22
Induction
We have statements P1, P2, P3, …
If we know
• for some k that P1, P2, …, Pk are true
• for any n >= k that
P1, P2, …, Pn imply Pn+1
Then
Every Pi is true
23
Treesroot
leaf
parent
child
Trees have no cycles
24
Proof by Induction• Inductive basis
Find P1, P2, …, Pk which are true
• Inductive hypothesis
Let’s assume P1, P2, …, Pn are true,
for any n >= k
• Inductive step
Show that Pn+1 is true
25
root
leaf
Level 0
Level 1
Level 2
Level 3
Height 3
26
Binary Trees
27
Example
Theorem: A binary tree of height n
has at most 2n leaves.
Proof:
let l(i) be the number of leaves at level i
l(0) = 0
l(3) = 8
28
Induction Step
hypothesis: l(n) <= 2n
Level
n
n+1
29
We want to show: l(i) <= 2i
• Inductive basis
l(0) = 1 (the root node)
• Inductive hypothesis
Let’s assume l(i) <= 2i for all i = 0, 1, …, n
• Induction step
we need to show that l(n + 1) <= 2n+1
30
hypothesis: l(n) <= 2n
Level
n
n+1
l(n+1) <= 2 * l(n) <= 2 * 2n = 2n+1
Induction Step
31
Proof by Contradiction
We want to prove that a statement P is true
• we assume that P is false
• then we arrive at an incorrect conclusion
• therefore, statement P must be true
32
Example
Theorem: is not rational
Proof:
Assume by contradiction that it is rational
= n/m
n and m have no common factors
We will show that this is impossible
2
2
33
= n/m 2 m2 = n2
Therefore, n2 is evenn is even
n = 2 k
2 m2 = 4k2 m2 = 2k2m is even
m = 2 p
Thus, m and n have common factor 2
Contradiction!
2
34
Basic Terms
Alphabet: A finite non empty set of elements.
={a,b,c,d,…z}
35
• String: A sequence of letters
– Examples: “cat”, “dog”, “house”, …
– Defined over an alphabet:
zcba ,,,,
Language: It is a set of strings on some alphabet
36
Alphabets and Strings
• We will use small alphabets:
• Strings
abbaw
bbbaaav
abu
ba,
baaabbbaaba
baba
abba
ab
a
37
String Operations
m
n
bbbv
aaaw
21
21
bbbaaa
abba
mn bbbaaawv 2121
Concatenation
abbabbbaaa
38
12aaaw nR
naaaw 21 ababaaabbb
Reverse
bbbaaababa
39
String Length
• Length:
• Examples:
naaaw 21
nw
1
2
4
a
aa
abba
40
Recursive Definition of Length
For any letter:
For any string :
Example:
1a
1wwawa
4
1111
111
11
1
a
ab
abbabba
41
Length of Concatenation
• Example:
vuuv
853
8
5,
3,
vuuv
aababaabuv
vabaabv
uaabu
42
Proof of Concatenation Length
• Claim:
• Proof: By induction on the length
– Induction basis:– From definition of length:
vuuv v
1v
vuuuv 1
43
– Inductive hypothesis:
• for
– Inductive step: we will prove–
– for
vuuv
nv ,,2,1
1nv
vuuv
44
Inductive Step
• Write , where
• From definition of length:
• From inductive hypothesis:
• Thus:
wav 1, anw
1
1
wwa
uwuwauv
wuuw
vuwauwuuv 1
45
Empty String
• A string with no letters:
• Observations:
abbaabbaabba
www
0
46
Substring
• Substring of string:
– a subsequence of consecutive characters
• String Substring
bbab
b
abba
ab
abbab
abbab
abbab
abbab
47
Prefix and Suffix
• Prefixes Suffixes abbab
abbab
abba
abb
ab
a
b
ab
bab
bbab
abbabuvw
prefix
suffix
48
Another Operation
• Example:
• Definition:–
n
n wwww
abbaabbaabba 2
0w
0abba
49
The * Operation
• : the set of all possible strings from• alphabet
•
*
,,,,,,,,,*
,
aabaaabbbaabaaba
ba
50
The + Operation : the set of all possible strings from alphabet except
,,,,,,,,,*
,
aabaaabbbaabaaba
ba
*
,,,,,,,, aabaaabbbaabaaba
51
Language
• A language is any subset of
• Example:
• Languages:
*
,,,,,,,,*
,
aaabbbaabaaba
ba
},,,,,{
,,
aaaaaaabaababaabba
aabaaa
52
Another Example
• An infinite language
}0:{ nbaL nn
aaaaabbbbb
aabb
ab
L Labb
53
Operations on Languages
• The usual set operations
• Complement:
aaaaaabbbaaaaaba
ababbbaaaaaba
aaaabbabaabbbaaaaaba
,,,,
}{,,,
},,,{,,,
LL * ,,,,,,, aaabbabaabbaa
54
Reverse
• Definition:
• Examples:
}:{ LwwL RR
ababbaabababaaabab R ,,,,
}0:{
}0:{
nabL
nbaL
nnR
nn
55
Concatenation
• Definition:
• Example:
2121 ,: LyLxxyLL
baaabababaaabbaaaab
aabbaaba
,,,,,
,,,
56
Another Operation
• Definition:
• Special case:
n
n LLLL
bbbbbababbaaabbabaaabaaa
babababa
,,,,,,,
,,,, 3
0
0
,, aaabbaa
L
57
More Examples
• }0:{ nbaL nn
}0,:{2 mnbabaL mmnn
2Laabbaaabbb
58
Star-Closure (Kleene *)
• Definition:
• Example:
•
210* LLLL
,,,,
,,,,
,,
,
*,
abbbbabbaaabbaaa
bbbbbbaabbaa
bbabba
59
Positive Closure
• Definition:
*
21
L
LLL
,,,,
,,,,
,,
,
abbbbabbaaabbaaa
bbbbbbaabbaa
bba
bba
60
Finite AutomatonInput
String
•
Output
String
FiniteAutomaton
61
Finite Accepter
•
Input
“Accept” or“Reject”
String
FiniteAutomaton
Output
62
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,
63
Initial Configuration
•
1q 2q 3q 4qa b b a
5q
a a bb
ba,
Input Stringa b b a
ba,
0q
64
Reading the Input
•
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
a b b a
ba,
65
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
a b b a
ba,
66
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
a b b a
ba,
67
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
a b b a
ba,
68
0q 1q 2q 3q 4qa b b a
Output: “accept”
5q
a a bb
ba,
a b b a
ba,
Input finished
69
Rejection
•
1q 2q 3q 4qa b b a
5q
a a bb
ba,
a b a
ba,
0q
70
•
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
a b a
ba,
71
•
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
a b a
ba,
72
•
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
a b a
ba,
73
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
Output:“reject”
a b a
ba,
Input finished
74
Another Example
a
b ba,
ba,
0q 1q 2q
a ba
75
a
b ba,
ba,
0q 1q 2q
a ba
76
a
b ba,
ba,
0q 1q 2q
a ba
77
a
b ba,
ba,
0q 1q 2q
a ba
78
a
b ba,
ba,
0q 1q 2q
a ba
Output: “accept”
Input finished
79
Rejection
a
b ba,
ba,
0q 1q 2q
ab b
80
a
b ba,
ba,
0q 1q 2q
ab b
81
a
b ba,
ba,
0q 1q 2q
ab b
82
a
b ba,
ba,
0q 1q 2q
ab b
83
a
b ba,
ba,
0q 1q 2q
ab b
Output: “reject”
Input finished
84
• 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
85
Input Alphabet
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
ba,
86
Set of States
Q
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
543210 ,,,,, qqqqqqQ
ba,
87
Initial State
0q
1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
0q
88
Set of Final States
F
0q 1q 2q 3qa b b a
5q
a a bb
ba,
4qF
ba,
4q
89
Transition Function
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
QQ :
ba,
90
10 , qaq
2q 3q 4qa b b a
5q
a a bb
ba,
ba,
0q 1q
91
50 , qbq
1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
0q
92
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
32 , qbq
93
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
94
Extended Transition Function
*
QQ *:*
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
95
20 ,* qabq
3q 4qa b b a
5q
a a bb
ba,
ba,
0q 1q 2q
96
40 ,* qabbaq
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
97
50 ,* qabbbaaq
1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
0q
98
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
1q
99
Recursive Definition
•
)),,(*(,*
,*
awqwaq
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
100
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
101
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
102
Example
•
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
abbaML M
accept
103
Another Example
0q 1q 2q 3q 4qa b b a
5q
a a bb
ba,
ba,
abbaabML ,, M
acceptacceptaccept
•
104
Formally
• For a DFA Language accepted by :
FqQM ,,,, 0
M
FwqwML ,*:* 0
alphabet transitionfunction
initialstate
finalstates
105
Observation
• Language accepted by :
• Language rejected by :
FwqwML ,*:* 0M
FwqwML ,*:* 0M
106
More Examples•
a
b ba,
ba,
0q 1q 2q
}0:{ nbaML n
accept trap state or dead state
107
•
ML = { all strings with prefix }ab
a b
ba,
0q 1q 2q
accept
ba,3q
ab
108
•
ML = { all strings without substring }001
0 00 001
1
0
1
10
0 1,0
109
Regular Languages
• A language is regular if there is
• a DFA such that
• All regular languages form a language family–
LM MLL
110
Example
• The language
• is regular:
•
*,: bawawaL
a
b
ba,
a
b
ba
0q 2q 3q
4q
111
Non Deterministic Automata
112
Non Deterministic Finite Accepter
FqQM ,,,, 0
: 2QQ
113
1q 2q
3q
a
a
a
0q
}{aAlphabet =
Nondeterministic Finite Accepter (NFA)
114
1q 2q
3q
a
a
a
0q
Two choices
}{aAlphabet =
Nondeterministic Finite Accepter (NFA)
115
No transition
1q 2q
3q
a
a
a
0q
Two choices No transition
}{aAlphabet =
Nondeterministic Finite Accepter (NFA)
116
a a
0q
1q 2q
3q
a
a
First Choice
a
117
a a
0q
1q 2q
3q
a
a
a
First Choice
118
a a
0q
1q 2q
3q
a
a
First Choice
a
119
a a
0q
1q 2q
3q
a
a
a “accept”
First Choice
All input is consumed
120
a a
0q
1q 2q
3q
a
a
Second Choice
a
121
a a
0q
1q 2qa
a
Second Choice
a
3q
122
a a
0q
1q 2qa
a
a
3q
Second Choice
No transition:the automaton hangs
123
a a
0q
1q 2qa
a
a
3q
Second Choice
“reject”
Input cannot be consumed
124
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
125
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
126
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
127
a
0q
1q 2q
3q
a
a
Rejection example
a
128
a
0q
1q 2q
3q
a
a
a
First Choice
129
a
0q
1q 2q
3q
a
a
a
First Choice
“reject”
130
Second Choice
a
0q
1q 2q
3q
a
a
a
131
Second Choice
a
0q
1q 2qa
a
a
3q
132
Second Choice
a
0q
1q 2qa
a
a
3q “reject”
133
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
134
Rejection example
a a
0q
1q 2q
3q
a
a
a
a
135
a a
0q
1q 2q
3q
a
a
a
First Choice
a
136
a a
0q
1q 2q
3q
a
a
First Choice
a
a
No transition:the automaton hangs
137
a a
0q
1q 2q
3q
a
a
a “reject”
First Choice
a
Input cannot be consumed
138
a a
0q
1q 2q
3q
a
a
Second Choice
a
a
139
a a
0q
1q 2qa
a
Second Choice
a
3q
a
140
a a
0q
1q 2qa
a
a
3q
Second Choice
No transition:the automaton hangs
a
141
a a
0q
1q 2qa
a
a
3q
Second Choice
“reject”
a
Input cannot be consumed
142
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
143
1q 2q
3q
a
a
a
0q
Language accepted: }{aaL
144
Lambda →Transitions
1q 3qa0q
2q a
145
a a
1q 3qa0q
2q a
146
a a
1q 3qa0q
2q a
147
a a
1q 3qa0q
2q a
(read head doesn’t move)
148
a a
1q 3qa0q
2q a
149
a a
1q 3qa0q
2q a
“accept”
String is acceptedaa
all input is consumed
150
a a
1q 3qa0q
2q a
Rejection Example
a
151
a a
1q 3qa0q
2q a
a
152
a a
1q 3qa0q
2q a
(read head doesn’t move)
a
153
a a
1q 3qa0q
2q a
a
No transition:the automaton hangs
154
a a
1q 3qa0q
2q a
“reject”
String is rejectedaaa
a
Input cannot be consumed
155
Language accepted: }{aaL
1q 3qa0q
2q a
156
Another NFA Example
0q 1q 2qa b
3q
157
a b
0q 1q 2qa b
3q
158
0q 2qa b
3q
a b
1q
159
a b
0q 1qa b
3q2q
160
a b
0q 1qa b
3q2q
“accept”
161
0qa b
a b
Another String
a b
1q 2q 3q
162
0qa b
a b a b
1q 2q 3q
163
0qa b
a b a b
1q 2q 3q
164
0qa b
a b a b
1q 2q 3q
165
0qa b
a b a b
1q 2q 3q
166
0qa b
a b a b
1q 2q 3q
167
0qa b
a b a b
1q 2q 3q
168
a b a b
0qa b
1q 2q 3q
“accept”
169
ab
ababababababL ...,,,
Language accepted
0q 1q 2qa b
3q
170
Another NFA Example
0q 1q 2q0
11,0
171
{ }{ } *10=
...,101010,1010,10,λ=)(ML
0q 1q 2q0
11,0
Language accepted
172
Remarks:
•The symbol never appears on the input tape
0q2M
0q1M
{}=)M(L 1 }λ{=)M(L 2
•Extreme automata:
173
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
174
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
175
10 1, qq
0
11,0
Transition Function
0q 1q 2q
176
0q
0
11,0
},{)0,( 201 qqq
1q 2q
177
0q
0
11,0
1q 2q
},{),( 200 qqq
178
0q
0
11,0
1q 2q
)1,( 2q
179
Extended Transition Function
•
*
0q
5q4q
3q2q1qa
aa
b
10 ,* qaq
180
540 ,,* qqaaq
0q
5q4q
3q2q1qa
aa
b
181
0320 ,,,* qqqabq
0q
5q4q
3q2q1qa
aa
b
182
Formally
wqq ij ,*
It holdsif and only if
there is a walk from towith label
iq jqw
183
The Language of an NFA
•
0q
5q4q
3q2q1qa
aa
b
540 ,,* qqaaq
M
)(MLaa
50 ,qqF
184
0q
5q4q
3q2q1qa
aa
b
0320 ,,,* qqqabq MLab
50 ,qqF
185
•
0q
5q4q
3q2q1qa
aa
b
50 ,qqF
540 ,,* qqabaaq )(MLaaba
186
0q
5q4q
3q2q1qa
aa
b
50 ,qqF
10 ,* qabaq MLaba
187
0q
5q4q
3q2q1qa
aa
b
aaababaaML *
188
Formally
• The language accepted by NFA is:
• where
• and there is some
M
,...,, 321 wwwML
,...},{),(* 0 jim qqwq
Fqk (final state)
189
•
0q kq
w
w
w
),(* 0 wq MLw
Fqk
iq
jq
190
Equivalence of NFAs and DFAs
191
Equivalence of Machines
• For DFAs or NFAs:
• Machine is equivalent to machine
• if
•
1M 2M
21 MLML
192
• 0q 1q 2q
0
11,0
0q 1q 2q
0
11
0
1,0
NFA
DFA
*}10{1 ML
*}10{2 ML
1M
2M
193
• 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
194
Equivalence of NFAs and DFAs
Question: NFAs = DFAs ?
Same power?Accept the same languages?
195
Equivalence of NFAs and DFAs
Question: NFAs = DFAs ?
Same power?Accept the same languages?
YES!
196
We will prove:
Languages acceptedby NFAs
Languages acceptedby DFAs
NFAs and DFAs have the same computation power
197
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
198
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
199
NFA to DFA
•
a
b
a
0q 1q 2q
NFA
DFA
0q
M
M
200
•
a
b
a
0q 1q 2q
NFA
DFA
0q 21,qqa
M
M
201
•
a
b
a
0q 1q 2q
NFA
DFA
0q 21,qqa
b
M
M
202
•
a
b
a
0q 1q 2q
NFA
DFA
0q 21,qqa
b
a
M
M
203
•
a
b
a
0q 1q 2q
NFA
DFA
0q 21,qqa
b
ab
M
M
204
•
a
b
a
0q 1q 2q
NFA
DFA
0q 21,qqa
b
ab
ba,
M
M
205
•
a
b
a
0q 1q 2q
NFA
DFA
0q 21,qqa
b
ab
ba,
M
M
)(MLML
206
NFA to DFA: Remarks
• We are given an NFA
• We want to convert it to an equivalent DFA
M
M
)(MLML
207
• If the NFA has states
• the DFA has states in the power set
•
,...,, 210 qqq
,....,,,,,,, 7432110 qqqqqqq
208
Procedure NFA to DFA
•
• 1. Initial state of NFA:
•
• Initial state of DFA:
0q
0q
209
•
a
b
a
0q 1q 2q
NFA
DFA
0q
M
M
210
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
211
•
a
b
a
0q 1q 2q
NFA
0q 21,qqa
DFA
},{),(* 210 qqaq
210 ,, qqaq
M
M
},{),(* 210 qqaq
212
Procedure NFA to DFA
• Repeat Step 2 for all letters in alphabet,
• until
• no more transitions can be added.
213
•
a
b
a
0q 1q 2q
NFA
DFA
0q 21,qqa
b
ab
ba,
M
M
214
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
215
•
a
b
a
0q 1q 2q
NFA
DFA
0q 21,qqa
b
ab
ba,
Fq 1
Fqq 21,
M
M
216
Theorem
• Take NFA M
Apply procedure to obtain DFA M
Then and are equivalent :M M
MLML
217
FinallyWe have proven
Languages acceptedby NFAs
Languages acceptedby DFAs
218
Languages acceptedby NFAs
Languages acceptedby DFAs
We have proven
Regular Languages
219
Languages acceptedby NFAs
Languages acceptedby DFAs
We have proven
Regular LanguagesRegular Languages
220
Languages acceptedby NFAs
Languages acceptedby DFAs
We have proven
Regular LanguagesRegular Languages
Thus, NFAs accept the regular languages
221
Single Final State for NFAs and DFAs
222
Observation
• Any Finite Automaton (NFA or DFA)
• can be converted to an equivalent NFA
• with a single final state
223
a
b
b
aNFA
Equivalent NFA
a
b
b
a
Example
224
NFAIn General
Equivalent NFA
Singlefinal state
225
Extreme Case
NFA without final state
Add a final stateWithout transitions
226
Some Properties of Regular Languages
227
1L2L
21LLConcatenation:
*1LStar:
21 LL Union:
Are regularLanguages
For regular languages and we will prove that:
properties
228
We Say:Regular languages are closed under
21LLConcatenation:
*1LStar:
21 LL Union:
229
Properties
1L 2L
21LLConcatenation:
*1LStar:
21 LL Union:
Are regularLanguages
For regular languages and we will prove that:
230
1LRegular language
11 LML
1M
Single final state
NFA 2M
2L
Single final state
22 LML
Regular language
NFA
231
Example
}{1 baL na
b 1M
baL 2ab
2M
232
Union
• NFA for 1M
2M
21 LL
233
• a
b
ab
}{1 baL n
}{2 baL
}{}{21 babaLL n NFA for
234
Concatenation
• NFA for
21LL
1M 2M
235
Example
•
• NFA for
a
b ab
}{1 baL n}{2 baL
}{}}{{21 bbaababaLL nn
236
Star Operation• NFA for *1L
1M
*1L
237
Example
• NFA for
*}{*1 baL n
a
b
}{1 baL n
N>= 0
238
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.
**
*
*
239
q0 q2q10,1 0,1
10
240
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