NFAs, DFAs, and regular expressions The Chinese University of Hong Kong Fall 2010 CSCI 3130: Automata theory and formal languages NFAs, DFAs, and regular expressions Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130
Three ways of doing it L = {x Î S*: x ends in 01} S = {0, 1} (0+1)*01 1 0, 1 q0 q1 q2 qe 1 q0 q1 q00 q10 q01 q11 (0+1)*01 regular expression NFA DFA
They are all the same NFA regular expression DFA regular languages
Road map regular expression NFA NFA without e DFA
Examples: regular expression → NFA q0 q1 R1 = 0 R2 = 01 1 q0 q1 q2
Examples: regular expression → NFA q1 q2 q0 q6 R3 = 0 + 01 q3 q4 q5 1 q0’ q1’ R4 = (0 + 01)* NFA3
Regular expressions → NFA In general, how do we convert a regular expression to an NFA? A regular expression over S is an expression formed using the following rules: The symbol Æ is a regular expression The symbol e is a regular expression For every a S, the symbol a is a regular expression If R and S are regular expressions, so are R+S, RS and R*.
General method Æ e regular expr NFA q0 q0 q0 q1 a a S RS q0 q1 NFAR NFAS
General method continued regular expr NFA NFAR q0 q1 R + S NFAS q0 q1 R* NFAR
Road map regular expression NFA NFA without e DFA
Road map regular expression NFA NFA without e DFA
Road map regular expression NFA NFA without e DFA 2-state GNFA GNFA
Generalized NFAs A generalized NFA is an NFA whose transitions are labeled by regular expressions, like moreover It has exactly one accept state, different from its start state No arrows come into the start state No arrows go out of the accept state 0*1 e+10* 0*11 q0 q1 q2 01
Converting a DFA to a GNFA regular expression NFA NFA without e DFA 2-state GNFA GNFA q1 q3 q5 q0 qf e
Conversion example 1 q0 e q3 1 q1 q2 It has exactly one accept state, different from its start state No arrows come into the start state No arrows go out of the accept state
GNFA state reduction regular expression NFA NFA without e DFA 2-state GNFA GNFA We will eliminate every state but the start and accept states
State elimination e+10* 0*11 0*1 q0 q1 q2 01 q0 q2 01
State elimination – general method To eliminate state qk, for every pair of states (qi, qj) remember to do this even when qi = qj! R2 R1 R3 Replace qi qk qj R4 R1R2*R3 + R4 by qi qj
Road map NFA without e NFA regular expression DFA GNFA 2-state GNFA GNFA q0 q1 R A 2-state GNFA is the same as a regular expression R!
Conversion example Eliminate q1: Eliminate q2: Check: 0*1(00*1+1)* = 1 1 e 1 e q0 q1 q2 q3 00*1+1 0*1 e Eliminate q1: q0 q2 q3 0*1(00*1+1)* Eliminate q2: q0 q3 q1 q2 1 ? Check: 0*1(00*1+1)* =
Check your answer! 0*1(00*1+1)* 0*1(0*1)* 011001000101 q1 q2 1 All strings that end in 1 (0 + 1)*1 0*1(00*1+1)* Always ends in 1 = Does every string that ends in 1 have this form? 0*1(0*1)* 011001000101 Yes!
Design Analyze Convert What you need to know regular expression NFA DFA regular languages Design Analyze Convert