Moile Computing nd Softwre Engineering p. 1/5 Forml Lnguges nd Automt Chpter 2 Finite Automt Chun-Ming Liu cmliu@csie.ntut.edu.tw Deprtment of Computer Science nd Informtion Engineering Ntionl Tipei University of Technology Tipei, TAIWAN
Moile Computing nd Softwre Engineering p. 2/5 Finite Automt A finite utomton (or finite-stte mchine) is severely restricted model of n ctul computer. A finite utomton is chrcterized y hving no temporry storge.
Moile Computing nd Softwre Engineering p. 3/5 Contents Deterministic Finite Automt Nondeterministic Finite Automt Equivlence of Deterministic nd Nondeterministic Finite Automt Reduction of the Numer of Sttes in Finite Automt
Moile Computing nd Softwre Engineering p. 4/5 Deterministic Finite Automt A deterministic finite utomt (df) is defined y the quintuple where M = (Q, Σ,δ,q 0,F), Q is finite set of internl sttes, Σ is n lphet, δ : Q Σ Q is the trnsition function, q 0 Q is the initil stte, nd F Q is the set of finl sttes.
Moile Computing nd Softwre Engineering p. 5/5 Visuliztion of Finite Automt We cn use trnsition grphs to represent finite utomt: vertices sttes edges trnsitions lels on vertices re the nmes of the sttes lels on edges re the vlues of the input symols initil stte hs n incoming edge unleled finl sttes re drwn with doule cycle.
Moile Computing nd Softwre Engineering p. 6/5 Exmple 1 Consider the df M = (Q, Σ,δ,q 0,F), where Q = {q 0,q 1,q 2 }, Σ = {0, 1}, F = {q 1 }, nd the trnsition function δ is tulted elow stte input δ(stte,input) 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
Moile Computing nd Softwre Engineering p. 7/5 Exmple - Visuliztion of df Remrks: q 0 is n initil stte nd hs n incoming unleled edge. q 1 is finl stte nd with doule cycle.
Moile Computing nd Softwre Engineering p. 8/5 String Inputs Extending the df to the notion of computtion y n utomton on n input string consider the extended trnsition function δ : Q Σ Q. A configurtion is determined y the current stte nd the unred prt of the string eing processed.
Moile Computing nd Softwre Engineering p. 9/5 Exmple 2 M = ({q 0,q 1 }, {,},δ,q 0, {q 0 }), where the trnsition function δ is tulted elow stte input δ(stte,input) q 0 q 0 q 0 q 1 q 1 q 1 q 1 q 0
Moile Computing nd Softwre Engineering p. 10/5 Visuliztion of Exmple 2 Remrks: If the input string is, the initil configurtion is (q 0,).
Moile Computing nd Softwre Engineering p. 11/5 Reltions etween δ nd δ δ cn e derived from δ recursively s δ (q,λ) = q δ (q,w) = δ(δ (q,w),) (1) q Q,w Σ, nd Σ. Eqution (1) cn e written lterntively s δ (q,w) = δ (δ(q,),w).
Moile Computing nd Softwre Engineering p. 12/5 Configurtion Trnsitions If (q,w) nd (q,w ) re two configurtions of M, then (q,w) M (q,w ) if nd only if w = w for some Σ nd δ(q,) = q. (q,w) yields (q,w ) in one move using M for the reflexive nd trnsitive closure of M, for exmple, (q,w) M (q,w ) denotes tht (q,w) yields (q,w ) fter some numer, including zero, of steps.
Moile Computing nd Softwre Engineering p. 13/5 Exmple 3 Consider the df in Exmple 2, M = ({q 0,q 1 }, {,},δ,q 0, {q 0 }), with the input string is. Then, the initil configurtion is (q 0,) nd (q 0,) M (q 0,) M (q 0,) M (q 1,) M (q 0,) (q 0,λ) M Hence, (q 0,) M (q 0,λ).
Moile Computing nd Softwre Engineering p. 14/5 Lnguges nd Df s A string w Σ is sid to e ccepted y M if nd only if there is stte q f F s.t. (q 0,w) M (q f,λ). Exmple: is ccepted since q 0 F in the lst exmple. The lnguge ccepted y df M is the set of ll strings on Σ ccepted y M nd is denoted y L(M) nd L(M) = {w Σ : δ (q 0,w) F }. Note: L(M) = {w Σ : δ (q 0,w) F }
Moile Computing nd Softwre Engineering p. 15/5 Exmple 4 Remrks: q 2 is trp stte from which the df cn never escpe. L(M) = { n : n 0}
Moile Computing nd Softwre Engineering p. 16/5 Exercise Descrie the lnguge ccepted y the following df M = ({q 0,q 1,q 2,q 3 }, {,},δ,q 0, {q 0,q 1,q 2 }), where the trnsition function δ is tulted elow q σ δ(q,σ) q σ δ(q,σ) q 0 q 0 q 2 q 0 q 0 q 1 q 2 q 3 q 1 q 0 q 3 q 3 q 1 q 2 q 3 q 3
Moile Computing nd Softwre Engineering p. 17/5 Theorem Let M = (Q, Σ,δ,q 0,F) e df nd G M e the ssocited trnsition grph. Then, q i,q j Q nd w Σ +, δ (q i,w) = q j wlk in G M from q i to q j.
Moile Computing nd Softwre Engineering p. 18/5 Exmple 5 Find df tht recognizes the set of ll strings on Σ = {,} strting with the prefix., q 0 q 1 q 2 q 3,
Moile Computing nd Softwre Engineering p. 19/5 Exmple 6 Find df tht ccepts ll the strings on {0, 1}, except those contining the sustring 001. 1 0 0, 1 0 q 0 q 1 q 3 1 0 1 q 2
Moile Computing nd Softwre Engineering p. 20/5 Regulr Lnguges A lnguge L is clled regulr if nd only if there exists some deterministic finite utomton M such tht L = L(M). Exmple 7: Show tht the lnguge is regulr. L = {w : w {,} } Note tht, to show lnguge is regulr, we need to find df which ccepts it.
Moile Computing nd Softwre Engineering p. 21/5 Exmple 7 q 0 q 1 q 2, q 3 Remrks:
Moile Computing nd Softwre Engineering p. 22/5 Contents Deterministic Finite Automt Nondeterministic Finite Automt Equivlence of Deterministic nd Nondeterministic Finite Automt Reduction of the Numer of Sttes in Finite Automt
Moile Computing nd Softwre Engineering p. 23/5 Nondeterministic Finite Automt A nondeterministic finite utomt (nf) is defined y the quintuple where M = (Q, Σ,δ,q 0,F), Q is finite set of internl sttes, Σ is n lphet, δ : Q (Σ {λ}) 2 Q is the trnsition function (2 Q = the set of ll susets of Q), q 0 Q is the initil stte, nd F Q is the set of finl sttes.
Moile Computing nd Softwre Engineering p. 24/5 Aout Nf s λ is llowed to e the second rgument of δ it is possile tht δ(q i,) = λ (i.e. no trnsition) An nf cn lso e presented y stte digrm (grph) A string is ccepted y n nf if there exist some possile moves tht rech to finl stte.
Moile Computing nd Softwre Engineering p. 25/5 Exmple 8 q 2 q 1 q 3 q 0 q 4 q 5
Moile Computing nd Softwre Engineering p. 26/5 Exmple 9 An nf tht ccepts the set of ll strings contining either two consecutive 0 s or two consecutive 1 s. 0, 1 0, 1 0 0 q 0 q 3 q 4 1 q 3 q 4 1 0, 1
Moile Computing nd Softwre Engineering p. 27/5 String Inputs The trnsition cn e extended to the notion of computtion on n input string consider the extended trnsition function δ : Q Σ 2 Q. A recursive definition of δ is lso nlogous to the one for df. δ (q i,w) contins q j if nd only if there exists wlk in the stte grph from q i to q j.
Moile Computing nd Softwre Engineering p. 28/5 Acceptnce The lnguge ccepted y n nf M is the set of ll strings on Σ ccepted y M nd is defined s L(M) = {w Σ : δ (q 0,w) F }. Exmple 10: In the ove nf, L(M) = {(10) n : n 0}. A configurtion is ded configurtion if the trnsition on it is undefined.
Moile Computing nd Softwre Engineering p. 29/5 Resons for Nondeterminism voiding cktrcking helpful in solving prolems esily effective mechnism for descriing some complicted lnguges concisely certin theoreticl results re more esily estlished for nf s
Moile Computing nd Softwre Engineering p. 30/5 Contents Deterministic Finite Automt Nondeterministic Finite Automt Equivlence of Deterministic nd Nondeterministic Finite Automt Reduction of the Numer of Sttes in Finite Automt
Moile Computing nd Softwre Engineering p. 31/5 Nf s v.s. Df s Intuitively, the fmily of ll nf s includes the fmily of ll df s. We re therefore thinking if for ech nf, there exists corresponding df tht ccepts the sme lnguge.
Moile Computing nd Softwre Engineering p. 32/5 Equivlence Two finite utomt M 1 nd M 2 re sid to e equivlent if L(M 1 ) = L(M 2 ). Exmple 11:
Moile Computing nd Softwre Engineering p. 33/5 Theorem Let L e set ccepted y n nf. Then there exists df tht ccepts L. Notes: The proof is constructive nd shows wy to convert.
Deriving Df from Nf Let M = (Q, Σ,δ,q 0,F) e n nf ccepting L. We cn define df M = (Q, Σ,δ,q 0,F ) s follows: Q = 2 Q ; q 0 = [q 0 ]; F = set of ll sttes in Q contining stte of F ; For n element q = [q 1,q 2,,q i ] Q, δ (q,) is computed y pplying δ to ech stte of q 1,q 2,,q i, nd tking the union. The resulting union is the corresponding stte of δ (q,) in M. Note: Only the meningful sttes re kept. Tht is, we only reserve the stte hving pth from q 0 to itself in the trnsition grph. Moile Computing nd Softwre Engineering p. 34/5
Moile Computing nd Softwre Engineering p. 35/5 Exmple 12 δ [q 0 ] [q 1,q 2 ] [ ] [q 1,q 2 ] [q 1,q 2 ] [ ] [ ] [q 0 ] [ ]
Moile Computing nd Softwre Engineering p. 36/5 Exmple 13 δ [q 0 ] [q 1 ] [q 2 ] [q 0,q 1 ] [q 1,q 2 ] [q 0,q 1,q 2 ] 0 [q 0,q 1 ] [q 2 ] [ ] [q 0,q 1,q 2 ] [q 2 ] [q 0,q 1,q 2 ] 1 [q 1 ] [q 2 ] [q 2 ] [q 1,q 2 ] [q 2 ] [q 1,q 2 ]
Moile Computing nd Softwre Engineering p. 37/5 Contents Deterministic Finite Automt Nondeterministic Finite Automt Equivlence of Deterministic nd Nondeterministic Finite Automt Reduction of the Numer of Sttes in Finite Automt
Moile Computing nd Softwre Engineering p. 38/5 Stte Minimiztion Any df defines unique lnguge, ut the converse is not true. To minimize the numer of sttes of given df is to determine n equivlent df tht hs s few sttes s possile. For storge efficiency
Unrechle Sttes A stte is unrechle if there is no pth from the strt stte to it. Exmple 14: q 1 q 2 q 3 q 8 q 4 q 5 q 6 q 7, Sttes q 7 nd q 8 re unrechle. For stte minimiztion, sttes q 7 nd q 8 cn e removed. Moile Computing nd Softwre Engineering p. 39/5
Moile Computing nd Softwre Engineering p. 40/5 Removing Unrechle Sttes The set of rechle sttes cn e defined s the closure of {s} under the reltion {(p,q) : δ(p,) = q for some Σ}. computed y the following lgorithm: R := s; while there is stte p R nd Σ such tht δ(p,) R do dd δ(p,) to R.
Moile Computing nd Softwre Engineering p. 41/5 Exmple 15: Removing unrechle sttes in previous df q 1 q 2 q 3 q 4 q 5 q 6, Note: Sttes q 4 nd q 6 re equivlent.
Moile Computing nd Softwre Engineering p. 42/5 Right Congruence (Invrint) Definition: Let L Σ e lnguge, nd let x,y Σ. We sy tht x nd y re equivlent with respective to L, denoted x L y, if for ll z Σ, the following is true: xz L if nd only if yz L. Note: 1. L is n equivlence reltion. 2. x L y if either oth strings elong to L or neither is in L. 3. ppending ny string to oth strings results in two strings tht re either oth in L or oth not in L.
Moile Computing nd Softwre Engineering p. 43/5 Exmple 16: q 1 q 2 q 3 q 4 q 5 q 6, L = ( ) 1. If x is string, we denote y [x] the equivlent clss with respective to L to which x elongs. 2. L hs four equivlent clsses: (1) [e] = L, (2) [] = L, (3) [] = L, (4) [] = L( )Σ
Equivlence Reltion on Df s Definition: Let M = (Q, Σ,δ,s,F) e df. We sy tht two strings x,y Σ re equivlent with respective to M, denoted x M y, if, intuitively, they oth drive M from s to the sme stte. Formlly, x M y if there is stte q such tht (s,x) M (q,e) nd (s,y) M (q,e). Note: 1. M is n equivlence reltion. 2. Its equivlence clsses cn e identified y the sttes of M. 3. We denote the equivlence clss corresponding to stte q of M s E q. Moile Computing nd Softwre Engineering p. 44/5
Moile Computing nd Softwre Engineering p. 45/5 Exmple 17: q 1 q 2 q 3 q 4 q 5 q 6, L = ( ) The equivlence clsses of M re: (1) E q1 = (), (2) E q2 = L, (3) E q3 = L, (4) E q4 = (), (5) E q5 = L( )Σ, (6) E q6 = () L.
Moile Computing nd Softwre Engineering p. 46/5 Theorem Theorem: For ny df M = (Q, Σ,δ,s,F) nd ny strings x,y Σ, if x M y, then x L y. M is refinement of L. Exmple 18: Clsses E q5 nd [] coincide, while clsses E q1 nd E q3 re oth susets of [e]. Note: The numer of equivlence clsses of L under is nturl lower ound on the numer of sttes of ny df ccepting L.
Moile Computing nd Softwre Engineering p. 47/5 The Myhill-Nerode Theorem Theorem: Let L Σ e regulr lnguge. Then there is df with precisely s mny sttes s there re equivlence clsses in L tht ccepts L. Exmple 19: The stndrd utomton corresponding the L = ( ) is s follows nd hs four sttes. [e] [] {q 1, q 3 } {q 2 } {q 4, q 6 } {q 5 } [] [,],
Moile Computing nd Softwre Engineering p. 48/5 Corollry Corollry: A lnguge L is regulr if nd only if L hs finitely mny equivlence clsses. This corollry provides wy to for proving tht lnguge is not regulr. Exmple 20: L = { n n : n 1} is not regulr.
Moile Computing nd Softwre Engineering p. 49/5 Indistinguishle Sttes Let M = (Q, Σ,δ,s,F) e df. Definition: Define reltion A M Q Σ s (q,z) A M δ (q,z) F. We cll two sttes p,q Q equivlent (or, indistinguishle), denoted p q if for ll w Σ, (q,w) A M (p,w) A M. Two sttes which re not equivlent re distinguishle.
Moile Computing nd Softwre Engineering p. 50/5 Notes on Indistinguishle Sttes if two sttes re equivlent, then the corresponding equivlence clsses of M re susets of the sme equivlence clss of L. the equivlence clsses of re precisely those sets of stte of M tht must clumped together in order to otined the stndrd utomton of L(M).
Moile Computing nd Softwre Engineering p. 51/5 Sequence of Equivlence Reltions Definition: For two sttes p,q Q, p n q if the following is true: Note: (q,w) A M (p,w) A M, w with w n. n is corser equivlence reltion thn. Ech equivlence reltion in 0, 1, 2,... is refinement of the previous one. There re precisely two equivlence clsses of 0 : F nd Q F.
Moile Computing nd Softwre Engineering p. 52/5 Deriving n Lemm: For ny two sttes p,q Q nd ny integer n 1, p n q if nd only if () p n 1 q, nd () for ll Σ, δ(q,) n 1 δ(p,). Note: This lemm suggests wy to compute
Moile Computing nd Softwre Engineering p. 53/5 Constructing the Miniml df Algorithm: Initilly the equivlence clsses of 0 re F nd Q F ; repet for n = 1, 2,... Compute the equivlence clsses of n from those of n 1 until n is the sme s n 1.
Moile Computing nd Softwre Engineering p. 54/5 Exmple 21: Agin, we consider the following exmple q 1 q 2 q 3 q 4 q 5 q 6,