Finite Automata Wen-Guey Tzeng Computer Science Department National Chiao Tung University
Syllabus Deterministic finite acceptor Nondeterministic finite acceptor Equivalence of DFA and NFA Reduction of the number of states 2
Deterministic Finite Acceptor 3
A very low-level machine It has only states (control unit). It has no memory of any type. It has no output. Clock 4
A DFA M is defined by M=(Q,,, q 0, F) Q: a finite set of states : input alphabet (set of symbols) : Q x Q: a total function called transition function q 0 Q: the initial state F Q: set of final states A dfa is an acceptor for strings. 5
Example M=({q 0, q 1, q 2 }, {0,1},, q 0, {q 1 } ), where (q 0, 0)=q 0, (q 0, 1)=q 1, (q 1, 0)=q 0, (q 1, 1)=q 2, (q 2, 0)=q 2, (q 2, 1)=q 1 Transition table 0 1 q 0 q 0 q 1 q 1 q 0 q 2 q 2 q 2 q 1 6
Graph representation 7
How does it work? What does it represent? accept strings reject strings 8
From to * * : Qx * Q * (q,w) = the reached state after reading a string w by starting from state q. * (q, ) = q * (q, wa) = ( * (q, w), a)=p 9
Language L(M) w is accepted by M if *(q 0, w) F w is not accepted by M if *(q 0, w) F The language accepted by M is L(M)= {w * : * (q 0, w) F } 10
What language is accepted by M 11
What language is accepted by M L(M) = {a n b : n 0} 12
DFA design Find a dfa that accepts strings starting with prefix ab, where ={a, b} 13
Find a dfa that accepts strings ending with suffix ab. Problem: we don t know when the input ends until the machine stops Technique: each state records the characteristic of the input string up to the current position 14
Design a dfa for L={w {0,1}* : w contains substring 001} Consider 100001 15
Design a dfa accepting all strings over {0, 1} except those containing substring 001 16
Design a dfa for L = {w {a,b}* : n a (w) is even and n b (w) is odd} 17
Can we design a dfa for L={a n b n : n 1}? 18
Regular languages Definition: a language L is regular if and only if some dfa M accepts L, that is, L=L(M). Show that L={awa : w {a,b}*} is regular. 19
Every finite language L is regular. 20
Questions? What languages are regular? How to determine whether a language L is regular or not? Yes design a DFA for L No show every DFA cannot accept it. Feasible? Find other ways. 21
Nondeterministic Finite Acceptor 22
An imaginary computing model As intermediate object to establish the relation between regular expressions and dfa Find important use in classifying complexity classes 23
An NFA M is defined by M=(Q,,, q 0, F) Q: finite set of states : input alphabet (set of symbols) : Qx( { }) 2 Q : nondeterministic transition function q 0 Q: the initial state F Q: set of final states Note (q 1, a)={q 0, q 2 } -transition: (q 1, )={q 1, q 2 } Unspecified transition: (q 1, b): not defined 24
Example: NFA with -transitions 0 1 q 0 {q 2 } - {q 1 } q 1 - {q 0, q 2 } {q 2 } F={q 0 } q 2 - - - 25
-transition (q 0, )={q 2 } Unspecified (undefined) transition (q 0, 0), Going into a dead state 26
Transition function * *(q i, w)=s : the states reachable from q i with input w q j S if and only if there is a walk, labeled with w, from q i to q j We can insert as many s as we want into w. Example *(q 0, a) = {q 1, q 2 } *(q 1, b) = {q 0 } *(q 0, aa)={q 1, q 2 } *(q 0, ab)= {q 0 } *(q 0, wa)= ڂ p δ (q 0,w) δ (p, a) 27
How to compute * Compute *(q, ) The states are reachable from q by a sequence of - transitions. Note q *(q, ) Note the difference between (q, ) and *(q, ) (q, ) = {p, r}, *(q, ) = {q, p, r, t, s, u} q p t r s u 28
*(q 0, ) = {q 0 }, *(q 1, ) = {q 1, q 2 }, *(q 2, ) = {q 2 }, Compute *(q 0, a) = Compute *(q 1, b) = Compute *(q 0, ab) = ڂ p δ (q,a) δ (p, b) 29
Language accepted by nfa A string w is accepted by nfa M if there is a walk w from q 0 to a final state q f, that is, *(q 0, w) F A string w is not accepted by M if there is not any walk w from q 0 to any final state q f The language L(M) accepted by nfa M is L(M)={ w * : *(q 0, w) F } Note: if *(q 0, w) is undefined (dead), then w is not accepted. 30
Example, For the following nfa, L(M)={ (10) n : n 0} *(q 0, 10111) cannot reach a final state no matter what paths it takes. Thus, 10111 is not accepted by M 31
Design nfa s Design an nfa for L={w {a,b}* : w contains substring abb } 32
Design an nfa for L={w {a,b}* : w does not contain substring abb } Cannot exchange final and non-final states 33
Design an nfa for L={a n b : n 0} {b n a : n 1} 34
Equivalence of nfa and dfa Two finite acceptors M 1 and M 2 are equivalent if and only if L(M 1 )=L(M 2 ) 35
C dfa : the class of dfa s C nfa : the class of nfa s Is the class C nfa more powerful than C dfa? Is there a language accepted by an nfa, but not accepted by any dfa? 36
Convert nfa to dfa: M N =(Q N,, N, q 0, F N ) M D =(Q D,, D, {q 0 }, F D ) Key point: dfa simulates transitions of nfa M N : q 0 {q i, q j,, q k } (state set) by * N M D : [q 0 ] [q i, q j,, q k ] (a state) by * D A set of states in M N is a state in M D q 0 w w w q i q j {q 0 } w {q i q j q k } q k 37
Recursion property M N : {q i, q j,, q k } {p r, p s,, p t } by N* in one step (on input symbol) M D : [q i, q j,, q k ] [p r, p s,, p t ] by D in one step q i a p r q i a p s {q i q j q k } a {p r p s p t } a q i a p t 38
Algorithm: nfa dfa 1. Compute N *(q, a) for all q Q N, a, 2. Start with {q 0 } in Q D 3. Expand to {q i, q j,, q k } from existent states For any unspecified {q i, q j,, q k } and a, compute D ({q i, q j,, q k }, a) = N *(q i, a) N *(q j, a) N *(q k, a) Repeat until all states are expanded. 4. For F D, a state {q i, q j,, q k } is in F D if any of q i, q j,, q k is in F N. {q 0 } is in F D if is accepted by M N 39
Example: Preparation *(q 0, 0) *(q 0, 1) *(q 1, 0) *(q 1, 1) *(q 2, 0) *(q 2, 1) 40
Expand from {q 0 } 41
42
Why correct? For any w +, D ({q 0 }, w) = {q i, q j,, q k } if and only if N *(q 0, w) = {q i, q j,, q k } Base: D ({q 0 }, a) = N *(q 0, a) for any a Hypothesis: true for w n D ({q 0 }, w) = N *(q 0, w) = {q i, q j,, q k } Induction step: true for wa =n+1 D *({q 0 }, wa) = D ({q i, q j,, q k }, a) = N *(q i, a) N *(q j, a) N *(q k, a) = N *(q 0, wa) 43
Some thoughts Every dfa accepts a unique language. For a given language, there are many dfa s that accept it. Questions How do we know two dfa s are equivalent? How do find a minimum-state dfa for a given L, if existing? For any regular language, is the minimum-state dfa s unique? 44
Reduction of States Ideas: Remove inaccessible states Find equivalent states Mark() Merge equivalent states Reduce() 45
Indistinguishable states Two states p and q are indistinguishable if for every w *, *(p, w) F *(q, w) F *(p, w) F *(q, w) F Merge indistinguishable states as a new state. How to find indistinguishable states? 46
Two states are distinguishable if there is w *, *(p, w) F and *(q, w) F, or *(p, w) F and *(q, w) F x p w q 0 y q w States p and q are distinguishable. They cannot be merged. 47
Observation (backward check) Start: Final and non-final states are distinguishable Recursion: if (p, a)=r, (q, a)=s, and (r, s) are distinguishable, then (p, q) are distinguishable. 48
Find distinguishable states: Procedure Mark(): 1. Remove all inaccessible states. 2. For all pairs of states (p, q), if p F and q F, or vice versa, then mark (p, q) as distinguishable. 3. Repeat the following until no un-marked pairs are marked For all pairs (p, q) and a, compute (p, a)=p a and (q, a)=q a. If (p a, q a ) is distinguishable, then mark (p, q) as distinguishable. 49
From equivalence classes to the dfa: Procedure: Reduce() 1. Use Mark() to find equivalence classes. 2. For each class {q i, q j,, q k }, create a state [ij k]. 3. ([ij k], a) = [lm n] if (q r, a)=q p, q r {q i, q j,,q k}, q p {q l, q m,, q n } 4. For each state [ij k], if some r [ij k] and q r F, then[ij k] F. 5. Output the reduced dfa, described above. 50
Example q 0 q 1 q 2 q 3 q 4 q0 q1 q2 q3 q4 51
52
Exercise p 3 p 0 b b b a a p 2 p 4 a b a a p 6 b p1 b a a p 5 53
Remarks For every regular language L, the minimum-state dfa is unique. The minimun-state dfa for L can be found from any dfa for L by the procedure mark() and reduce(). 54
Example Find a minimum-state dfa for L={a n b : n 0} {b n a : n 1} 55