25. Finite Automt AUTOMATA AND LANGUAGES A system of computtion tht only hs finite numer of possile sttes cn e modeled using finite utomton A finite utomton is often illustrted s stte digrm d d d. d q q q 2 q 3 E E d q 4 q 6 d +, - d q 5 26 Definition.5: Finite Automton A finite utomton is 5-tuple M = (Q,,, q, F), where Q is finite set clled the sttes, is finite set clled the lphet, : Q Q is the trnsition function, q Q is the strt stte, nd F Q is the set of (ccepting) finl sttes. A mchine M ccepts the string w = w w 2...w n n if sequence of sttes r, r,, r n in Q exists s.t. r = q, (r i, w i+ ) = r i+, i =,, n, r n F.
27 The lnguge recognized y M is L(M)={w* M ccepts w } A lnguge is clled regulr lnguge, if some finite utomton recognizes it Bsic opertions on lnguges A nd B re Union A B = { x x A x B }, Conctention AB = { xy x A y B } nd (Kleene) Str (closure) A* = { x x 2 x k k x i A i } 28 Properties of Regulr Lnguges Theorem.25 The clss of regulr lnguges is closed under the union opertion. In other words, if A nd A 2 re regulr lnguges, so is A A 2. Theorem.26 The clss of regulr lnguges is closed under the conctention opertion. DFA = Deterministic Finite Automton 2
29.. Minimiztion of DFAs Two utomt tht recognize exctly the sme lnguge re equivlent with ech other A finite utomton is miniml if it hs the smllest numer of sttes mong equivlent utomt An utomton tht hs more sttes thn in n equivlent miniml utomton is clled redundnt Algorithms producing utomt do not lwys generte miniml utomton Hndling miniml utomton is more efficient thn tht of redundnt utomton 3 Algorithm MINIMIZE Input: DFA M = (Q,,, q, F).. Remove ll sttes of M tht re unrechle from the strt stte. 2. Construct the following undirected grph G whose nodes re the sttes of M. 3. Plce n edge in G connecting every ccept stte with every nonccept stte. Add dditionl edges s follows. 4. Repet until no new edges re dded to G:. For every pir q, r Q, q r, nd every : dd the edge (q, r) to G if ((q, ), (r, )) is n edge of G. 2. For ech stte q Q let [q] e the collection of sttes 2. [q]= { q} {rq no edge joins q nd r in G }. 5. Form new DFA M = (Q,,, q, F ), where Q' = { [q] q Q }, (removing doules) '([q], ) = [(q, )], for every q Q nd, q = [q ] nd F' = { [q] q F }. 6. Output M. 3
3 2 4 6 3 5 32 [2] 2 4 [4, 5] [, 3] 3 [, 3] 5 [4, 5] 2 3 4 5 4
33 The End Result An utomton M tht is equivlent with the input utomton M', such tht it hs the minimum numer of sttes Automton M' is unique (up to the nming of the sttes)., 3 2 4, 5 34.2 Nondeterministic Finite Automt (NFAs) In n NFA stte cn hve mny possile lterntive trnsitions with the sme symol of the lphet Also -trnsitions re llowed Implementing nondeterministic ehvior is not strightforwrd (though possile), ut s modeling tool it is quite useful Vi NFAs we cn connect DFAs nd regulr expressions 5
35,,, q q q 2 q 3 36 The definition of n utomton requires the trnsition function to e function On the other hnd, in n NFA the trnsition function should get mpped to set of vlues An NFA ccepts string if sequence of possile sttes leds to finl stte. Only if no such sequence exists will the NFA reject the input string E.g. the previous NFA ccepts the string ecuse it cn e processed s follows (q, ) (q, ) (q, ) (q 2, ) (q 3,) (q 3, ) (q 3, ) 6
37,,, q q q 2 q 3 On the other hnd, we cn end up in rejecting stte: (q, ) (q, ) (q, ) (q, ) (q, ) (q, ) (q 2, ) 38 Definition of n NFA Let P(A) = { B B A } denote the power set of the set A nd for n lphet = { } A nondeterministic finite utomton is 5-tuple N = (Q,,, q, F) Q is finite set of sttes, is finite lphet, : Q P(Q) is the (set-vlued) trnsition function, tht lso llows -trnsitions q Q is the strt stte, nd F Q is the set of (ccepting) finl sttes 7
39 The trnsition function of the previous utomton is q {q } {q q } Ø q {q 2 } Ø {q 2 } q 2 Ø {q 3 } Ø q 3 {q 3 } {q 3 } Ø Now we cn esily express the error stte s n empty set of possile next sttes 4 An NFA N = (Q,,q, F) ccepts the string w, If we cn write it s w = y y 2 y m m nd sequence of sttes r, r,, r m exists in Q s.t. r = q, r i+ (r i, y i+ ), i =,, m, nd r m F. DFAs re specil cse of NFAs ll lnguges tht cn e recognized using the former cn lso e recognized using the ltter Also the other wy round: DFAs nd NFAs recognize the sme set of lnguges 8
4 Theorem.39 Let A = L(N) e the lnguge recognized y some NFA N. There exists DFA M such tht L(M) = A Proof. Let N = (Q,,q, F). We construct DFA M =(Q,,q, F ) tht simultes the computtion of N in prllel in ll its possile sttes t ll times. Let us first consider the esier sitution where N hs no rrows. Every stte of M is set of sttes of N Q' = P(Q) q ' = { q } F' ={RQ R contins n ccept stte r F } '(R, ) = r R (r, ) 42 Without rrows,,, q q q 2 q 3 {q } {q, q } {q,q 2 } {q, q, q 3 } {q, q 2, q 3 } {q, q 3 } 9
43 After Minimiztion R R R 2 R 3, 44 Let us check tht L(M) = L(N). The equivlence of the lnguges follows when we prove for ll x * nd r Q tht (q, x) N (r, ) ({q }, x) M (R, ) nd r R, where the nottion (q, x) N (r, ) mens tht in utomton N we cn process the string x strting from stte q so tht we end up in stte r nd there re no more symols to process (). We prove it using induction over the length of the string x :. Bsis: x = : (q, ) N (r, ) r = q. Similrly ({q }, ) M (R, ) R = {q }
45 2. Induction hypothesis: the clim holds when x k 3. x = k+: Then x = y for some y, y = k, for which the clim holds y the induction hypothesis. Now, (q, x) = (q, y) N (r, ) r' Q s.t. (q, y) N (r', ) nd (r', ) N (r, ) = in one trnsition r' Q s.t. (q, y) N (r', ) nd (r', ) N (r, ) By induction hypothesis we get r Q s.t. ({q }, y) M (R', ) nd r' R nd r (r', ) Rerrnging yields ({q }, y) M (R', ) nd r' R' s.t. r (r', ) By the definition of the trnsition function ' 46 ({q }, y) M (R', ) nd r r' R' (r', ) = '(R', ) Let us return nd nme '(R', ) ({q }, y) M (R', ) nd r '(R', ) = R ({q }, y) M (R', ) nd (R', ) M (R, ) nd r R Concluding ({q }, x) = ({q }, y) M (R, ) nd r R Which completes the proof of the clim
47 In order to tke the rrows into ccount, we compute for ech stte R Q of M the collection of sttes tht cn e reched from R y going only long rrows: E(R)={q q cn e reched from R y trveling long or more rrows } It is enough to modify the trnsition function of M nd strt stte to tke the rrows into ccount: '(R, ) = rr E(r, )) q ' = E({q }) 48 With rrows,,, q q q 2 q 3 {q } {q, q,q 2 } {q q 3 } {q,q 2, q 3 } {q,q 2 } {q, q 3 } 2
49 After Minimiztion R R R 2 R 3, 5 Theorem.45 The clss of regulr lnguges is closed under the union opertion. Proof. Let the lnguges A nd A 2 e regulr. Then, there exists (nondeterministic) finite utomt N = (Q,,q, F ) nd N 2 = (Q 2, 2,q 2, F 2 ), which recognize these two lnguges. Let us construct n utomton N = (Q,,q, F) for recognizing the lnguge A A 2. Q = { q } Q Q 2, The strt stte of N is q, ( q,), q Q F = F F 2 nd 2( q,), q Q2 ( q,) { q, q2}, q q nd Ø, q q nd 3
5 q N q q 2 N 2 52 Theorem.47 The clss of regulr lnguges is closed under the conctention opertion. Proof. Let the lnguges A nd A 2 e regulr. Then, there exists (nondeterministic) finite utomt N = (Q,,q,F )nd N 2 = (Q 2, 2,q 2,F 2 ),which recognize these two lnguges. Let us construct n utomton N = (Q,,q,F 2 )for recognizing A A 2. Q = Q Q 2, The strt stte of N is q, The finl sttes of N re those in F 2 nd ( q, ), ( q, ), ( q, ) ( q, ) { q2}, 2( q, ), qq nd q F q F nd q F nd qq 2 4
53 q q 2 N N 2 54 Theorem.49. The clss of regulr lnguges is closed under the str opertion. Proof. Let the lnguge A e regulr. Then, there exists (nondeterministic) finite utomton N = (Q,,, q, F ), which recognizes the lnguge. Let us construct n utomton N = (Q,,, q, F) for recognizing A*. Q = { q } Q, The new strt stte of N is q, F = { q } F nd ( q, ), ( q, ), ( q, ) ( q, ) { q}, { } Ø, q q Q nd q F q F nd q F nd q q q q nd nd 5
55 q q N 6