Chap. 2 Finite Automata 2.1 An Informal Picture of Finite Automata (See 2nd Edition) A man with a wolf, goat, and cabbage is on the left bank of a river A boat carries one man and only one of the other three. The wolf eats the goat without the man. The goat eats the cabbage without the man. Is it possible to cross the river without the goat or cabbage being eaten? states: occupants of each bank after a crossing 16 subsets of the man(m), wolf(w), goat(g), and cabbage(c). 16 states: mwgc-, WC-mG,, -mwgc input symbol: four ways of crossing the river A man crosses alone(m) with the wolf(w), goat(g), or cabbage(c). input string: a sequence of crossings (input symbols = {m, w, g, c}) state transition: From a state to another state by an input symbol (crossing) 9/14/17 Kwang-Moo Choe 1
mcgw- m,c,w,g -mcgw g g m,c,w m-cgw mc-gw mw-cg m,c,w g g CW-mG m m mg-cw m mcw-g m c c w w W-mCG mgw-c w w m m m G-mCW C-mGW g g g g m mcg-w c c 9/14/17 Kwang-Moo Choe 2
2.2 Deterministic Finite Automata(DFA) 2.2.1 Definition of a DFA A deterministic finite automaton A = (Q, T,, q 0, F) consists of: 1. A finite set of states, denoted Q, 2. A finite set of input symbols, denoted T, in the text 3. A transition function, : Q T Q Let p, q Q, and a T, if we write (q, a) = p, meaning that when current state is in q and input symbol is a, the current state is changed to p, and input symbol is changed to the next(right) one. 4. A start(initial) state, q 0 Q, and 5. A set of final or accepting states, F Q. A DFA A = (Q, T,, q 0, F) five-tuple notation 9/14/17 Kwang-Moo Choe 3
2.2.2 How a DFA Processes Strings How a DFA decides whether or not accept a input string(sequence of input symbols) Suppose a 1 a 2 a n is a sequence of input symbols 1. We start out the DFA with in its start state q 0. 2. We consult the transition function,, with current state and current input symbol, assume in the first trial (q 0, a 1 ) = q 1, 3. Repeat this step with the next state q 1 and the next input symbol a 2. Assume (q 1, a 2 ) = q 2, (q 2, a 3 ) = q 3,, (q n-1, a n ) = q n. 4. Finally, check if (q n-1, a n ) = q n F or not. If q n F, accept a 1 a 2 a n. If q n F does not accept a 1 a 2 a n. ( ( ( (q 0, a 1 ), a 2 ), ), a n-1 ), a n ) = q n where (q i-1, a i ) = q i for 1 i n. 9/14/17 Kwang-Moo Choe 4
function DFA( D = (Q:set of states, T:set of symbols, : Q Q, q 0 Q, F Q); x = a 1 a 2 a n T * ) /* (D; x T * ) are two formal parameters */ /* D has five tuples, D = (Q, T,, q 0, F) */ return {D accept x, D accept x} q Q, i N; /* q and i are two local variables */ q, i := q 0, 1; do i n if (q, a i ) q, i := (q, a i ), i+1 (q, a i ) return D accept x fi od if q F return D accept x q F return D accept x fi end function DFA. 9/14/17 Kwang-Moo Choe 5
2.2.3 Simple notations for DFA s Example 2.2 Let T = {0, 1} and L = {x01y T * x, y T * }. substring 01. (0 1) * 01(0 1) *. 1. Transition diagram Figure 2.4 in p. 48. 1 0 0, 1 0 q 0 q 1 1 q 2 State q 0 : initial state If it sees 0, go to a new state q 1. If it sees 1 stay in the state q 0. It has never seen 0. State q 1 : The last symbol it has seen is 0. If it sees 1, go to a new state q 2. If it sees 0, stay in the state q 1. State q 2 : It has ever seen is 01. final state Whether it sees 0 or 1, stay in the state q 2. 9/14/17 Kwang-Moo Choe 6
2. Transition table Figure 2.5 in p. 49. 0 1 q 0 q 1 q 0 q 1 q 1 q 2 * q 2 q 2 q 2 Formal definition of DFA D D = (Q, T,, q 0, F) where 1. Q = {q 0, q 1, q 2 } 2. T = {0, 1} 3. = { (q 0, 0)=q 1, (q 0, 1)=q 0, (q 1, 0)=q 1, (q 1, 1)=q 2, (q 2, 0)=q 2, (q 2, 1)=q 2 } 4. q 0 Q 5. F = {q 2 } 9/14/17 Kwang-Moo Choe 7
2.2.4 Extend the domain of transition function from symbols to strings : Q T Q ^: Q T * Q ^(q, ) = B q basis ^(q, xa) = R ( ^(q, x), a) recursion (2.1) q Q, x T *, a, xa T +. ^(q 0, a 1 a 2 a n ) = R ( ^(q 0, a 1 a 2 a n-1 ), a n ) 1st recursion = R ( ( ^(q 0, a 1 a 2 a n-2 ), a n-1 ), a n ) 2nd recursion = R = R ( ( ( ( ^(q 0, ), a 1 ), a 2 ), ), a n-1 ), a n ) n-th recursion = B ( ( ( (q 0, a 1 ), a 2 ), ), a n-1 ), a n ) basis We may use * instead of ^ to show that the 2nd domain of is T *. 9/14/17 Kwang-Moo Choe 8
Example 2.4 in p. 50. 1 q 0 1 q 1 0 0 Even numbers of 0 s and even numbers of 1 s. q 2 2.2.5 The Language of DFA We define the language of a DFA D = (Q, T,, q 0, F), L(D), L(D) = {w T * * (q 0, w) F}. L(D) T * or L(D) 2 T*. 0 1 1 0 q 3 9/14/17 Kwang-Moo Choe 9
Def. A Class of Regular languages Let L be a language. If L = L(A) for some DFA A, then we say L is a regular language. A class of regular languages type-3 languages in Chomsky s language hierarchy Automata M 1 M 2 DFA L L L L Languages 2 T* regular L Def. Equivalence of two Automata Let M 1 and M 2 be two automata. We say two automata M 1 and M 2 are equivalent, if L(M 1 ) = L(M 2 ) = L. 9/14/17 Kwang-Moo Choe 10
DFA: : Q T Q q Q, a T, (q, a) Q. (total) function q Q, a T \ (q, a). partial function If A = (Q, T,, q 0, F) be a FA with : Q T \ Q partial function, L(A) = L(B) for DFA B = (Q {d}, T,, q 0, F) where d Q, DFA but partial function: : Q T Q { }. = { (q, a) = d (q, a) Q} { (d, a) = d a T} d: dead state in B is the DFA(with total function) but? : Q T Q. Dead state: m cgw, mc gw, mw cg state in the 1st Example. eliminate the state and relevant transition functions partial function. DFA with partial function can be considered as a DFA. 9/14/17 Kwang-Moo Choe 11
2.3 Nondeterministic Finite Automata(NFA) 2.3.1 An Informal View of NFA Example 2.9 An NFA accepting all strings that ends in 01(postfix 01). Fig. 2.9(p. 56) 0, 1 0 q 0 q 1 1 q 2 Consider an input string 00101 {0, 1} *. q 0 0 q 1 0 X(stuck) q 0 0 q 0 0 q 1 1 q 2 0 X(stuck) q 0 0 q 0 0 q 0 1 q 0 0 q 0 1 q 0 F(fail) q 0 0 q 0 0 q 0 1 q 0 0 q 1 1 q 2 F(success) 9/14/17 Kwang-Moo Choe 12
2.3.2 Definition of NFA A = (Q, T,, q 0, F) is a nondeterministic finite automaton(nfa), if 1. Q is a finite set of states, 2. T is a finite set of input symbols, 3. is a transition function, : Q T 2 Q. If q Q, a T, and {p 1, p 2,, p n } Q (1 i n, p i Q) then we write (q, a) = {p 1, p 2,, p n }, meaning that when current state is in q and input symbol is a, the state may be changed to any one of p 1, p 2,, p n, and input symbol is changed to the next(right) one. 4. q 0 Q is a start state, and 5. F Q is a set of final or accepting states. 9/14/17 Kwang-Moo Choe 13
2.3.3 The Extended Transition Function DFA: : Q T Q Definition * : Q T * Q ^: Q * Q in the text NFA: : Q T 2 Q Definition : 2 Q T 2 Q (1) set extension in this lecture * : 2 Q T * 2 Q (2) string extension in this lecture ^: Q * 2 Q in the text Two step extensions 1. Set extension: : We extend the 1st domain of to set of states. : 2 Q T 2 Q. Let P Q. Then we define (P, a) = {q Q p P, q (p, a)} or = p P (p, a) We may write (p, a) instead of ({p}, a) when needed. 9/14/17 Kwang-Moo Choe 14
2. String extension: * (same as ^ = * ) * : 2 Q T * 2 Q. * (P, ) = B P basis * (P, wa) = R ( * (P, w), a) recursion P Q(or P 2 Q ), w T *, a T, wa T +. We may write and * instead of and * since. 2.3.4 The Language of an NFA Let N = (Q,,, q 0, F) be an NFA. Then the language defined by the NFA N, denoted as L(N) is, L(N) = {w T * * (q 0, w) F }. L(N) T * or L(N) 2 T*. 9/14/17 Kwang-Moo Choe 15
2.3.5 Equivalence of DFA and NFA Example 2.10 NFA of Fig. 2.9(p. 56) postfix 01. 0, 1 0 q 0 q 1 1 q 2 Transition table for NFA 0 1 q 0 {q 0, q 1 } {q 0 } (q 0, 0) = {q 0, q 1 }: NFA q 1 {q 2 } * q 2 Transition table for subsets of NFA 2 3 = 8 subset-states(see Fig. 2.12) 0 1 0 1 {q 0, q 1 } {q 0, q 1 } {q 0, q 2 } {q 0 } {q 0, q 1 } {q 0 } * {q 0, q 2 } {q 0, q 1 } {q 0 } {q 1 } {q 2 } * {q 1, q 2 } {q 2 } * {q 2 } * {q 0, q 1, q 2 } {q 0, q 1 } {q 0, q 2 } 9/14/17 Kwang-Moo Choe 16
Starting from {q 0 } See Fig. 2.14 in p. 63. 0 1 0 1 {q 0 } {q 0, q 1 } {q 0 } [ ] [0] [ ] {q 0, q 1 } {q 0, q 1 } {q 0, q 2 } [0] [0] [01] * {q 0, q 2 } {q 0, q 1 } {q 0 } * [01] [0] [ ] Only 3 subset-states are reachable from the initial state {q 0 }. Renaming of states {q 0 } [ ], {q 0, q 1 } [0], and {q 0, q 2 } [01]. 1 0 [ ] 0 [0] 1 0 [01] 1 Fig. 2.14 DFA from NFA Fig. 2.9.(postfix 01) 9/14/17 Kwang-Moo Choe 17
Subset Construction(NFA DFA) Q D = 2 Q N. Given an NFA N = (Q N, T, N, q 0N, F N ), construct an equivalent DFA D = (Q D,, D, q 0D, F D ) such that L(D) = L(N). 1. Q D = {P P Q N } = 2 Q N. 3. D (P, a) = {q Q N p P Q N, q N (p, a)} Q N or Q D. or = p P N (p, a) or = N (P, a) set extension of N. D (P, a) = N (P, a) or D = N for short, where D : Q D T Q D and N : 2 Q N T 2 Q N(Q D = 2 Q N). 4. q 0D ( Q D ) = {q 0N }( Q N ). 5. F D = {F Q D F F N } 9/14/17 Kwang-Moo Choe 18
Thm. 2.11 L(D) = L(N) in the above subset construction Proof * D (q 0D, w) = N * ({q 0N }, w) for all w *. D * : Q D T * Q D (= 2 Q N * 2 Q N). N * : 2 Q N T * 2 Q N. We may use q 0N instead of {q 0N }. Induction on w. basis: Let w = 0, * D (q 0D, ) = * D ({q 0N }, ) = {q 0N }= ^N ({q 0N }, ). induction: Let w = xa where a T, x T *, and w T +. ( w = x + 1) * D (q 0D, xa) = D ( * D ({q 0N }, x), a) by string extension of * D. = N ( D * ({q 0N }, x), a) by subset construction = N ( N * ({q 0N }, x), a) by induction hypothesis = N * ({q 0N }, xa) by string extension of N *. D * (q 0D, x) F D, if and only if, N * (q 0, x) F N. L(D) = L(N). 9/14/17 Kwang-Moo Choe 19
Thm. 2.12 A language L is defined by some DFA, if and only if L is defined by some NFA.(NFA DFA) Proof: (If) subset construction(nfa DFA) Thm. 2.11 (Only if) NFA can easily simulate DFA(DFA NFA). Let a DFA D = (Q, T, D, q 0, F). We define an NFA(Q, T, N, q 0, F) N (q, a) = {p D (q, a) = p}. Proof for w *, if and only if, ^ D (q, w) = p, then ^D (q, w) = {p}. L is regular, iff L = L(N) for some NFA N. Automata NFA N DFA D L L L L L L Languages 2 T* regular L 9/14/17 Kwang-Moo Choe 20
2.3.6 A Bad Case for the Subset Construction See Ex. 2.13 (p64-65) (0+1) * 1(0+1) n-1. The state of DFA summarizes the information concerning past inputs that is needed to determine the behaviour of the automata on subsequent inputs. Aho and Ullman in the 2nd Edition. [(0+1) * ] 1 [(0+1) * 1] 0 [(0+1) * 10] 0 [(0+1) * 100] * [(0+1) * 10 n-2 0] * [(0+1) * 10 n-2 1] 1 [(0+1) * 101]...... 1 [(0+1) * 11] 0 [(0+1) * 110]... * [(0+1) * 11 n-2 0] 1 [(0+1) * 111] * [(0+1) * 11 n-2 1] 9/14/17 Kwang-Moo Choe 21
2.4 An Application: Text Search Read text 2.5 Finite Automata with Epsilon-Transitions A = (Q, T,, q 0, F) is a fa with epsilon transition( -NFA), if Q, T, q 0, and F are same as NFA, but 3. is a transition function, : Q (T { }) 2 Q. If q Q and {p 1, p 2,, p n } Q, then we write (q, ) = {p 1, p 2,, p n }, meaning that when current state is in q and regardless of input symbol the state may be changed to any one of p 1, p 2,, p n, and next input symbol is not changed. Ex. 2.16 in Fig. 2.18 for an -NFA accepting decimal numbers in p. 73. Ex. 2.17 in Fig. 2.19 for an -NFA accepting keywords web and ebay in p. 73. 9/14/17 Kwang-Moo Choe 22
2.5.3 Epsilon-closure basis: q * (q) (same as ECLOSE(q) in the text) recursion: If p * (q), and r (p, ), then r * (q). Example 2.19 in p. 74. Why * (q) instead of ECLOSE(q) Let = {(p, q) Q Q q (p, )}. Then Q Q(a binary relation on Q) and ECLOSE(q) = * (q) reflexive-transitive closure of. 9/14/17 Kwang-Moo Choe 23
2.5.4 The Extended Transition and Language for -NFA s NFA: : Q T 2 Q. -NFA: : Q (T { }) 2 Q. Let s divide of -NFA into 1 and 0 (= ). 1 : Q T 2 Q. 0 : Q { } 2 Q. = 1 0. 1 : is same as in NFA, we use instead of 1, since 1 =. 0 f what is the bijection f? We use instead of 0. = {(p, q) Q Q q (p, )} binary relation on Q. * = i N i 0 Q Q binary relation on Q., * : Q 2 Q. function from Q to 2 Q. 9/14/17 Kwang-Moo Choe 24
Let s extend the domain of (= 1 ),, and * from state to set of states. Let P Q, we define : 2 Q T 2 Q. (P, a) = {q Q p P, (p, a) = q} or = p P (p, a) We may write (p, a) instead of ({p}, a) when needed. We may write (P, a) instead of (P, a) when needed. : 2 Q 2 Q. (P) = {q Q p P, (p) = q} or = p P (p) * : 2 Q 2 Q. * (P) = {q Q p P, * (p) = q} = p P * (p) We write (P) instead of (P) and * (P) instead of * (P). 9/14/17 Kwang-Moo Choe 25
Let s extend the 2nd domain of from symbol to strings. * : 2 Q T * 2 Q. * (P, ) = B * (P) basis * (P, wa) = R * ( ( * (P, w), a)) recursion P Q(or P 2 Q ), w *, a T, wa T +. n (q 0, a 1 a 2 a n ) = R * ( ( n-1 (q 0, a 1 a 2 a n-1 ), a n )) 1st recursion = R * ( ( * ( ( n-2 (q 0, a 1 a 2 a n-2 ), a n-1 )), a n )) 2nd recursion = R = R * ( ( * ( ( * ( ( * ( ( 0 (q 0, ), a 1 )), a 2 )), )), a n-1 )), a n )) n-th = B * ( ( * ( ( * ( ( * ( ( * (q 0 ), a 1 )), a 2 )), )), a n-1 )), a n )) basis n = * * * = * ( * ) n = * ( 1 ) n. where 1 = *. * = i N 0 i = *. 9/14/17 Kwang-Moo Choe 26
2.5.5 Eliminating -Transitions Given an -NFA E = (Q E, T, E, q 0E, F E ), construct the equivalent DFA D = (Q D, T, D, q 0D, F D ) such that L(D) = L(N). 1. Q D = {P P Q E } = 2 Q E. 3. q 0D = * ({q 0E }) 4. D (P, a) = * ( E (P, a)) set extension of E and -closure( 1 = * ) where P Q E (P Q D ), a T, and D (P, a) Q E (or D (P, a) Q D ) 5. F D = {F Q D F F E } Theorem 2.22 A language L is accepted by some -NFA if and only if L is accepted by some DFA. Proof: (If) -NFA can easily simulate DFA(DFA -NFA). (Only if) Eliminating -transitions( -NFA DFA) 9/14/17 Kwang-Moo Choe 27
Let E = (Q E, T, E, q 0E, F E ) be a -NFA and DFA D = (Q D,, D, q 0D, F D ) is the one in 2.5.5. We show E * (q 0E, w) = D * (q 0D, w) by induction on w. basis: * E (q 0E, ) = * (q 0E ) by basis definition of * E. = q 0D by construction of D. = D * (q 0D, ) by basis definition of D *. Induction: Let w = xa T +, a T, and x T *. E * (q 0, xa) = E ( * ( E * (q 0, x), a) by recursive definition of E *. = E ( * ( D * (q 0D, x), a)) by induction hypothesis. = D ( D * (q 0D, x), a) by construction of D. = D * (q 0D, xa) by recursive definition of D *. 9/14/17 Kwang-Moo Choe 28
-NFA Transition Table for Fig. 2.18 in p. 73. 1 +,. d * q 0 {q 1 } q 0 {q 1 } {q 0, q 1 } q 1 {q 2 } {q 1, q 4 } q 1 {q 1 } q 2 {q 3 } q 2 {q 2 } * q 3 {q 3 } q 3 {q 5 } {q 3, q 5 } q 4 {q 3 } q 4 {q 4 } q 5 q 5 {q 5 } DFA Transition Table for Fig. 2.22 in p. 79. * 1 * +,. d {q 0, q 1 } {q 1 } {q 2 } {q 1, q 4 } {q 1 } {q 2 } {q 1, q 4 } {q 2 } {q 3, q 5 } {q 1, q 4 } {q 2, q 3, q 5 } {q 1, q 4 } * {q 3, q 5 } {q 3, q 5 } * {q 2, q 3, q 5 } {q 3, q 5 } 9/14/17 Kwang-Moo Choe 29
2.A Extended Finite Automata(XFA) X = (Q, T, q 0,, F) is an extended Finite state Automata(XFA), if : Q T * 2 Q. Given an XFA X = (Q X, T, q 0, X, F), construct an equivalent -NFA E = (Q E, T, q 0, E, F) such that L(X) = L(E). Construction algorithm Q E := Q X ; E := ; for ( X (q, x) = P) X where x = a 1 a 2 a n do if 0 n 1 E := E { E (q, x) = P} where x { } n 2 Q E := Q E {p 1 }; E := E { E (q, a 1 ) = {p 1 }}; od for 2 i n do Q E :=Q E {p i }; E := E { E (p i-1, a i )= {p i }} od; E := E { E (p n-1, a n ) = P} where Q X {p 1, p 2,, p n-1 } fi 9/14/17 Kwang-Moo Choe 30
XFA s = -NFA s = NFA s = DFA s = Class of regular languages = FA s Automata XFA=FA X -NFA E NFA N DFA D L(X) L(N) L(E) L(D) Languages 2 T* regular L 9/14/17 Kwang-Moo Choe 31
The following six statements are equivalent, 1. A language L is regular. 2. L = L(D) for some DFA D where : Q T Q. 3. L = L(P) for some DFA P where : Q T Q { }. with partial function. 4. L = L(N) for some NFA N where : Q T 2 Q. 5. L = L(E) for some -NFA E where : Q ({ } T) 2 Q. 6. L = L(X) for some XFA X where : Q T * 2 Q. The following two statements are equivalent, 1. A language L is regular. 2. L = L(A) for some FA A where : Q T * 2 Q. We ve changed the definition of FA from DFA to XFA. 9/14/17 Kwang-Moo Choe 32
2.B Configuration rewriting system for FA Let A = (Q, T,, q 0, F) be a Finite automata where : Q T * 2 Q.Then A configuration rewritng system R A = (Q T *, ) on Q T * is = {(q, x) (p, ) p (q, x)}. (q, x), (p, ) Q T * : configurations Assume x = x 1 x 2...x n T * and 1 i n: q i (q i-1, x i ). Then x q 1 x 2 x 3 x 0 q 1 q 2 n-1 x q n n-1 q n (... ( (q 0, x 1 ), x 2 ),..., x n-1 ), x n ) = q 1 (q 0, x 1 ) ( (... (q 1, x 2 ),...,x n-1 ) x n ) =... = (q n-2, x n-1 ), x n ) = q n-1 (q n-2, x n-1 ) (q n-1, x n ) = q n (q n-1, x n ) q n. (q 0, x 1 x 2...x n-1 x n ) (q 0, x 1 ) (q 1, ) (q 1, x 2...x n-1 x n )... (q n-2, x n-1 x n ) (q n-2, x n-1 ) (q n-1, ) (q n-1, x n ) (q n-1, x n ) (q n, ) (q n, ). L(R A ) = {x T * (q 0, x) * (f, ), f F}. 9/14/17 Kwang-Moo Choe 33
Read this page for the minimal state DFA TBD in Chap. 4. Consider a language L T * and a binary relation R L on T * where x, y * : x R L y, if z T * : xz, yz L or xz, yz L. Then R L is an equivalent(proof? ) binary relation on T *. Consider a DFA M = (Q, T,, q 0, F) and a binary relation R M on T * x, y * : x R M y, if * (q 0, x) = * (q 0, y). Then Par RM ( * ) Q. Compare two quivalent relation on T *, R L and R M where L = L(M). Par RL (T * ) Par RM (T * ) in general. If Par RL (T * ) = Par RM (T * ) for some M = (Q, T,, q 0, F ). M is a minimal state DFA that accepting L. 9/14/17 Kwang-Moo Choe 34