Title: FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
115-453
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
(For next time Read Chapter 1.3 of the book)
21
q4
q2
0
e
q3
0
q1
3A non-deterministic finite automaton (NFA) is a
5-tuple N (Q, S, ?, Q0, F)
Q is the set of states
S is the alphabet
? Q ? Se ? 2Q is the transition function
Q0 ? Q is the set of start states
F ? Q is the set of accept states
2Q is the set of subsets of Q and Se S ? e
4N (Q, S, ?, Q0, F)
1
q4
q2
Q q1, q2, q3, q4
S 0,1
0
Q0 q1, q2
e
q3
F q4 ? Q
0
0 1 e
q1
q2
q3
q4
q1
5N (Q, S, ?, Q0, F)
1
q4
q2
Q q1, q2, q3, q4
S 0,1
0
Q0 q1, q2
e
q3
F q4 ? Q
0
0 1 e
q1 q3 ? ?
q2 ? q4 ?
q3 q4 ? q2
q4 ? ? ?
q1
6N (Q, S, ?, Q0, F)
1
q4
q2
Q q1, q2, q3, q4
S 0,1
0
Q0 q1, q2
e, 0
q3
F q4 ? Q
0
0 1 e
q1 q3 ? ?
q2 ? q4 ?
q3 q2,q4 ? q2
q4 ? ? ?
q1
00 ? L(N)? 01 ? L(N)?
7- Let w? S and suppose w can be written as
- w1... wn where wi ? Se (e is viewed as
representing the empty string) - Then N accepts w if there are r0, r1, ..., rn ? Q
such that - r0 ? Q0
- ri1 ? ?(ri, wi1 ) for i 0, ..., n-1, and
- rn ? F
L(N) the language of machine N set of all
strings machine N accepts
A language L is recognized by an NFA N if L L
(N).
8N (Q, S, ?, Q0, F)
1
q4
q2
Q q1, q2, q3, q4
S 0,1
0
Q0 q1, q2
e, 0
q3
F q4 ? Q
0
0 1 e
q1 q3 ? ?
q2 ? q4 ?
q3 q2,q4 ? q2
q4 ? ? ?
q1
00 ? L(N)? 01 ? L(N)?
9FROM NFA TO DFA
Input N (Q, S, ?, Q0, F)
Output M (Q?, S, ??, q0?, F?)
Q? 2Q
?? Q? ? S ? Q?
q0? e(Q0)
F? R ? Q? f ? R for some f ? F
For R ? Q, the e-closure of R, e(R) q that
can be reached from some r ? R by traveling along
zero or more e arrows,
10Given NFA N ( 1,2,3, a.b, ? , 1, 1
) Construct equivalent DFA M
N
1
a
b
a
e
a , b
2
3
e(1) 1,3
11N ( Q, S, ?, Q0, F )
Given NFA N ( 1,2,3, a,b, ? , 1, 1
) Construct equivalent DFA M
(Q?, S, ??, q0?, F?)
N
?? a b
? ? ?
1 ? 2
2 2,3 3
3 1,3 ?
1,2 2,3 2,3
1,3 1,3 2
2,3 1,2,3 3
1,2,3 1,2,3 2,3
1
a
b
a
e
a, b
2
3
q0?
e(1) 1,3
12N ( Q, S, ?, Q0, F )
Given NFA N ( 1,2,3, a,b, ? , 1, 1
) Construct equivalent DFA M
(Q?, S, ??, q0?, F?)
N
?? a b
? ? ?
1 ? 2
2 2,3 3
3 1,3 ?
1,2 2,3 2,3
1,3 1,3 2
2,3 1,2,3 3
1,2,3 1,2,3 2,3
1
a
b
a
e
a, b
2
3
q0?
e(1) 1,3
13REGULAR LANGUAGES CLOSED UNDER STAR
Let L be a regular language and M be a DFA for L
We construct an NFA N that recognizes L
e
e
e
14Formally
DFA
Input M (Q, S, ?, q1, F)
Output N (Q?, S, ??, q0, F?)
NFA
Q? Q ? q0
F? F ? q0
?(q,a)
if q ? Q and a ? e
q1
if q ? F and a e
if q q0 and a e
q1
??(q,a)
if q q0 and a ? e
?
else
?
15- Show L(N) L
- L(N) ? L
- L(N) ? L
161. L(N) ? L
Assume w w1wk is in L, where w1,,wk ? L
We show N accepts w by induction on k
Base Cases
?
(w e)
k 0
?
(w ? L)
k 1
Inductive Step
Assume N accepts all strings v v1vk ? L, vi ?
L and let u u1ukuk1 ? L , vj? L
Since N accepts u1uk (by induction) and M
accepts uk1, N must accept u
172. L(N) ? L
Assume w is accepted by N, we show w ? L
If w e, then w ? L
e
If w ? e
? L
By induction
e
? L
By induction
accept
18REGULAR LANGUAGES ARE CLOSED UNDER REGULAR
OPERATIONS
Union A ? B w w ? A or w ? B
Intersection A ? B w w ? A and w ? B
Negation ?A w ? S w ? A
Reverse AR w1 wk wk w1 ? A
Concatenation A ? B vw v ? A and w ? B
Star A w1 wk k 0 and each wi ? A
19The PUMPING LEMMA and REGULAR EXPRESSIONS
20SOME LANGUAGES ARE NOT REGULAR
B 0n1n n 0 is NOT regular!
21WHICH OF THESE ARE REGULAR
C w w has equal number of 1s and 0s
NOT REGULAR
D w w has equal number of occurrences of
01 and 10
REGULAR!!!
22THE PUMPING LEMMA
Let L be a regular language with L ?
Then there exists a positive integer P such that
if w ? L and w P then w xyz, where
1. y gt 0 2. xy P 3. xyiz ? L for any i
0
23Let M be a DFA that recognizes L
Let P be the number of states in M
Assume w ? L is such that w P
1. y gt 0 2. xy P 3. xyiz ? L for any i
0
We show w xyz
x
q0
qi
qj
There must be j gt i such that qi qj
24USING THE PUMPING LEMMA
Use the pumping lemma to prove that B 0n1n
n 0 is not regular
Hint Assume B is regular Let B L(M), for DFA
M, and let P be larger than the number of states
in M Try pumping s 0P1P
25USING THE PUMPING LEMMA
Use the pumping lemma to prove that B 0n1n
n 0 is not regular
Hint Assume B is regular, and try pumping s
0P1P
If B is regular, s can be split into s xyz,
where for any i 0, xyiz is also in B
xyyz has more 0s than 1s
If y is all 0s
xyyz has more 1s than 0s
If y is all 1s
If y has both 1s and 0s
xyyz will have some 1s before some 0s
26Use the pumping lemma to prove that C w w
has an equal number of 0s and 1s is not regular
Hint Try pumping s 0P1P
If C is regular, s can be split into s xyz,
where for any i 0, xyiz is also in C and xy
P
27WHAT DOES D LOOK LIKE?
D w w has equal number of
occurrences of 01 and 10
w w 1, w 0, w e or w starts with a
0 and ends with a 0 or w starts with a 1 and
ends with a 1
(0(0?1)0) ? (1(0?1)1) ? 1 ? 0 ? e
28REGULAR EXPRESSIONS
- is a regular expression representing ?
e is a regular expression representing e
? is a regular expression representing ?
If R1 and R2 are regular expressions representing
L1 and L2 then
(R1R2) represents L1?L2
(R1 ? R2) represents L1 ? L2
(R1) represents L1
29PRECEDENCE
Tightest Star () Concatenation (.,
) Loosest Union (?, , )
30EXAMPLE
R2
R1
(
R1R2 ? R3
(
)
)
?
R3
31 w w has exactly a single 1
010
32 What language does ? represent?
e
33 w w has length 3 and its 3rd symbol is 0
000(0?1) ? 010(0?1) ? 100(0?1) ? 110(0?1)
(0?1)(0?1)0(0?1)
34 w every odd position of w is a 1
1((0?1)1)(0?1?e) ? e
35EQUIVALENCE
L can be represented by a regexp ? L is a regular
language
36L can be represented by a regexp ? L is a regular
language
37Base Cases (R has length 1)
R ? (matches a single symbol)
R e (matches the empty string)
R ? (matches nothing)
38Inductive Step
Assume R has length k gt 1 and that any regular
expression of length lt k represents a language
that can be recognized by an NFA
Three possibilities for R
R R1 ? R2
(Union Theorem!)
R R1 R2
R (R1)
39Have Shown
L can be represented by a regexp ? L is a regular
language
40Transform (1(0 ? 1)) to an NFA
1
e
1,0
e
41L can be represented by a regexp ? L is a regular
language
?
42L can be represented by a regexp ? L is a regular
language
?
Proof idea Transform an NFA for L into a regular
expression by removing states and re-labeling the
arrows with regular expressions
43NFA
Add unique and distinct start and accept states
While machine has more than 2 states
Pick an internal state, rip it out and re-label
the arrows with regexps, to account for the
missing state
44NFA
While machine has more than 2 states
Pick an internal state, rip it out and re-label
the arrows with regexps, to account for the
missing state
45q3
q0
R(q0,q3) (ab)(a?b)
46q3
q0
R(q0,q3) (ab)(a?b)
47q3
q0
R(q0,q3) (ab)(a?b)
48b
bb
a
q2
q1
a
e
b
e
b
a
e
q3
49b
bb
a
q2
q1
a ? ba
a
e
b
e
b
a
e
50b
R(q1,q1)
bb ? (a ? ba)ba
bb
a
q2
q1
a ? ba
e
e
b ? (a ? ba)b
b
(bb ? (a ? ba)ba)
(b ? (a ? ba)b)
51Convert the NFA to a regular expression
a, b
(a ? b)bb(bbb)
b
q2
q1
(a ? b)bb
e
b
(a ? b)bb(bbb)a
bbb
a
b
e
q3
e
52((a ? b)bb(bbb)a) ? ((a ? b)bb(bbb)a)(a
? b)bb(bbb)
53Formally
Add qstart and qaccept to create G
Run CONVERT(G) (return regexp)
If states 2
return the expression on the arrow going from
qstart to qaccept
If states gt 2
54Formally
Add qstart and qaccept to create G
Run CONVERT(G) (return regexp)
If states gt 2
select qrip?Q different from qstart and qaccept
Defines G? (GNFA)
define Q? Q qrip
define R? as
R?(qi,qj) R(qi,qrip)R(qrip,qrip)R(qrip,qj) ?
R(qi,qj)
return CONVERT(G?)
55CONVERT(G) is equivalent to G
Proof by induction on k (number of states in G)
Base Case
k 2
?
Inductive Step
Assume claim is true for k-1 states
We first note that G and G? are equivalent
But, by the induction hypothesis, G? is
equivalent to CONVERT(G?)
And CONVERT(G?) is equivalent to CONVERT(G ) QED
56 e
57NFA
DFA
DEF
Regular Language
Regular Expression
58Read Chapter 1.3 of the book for next time