Date post: | 02-Jan-2016 |
Category: |
Documents |
Upload: | ruth-knight |
View: | 73 times |
Download: | 4 times |
CSCI 3130: Automata theory and formal languages
Andrej Bogdanov
http://www.cse.cuhk.edu.hk/~andrejb/csc3130
The Chinese University of Hong Kong
NFA to DFA conversionand regular expressions
Fall 2011
NFAs are as powerful as DFAs
• An NFA can do everything a DFA can do
• How about the other way?
Every NFA can be converted intoa DFA for the same language.
YES
NFA → DFA in two easy steps
We do this first
➊ Eliminate -transitions
➋ Convert simplified NFA
NFA → DFA: intuition
1 0
0, 1
q q qNFA:
DFA: 1q q0 or q1
1
q0 or q2
1
0 0
0
NFA → DFA: intuition
1 0
0, 1
q q qNFA:
DFA: 1q {q0, q1}
1
{q0, q2}
1
0 0
0
NFA → DFA: states
1 0
0, 1
q q qNFA:
DFA: {q0, q1}
{q0, q2} {q0, q1, q2}
{q1, q2}
{q0}
{q1}
{q2}
DFA has a state for every subset of NFA states
NFA → DFA: transitions
1 0
0, 1
q q qNFA:
DFA: {q0, q1}
{q0, q2} {q0, q1, q2}
{q1, q2}
{q0}
{q1}
{q2}
0
1
0
1
0, 1
0
1
01
0
1
0
10, 1
NFA → DFA: accepting states
1 0
0, 1
q q qNFA:
DFA: {q0, q1}
{q0, q2} {q0, q1, q2}
{q1, q2}
{q0}
{q1}
{q2}
0
1
0
1
0, 1
0
1
01
0
1
0
10, 1
NFA accepts if it contains a DFA final state
NFA → DFA: dead state elimination
1 0
0, 1
q q qNFA:
DFA: {q0, q1}
{q0, q2}
{q0}
0
1
0
1
01
{q0, q1, q2}0
1
0
{q1, q2}
{q1}
{q2}
0
1
, 10
1
0, 1
At the end, you can eliminate the unreachable states
General method
NFA DFAstates q0, q1, …,
qn
q0}, {q1}, {q0,q1}, …, {q0,…,qn}
one for each subset of states in the NFA
initial state
q0 q0}
transitions
’({qi1,…,qik}, a) =
(qi1, a) ∪…∪ (qik, a)
accepting states
F Q F ’ = {S: S contains some state in F}
NFA → DFA in two easy steps
➊ Eliminate -transitions
➋ Convert simplified NFA ✔
Eliminating -transitions
q0 q1 q2
, 1 0
0
NFA:
NFA without s:
{q1, q2}
0 1
q0
q1
q2
{q0, q1, q2}
{q0, q1, q2}
Accepting states:q2, q1, q0
Eliminating -transitions
NFA:
new NFA:
q0 q1 q2
, 1 0
0
q0 q1 q20, 1
0
0
0, 1
00
{q1, q2}
0 1
q0
q1
q2
{q0, q1, q2}
{q0, q1, q2}
Eliminating -transitions
• Paths with s are replaced by a single transition
• States that can reach final state by are all accepting
q5 q0 q2 q0 q3 a
q4
q3
q5
a
q5 q3a q3
a
q9 q7 q3 q2
Regular expressions
String concatenation
st = abbbabs = abb t = bab
s = x1…xn st = x1…xny1…ymt = y1…ym
ts = bababbss = abbabbsst = abbabbbab
Operations on languages
• The concatenation of languages L1 and L2 is
• The n-th power of Ln is
• The union of L1 and L2 is
L1L2 = {st: s L1, t L2}
Ln = {s1s2...sn: s1, s2, ..., sn L}
L1 L2 = {s: s L1 or s L2}
Example
L1 = {0, 01}
L2 = {, 1, 11, 111, …}
L1L2
L12 L2
2
L1 L2
= {0, 01, 011, 0111, …} {01, 011, 0111, …}
= {0, 01, 011, 0111, …}
0 followed by any number of 1s
= L2 L2
n= L2 (n ≥ 1)= {00, 001, 010, 0101}
any number of 1s
= {0, 01, ,1, 11, 111, ...}
Operations on languages
• The star of L are all strings made up of zero or more chunks from L:
• Example: L1 = {01, 0}, L2 = {, 1, 11, 111, …}. What is L1
* and L2*?
L* = L0 L1 L2 …
Example
L1 = {0, 01}
L10= {}
L1*:00100001
00110001
is in L1*
is not in L1*
L1* are all strings that
start with 0 and do not contain consecutive 1s
10010001is not in L1*
L12= {00, 001, 010, 0101}
L11= {0, 01}
L13= {000, 0001, 0010, 00101,
0100, 01001, 01010, 010101}
(plus the empty string)
Example
L2 = {, 1, 11, 111, …}
L20= {}
L21 = L2
any number of 1s
L2*
= L20 L2
1 L22 …
= {} L21 L2
2 …
= L2
L2* =
L2
L22 = L2
L2n= L2 (n ≥ 1)
Combining languages
• We can construct languages by starting with simple ones, like {0}, {1} and combining them
{0}({0}{1})*
({0}{1}*)({1}{0}*)
0(0 + 1)*
01* + 10*
all strings that start with 0
0 followed by any number of 1s, or1 followed by any number of 0s
Regular expressions
• A regular expression over is an expression formed using the following rules:– The symbols and are regular expressions– Every a in is a regular expression– If R and S are regular expressions, so are R+S, RS
and R*.
A language is regular if it is represented by a regular expression
0(0 + 1)*01* + 10*
1*( + 0)
(0 + 1)*01(0 + 1)*
Analyzing regular expressions
01*
(01*)(01)
0 followed by any number of 1s
= 0(1*)= {0, 01, 011, 0111, …}
= {001, 0101, 01101, 011101, …}
0 followed by any number of 1s and then 01
= {0, 1}
Analyzing regular expressions
(0+1)*= {, 0, 1, 00, 01, 10, 11, …} any string
0+1 = {0, 1}
(0+1)*01(0+1)*
strings of length 1
any string that contatins the pattern 01
(0+1)*010 any string that ends in 010
Analyzing regular expressions
(0+1)(0+1)
(0+1)(0+1)(0+1)
strings of length 2
strings of length 3
((0+1)(0+1))*+((0+1)(0+1)(0+1))*
((0+1)(0+1))* strings of even length
((0+1)(0+1)(0+1))* strings of length a multiple of 3
all strings whose length is even or a mutliple of 3= strings of length 0, 2, 3, 4, 6, 8, 9, 10, 12, ...
Analyzing regular expressions
((0+1)(0+1)+(0+1)(0+1)(0+1))*
(0+1)(0+1)
(0+1)(0+1)(0+1)
strings of length 2
strings of length 3
(0+1)(0+1)+(0+1)(0+1)(0+1)strings of length 2 or 3
strings that can be broken in blocks, where each block has length 2 or 3
Analyzing regular expressions
((0+1)(0+1)+(0+1)(0+1)(0+1))*
strings that can be broken in blocks, where each block has length 2 or 3
0011010 011 1 011010110✓ ✓ ✓ ✓ ✓✗
this includes all strings except those of length 1
((0+1)(0+1)+(0+1)(0+1)(0+1))*= all strings except 0 and 1
Analyzing regular expressions
(1+01+001)*(+0+00)
ends in at most two 0s
there can be at most two 0s betweenconsecutive 1s
Guess: (1+01+001)*(+0+00) = {x: x does not contain 000}
there are never three consecutive 0s
0110010110 001001000
Writing regular expressions
• Write a regular expression forall strings with two consecutive 0s.
= {0, 1}
(0+1)*00(0+1)*
(anything) 00 (anything else)
Writing regular expressions
• Write a regular expression forall strings that do not contain two consecutive 0s.
= {0, 1}
... every middle 0 followed by one or more 1s
... and at most one 0 in the last block
(011*)
( + 0)
1*(011*)*( + 0)
1110110101101010every 0
followed byone or more 1s
maybe a 0 at the endsome 1s at the beginning
Writing regular expressions
• Write a regular expression forall strings with an even number of 0s.
= {0, 1}
even number of zeros = (two zeros)*
two zeros = 1*01*01*
(1*01*01*)*