Chpte 7 Kleene s Theoem 7.1 Kleene s Theoem The following theoem is the most impotnt nd fundmentl esult in the theoy of FA s: Theoem 6 Any lnguge tht cn e defined y eithe egul expession, o finite utomt, o tnsition gph cn e defined y ll thee methods. Poof. The poof hs thee pts: Pt 1: (FA TG) Evey lnguge tht cn e defined y n FA cn lso e defined y tnsition gph. Pt 2: (TG RegExp) Evey lnguge tht cn e defined y tnsition gph cn lso e defined y egul expession. Pt 3: (RegExp FA) Evey lnguge tht cn e defined y egul expession cn lso e defined y n FA. 7-1
CHAPTER 7. KLEENE S THEOREM 7-2 7.2 Poof of Pt 1: FA TG We peviously sw tht evey FA is lso tnsition gph. Hence, ny lnguge tht hs een defined y FA cn lso e defined y tnsition gph. 7.3 Poof of Pt 2: TG RegExp We will give constuctive lgoithm fo poving pt 2. Thus, we will descie n lgoithm to tke ny tnsition gph T nd fom egul expession coesponding to it. The lgoithm will wok fo ny tnsition gph T. The lgoithm will finish in finite time. An oveview of the lgoithm is s follows: Stt with ny tnsition gph T. Fist, tnsfom it into n equivlent tnsition gph hving only one stt stte nd one finl stte. In ech following step, eliminte eithe some sttes o some cs y tnsfoming the TG into nothe equivlent one. We do this y eplcing the stings lelling cs with egul expessions. We cn tvese n c lelled with egul expession using ny sting tht cn e geneted y the egul expession. End up with TG hving only two sttes, stt nd finl, nd one c going fom stt to finl. The finl TG will hve egul expession on its one c Note tht in ech step we eliminte some sttes o cs. Since the oiginl TG hs finite nume of sttes nd cs, the lgoithm will teminte in finite nume of itetions.
CHAPTER 7. KLEENE S THEOREM 7-3 Algoithm: 1. If T hs moe thn one stt stte, dd new stte nd dd cs leled Λ going to ech of the oiginl stt sttes. 1-3 1 3 2-4... => - /\ /\ 2 4... 2. If T hs moe thn one finl stte, dd new stte nd dd cs leled Λ going fom ech of the oiginl finl sttes to the new stte. Need to mke sue the finl stte is diffeent thn the stt stte.... +, =>... /\, +, +, /\
CHAPTER 7. KLEENE S THEOREM 7-4 3. Now we give n itetive pocedue fo eliminting sttes nd cs () If T hs some stte with n > 1 loops cicling ck to itself, whee the loops e leled with egul expessions 1, 2,..., n, then eplce the n loops with single loop leled with the egul expession 1 + 2 + + n. 1 2 => + + 1 2 3 3 () If two sttes e connected y n > 1 diect cs in the sme diection, whee the cs e lelled with the egul expessions 1, 2,..., n, then eplce the n cs with single c leled with the egul expession 1 + 2 + + n. 1 2 => + 1 2
CHAPTER 7. KLEENE S THEOREM 7-5 (c) Bypss opetion: i. If thee e thee sttes x, y, z such tht thee is n c fom x to y lelled with the egul expession 1 nd n c fom y to z lelled with the egul expession 2, then eplce the two cs nd the stte y with single c fom x to z lelled with the egul expession 1 2. 1 x y 2 z => x 1 2 z 3 1 x y 2 z x 1 3 * 2 => z
CHAPTER 7. KLEENE S THEOREM 7-6 ii. If thee e n+2 sttes x, y, z 1, z 2,..., z n such tht thee is n c fom x to y lelled with the egul expession 0, nd n c fom y to z i, i = 1, 2,..., n, lelled with the egul expession i, nd n c fom y ck to itself lelled with egul expession n+1, then eplce the n + 1 oiginl cs nd the stte y with n cs fom x to z i, i = 1, 2,..., n, ech lelled with the egul expession 0 n+1 i. x 0 n+1 y 1 2 n z 1 z 2. z n => 0 * n+1 1 x 0 * n+1 2 0 * n+1 n z 1 z 2. z n iii. If ny othe cs led diectly to y, divet them diectly to the z i s.
CHAPTER 7. KLEENE S THEOREM 7-7 iv. Need to mke sue tht ll pths possile in the oiginl TG e still possile fte the ypss opetion. Exmple w 1 x 2 3 y 4 5 z 1 ( 2 3 )* 4 => w z 1 2 ( 3 2 )* 5 => w z 1 ( 2 3 )* 4 + 1 2 ( 3 2 )* 5
CHAPTER 7. KLEENE S THEOREM 7-8 Exmple: Suppose we wnt to get id of stte y. Need to ccount fo ll pths tht go though stte y. Thee e cs coming fom x, w, nd z going into y. Thee e cs fom y to x nd z. Thus, we need to ccount fo ech possile pth fom stte hving n c into y (i.e., x, w, z) to ech stte hving n c fom y (i.e., x, z) Thus, we need to ccount fo the pths fom x to y to x, which hs egul expession 1 2 5 x to y to z, which hs egul expession 1 2 3 w to y to x, which hs egul expession 7 2 5 w to y to z, which hs egul expession 7 2 3 z to y to x, which hs egul expession 6 2 5 z to y to z, which hs egul expession 6 2 3 Thus, fte eliminting stte y, we get the following: v. Neve delete the unique stt o finl stte.
CHAPTER 7. KLEENE S THEOREM 7-9 Exmple: 1-2 3 /\ 4 5 + /\, => 1-5 + + => 1 - *(++)(+)* 5 +
CHAPTER 7. KLEENE S THEOREM 7-10 Exmple: 1-2 - 3 + => 4 5 +, - /\ /\ 1 2 3 /\ 4 5 /\ + + => - => - /\ /\+ 2 * 2 * * *(+)* 4 5 /\ + 4 (+)* + +
CHAPTER 7. KLEENE S THEOREM 7-11 => - /\+ => - /\+ 2 *(/\+(+)*) 2 *(/\+(+)*) 4 (+)* + 4 (+)* + ()*(+)* + ()**(/\+(+)*) (/\+)(()**(/\+(+)*) + ()*(+)*) => - ()*(+)* + ()**(/\+(+)*) + => (/\+)(()**(/\+(+)*) + ()*(+)*) + ()*(+)* + ()**(/\+(+)*)
CHAPTER 7. KLEENE S THEOREM 7-12 7.4 Poof of Pt 3: RegExp FA To show: evey lnguge tht cn e defined y egul expession cn lso e defined y FA. We will do this y using ecusive definition nd constuctive lgoithm. Recll evey egul expession cn e uilt up fom the lettes of the lphet nd Λ nd. Also, given some existing egul expessions, we cn uild new egul expessions y pplying the following opetions: 1. union (+) 2. conctention 3. closue (Kleene st) We will not include + in ou discussion hee, ut this will not e polem since + =.
CHAPTER 7. KLEENE S THEOREM 7-13 Recll tht we hd the following ecusive definition fo egul expessions: Rule 1: If x Σ, then x is egul expession. Λ is egul expession. is egul expession. Rule 2: If 1 nd 2 e egul expessions, then 1 + 2 is egul expession. Rule 3: If 1 nd 2 e egul expessions, then 1 2 is egul expession. Rule 4: If 1 is egul expession, then 1 is egul expession. Bsed on the ove ecusive definition fo egul expessions, we hve the following ecusive definition fo FA s ssocited with egul expessions: Rule 1: Thee is n FA tht ccepts the lnguge L defined y the egul expession x; i.e., L = {x}, whee x Σ, so lnguge L consists of only single wod nd tht wod is the single lette x. Thee is n FA tht ccepts the lnguge defined y egul expession Λ; i.e., the lnguge {Λ}. Thee is n FA defined y the egul expession ; i.e., the lnguge with no wods, which is. Rule 2: If thee is n FA clled F A 1 tht ccepts the lnguge defined y the egul expession 1 nd thee is n FA clled F A 2 tht ccepts the lnguge defined y the egul expession 2, then thee is n FA clled F A 3 tht ccepts the lnguge defined y the egul expession 1 + 2. Rule 3: If thee is n FA clled F A 1 tht ccepts the lnguge defined y the egul expession 1 nd thee is n FA clled F A 2 tht ccepts the lnguge defined y the egul expession 2, then thee is n FA clled F A 3 tht ccepts the lnguge defined y the egul expession 1 2, which is the conctention. Rule 4: If thee is n FA clled F A 1 tht ccepts the lnguge defined y the egul expession 1, then thee is n FA clled F A 2 tht ccepts the lnguge defined y the egul expession 1.
CHAPTER 7. KLEENE S THEOREM 7-14 Let s now show tht ech of the ules hold y constuction: Rule 1: Thee is n FA tht ccepts the lnguge L defined y the egul expession x; i.e., L = {x}, whee x Σ. Thee is n FA tht ccepts lnguge defined y the egul expession Λ. Thee is n FA tht ccepts the lnguge defined y the egul expession. If x Σ, then the following FA ccepts the lnguge {x}: x - + - {x} An FA tht ccepts the lnguge {Λ} is + _ An FA tht ccepts the lnguge is -
CHAPTER 7. KLEENE S THEOREM 7-15 Rule 2: If thee is n FA clled F A 1 tht ccepts the lnguge defined y the egul expession 1 nd thee is n FA clled F A 2 tht ccepts the lnguge defined y the egul expession 2, then thee is n FA clled F A 3 tht ccepts the lnguge defined y the egul expession 1 + 2. Suppose egul expessions 1 nd 2 e defined with espect to common lphet Σ. Let L 1 e the lnguge geneted y egul expession 1. L 1 hs finite utomton F A 1. Let L 2 e the lnguge geneted y egul expession 2. L 2 hs finite utomton F A 2. Regul expession 1 + 2 genetes the lnguge L 1 + L 2. Recll L 1 + L 2 = {w Σ : w L 1 o w L 2 }. Thus, w L 1 + L 2 if nd only if w is ccepted y eithe F A 1 o F A 2 (o oth). We need F A 3 to ccept sting if the sting is ccepted y F A 1 o F A 2 o oth. We do this y constucting new mchine F A 3 tht simultneously keeps tck of whee the input would e if it wee unning on F A 1 nd whee the input would e if it wee unning on F A 2. Suppose F A 1 hs sttes x 1, x 2,..., x m, nd F A 2 hs sttes y 1, y 2,..., y n. Assume tht x 1 is the stt stte of F A 1 nd tht y 1 is the stt stte of F A 2. We will cete F A 3 with sttes of the fom (x i, y j ). The nume of sttes in F A 3 is t most mn, whee m is the nume of sttes in F A 1 nd n is the nume of sttes in F A 2. Ech stte in F A 3 coesponds to stte in F A 1 nd stte in F A 2. F A 3 ccepts sting w if nd only if eithe F A 1 o F A 2 ccepts w. So finl sttes of F A 3 e those sttes (x, y) such tht x is finl stte of F A 1 o y is finl stte of F A 2.
CHAPTER 7. KLEENE S THEOREM 7-16 We use the following lgoithm to constuct F A 3 fom F A 1 nd F A 2. Suppose tht Σ is the lphet fo oth F A 1 nd F A 2. Given F A 1 = (K 1, Σ, π 1, s 1, F 1 ) with Set of sttes K 1 = {x 1, x 2,..., x m } s 1 = x 1 is the initil stte F 1 K 1 is the set of finl sttes of F A 1. π 1 : K 1 Σ K 1 is the tnsition function fo F A 1. Given F A 2 = (K 2, Σ, π 2, s 2, F 2 ) with Set of sttes K 2 = {y 1, y 2,..., y n } s 2 = y 1 is the initil stte F 2 K 2 is the set of finl sttes of F A 2. π 2 : K 2 Σ K 2 is the tnsition function fo F A 2. We then define F A 3 = (K 3, Σ, π 3, s 3, F 3 ) with Set of sttes K 3 = K 1 K 2 = {(x, y) : x K 1, y K 2 } The lphet of F A 3 is Σ. F A 3 hs tnsition function π 3 : K 3 Σ K 3 with The initil stte s 3 = (s 1, s 2 ). The set of finl sttes π 3 ((x, y), l) = (π 1 (x, l), π 2 (y, l)). F 3 = {(x, y) K 1 K 2 : x F 1 o y F 2 }. Since K 3 = K 1 K 2, the nume of sttes in the new mchine F A 3 is K 3 = K 1 K 2. But we cn leve out stte (x, y) K 1 K 2 fom K 3 if (x, y) is not echle fom F A 3 s initil stte (s 1, s 2 ). This would esult in fewe sttes in K 3, ut still we hve K 1 K 2 s n uppe ound fo K 3 ; i.e., K 3 K 1 K 2.
CHAPTER 7. KLEENE S THEOREM 7-17 Exmple: L 1 = { wods with s second lette} with egul expession 1 = ( + )( + ) L 2 = { wods with odd nume of s} with egul expession 2 = ( + ) FA1 fo L1:, x1- x2 x3+, FA2 fo L2: y1- y2+ x4, FA3 fo L1+L2: x1,y1- x3,y1+ x2,y2+ x3,y2+ x4,y1 x2,y1 x4,y2+
CHAPTER 7. KLEENE S THEOREM 7-18 Rule 3: If thee is n FA clled F A 1 tht ccepts the lnguge defined y the egul expession 1 nd thee is n FA clled F A 2 tht ccepts the lnguge defined y the egul expession 2, then thee is n FA clled F A 3 tht ccepts the lnguge defined y the egul expession 1 2. Fo this pt, we need F A 3 to ccept sting if the sting cn e fctoed into two sustings, whee the fist fcto is ccepted y F A 1 nd the second fcto is ccepted y F A 2. One polem is we don t know when we ech the end of the fist fcto nd the eginning of the second fcto. Exmple: L 1 = {wods tht end with } with egul expession 1 = ( + ) L 2 = {wods with odd length} with egul expession 2 = ( + )(( + )( + )) Conside the sting. If we fcto it s ()(), then L 1 ut L 2. Howeve, nothe fctoiztion, ()(), shows tht L 1 L 2 since L 1 nd L 2. FA1 fo L1: x1- x2 FA2 fo L2:, y1- y2+, x3+
CHAPTER 7. KLEENE S THEOREM 7-19 Bsiclly ide of uilding F A 3 fo L 1 L 2 fom F A 1 fo L 1 nd F A 2 fo L 2 : Recll L 1 L 2 = {w = w 1 w 2 : w 1 L 1, w 2 L 2 }. So sting w is in L 1 L 2 if nd only if we cn fcto w = w 1 w 2 such tht w 1 is ccepted y F A 1 nd w 2 is ccepted y F A 2. F A 3 initilly cts like F A 1. When F A 3 hits stte of F A 1, Stt vesion of F A 2. Keep pocessing on F A 1 nd ny pevious vesions of F A 2. We need to keep pocessing on F A 1 ecuse we don t know whee the fist fcto w 1 ends nd the second fcto w 2 egins Finl sttes of F A 3 e those sttes tht hve t lest one finl stte fom F A 2. Moe fomlly, we uild mchine F A 3 in following wy: Suppose tht F A 1 nd F A 2 hve the sme lphet Σ. Let L 1 e lnguge geneted y egul expession 1 nd hving FA F A 1 = (K 1, Σ, π 1, s 1, F 1 ). Let L 2 e lnguge geneted y egul expession 2 nd hving FA F A 2 = (K 2, Σ, π 2, s 2, F 2 ). Definition: Fo ny set S, define 2 S to e the set of ll possile susets of S. Exmple: If S = {,, }, then 2 S = {, {}, {}, {}, {, }, {, }, {, }, {,, }}. Fct: If S <, then 2 S = 2 S ; i.e., thee e 2 S diffeent susets of S. Mchine F A 3 = (K 3, Σ, π 3, s 3, F 3 ) fo L 1 L 2 is s follows: Sttes K 3 = {{x} + Y : x K 1, Y 2 K 2 }; i.e., ech stte of F A 3 is set of sttes, whee exctly one of the sttes is fom F A 1 nd the est (possily none) e fom F A 2. Initil stte s 3 = {s 1 }; i.e., the initil stte of F A 3 is the set consisting of only the initil stte of F A 1.
CHAPTER 7. KLEENE S THEOREM 7-20 Tnsition function π 3 : K 3 Σ K 3 is defined s π 3 ({x, y 1,..., y n }, l) { {π1 (x, l), π = 2 (y 1, l),..., π n (y 2, l)} if π 1 (x, l) F 1, {π 1 (x, l), π 2 (y 1, l),..., π n (y 2, l), s 2 } if π 1 (x, l) F 1, whee {x, y 1,..., y n } K 3, n 0, x K 1, y i K 2 fo i = 1,..., n, nd l Σ. Finl sttes F 3 = {{x, y 1,..., y n } : n 1, y i F 2 fo some i = 1,..., n}. The nume of sttes in F A 3 is K 3 = K 1 2 K 2 = K 1 2 K 2. Actully, we cn leve out fom K 3 ny sttes {x, y 1,..., y n } tht e not echle fom the initil stte s 3. In this cse, K 1 2 K 2 still povides n uppe ound fo K 3 ; i.e., K 3 K 1 2 K 2.
CHAPTER 7. KLEENE S THEOREM 7-21 Exmple: L 1 = {wods tht end with } with egul expession 1 = ( + ) L 2 = {wods with odd length} with egul expession 2 = ( + )(( + )( + )) FA1 fo L1: x1- x2 FA2 fo L2:, y1- y2+, x3+ FA3 fo L1 L2: x1- x2 x2,y2+ x3,y1 x3,y2,y1+ x1,y1,y2+ x1,y1 x1,y2+ x2,y1 x2,y2,y1+
CHAPTER 7. KLEENE S THEOREM 7-22 Rule 4: If thee is n FA clled F A 1 tht ccepts the lnguge defined y the egul expession 1, then thee is n FA clled F A 2 tht ccepts the lnguge defined y the egul expession 1. Bsic ide of how to uild mchine F A 2 : Ech stte of F A 2 coesponds to one o moe sttes of F A 1. F A 2 initilly cts like F A 1. when F A 2 hits stte of F A 1, then F A 2 simultneously keeps tck of how the est of the sting would e pocessed on F A 1 fom whee it left off nd how the est of the sting would e pocessed on F A 1 stting in the stt stte. Wheneve F A 2 hits stte of F A 1, we hve to stt new pocess stting in the stt stte of F A 1 (if no vesion of F A 1 is cuently in its stt stte.) The finl sttes of F A 2 e those sttes which hve coespondence to some finl stte of F A 1. We need to e ceful out mking sue tht F A 2 ccepts Λ. To hve F A 2 ccept Λ, we mke the stt stte of F A 2 lso finl stte. But we need to e ceful when thee e cs going into the stt stte of F A 1.
CHAPTER 7. KLEENE S THEOREM 7-23 Fomlly, we uild the mchine F A 2 fo L 1 s follows: Let L 1 e lnguge geneted y egul expession 1 nd hving finite utomton F A 1 = (K 1, Σ, π 1, s 1, F 1 ). Fo now, ssume tht F A 1 does not hve ny cs enteing the initil stte s 1. Know tht lnguge L 1 is geneted y egul expession 1. Define F A 2 = (K 2, Σ, π 2, s 2, F 2 ) fo L 1 with Sttes K 2 = 2 K 1. Initil stte s 2 = {s 1 }. Tnsition function π 2 : K 2 Σ K 2 with π 2 ({x 1,..., x n }, l) { {π1 (x = 1, l),..., π 1 (x n, l)} if π 1 (x k, l) F 1 fo ll k = 1,..., n, {π 1 (x 1, l),..., π 1 (x n, l), s 1 } if π 1 (x k, l) F 1 fo some k = 1,..., n, whee {x 1,..., x n } K 2, n 1, x i K 1 fo ll i = 1,..., n, nd l Σ. Finl sttes F 2 = {s 1 }+{{x 1,..., x n } : n 1, x i F 1 fo some i = 1,..., n}. The nume of sttes in F A 2 is K 2 = 2 K 1 = 2 K 1. Actully, we cn leve out fom K 2 ny stte {x 1,..., x n } tht is not echle fom the initil stte s 2. In this cse, 2 K 1 still povides n uppe ound fo K 2 ; i.e., K 3 2 K 1.
CHAPTER 7. KLEENE S THEOREM 7-24 Exmple: Conside lnguge L hving egul expession = ( + )(( + ) ) FA fo L: x1- x2+ x3 x4 FA fo L*: x1+ - x4,x2,x1+ x4 x3 x2,x1+ x2,x1,x3+ x3,x4 x1,x2,x3,x4+
CHAPTER 7. KLEENE S THEOREM 7-25 Exmple: Conside lnguge L hving egul expession ( + ) Need to e ceful since we cn etun to the stt stte. FA fo L: x1- x2+ If we lindly pplied pevious method fo constucting F A fo L, we get the following: x1+ - x2,x1+ Polem: Note tht stt stte is finl stte. But this F A ccepts L, nd so this FA is incoect. Polem occus ecuse we cn etun to stt stte in oiginl FA, nd since we mke the stt stte finl stte in new FA.
CHAPTER 7. KLEENE S THEOREM 7-26 Solution: Given oiginl FA F A 1 hving cs going into the initil stte, cete n equivlent FA F A 1 hving no cs going into the initil stte y splitting the oiginl stt stte x 1 of F A 1 into two sttes x 1.1 nd x 1.2 x 1.1 is the new stt stte of F A 1 nd is neve visited gin fte the fist lette of the input sting is ed. x 1.2 in F A 1 coesponds to x 1 fte the fist lette of the input sting is ed. Then un lgoithm to cete FA fo L fom the new FA F A 1. new FA fo L: x1.1- x2+ x1.2 FA fo L*: x1.1+ - x2, x1.1+ x1.2
CHAPTER 7. KLEENE S THEOREM 7-27 7.5 Nondeteministic Finite Automt Definition: A nondeteministic finite utomton (NFA) is given y M = (K, Σ, Π, s, F ), whee 1. K is finite set of sttes. s K is the initil stte, which is denoted pictoilly y, nd thee is exctly one initil stte. F K is set of finl sttes (possily empty), whee ech finl stte is denoted pictoilly y. 2. An lphet Σ of possile input lettes. 3. Π K Σ K is finite set of tnsitions, whee ech tnsition (c) fom one stte to nothe stte is leled with lette l Σ. (We do not llow fo Λ to e the lel of n c since Λ is sting nd not lette of Σ.) We llow fo the possiility of moe thn one edge with the sme lel fom ny stte nd thee my e stte (o sttes) fo which cetin input lettes hve no edge leving tht stte.
CHAPTER 7. KLEENE S THEOREM 7-28 Exmple:, - + Note tht definition of NFA is diffeent fom tht of FA since FA must hve fom ech stte n c leled with ech lette of lphet, while NFA does not. FA is deteministic, while NFA my e nondetemisic. An NFA cn hve epeted lels fom ny single stte. NFA llows fo humn choice to ecome fcto in selecting wy to pocess n input sting. The definition of NFA is diffeent fom tht of TG since TG cn hve cs leled with sustings of lettes while NFA hs cs leled with only lettes. TG cn hve cs leled with Λ while NFA cnnot. TG cn hve moe thn one stt stte while NFA cn only hve one.
CHAPTER 7. KLEENE S THEOREM 7-29 Cn tnsfom ny NFA with epeted lels fom ny single stte to n equivlent TG with no epeted lels fom ny single stte. 2... 1 3 4...... => 1 ^ 2 3 ^ 4 5 5... 7.6 Popeties of NFA Theoem 7 FA = NFA; i.e., ny lnguge definle y NFA is lso definle y deteministic FA nd vice ves. Poof. Note tht Evey FA is n NFA since we cn conside n FA to e n NFA without the ext possile fetues. Evey NFA is TG. Kleene s theoem sttes tht evey TG hs n equivlent FA. NFA useful ecuse pplictions in tificil intelligence (AI).
CHAPTER 7. KLEENE S THEOREM 7-30 given two FA s fo two lnguges with egul expessions 1 nd 2, it is esy to constuct n NFA to ccept lnguge coesponding to egul expession 1 + 2.
CHAPTER 7. KLEENE S THEOREM 7-31 Exmple: FA1: FA2:, + +,, - - +, NFA 1+2, + +,, - +, This woks when neithe of the oiginl FA s hs ny cs going into thei oiginl initil sttes. If one o oth of the oiginl FA s hs n c going into its oiginl initil stte, the newly constucted FA fo the lnguge coesponding
CHAPTER 7. KLEENE S THEOREM 7-32 to egul expession 1 + 2 my e incoect. This is ecuse the new FA my pocess pt of the wod on one of the oiginl FA s nd then pocess the est of the wod on the othe FA, nd then incoectly ccept the wod.