4CS4-6:Theory of Computtion(Closure on Reg. Lngs., regex to NDFA, DFA to regex) Prof. K.R. Chowdhry Lecture 08: Fe. 08, 2019 : Professor of CS Disclimer: These notes hve not een sujected to the usul scrutiny reserved for forml pulictions. They my e distriuted outside this clss only with the permission of the Instructor. 8.1 Closure properties of Regulr lnguges We know tht regulr lnguges re expressile s Sets. The set opertions of union nd conctention correspond to lnguge opertions. Hence regulr lnguges hve closure properties under these opertions. However, there is no opertion on sets corresponding to Kleene Str opertion on Regulr Sets. In fct, this cn e considered s repeted conctention opertion. This section explores the closure properties of regulr lnguges corresponding to other opertions in sets, like intersections, complementtion, difference, nd symmetric difference. Theorem 8.1 The clss of lnguges ccepted y Finite Automt re closed under the opertion of (i) Complementtion, nd (ii) Intersection. Proof: I. Complementtion: Let the M = (Q,Σ,δ,s 0,F) e the finite utomton nd L = L(M) e the lnguge ccepted y it. The complement of M is M. Let M e the FA tht ccepts the lnguge L. Oviously M would ccept lnguges tht re not ccepted y M nd vice-vers. Therefore, the finl sttes in M re those tht re non-finl sttes in M nd, wht re non-finl sttes in M re finl sttes in M. In other words, the finl sttes nd non-finl sttes re interchnged in M nd M. However, the totl sttes remin the sme in oth the utomt. Thus, L = L(M) = Σ L(M), nd M = (Q,Σ,δ,s 0,Q F). Since M is finite utomton, the L lnguge ccepted y this is regulr. This shows tht complement of regulr lnguge is lso regulr, hence the regulr lnguges re closed under the opertion of complementtion. II. Intersection: LetusssumethtintersectionofregulrlngugesL 1 ndl 2 islnguge L = L 1 L 2. We wnt to determine whether L is regulr. Using De Morgn s theorem we rewrite the expression for intersection of L 1 nd L 2 s, L 1 L 2 = (L 1 L 2 ) (8.1) This mens tht, the lnguge L = L 1 L 2 consists ll words tht re in (L 1 L 2 ). In other words L consists those words tht re not in (L 1 L 2 ), tht is they re neither in L 1 8-1
8-2 Lecture 08: Fe. 08, 2019 nor in L 2. Since L 1 nd L 2 re regulr, hence L 1 nd L 2 re lso regulr. In ddition, their union (L 2 L 2 ) is regulr nd its complement (L 1 L 2 ) too. Hence, the intersection of two regulr lnguges results to regulr lnguge. Therefore, the regulr lnguges re closed under the opertion of intersection. Regrding the sttes of utomtion recognizing the lnguge (L 1 L 2 ), the following proof clrifies it: Let L 1 = L(M 1 ), nd L 2 = L(M 2 ), where M 1 = (Q 1,Σ 1,δ 1,s 1,F 1 ), nd M 2 = (Q 2,Σ 2,δ 2,s 2,F 2 ). A comined utomtion out of M 1 nd M 2 cn e constructed s M = (Q,Σ,δ,(s 1,s 2 ),F), where Q = Q 1 Q 2 = {(,q j ) Q 1,q j Q 2 }. The trnsition function δ is defined in such wy tht M is in stte (,q j ) whenever the M 1 is in stte nd M 2 is in stte q j. Thus, the stte trnsitions in M re defined s, δ((,qj ),) = (q k,q l ) If nd only if, δ 1 (,) = q k, nd, δ 2 (q j,) = q l. Similrly F is defined s the set of ll the pirs (,q j ), such tht F 1, nd q j F 2. Exmple 8.2 Consider regulr expression r = ( + ), nd construct finite utomtion which ccepts the lnguge L(r). Let us first consider only prt of the regulr expression, +, nd let = nd =. Therefore, =. Applying the rule of conctention from Theorem??, the FA which cn recognize L( ) is given s shown in Figure 8.1(). q 1 () q 7 q 8 q 9 0 () 1 q 1 2 q 7 q 8 q 9 0 (c) Figure 8.1: () FA for =, () FA for =, (c) FA for r 3 = + Similrly, the FA which cn recognize L( ) is shown in figure 8.1(). Let r 3 = +. By pplying the union opertion on the set of lnguges L( ) nd L( ), the new lnguge otined is L(r 3 ) = L( )L( ). The finite utomt for L(r 3 ) is s shown in figure 8.1(c).
Lecture 08: Fe. 08, 2019 8-3 Finlly, pplying the opertion of Kleene Str on the lnguge L(r 3 ) we get (L(r 3 )) = L(r3 ). The finite utomton for this is shown in figure 8.2. The strt stte for the NFA M is nd finl stte is 2. 1 1 q 7 q 8 q 9 0 2 Figure 8.2: FA for regulr expression (+) We hve noted tht, the min prt in deriving FA for given regulr expression is to find suexpressions in the regulr expressions, in the form of r+s, r q nd r. Therefore, prentheses re essentil for complex expression to mke precedence explicit. In this wy, we cn construct n NFA for ny given regulr expression. This NFA cn e converted into DFA s discussed in the previous clsses. 8.1.1 Generlized NFA Unlike the NFA, which reds one symol t time, generlized NFA (GNFA) reds lock of symols from the input, nd moves long the trnsition rrows connecting two sttes when lock of symols is red. This lock of symols is nothing ut regulr expression. The GNFA ccepts the input string corresponding to the lel if it leds to the finl stte when the string is completely red. A GNFA is formlly defined s, where, G = (Q,Σ,ρ,,q f ), (8.2) Q is finite set of sttes, Σ is lphet set, Q is initil stte, q f F is single finl stte, ρ : (Q {q f }) (Q { }) R Σ. In the ove R Σ is set of ll regulr expressions over the lphet set Σ. An input string is ccepted if there is wy to red the input string such tht the GNFA is in the ccepting (finl) stte fter processing of the entire string. A GNFA hs one strt stte (q strt ) with no incoming rrows, nd one ccepting stte (q finl ) with no outgoing rrows.
8-4 Lecture 08: Fe. 08, 2019 We rek this procedure into two prts, using GNFA. First we show how to convert DFAs into GNFAs, nd then GNFAs into regulr expressions. The method is clled stte removl method (see figure 8.3). DFA GNFA REG. EXP. Figure 8.3: Conversion of DFA to Regulr Expression The process of constructing regulr expression requires the finite utomton with only one initil nd one finl stte. To chieve this, we eliminte one stte t time from the initil trnsition-digrm. The resulting successive trnsition grphs re with r rrows, where r is regulr expression. Converting DFA into GNFA: 1. Add new strt stte (q strt ), with -trnsition from this to ). 2. Add new ccept stte (q ccept ), with -trnsition from ech of the finl stte to q ccept. 3. If n edge in originl DFA hs multiple lels, replce the lels with regulr expression formed y the union of these lels. 4. If originlly there is no rrow etween two sttes, dd new edge with lel φ. Converting GNFA into Regulr Expression: 1. We interctively remove sttes one y one in GNFA such tht, fter ech stte is removed, the new GNFA otined will recognize the sme lnguge s the previous one. 2. When sttes in the GNFA re two, we hve chieved the regulr expression for the given DFA, represented y the lel of the edge connecting these sttes. Removing stte q (not q strt or q ccept ) in the process of construction of GNFA is demonstrted in Figure 8.3. The new lel on the rrow from to q j ecomes Regulr Expression tht descries ll strings tht tke GNFA from to q j either directly or vi q (the removed stte). The Figure 8.4 shows n exmple of conversion of DFA to n equivlent GNFA. r q j r 3 r+(r1 r 2 r 3) f () () q j Figure 8.4: Conversion of DFA to GNFA: () DFA, () Equivlent GNFA.
Lecture 08: Fe. 08, 2019 8-5 Exmple 8.3 Otin Regulr expressions for given finite Automt using the pproch of GNFA construction. Vrious possile cses re shown for otining regulr expressions for given finite utomt. Ech cse shows n equivlent utomton for given regulr expression. Cse-I. A finite utomton ccepts the expression + + +r n (figure 8.5()). Replce it y the one shown in figure 8.5(). + +...+r n f f r n () () Figure 8.5: FA to Regulr expression(cse-i) Cse-II). If the trnsition digrm is like the one shown in figure 8.6(), replce it y tht in figure 8.6().,,...,r n r1 + +...+r n () () Figure 8.6: FA to Regulr expression(cse-ii) Cse-III. If there re more thn one finl sttes, sy, f 1, f 2, f 3 (figure 8.7(), then replce them with single finl stte (figure 8.7()). f 1 f 1 f 2 f 2 f f 3 f 3 () () Figure 8.7: FA to Regulr expression(cse-iii) Cse-IV. Simplify the trnsition digrm of three sttes s in figure 8.8() nd replce it y figure 8.8().
8-6 Lecture 08: Fe. 08, 2019 ( ) r3 r 3 q j q k qi q k () () Figure 8.8: FA to Regulr expression(cse-iv) Cse-V. Replce the old trnsition digrm in figure 8.9() y shown in figure 8.9(). ( ) r3 r 3 q j q k q k () () Figure 8.9: FA to Regulr expression(cse-v). Cse VI. Replce the old trnsition digrm in figure 8.10() y tht in figure 8.10(). q k q k r 3 q j r 3 r 3 q l q l () () Figure 8.10: FA to Regulr expression(cse-vi) In the similr wy, using these primitives, ny complex utomton cn e reduced to GNFA, which ultimtely produce the regulr expression. Exmple 8.4 Find the regulr expression for the utomton shown in the Figure 8.11. Figure 8.11: Finite utomton The reduction steps discussed ove mke use of rules discussed in Lemm??. The resultnt regulr expression is (+ ).
Lecture 08: Fe. 08, 2019 8-7 () () (c) (d), (e) q0 (+ ) Figure 8.12: Steps()-(e) re for construction of regulr expression from Finite utomton. The reduction steps for the finite utomtion, to otin the regulr expression, fter conversion of FA into GNFA, re shown in Figure 8.12. Using the primitives discussed ove in most cses, DFA cn e converted into GNFA, nd then to regulr expression. However, some times the ove method is too cumersome, nd even my not e universl solution some times.