180 likes | 295 Views
Intro to Theory of Computation. L ECTURE 4 Theory of Computation Equivalence of NFAs, DFAs and regular expressions. CS 464. Sofya Raskhodnikova. The class of regular languages is closed under. Regular operations. Union: A B = { w | w A or w B }.
E N D
Intro to Theory of Computation • LECTURE 4 • Theory of Computation • Equivalence of NFAs, DFAs and regular expressions CS 464 Sofya Raskhodnikova SofyaRaskhodnikova; 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
Nondeterminism Deterministic Computation Nondeterministic Computation reject accept accept or reject SofyaRaskhodnikova; based on slides by Nick Hopper Ways to think about nondeterminism parallel computation tree of possible computations guessing and verifying the “right” choice
Equivalence of NFAs & DFAs Theorem.Every NFA has an equivalent DFA. Corollary: A language is regular iff it is recognized by an NFA. Sofya Raskhodnikova; based on slides by Nick Hopper
From NFA to DFA Input: N = (Q, Σ, , q0, F) Output: M = (Q, Σ, , q0, F) Intuition: Do the computation in parallel, maintaining the set of states where all threads are reject Idea: Q = P(Q) accept Sofya Raskhodnikova; based on slides by Nick Hopper
From NFA to DFA Input: N = (Q, Σ, , q0, F) Output: M = (Q, Σ, , q0, F) For RQ, let E(R) be the set of states reachable by ε-transitions from the states in R. Q = P(Q) : Q Σ→ Q (R,) = E( (r,) ) for all RQ and Σ. rR q0 = E({q0}) F = { R Q | f R for some f F } Sofya Raskhodnikova; based on slides by Nick Hopper
Examples: NFA to DFA 1 a b 0,1 ε 0 3 1 2 1 Sofya Raskhodnikova; based on slides by Nick Hopper 1) 2)
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 ( ( R1* ) R2 ) R3 R1*R2 R3 = Sofya Raskhodnikova; based on slides by Nick Hopper
Regular expressions: examples 1) 2) 3) { w | w has exactly one character 1 } 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 Induction hypothesis: follows from closure of the class of regular languages under the regular operations. R = ε R = R = Sofya Raskhodnikova; based on slides by Nick Hopper
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
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 a[b a*b a qs q2 qa G accepts w if it finds q0q1…qk, w1…wk: wi2 R(qi,qi+1) w = w1w2…wk q0=qs, qk=qa R(qs,q2) = a*b R(qa,q) = Ø R(q,qs) = Ø
NFA to GNFA ε ε ε NFA ε ε Add unique and distinct start and accept states 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 relabelthe 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)*