1 / 32

CSCI 3130: Automata theory and formal languages

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

ruth-knight
Download Presentation

CSCI 3130: Automata theory and formal languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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.

  3. NFA → DFA in two easy steps ➊Eliminate e-transitions ➋ Convert simplified NFA We do this first

  4. 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

  5. NFA → DFA: intuition 0, 1 1 0 NFA: q0 q1 q2 0 0 0 1 {q0, q1} {q0, q2} DFA: q0 1 1

  6. 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

  7. 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

  8. 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

  9. 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

  10. General method

  11. NFA → DFA in two easy steps ➊Eliminate e-transitions ➋ Convert simplified NFA ✔

  12. Eliminating e-transitions 0 , 1  q0 q1 q2 NFA: 0 NFA withouts: 0 1 q0 {q0, q1, q2} {q1, q2} q1 {q0, q1, q2} Æ q2 Æ Æ q2 , q1 , q0 Accepting states:

  13. 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

  14. 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

  15. Regular expressions

  16. String concatenation s = abbt = bab st = abbbab ts = bababb ss = abbabb sst = abbabbbab s = x1…xn st = x1…xny1…ym t = y1…ym

  17. 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L1orsL2}

  18. 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, ...}

  19. 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* = L0L1L2 …

  20. 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}

  21. Example L2 = {e, 1, 11, 111, …} any number of 1s L20 = {e} L2* = L20L21L22 … L21 = L2 = {e}L21L22 … L22 = L2 = L2 (n ≥ 1) L2n = L2 L2*=L2

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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)*

  31. 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)

  32. 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*)*

More Related