230 likes | 240 Views
L ECTURE 4 Last time: Equivalence of NFAs and DFAs Closure properties of regular languages Today : Finish closure properties Equivalence of NFAs, DFAs and regular expressions. Intro to Theory of Computation. CS 332. Sofya Raskhodnikova. Operations on languages.
E N D
LECTURE 4 • Last time: • Equivalence of NFAs and DFAs • Closure properties of regular languages • Today: • Finish closure properties Equivalence of NFAs, DFAs and regular expressions Intro to Theory of Computation CS 332 Sofya Raskhodnikova SofyaRaskhodnikova; based on slides by Nick Hopper
Operations on languages Complement: A = { w | w A } Union: A B = { w | w A or w B } Intersection: A B = { w | w A and w B } 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 } Sofya Raskhodnikova; based on slides by Nick Hopper
Closure properties of the class of regular languages THEOREM. The class of regular languages is closedunder all 6 operations. If A and B are regular, applying any of these operation yields a regular language. Sofya Raskhodnikova; based on slides by Nick Hopper
Star operation Star: A* = { w1 …wk | k ≥ 0 and each wi A } Theorem. If A is regular, A* is also regular. ε ε L(M)=A ε Sofya Raskhodnikova; based on slides by Nick Hopper
The class of regular languages is closed under Regular operations Union: A B = { w | w A or w B } Union: A B = { w | w A or w B } Concatenation: A B = { vw | v A and w B } Star: A* = { w1 …wk | k ≥ 0 and each wi A } Other operations Complement: A = { w | w A } Intersection: A B = { w | w A and w B } Reverse: AR = { w1 …wk | wk …w1 A } Sofya Raskhodnikova; based on slides by Nick Hopper
Regular expressions • In a regular expression, we can use • Constants: ε , , a setΣ, members of Σ. • Regular operations:*, , • Examples: • 0*1* • (01)* • 0*1*(ε 0) • L(R) = the language regular expression R describes = { w | w has a run of 0s followed by a run of 1s} • = the set of all strings over the alphabet Σ={0,1} Sofya Raskhodnikova; based on slides by Nick Hopper
Precedence * EXAMPLE Sofya Raskhodnikova; based on slides by Nick Hopper
Regular expressions: examples 1) 2) 3) { w | w has exactly one character 1, any # of 0s} 0*10* { w | w has length ≥ 3 and its 3rd symbol is 0 } (01)(01) 0 (01)* { w | every odd position of w is a 1 } (1(01))* (ε 1) Sofya Raskhodnikova; based on slides by Nick Hopper
Regular expressions to NFAs Theorem.Every regular expression has an equivalent NFA. Proof: Induction on the length of regular expression R. Base case: length 1 Inductive step: follows from closure of the class of regular languages under the regular operations. R = ε R = R = Sofya Raskhodnikova; based on slides by Nick Hopper
Exercise What should the induction hypothesis be? • Suppose some regular expression of length can be converted an NFA, for some . • Suppose each regular expression of length can be converted an NFA, for some . • Suppose each regular expression of length at most k can be converted an NFA, for some . • None of the above.
Regular expression to NFA Transform (1(0 1))* to an NFA ε 1 1,0 ε Sofya Raskhodnikova; based on slides by Nick Hopper
01*0 0 0 1 NFAs to regular expressions Theorem.Every NFA has an equivalent regular expression. Proof idea: Transform NFA to a regular expression by removing states and relabeling the arrows with regular expressions. Sofya Raskhodnikova; based on slides by Nick Hopper
Exercise qs q qa What is the regular expression that generates all strings that take this machine from to ? • None of the above.
Generalized NFAs Sofya Raskhodnikova; based on slides by Nick Hopper Each transition is labeled with a regular expression Unique and distinct start and accept states No transitions to the start state No transitions from the accept state
Generalized NFAs ab a*b a qs q qa A GNFA accepts w if q0,q1,…,qk, w1,…,wk: wiis generated by R(qi-1,qi) w = w1w2…wk q0=qs, qk=qa R(qs,q) = a*b R(qa,q) = Ø R(q,qs) = Ø
NFA to GNFA ε ε ε NFA ε Add a new start state with no incoming arrows. Make a unique accept state with no outgoing arrows. Sofya Raskhodnikova; based on slides by Nick Hopper
GNF to regular expression ε ε ε NFA While machine has more than 2 states: Pick an internal state, rip it out and relabel the arrows with regular expressions to account for the missing state. Sofya Raskhodnikova; based on slides by Nick Hopper
a a,b (a*b)(ab)* ε b ε a*b q1 q2 q3 q0 R(q0,q3) = (a*b)(ab)*
b bb a q1 q2 a ε b ε b a ε q3 Sofya Raskhodnikova; based on slides by Nick Hopper
b bb a q1 q2 a ba a ε b ε b a ε Sofya Raskhodnikova; based on slides by Nick Hopper
b = R(q1,q1) bb (a ba)b*a bb a q1 q2 a ba ε ε b (a ba)b* b Sofya Raskhodnikova; based on slides by Nick Hopper
= R(q1,q1) bb (a ba)b*a q1 ε b (a ba)b* = R(q1,qa) qs qa (bb (a ba)b*a)*(b (a ba)b*) Sofya Raskhodnikova; based on slides by Nick Hopper
Conversion procedures NFA DFA definition Regular Language Regular Expression Sofya Raskhodnikova; based on slides by Nick Hopper