cse 311 foundations of computing
play

cse 311: foundations of computing Fall 2015 Lecture 24: DFAs, NFAs, - PowerPoint PPT Presentation

cse 311: foundations of computing Fall 2015 Lecture 24: DFAs, NFAs, and regular expressions highlights FSMs with output at states State minimization 0 2 0 S0 S1 [1] [0] 1 2 2 3 0 3 0 S0 S1 0 1 1 [1] [0] 1 0 2


  1. cse 311: foundations of computing Fall 2015 Lecture 24: DFAs, NFAs, and regular expressions

  2. highlights • FSMs with output at states • State minimization 0 2 0 S0 S1 [1] [0] 1 2 2 3 0 3 0 S0 S1 0 1 1 [1] [0] 1 0 ⇒ 2 S2 S3 3 0 1 1,3 [1] [0] 3 1,2 2 0 2 3 3 S2 S3 1 1 [1] [0] 2 2 2 3 3 S4 S5 0 0 [1] [0] 1 3

  3. highlights Lemma: The language recognized by a DFA is the set of strings x that label some path from its start state to one of its final states 0 0 1 1 1 s 3 s 0 s 1 s 2 0 0,1

  4. nondeterministic finite automaton (NFA) • Graph with start state, final states, edges labeled by symbols (like DFA) but – Not required to have exactly 1 edge out of each state labeled by each symbol--- can have 0 or >1 – Also can have edges labeled by empty string ɛ • Definition: x is in the language recognized by an NFA if and only if x labels a path from the start state to some final state 1 1 1 s 3 s 0 s 1 s 2 0,1 0,1

  5. building an NFA binary strings that have - an even # of 1’s - or contain the substring 111 or 1000

  6. NFAs and regular expressions Theorem: For any set of strings (language) 𝐵 described by a regular expression, there is an NFA that recognizes 𝐵 . Proof idea: Structural induction based on the recursive definition of regular expressions... 𝐵 ∪ 𝐶 𝐵 ∗ 𝐵𝐶

  7. build an NFA for (01  1)*0

  8. solution (01  1)*0 ɛ 0 1 ɛ ɛ ɛ ɛ 0 ɛ ɛ ɛ ɛ 1

  9. NFAs vs. DFAs Every DFA is an NFA – DFAs have requirements that NFAs don’t have Can NFAs recognize more languages?

  10. NFAs vs. DFAs Every DFA is an NFA – DFAs have requirements that NFAs don’t have Can NFAs recognize more languages? No! Theorem: For every NFA there is a DFA that recognizes exactly the same language.

  11. conversion of NFAs to DFAs Proof Idea: – The DFA keeps track of ALL the states that the part of the input string read so far can reach in the NFA – There will be one state in the DFA for each subset of states of the NFA that can be reached by some string

  12. conversion of NFAs to a DFAs New start state for DFA – The set of all states reachable from the start state of the NFA using only edges labeled ɛ f ɛ a,b,e,f ɛ a b ɛ e NFA DFA

  13. conversion of NFAs to a DFAs For each state of the DFA corresponding to a set S of states of the NFA and each letter a – Add an edge labeled a to state corresponding to T, the set of states of the NFA reached by starting from some state in S, then following one edge labeled by a , and then following some number of edges labeled by ɛ – T will be  if no edges from S labeled a exist 1 f d ɛ ɛ 1 g 1 b b,e,f c,d,e,g c S ɛ T e 1 1

  14. conversion of NFAs to a DFAs Final states for the DFA – All states whose set contain some final state of the NFA c e a,b,c,e a b DFA NFA

  15. example: NFA to DFA a ɛ 1 0 b c 0 0,1 NFA DFA

  16. example: NFA to DFA a,b a ɛ 1 0 b c 0 0,1 NFA DFA

  17. example: NFA to DFA 0 a,b a 1 ɛ c 1 0 b c 0 0,1 NFA DFA

  18. example: NFA to DFA 0 a,b a 1 ɛ 1 b c 1 0 b c 0 0 0,1 b,c NFA DFA

  19. example: NFA to DFA 0  a,b a 1 0 1 ɛ 1 b c 1 0 b c 0 0 0,1 b,c NFA DFA

  20. example: NFA to DFA 0,1 0  a,b a 1 0 1 ɛ 1 b c 1 0 b c 0 0 0,1 b,c NFA DFA

  21. example: NFA to DFA 0,1 0  a,b a 1 0 1 ɛ 1 b c 1 0 b c 1 0 0 0,1 b,c a,b,c 0 NFA DFA

  22. example: NFA to DFA 0,1 0  a,b a 1 0 1 ɛ 1 b c 1 0 b c 1 0 0 0 0,1 1 b,c a,b,c 0 NFA DFA

  23. exponential blow-up in simulating mondeterminism • In general the DFA might need a state for every subset of states of the NFA – Power set of the set of states of the NFA – n-state NFA yields DFA with at most 2 n states – We saw an example where roughly 2 n is necessary Is the n th char from the end a 1? • The famous “P=NP?” question asks whether a similar blow - up is always necessary to get rid of nondeterminism for polynomial-time algorithms

  24. 1 in third position from end 1 0,1 0,1 D A B C 0,1

  25. 1 in third position from end 1 0,1 0,1 D A B C 0,1 1 {A, B, C, D} 1 {A, B, C} 0 0 1 {A, C, D} 0 1 {A} {A, B} 1 1 0 0 {A, B, D} 1 0 {A, C} 0 1 {A, D} 0

  26. 1 in third position from end 1 0,1 0,1 D A B C 0,1 1 {A,B,C} {A,B} 0 1 1 1 1 1 0 1 0 {A,B,C,D} {A} {A,C} {A,B,D} 0 1 0 0 0 {A,D} {A,C,D} 0

  27. DFAs ≡ regular expressions We have shown how to build an optimal DFA for every regular expression – Build NFA – Convert NFA to DFA using subset construction – Minimize resulting DFA Theorem: A language is recognized by a DFA if and only if it has a regular expression. We show the other direction of the proof at the end of these lecture slides.

  28. languages and machines! All Context-Free Regular DFA 0* NFA Regex Finite {001, 10, 12}

  29. languages and machines! All Context-Free Warmup: All finite Regular DFA languages are 0* NFA regular. Regex Finite {001, 10, 12}

  30. DFAs recognize any finite language Exercise: Hard code it into the NFA.

  31. languages and machines! All Context-Free Warmup 2: Surprising Regular DFA example here 0* NFA Regex Finite {001, 10, 12}

  32. languages and machines! All Context-Free Main Event: ??? Prove there is Regular DFA a context-free 0* NFA language that Regex isn’t regular. Finite {001, 10, 12}

  33. DFAs ≡ regular expressions Theorem: A language is recognized by a DFA if and only if it has a regular expression Proof: We already saw: RegExp → NFA → DFA Now: NFA → RegExp (Enough to show this since every DFA is also an NFA.)

  34. generalized NFAs • Like NFAs but allow – Parallel edges – Regular Expressions as edge labels NFAs already have edges labeled ɛ or a • An edge labeled by A can be followed by reading a string of input chars that is in the language represented by A • A string x is accepted iff there is a path from start to final state labeled by a regular expression whose language contains x

  35. starting from an NFA Add new start state and final state ɛ ɛ ɛ Then eliminate original states one by one, keeping the same language, until it looks like: A Final regular expression will be A

  36. only two simplification rules • Rule 1: For any two states q 1 and q 2 with parallel edges (possibly q 1 =q 2 ), replace A A ⋃ B by q 2 q 2 q 1 q 1 B • Rule 2: Eliminate non-start/final state q 3 by replacing all B A C AB*C by q 1 q 3 q 2 q 2 q 1 for every pair of states q 1 , q 2 (even if q 1 =q 2 )

  37. converting an NFA to a regular expression Consider the DFA for the mod 3 sum – Accept strings from {0,1,2}* where the digits mod 3 sum of the digits is 0 0 t 1 1 1 2 2 0 0 2 t 0 t 2 1

  38. splicing out a node Label edges with regular expressions t 0 → t 1 → t 0 : 10*2 t 0 → t 1 → t 2 : 10*1 0 t 2 → t 1 → t 0 : 20*2 t 1 t 2 → t 1 → t 2 : 20*1 s 1 1 ɛ 2 2 0 0 2 t 0 t 2 1 ɛ f

  39. finite automaton without t 1 s R 1 : 0 ∪ 10*2 ɛ R 2 : 2 ∪ 10*1 R 4 R 2 t 0 R 3 : 1 ∪ 20*2 R 1 t 2 R 4 : 0 ∪ 20*1 R 3 ɛ f s R 5 : R 1 ∪ R 2 R 4 *R 3 ɛ ɛ R 5 f t 0 Final regular expression: (0 ∪ 10*2 ∪ (2 ∪ 10*1)(0 ∪ 20*1)*(1 ∪ 20*2))*

Recommend


More recommend