COMP28: Decision, Compuion nd Lnguge Noe These noes re inended minly s supplemen o he lecures nd exooks; hey will e useful for reminders ou noion nd erminology. Some sic noion nd erminology An lphe is finie se of symols. We use A o denoe n lphe; ofen Σ is used. A word over A is defined o e sring of or more memers of A. The sring consising of excly memers of A is clled he empy sring nd will e denoed y ɛ. The empy sring is lso commonly denoed y he symol or λ. The se of ll words over A will e denoed y A. I is someimes convenien o exclude he empy sring nd define A + o e he se given y Exmple. If A = {} hen A + = A \{ɛ}. A = {ɛ,,,,,...} nd A + = {,,,,...} Exmple. If A = {, } hen A = { ɛ,,,,,,,,,,,,,,,,,... }. Noe h lhough he se A is finie he se A is infinie, s long s A. If w = 2... m (where, 2,..., m A) nd w 2 = 2... n (where, 2,..., n A) re words over n lphe A hen heir produc or concenion is he word w w 2 defined y w w 2 = 2... m 2... n. We lso define ɛw = w = wɛ for ll w A The produc of n copies of he word w will e denoed y w n, so for exmple w = ɛ, w = w, w 3 = www. I is esy o verify h his produc is ssociive, i.e. (uv)w = u(vw) for ll u, v, w A. The lengh of word w A is defined o e he numer of leers i conins nd will e denoed y w. In priculr we hve: w w 2 = w + w 2, for ll w,w 2 A. ɛ = = if A 2... n = n if, 2,..., n A Le u, v, w e words over A. If w = uv hen we sy h u is prefix of w nd v is suffix of w. Noe h he empy word is prefix nd suffix of every word. Also ech word is prefix nd suffix of iself. A prefix or suffix of w will e clled proper if i is neiher ɛ nor w. A lnguge (or forml lnguge) over n lphe A is defined o e suse of A. Le A e n lphe. If L, L 2 A hen he produc L L 2 is defined y L L 2 = {w w 2 : w L nd w 2 L 2 }. If L A hen he Kleene closure of L is he se L defined y L = {w w 2... w n : n nd w,w 2,... w n L} If we define L n y L = {ɛ}, L n+ = L n L for n, hen L = n= L n. Exmples. Le A = {,, c, d}. Then {}{} = {} {, }{c, d} = {c, d, c, d} {} = {ɛ,, 2, 3,...} {c, d} = {ɛ, c, d, c 2,d 2, cd, dc, c 3,c 2 d,...} {} {c, d} = {c, d, c, d, 2 c, 2 d,...} A regulr expression is n expression consruced from union, produc nd closure, for exmple {} {} = {ɛ,,,,, 3, 3,...}. A regulr lnguge is lnguge h cn e represened using regulr expression. Deerminisic Finie Auom A deerminisic finie uomonis mechnism for recognising words in lnguge; simplified model of compuion in which n inpu word is scnned lef-o-righ. The reding of ech leer cuses some chnge o ke plce in he mchine. When ll leers hve een red he finl se is used o deermine wheher he sring hs een cceped or no. This cn e viewed s simple form of oupu wih jus wo possiiliies ccepnce or rejecion. 2
A deerminisic finie uomon (or DFA) is quinuple A =(Q, A, φ, i, T ) where. Q is finie nonempy se whose memers re clled ses of he uomon; 2. A is finie nonempy se clled he lphe of he uomon; 3. φ is mp from Q A o Q clled he rnsiion funcion of he uomon; 4. i is memer of Q nd is clled he iniil se; 5. T is nonempy suse of Q whose memers re clled erminl ses or cceping ses. The mp φ descries he chnge of se when single leer is red in if he uomon in se q reds he leer hen is se chnges o φ(q, ). Iniilly he se is i nd if he inpu word is w = 2... n hen, s ech leer is red, he se chnges nd we ge q,q 2,..., q n defined y q = φ(i, ) q 2 = φ(q, 2 ) q 3 = φ(q 2, 3 )... q n = φ(q n, n ) I is useful o exend he definiion of φ so h is second rgumen cn e word insed of jus leer nd we cn wrie q n = φ(i, w). This is done s follows. For deerminisic finie uomon defined using he ove noion, exend he mp φ : Q A Q o φ : Q A Q y defining: φ(q, ɛ) = q for ll q Q φ(q, w) = φ(φ(q, w),) for ll q Q; w A ; A. I is esy o show y inducion h his exended mp sisfies φ(q, vw) =φ(φ(q, v),w) for ll q Q; v, w A. We sy h word w A is cceped (or recognised) y he uomon A if φ(i, w) T, oherwise i is sid o e rejeced. The se of ll words cceped y A is clled he lnguge cceped y A nd will e denoed y L(A). Thus L(A) ={w A : φ(i, w) T }. In priculr word w is cceped y n uomon if nd only if here is ph from he iniil se o erminl se wih lel w (which we cll n cceping ph). Exmple. Le A e he uomon (Q, A, φ, i, T ) where Q = {i,, r}, A = {, }, T = {} nd he rnsiion funcion φ is given y φ(i, ) = r, φ(i, ) =, φ(, ) =, φ(, ) =, φ(r, ) = r, φ(r, ) = r. I is simpler o descrie rnsiion funcion y le of vlues. In his exmple we hve: i r r r r If for exmple he word is inpu o his uomon hen we oin he following ph: i r r r r so his word is rejeced. The word produces he following ph nd so is cceped. i An uomon cn lso e defined y giving is se digrm (which is direced grph wih lelled edges). The verices of he grph re he ses of he uomon. Two ses p nd q re joined y n edge lelled wih leer if nd only if φ(p, ) =q where φ is he rnsiion funcion. Muliple edges re usully comined nd loops re possile. The iniil se is indiced y n rrow poining o i, erminl ses y rrows poining wy (or someimes oher symols). Exmple. The uomon of he ls exmple hs he following se digrm: If w = 2... n A nd φ(p, w) =q hen here re ses r,r 2,..., r n wih r = φ(p, ),r 2 = φ(r, 2 ),r 3 = φ(r 2, 3 ),..., q = φ(r n, n ). We sy h he ses p nd q re conneced y ph wih lel w nd wrie p r 2 3 n r2 r3 r n 2 n w rn q or p q. 3 4
, i r,, Prolem. Find he lnguge cceped y he uomon wih he ove se digrm. Soluion. Afer he firs leer of n inpu word is red he uomon eners one of he ses r, nd remins here no mer wh he oher leers re. Thus he single cceping se cn e enered if nd only if he firs leer red is. So he lnguge cceped y A is he se of i srings which egin wih, i.e. L(A) ={w : w {, } }. Exmples of Deerminisic Finie Auom Prolem. Consruc deerminisic finie uomon cceping he lnguge of ll i srings wih hree consecuive s. Soluion. A sequence of ses cn e used o coun he numer of consecuive zeroes red in. If hree re red we should rech n cceping se nd remin here no mer wh leers follow. If is encounered efore he cceping se is reched hen we reurn o he iniil se nd sr couning zeroes gin. A suile uomon cn herefore e descried y he following se digrm., Prolem. Find he lnguge cceped y he deerminisic finie uomon defined y he following se digrm: 5 Soluion. Noe h he iniil se is lso erminl se his implied h he empy sring ɛ will e cceped. Any inpu sring consising of only s will leve he uomon in he iniil se nd so e cceped. Also from he iniil se ny sring consising of only s will led o n cceping se. This suggess h he lnguge cceped is L = { m n : m, n }. Clerly ny word in L is cceped. We need o show h here re no more words he uomon cceps. A word which is no in L will hve s susring nd will herefore e rejeced since in ech of he hree ses he inpu leds o he righ-hnd se. The lnguge L cn e wrien more compcly using Kleene closures since {w n : n } = {w} for ny w. We hve L = {} {}. Prolem. Give se digrm for deerminisic finie uomon wih lphe {, } which cceps he lnguge of ll words wih n odd numer of s nd n even numer of s. Soluion. A suile mchine cn e consruced using ses which record wheher odd or even numers of ech leer hve een red ech sep. Le he se of ses of he uomon e {odd/even, even/odd, odd/odd, even/even}, where ech is inerpreed s giving he priy of he numer of s followed y he priy of he numer of s red so fr. I is now esy o give le of vlues for he rnsiion funcion: odd/even even/even odd/odd even/odd odd/odd even/even odd/odd even/odd odd/even even/even odd/even even/odd For he se of erminl (i.e. cceping) ses we ke {odd/even}. Iniilly no s nd no s hve een red so we hve n even numer of ech nd cn ke even/even s he iniil se. A suile se digrm is herefore: 6
even/even even/odd odd/even odd/odd pril funcion from (Q A) o Q. Hence φ(q, ) my e undefined for some (q, ) pirs, nd if h hppens during compuion we regrd he enire sring s hving een rejeced. The reson why his does no chnge hings is h we my lwys complee n incomplee DFA y dding new rejecing se which he compleed mchine eners when no rnsiion is defined for he originl mchine. An incomplee DFA is someimes esier o define or depic. Nondeerminisic Finie Auom Incomplee rnsiion funcions We would like o e le o sy h he following digrm (lef hnd side) shows DFA h jus cceps words c nd dog. Bu so fr, our noion of vlid DFA requires i o show wh hppens for every se/symol cominion. Using lphe {, c, d, g, o, } we hve o complee i o ge he righ-hnd digrm. The mchines we hve looked so fr re clled deerminisic ecuse when leer is red in, he nex se is deermined y he rnsiion funcion. Suppose we widen he definiion o llow more hn one possile new se when leer is red in. So φ(q, ) is suse of Q, nd incompleeness cn e included y llowing h φ(q, ) my e he empy se. The rule for cceping sring is h where here is choice of new se, here should exis new se h leds o ccepnce. (In generl, here should exis sequence of choices.) This non-deerminism cn simplify he sk of consrucing complex mchines. Le P(S) denoe he se of ll suses of se S. A nondeerminisic finie uomon (or NFA) is quinuple A =(Q, A, φ, i, T ) where. Q is finie nonempy se whose memers re clled ses of he uomon; c d o g c,g,o, c,d,g,o,,c,d,g,o,,c,d,g,o,c,d,o,,c,d,g,o,,c,d,g,,c,d,g,o, d o g 2. A is finie nonempy se clled he lphe of he uomon; 3. φ is mp from Q A o P(Q) clled he rnsiion funcion of he uomon; 4. i is memer of Q nd is clled he iniil se; 5. T is nonempy suse of Q whose memers re clled erminl ses or cceping ses. The mp φ cn e exended o mp Q A P(Q) y defining φ(q, ɛ) = {q} for ll q Q φ(q, w) = p φ(q,w) φ(p, ) for ll q Q; w A ; A. We sr ou y exending our definiion of DFAs in wy h mkes no difference o he kinds of lnguge you cn descrie, u someimes simplifies he descripion of he DFA. We define n incomplee DFA (Q, A, φ, i, T ) y llowing φ o e 7 Thus φ(q, w) is he se of ll possile ses h cn rise when he inpu w is received in he se q. As efore we hen hve φ(q, vw) =φ(φ(q, v),w) for ll q Q; v, w A. 8
We sy h w A is cceped y A if φ(i, w) T, i.e. if he se φ(i, w) of ll possile ses rising from he inpu w conins les one erminl se. Prolem. Wrie down he le of vlues of he rnsiion funcion of he nondeerminisic finie uomon wih he following se digrm. Give he phs lelled y he inpu words nd nd decide if hese re cceped or rejeced. Find he lnguge cceped y he uomon. Soluion. s p φ i {p, q} p {i, s} q {} s The inpu word produces he following ph {i} i {p, q} {i, s} nd is cceped since he finl se conins erminl se. The inpu word produces he following ph {i} {p, q} {i, s} q {p, q} nd is rejeced since he finl se conins no erminl se. The erminl se cn only e reched if he inpu word is one of,,,..., while he erminl se s cn only e reched if he inpu is one of,,,... Hence he lnguge cceped y his uomon is {} { 2 } { } + where L + denoes L \ ɛ. We now show h inroducing nondeerminism does no ffec he lnguges which re cceped y our uom. Theorem. A lnguge which is cceped y nondeerminisic finie uomon is lso cceped y some deerminisic finie uomon. Proof. Le A =(Q, A, φ, i, T ) e nondeerminisic finie uomon. We define deerminisic finie uomon A s follows. The lphe of A is he sme s 9 h of A. The se of ses of A is P(Q). The rnsiion funcion of A is he mp ψ : P(Q) A P(Q) defined y ψ(p, ) = φ(p, ). For he iniil se of A we ke {i} P (Q). We now hve Hence defining T y p P w L(A) φ(i, w) T ψ({i},w) T. T = {P P(Q) :P T } we hve now specified deerminisic uomon A =(P(Q), A, ψ, {i},t ) nd w L(A ) ψ({i},w) T ψ({i},w) T w L(A). Thus L(A) =L(A ). Ofen i is simpler o define nondeerminisic uomon o perform given sk. We look he exmple of serching for pern in sring of ex. Prolem. Design finie se uomon which cceps he lnguge {, } {}{, } of ll i srings conining he susring. Soluion. The lnguge is cceped y he nondeerminisic uomon wih he following se digrm: If n inpu sring does conin hen in one possile compuion he mchine loops round he sr se unil is reched when i proceeds o he erminl se. Thus he inpu is cceped. If he inpu does no conin hen here is no compuion leding from he iniil o he erminl se so he inpu is rejeced.,, Noe h his nondeerminisic uomon hs simpler se digrm hn he following deerminisic one which serched for he sme susring.
, Limiions of Finie Auom If w A for some lphe A nd w = 2... n where, 2,... n A, hen he reverse of w is he word w R given y w R = n... 2. We lso define ɛ R = ɛ. If A hen clerly R =. If w = w R hen he word w is clled plindrome. Exmples. Le A = {,, c,..., x, y, z}. Then (compuer) R = reupmoc. The following words re plindromes in A : o, deed, roor. We now show h here re lnguges h cnno e cceped y ny finie-se uomon. Exmple. There is no deerminisic finie uomon which cceps he lnguge L = { n n : n =, 2, 3,...}. This is proved y conrdicion. Suppose h here is DFA (Q, {, }, φ, i, T ) which cceps he lnguge L. For ech n le q n e he se fer he word n is inpu, i.e. q n = φ(i, n ). Then q,q 2,q 3,... cnno ll e disinc since he se Q of ses is finie. Hence q r = q s for some r, s wih r s. Now if he word r s is inpu we oin he finl se φ(i, r s )=φ(q r, s )=φ(q s, s )=φ(i, s s ) T. Hence r s L. Bu since r s we hve r s L, conrdicion. If word z elongs o lnguge cceped y n uomon wih N ses nd z > Nhen he cceping ph for z mus conin repeed se nd herefore will hve he form i u q v q w where i is he iniil se, is n cceping se nd z = uvw, where v ɛ. Now we cn repe he ph lelled wih v ny numer n of imes nd oin n cceping ph wih lel uv n w. Hence uv n w is lso cceped y he uomon. Also if q is ken o e he firs repeed se hen he ph from i o he second q cn conin mos N + ses, so is lel uv hs lengh mos N. The word uv n w cn e hough of s hving een oined y pumping he susring v of z = uvw. We hve now proved he following resul which is ofen used for showing h cerin lnguges cnno e cceped y finie uom. The Pumping Lemm. Le L A e lnguge which is cceped y some deerminisic finie uomon wih N ses. Then every word z L wih z N cn e wrien in he form z = uvw for some u, v, w A wih. v ɛ 2. uv N 3. uv n w L for n =, 2, 3,... This jus sys h ny sufficienly long word which is cceped hs susring which cn e repeed ( pumped ) o give us more words which re cceped. As n pplicion of he pumping lemm we show h here is no deerminisic finie uomon which cn recognise wheher is inpu is plindrome or no. This lso shows he limied power of finie se uomon s model of compuion. Prolem. Le A e n lphe which conins les 2 leers nd le P e he se of plindromes over A, i.e. P = {w A : w = w R }. Show h here is no deerminisic finie uomon which cceps P. Soluion. Suppose y wy of conrdicion h here is n uomon h cceps P nd le N e he numer of ses h i possesses. Le, e disinc leers in A nd choose some m wih m > N. The word m m is plindrome nd hs lengh >N so y he pumping lemm we cn wrie m m = uvw where v ɛ nd uv N < m nd uv n w is plindrome for every n. Bu now m m = uvw where uv is shorer hn m. Compring he leers in hese wo words we see h uv mus e sring of s nd hence v mus e sring of les one. I now follows h uv 2 w = m+r m for some r. By he pumping lemm his word mus elong o he lnguge P of ll plindromes, u is clerly no plindrome, giving us conrdicion. Hence he lnguge P is no cceped y ny deerminisic finie uomon. 2