320 likes | 532 Views
Fall 2011. The Chinese University of Hong Kong. CSCI 3130: Automata theory and formal languages. NFA to DFA conversion and regular expressions. Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130. NFAs are as powerful as DFAs. An NFA can do everything a DFA can do
E N D
Fall 2011 The Chinese University of Hong Kong CSCI 3130: Automata theory and formal languages NFA to DFA conversionand regular expressions Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130
NFAs are as powerful as DFAs • An NFA can do everything a DFA can do • How about the other way? YES Every NFA can be converted into a DFA for the same language.
NFA → DFA in two easy steps ➊Eliminate e-transitions ➋ Convert simplified NFA We do this first
NFA → DFA: intuition 0, 1 1 0 NFA: q0 q1 q2 0 0 0 1 q0 or q1 q0 or q2 DFA: q0 1 1
NFA → DFA: intuition 0, 1 1 0 NFA: q0 q1 q2 0 0 0 1 {q0, q1} {q0, q2} DFA: q0 1 1
NFA → DFA: states 0, 1 1 0 NFA: q0 q1 q2 DFA: {q0} {q0, q1} Æ {q0, q2} {q0, q1, q2} {q1} {q2} {q1, q2} DFA has a state for every subset of NFA states
NFA → DFA: transitions 0, 1 1 0 NFA: q0 q1 q2 1 0 1 DFA: 1 0, 1 0 1 {q0} {q0, q1} 0 1 0 Æ {q0, q2} {q0, q1, q2} {q1} 0 0 , 1 0 {q2} {q1, q2} 1
NFA → DFA: accepting states 0, 1 1 0 NFA: q0 q1 q2 1 0 1 DFA: 1 0, 1 0 1 {q0} {q0, q1} 0 1 0 Æ {q0, q2} {q0, q1, q2} {q1} 0 0 , 1 0 {q2} {q1, q2} 1 NFA accepts if it contains a DFA final state
NFA → DFA: dead state elimination 0, 1 1 0 NFA: q0 q1 q2 1 0 1 DFA: {q0} {q0, q1} 1 0, 1 0 1 0 1 Æ {q0, q2} {q0, q1, q2} {q1} 0 0 0 , 1 0 {q2} {q1, q2} 1 At the end, you can eliminate the unreachable states
NFA → DFA in two easy steps ➊Eliminate e-transitions ➋ Convert simplified NFA ✔
Eliminating e-transitions 0 , 1 q0 q1 q2 NFA: 0 NFA withouts: 0 1 q0 {q0, q1, q2} {q1, q2} q1 {q0, q1, q2} Æ q2 Æ Æ q2 , q1 , q0 Accepting states:
Eliminating e-transitions 0 , 1 q0 q1 q2 NFA: 0 new NFA: 0 1 q0 {q0, q1, q2} {q1, q2} q1 {q0, q1, q2} Æ q2 Æ Æ 0 0 0, 1 0 q0 q1 q2 0 0, 1
Eliminating e-transitions • Paths with es are replaced by a single transition • States that can reach final state by e are all accepting q4 e a q3 e e e a q5 q0 q2 q0 q3 e q5 a a q5 q3 q3 e e e q9 q7 q3 q2
String concatenation s = abbt = bab st = abbbab ts = bababb ss = abbabb sst = abbabbbab s = x1…xn st = x1…xny1…ym t = y1…ym
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: sL1, tL2} Ln = {s1s2...sn: s1, s2, ..., snL} L1 L2 = {s: sL1orsL2}
Example L1 = {0, 01} L2 = {e, 1, 11, 111, …} any number of 1s = {0, 01, 011, 0111, …} {01, 011, 0111, …} L1L2 = {0, 01, 011, 0111, …} 0 followed by any number of 1s L12 = {00, 001, 010, 0101} L22 = L2 (n ≥ 1) L2n = L2 L1 L2 = {0, 01, e,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 = {e, 1, 11, 111, …}.What isL1* and L2*? L* = L0L1L2 …
Example L1 = {0, 01} L1*: 00100001 is in L1* is not in L1* 00110001 is not in L1* 10010001 L10 = {e} L1*are all strings that start with 0 and do not contain consecutive 1s L11 = {0, 01} L12 = {00, 001, 010, 0101} (plus the empty string) L13 = {000, 0001, 0010, 00101, 0100, 01001, 01010, 010101}
Example L2 = {e, 1, 11, 111, …} any number of 1s L20 = {e} L2* = L20L21L22 … L21 = L2 = {e}L21L22 … L22 = L2 = L2 (n ≥ 1) L2n = L2 L2*=L2
Combining languages • We can construct languages by starting with simple ones, like {0}, {1} and combining them {0}({0}{1})* 0(0 + 1)* all strings that start with 0 ({0}{1}*)({1}{0}*) 01* + 10* 0 followed by any number of 1s, or1 followed by any number of 0s
Regular expressions • A regular expression over S is an expression formed using the following rules: • The symbols Æ andeare regular expressions • Every ainS is a regular expression • If R and S are regular expressions, so are R+S, RS and R*. 1*(e + 0) Æ 0(0 + 1)* (0 + 1)*01(0 + 1)* 01* + 10* e A language is regular if it is represented by a regular expression
Analyzing regular expressions S = {0, 1} 01* = 0(1*) = {0, 01, 011, 0111, …} 0 followed by any number of 1s (01*)(01) = {001, 0101, 01101, 011101, …} 0 followed by any number of 1s and then 01
Analyzing regular expressions strings of length 1 0+1 = {0, 1} (0+1)* = {e, 0, 1, 00, 01, 10, 11, …} any string (0+1)*010 any string that ends in 010 (0+1)*01(0+1)* any string that contatins the pattern 01
Analyzing regular expressions ((0+1)(0+1))*+((0+1)(0+1)(0+1))* all strings whose length is even or a mutliple of 3 = strings of length 0, 2, 3, 4, 6, 8, 9, 10, 12, ... ((0+1)(0+1))* strings of even length (0+1)(0+1) strings of length 2 ((0+1)(0+1)(0+1))* strings oflength a multiple of 3 (0+1)(0+1)(0+1) strings of length 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 (0+1)(0+1)+(0+1)(0+1)(0+1) strings of length 2 or 3 (0+1)(0+1) strings of length 2 (0+1)(0+1)(0+1) strings of length 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 e ✓ 1 10 011 00110 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 there are never three consecutive 0s Guess: (1+01+001)*(+0+00) = {x: x does not contain 000} 0110010110 e 00 0010010
Writing regular expressions • Write a regular expression forall strings with two consecutive 0s. S = {0, 1} (anything) 00 (anything else) (0+1)*00(0+1)*
Writing regular expressions • Write a regular expression forall strings that do not contain two consecutive 0s. S = {0, 1} 1110110101101010 some 1s at the beginning every 0followed byone or more 1s maybe a 0 at the end (011*) ... everymiddle0 followed by one or more 1s (e + 0) ... and at most one 0 in the last block 1*(011*)*(e + 0)
Writing regular expressions • Write a regular expression forall strings with an even number of 0s. S = {0, 1} even number of zeros = (two zeros)* two zeros = 1*01*01* (1*01*01*)*