Professional Documents
Culture Documents
Computational Linguistics
Computational Linguistics
Equivalence of Machines
For DFAs or NFAs:
Machine M1 M2
is equivalent to machine
if
LM1 LM 2
3
Example NFA
M1
LM1 {10} * 0
q0 q1 0, 1 q2
1
DFA
M2 0,1
0
LM 2 {10} *
q0 q1 1 q2
1
0
Since
LM1 LM 2 10* 4
0
NFA
M1 q0 q1 0, 1 q2
1
0,1
0
DFA
M2 q0 q1 1 q2
1
0
5
Same power?
Accept the same languages?
Equivalence of NFAs and DFAs 6
YES!
Question: NFAs = DFAs ?
Same power?
Accept the same languages?
7
We will prove:
Languages Languages
accepted accepted
by NFAs by DFAs
8
We will prove:
Languages Languages
accepted accepted
by NFAs by DFAs
Languages Languages
accepted accepted
by NFAs by DFAs
10
Step 1
Languages Languages
accepted accepted
by NFAs by DFAs
Languages Languages
accepted accepted
by NFAs by DFAs
Languages Languages
accepted accepted
by NFAs by DFAs
13
Step 2
Languages Languages
accepted accepted
by NFAs by DFAs
Languages Languages
accepted accepted
by NFAs by DFAs
NFA
NFA to DFA a
q0 a q1 q2
b
DFA
q0
16
NFA
NFA to DFA a
q0 a q1 q2
b
DFA
q0 a
q1, q2
17
NFA
NFA to DFA a
q0 a q1 q2
b
DFA
q0 a
q1, q2
b
18
NFA
NFA to DFA a
q0 a q1 q2
b
DFA
a
q0 a
q1, q2
b
19
NFA
NFA to DFA a
q0 a q1 q2
b
DFA b a
q0 a
q1, q2
b
20
NFA
NFA to DFA a
q0 a q1 q2
b
DFA b a
q0 a
q1, q2
b
a, b
21
NFA
NFA to DFA a
q0 a q1 q2
b
a
DFA b
q0 a
q1, q2
b
a, b
22
M
We want to convert it
to an equivalent DFA
M
With LM L(M )
23
q0 , q1, q2 ,...
Observation
a NFA
a b
Equivalent NFA
a
a b
b
In General 27
NFA
Equivalent NFA
Single
final state
Extreme Case 28
Languages Languages
accepted accepted
by NFAs by DFAs
Regular Expressions
(a way to code the automata)
CPU memory
39
temporary memory
input memory
CPU
output memory
Program memory
3
Example: f ( x) x 40
temporary memory
input memory
CPU
output memory
Program memory
compute xx
2
compute x x
3
f ( x) x 41
temporary memory
input memory
x2
CPU
output memory
Program memory
compute xx
2
compute x x
3
temporary memory f ( x) x 42
z 2*2 4
f ( x) z * 2 8
input memory
x2
CPU
output memory
Program memory
compute xx
2
compute x x
3
temporary memory f ( x) x 43
z 2*2 4
f ( x) z * 2 8
input memory
x2
CPU
f ( x) 8
Program memory output memory
compute xx
2
compute x x
Automaton 44
temporary memory
Automaton
input memory
CPU
output memory
Program memory
Different Kinds of Automata 45
temporary memory
input memory
Finite
Automaton
output memory
input memory
Pushdown
Automaton
output memory
input memory
Turing
Machine
output memory
Chomsky Hierarchy
Generative Grammar
All
REC = computed by TM
Regular = computed by FA
52
Languages
A language is a set of strings
String Operations
w a1a2 an abba
v b1b2 bm bbbaaa
Concatenation
w a1a2 an ababaaabbb
Reverse
R
w an a2 a1 bbbaaababa
56
String Length
w a1a2 an
w n
Length:
abba 4
Examples: aa 2
a 1
57
Empty String
A string with no letters:
0
Observations:
w w w
Substring
Substring of string:
a subsequence of consecutive
characters
abbab ab
String Substring
abbab abba
abbab b
abbab bbab
59
ab bab suffix
abb ab
abba b
abbab
60
Another Operation
n
w ww
w
n
abba abbaabba
2
Example:
0
w
abba
Definition: 0
61
The * Operation
*: the set of all possible strings from
alphabet
a, b
* , a, b, aa, ab, ba, bb, aaa, aab,
The + Operation 62
: the set of all possible strings from
alphabet except
a, b
* , a, b, aa, ab, ba, bb, aaa, aab,
*
a, b, aa, ab, ba, bb, aaa, aab,
63
A language is any subset of
Language *
Example:
a, b
* , a, b, aa, ab, ba, bb, aaa,
Languages:
a, aa, aab
{ , abba, baba, aa, ab, aaaaaa}
64
Another Example
n n
L {a b : n 0}
An infinite language
ab
L abb L
aabb
aaaaabbbbb
65
Operations on Languages
The usual set operations
Complement:
Reverse
R R
L {w : w L}
Definition:
ab, aab, baba ba, baa, abab
R
Examples:
n n
L {a b : n 0}
R n n
L {b a : n 0}
67
Concatenation
L1L2 xy : x L1, y L2
Definition:
Another Operation
n
L LL L
Definition:
n
a, b a, ba, ba, b
3
More Examples
n n
L {a b : n 0}
2 n n m m
L {a b a b : n, m 0}
2
aabbaaabbb L
70
Star-Closure (Kleene *)
0 1 2
Definition: L* L L L
Example: ,
a, bb,
a, bb*
aa , abb , bba , bbbb ,
aaa, aabb, abba, abbbb,
71
Positive Closure
1 2
Definition: L L L
L *
a, bb,
a, bb aa, abb, bba, bbbb,
aaa, aabb, abba, abbbb,
72
Regular Language
The set of regular languages over an alphabet Σ is defined
recursively as below. Any language belonging to this set is a
regular language over .
Definition of Set of Regular Languages :
Basis Clause: , {} and {a} for any symbol a Σ are regular languages.
Inductive Clause: If Lr and Ls are regular languages, then Lr Ls , LrLs
and Lr* are regular languages.
Extremal Clause: Nothing is a regular language unless it is obtained
from the above two clauses.
For example, let Σ = {a, b}. Then since {a} and {b} are regular
languages, {a, b} ( = {a} {b} ) and {ab} ( = {a}{b} ) are regular
languages. Also since {a} is regular, {a}* is a regular language
which is the set of strings consisting of a's such as , a, aa, aaa,
aaaa etc. Note also that Σ*, which is the set of strings consisting
of a's and b's, is a regular language because {a, b} is regular.
73
Example
LM abba M
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
accept
74
Another Example
LM , ab, abba M
a, b
q5
b a a a, b
b
q0 a q1 b q2 b q3 a q4
accept accept accept
75
Formally
For a DFA M Q, , , q , F
0
Language accepted by M
alphabet
transition initial final
function state states
Observation
76
Language accepted by : M
Language rejected by :M
More Examples
LM {a b : n 0}
n
a a, b
q0 b q1 a, b q2
accept trap state
78
a, b
q0 a q1 b q2
b a accept
q3 a, b
79
1 0 0,1
1
0 1
0 00 001
0
80
Regular Languages
L is regular if there is
A language
Example
The language L awa : w a, b*
is regular:
a
b
b
q0 a q2 q3
b a
q4
a, b
82
Some Properties of
Regular Languages
83
Properties
L1 L2
For regular languages
we will prove that:
and
Union:
L1 L2
Are regular
Concatenation:
L1L2 Languages
Star:
L1 *
84
We Say:
Regular languages are closed under
Union:
L1 L2
Concatenation:
L1L2
Star:
L1 *
85
Regular language
L1 Regular language
L2
LM1 L1 LM 2 L2
NFA
M1 NFA
M2
Example M1
a
n
L1 {a b} b
M2
a
L2 ba b
87
UnionL1 L2
NFA for
M1
M2
88
Example n
NFA for
L1 L2 {a b} {ba}
n
L1 {a b}
a
b
L2 {ba}
b a
89
Concatenation
L1L2
NFA for
M1 M2
90
Example
n n
L1L2 {a b}{ba} {a bba}
NFA for
n
L1 {a b}
a L2 {ba}
b b a
91
Star Operation
L1 *
NFA for
L1 *
M1
92
Example
n
L1* {a b} *
NFA for
n
L1 {a b}
a
b
Regular Expressions
94
Example
r a b * a bb
Regular expression
Example
r aa * bb * b
Regular expression
Lr {a b 2n 2m
b : n, m 0}
96
Example
r (0 1) * 00 (0 1) *
Regular expression
Example
r (1 01) * (0 )
Regular expression
Definition:
equivalent if
are
L(r1 ) L(r2 )
99
Example
L = { all strings with at least
two consecutive 0 }
r1 (1 01) * (0 )
r2 (1* 011*) * (0 ) 1* (0 )
r1 and
r2
L(r1 ) L(r2 ) L are equivalent
regular expr.
100
Regular Expressions
and
Regular Languages
101
Theorem
Languages
Generated by Regular
Regular Expressions Languages
Theorem - Part 1 102
Languages
Generated by Regular
Regular Expressions Languages
r
1. For any regular expression
the language is regular
L(r )
Theorem - Part 2 103
Languages
Generated by Regular
Regular Expressions Languages
Proof - Part 1
1. For any regular expression r
the language is regular
L(r )
Induction Basis , ,
NFAs
Primitive Regular Expressions:
L( M1 ) L()
regular
L( M 2 ) {} L( ) languages
L( M 3 ) {a} L(a )
106
Inductive Hypothesis
r1 r2
Assume
107
Inductive Step
Lr1 r2
We will prove:
Lr1 r2
Are regular
Languages
Lr1 *
Lr1
By definition of regular expressions: 108
Lr1 Lr1
By inductive hypothesis we know: 109
L(r1 ) L(r2 )
We also know:
union
Lr1 Lr2
concatenation
Lr1 Lr2
star
Lr1 *
110
Therefore:
Lr1 r2 Lr1 Lr2
Are regular
Lr1 r2 Lr1 Lr2 languages
And trivially:
112
Proof – Part 2
2. For any regular language L
there is
a regular expression
r
with
L(r ) L
NFA
M that accepts it
L( M ) L
M
a c a c
a, b ab
115
b b
a
Another Example: q0 q1 a, b q2
b
b b
a
q0 q1 a b q2
b
116
b b
a
Reducing the states: q0 q1 a b q2
b
bb * a b
q0 bb * (a b) q2
117
bb * a b
q0 bb * (a b) q2
r (bb * a ) * bb * (a b)b *
L(r ) L( M ) L
118
In General e
d c
Removing states:
qi q qj
a b
ae * d ce * b
ce * d
qi qj
ae * b
119
r r1 * r2 (r4 r3r1 * r2 ) *
L(r ) L( M ) L
120
More Properties
of
Regular Languages
121
We have proven
Regular languages are closed under:
Union
Concatenation
Star operation
Reverse
122
Star operation
L1L2 Regular
Reverse Languages
L1
R
L1
123
Complement
Intersection
124
Complement L1
Regular
Intersection Languages
L1 L2
125
L
L L ( a * b)
Example: 126
a a, b
q0 b q1 a, b q2
q0 b q1 a, b q2
127
L1 L2 L1 L2
L1 , L2
128
regular
L1 , L2 regular
L1 L2 regular
L1 L2 regular
L1 L2 regular
129
Standard Representations
of
Regular Languages
Standard Representations 130
of Regular Languages
Regular Languages
DFAs
Regular
Grammars
NFAs
Regular
Expressions
131
Elementary Questions
about
Regular Languages
132
Membership Question
Question:
L
Given regular language
w
and string
how can we check if ?
w L
Answer: L
Take the DFA that accepts
w
and check if is accepted
133
DFA
w
w L
DFA
w
w L
134
Question: Given regular language
how can we check L
if is empty: ?
L ( L )
L
DFA
L
136
Question: Given regular language
how can we check L
if is finite?
L is infinite
DFA
L is finite
138
Question: Given regular languages
how can we check if
and
?
L1 L2
L1 L2
Answer: Find if
( L1 L2 ) ( L1 L2 )
( L1 L2 ) ( L1 L2 ) 139
L1 L2 and
L1 L2
L1 L2 L L2 L1 L1
2
L1 L2 L2 L1
L1 L2
( L1 L2 ) ( L1 L2 ) 140
L1 L2 or
L1 L2
L1 L2 L2 L1
L1 L2 L2 L1
L1 L2
141
Elementary Questions
about
Regular Languages
142
Non-regular languages
143
n n
Non-regular languages
{a b : n 0}
R
{ww : w {a, b}*}
Regular languages
a *b b*c a
b c ( a b) *
etc...
144
regular or not?
L
Prove that there is no DFA that accepts
3 pigeonholes
148
A pigeonhole must
contain at least two pigeons
149
n pigeons
...........
m pigeonholes
nm
...........
150
m pigeonholes
There is a pigeonhole
with at least 2 pigeons
nm
...........
The Pigeonhole Principle 151
and
DFAs
152
b
b b
a b b
q1 q2 q3 q4
a a
In walks of strings:
a no state
is repeated
153
aa
aab
b
b b
a a b
q1 q2 q3 q4
a a
aabb
154
In walks of strings: a state
is repeated
bbaa
abbabb
abbbabbabb...
b
b b
a a b
q1 q2 q3 q4
a a
w
If the walk of string has length
155
| w| 4
then a state is repeated
b
b b
a a b
q1 q2 q3 q4
a a
156
w
If in a walk of a string
transitions states of DFA
then a state is repeated
b
b b
a a b
q1 q2 q3 q4
a a
w
In other words for a string : 157
a
q
b
b b
a a b
q1 q2 q3 q4
a a
158
In general:
w
A string has length number of states
q A state w
must be repeated in the walk
walk of
w
...... ......
q
159
L
Take an infinite regular language
m
states
161
w
Take string
w
with
L
w
There is a walk with label :
.........
walk
w
162
wIf string | w| m
has length number
of states
q a state w
is repeated in the walk
......
q ......
walk
w
163
Write w x y z
......
q ......
x z
length
|x y| m number 164
of states
Observations:
length
| y | 1
......
q ......
x z
165
......
q ......
x z
166
Observation:
xyyz
The string
is accepted
......
q ......
x z
167
Observation: xyyyz
The string
is accepted
......
q ......
x z
168
i
xy z
The string
In General: is accepted
i 0, 1, 2, ...
y
......
q ......
x z
169
L
• there exists an integer
m
w L | w| m
• for any string with length
• we can write
w x y z
h
|x y| m and
| y | 1
i i 0, 1, 2, ...
• such that:
xy z L
171
Applications
of
n n
Theorem: L {a b : n 0}
The language
is not regular
n n
L {a b : n 0}
Assume for contradiction
that is a regular language
L
SinceL is infinite
we can apply the Pumping Lemma
174
n n
L {a b : n 0}
m Let be the integer in the Pumping Lemma
w
Pick a string such that: w L
length
| w| m
m m
Example: pick
wa b
m m
a b xyz
175
Write:
| x y | m, | y | 1
it must be that: length
m m
m m
Therefore:
a b a...aa...a...ab...b
x y z
k
y a , k 1
176
m m
x y za b
We have:
k
y a , k 1
From the Pumping Lemma:
i
xy z L
i 0, 1, 2, ...
Thus:
2
xy z L
2 m k m
xy z xyyz a b L
177
Therefore: m k m
a b L
n n
BUT: L {a b : n 0}
m k m
a b L
CONTRADICTION!!!
178
Therefore: L
Our assumption that
is a regular language is not true
Conclusion:
n n
Non-regular languages
{a b : n 0}
Regular languages
a *b b*c a
b c ( a b) *
etc...
180
Is the language
regular or not?
regular or not?
Theorem: n!
L {a : n 0}
The language
is not regular
n! 1 2 (n 1) n
The language
Theorem:
{a, b}
is not regular
Proof:
Use the Pumping Lemma
184
The language
Theorem:
R
L {ww : w *} {a, b}
is not regular
Proof:
Use the Pumping Lemma
185
The language
Theorem:
is not regular
The language
n l n l
Theorem: L {a b c : n, l 0}
is not regular
Is the language
regular or not?
Is the language
regular or not?
regular or not?
is not regular
R
n n
{a b } {ww }
Regular Languages
192
Context-Free Languages
R
n n
{a b } {ww }
Regular Languages
193
Context-Free Languages
Context-Free Pushdown
Grammars Automata
stack
automaton