Anupm Gupt Dnny Sletor Gret Theoreticl Ides In Computer Science CS 5-25 Fll 2 Lecture 2 Oct 28, 2 Crnegie Mellon University Deterministic Finite Automt Finite Automt A mchine so simple tht you cn understnd it in less thn one minute Wishful thinking, strt stte (q ) ccept sttes (F), trnsitions The mchine ccepts string if the process ends in doule circle The mchine ccepts string if the process ends in doule circle sttes Antomy of Deterministic Finite Automton The singulr of utomt is utomton. The Lnguge L(M) of Mchine M, The lphet of finite utomton is the set where the symols come from, for exmple {,} The lnguge of finite utomton is the set of strings tht it ccepts q L(M) = All strings of s nd s The lnguge of finite utomton is the set of strings tht it ccepts
The Lnguge L(M) of Mchine M q q L(M) = { w w hs n even numer of s} Nottion An lphet Σ is finite set (e.g., Σ = {,}) A string over Σ is finite-length sequence of elements of Σ For x string, x is the length of x The unique string of length will e denoted y ε nd will e clled the empty or null string A lnguge over Σ is set of strings over Σ A finite utomton is M = (Q, Σ, δ, q, F) Q is the finite set of sttes Σ is the lphet δ : Q Σ Q is the trnsition function q Q is the strt stte F Q is the set of ccept sttes L(M) = the lnguge of mchine M = set of ll strings mchine M ccepts M = (Q, Σ, δ, q, F) where q q M q 3 Q = {q, q, q 2, q 3 } Σ = {,} q Q is strt stte F = {q, q 2 } Q ccept sttes δ : Q Σ Q trnsition function, q 2 δ q q q q q 2 q 2 q 2 q 3 q 2 q 3 q q 2 EXAMPLE The finite-stte utomt re deterministic, if for ech pir Q Σ of stte nd input vlue there is unique next stte given y the trnsition function. There is nother type mchine in which there my e severl possile next sttes. Such mchines clled nondeterministic. Build n utomton tht ccepts ll nd only those strings tht contin {} {}, {} 2
Build n utomton tht ccepts ll inry numers tht re divisile y 3, i.e, L =,,,,,,, A lnguge over Σ is set of strings over Σ A lnguge is regulr if it is recognized y deterministic finite utomton L = { w w contins } is regulr L = { w w hs n even numer of s} is regulr Determine the lnguge recognized y Determine the lnguge recognized y,,, L(M)={ n n =,, 2, } L(M)={, } Determine the lnguge recognized y DFA Memership prolem Determine whether some word elongs to the lnguge.,, L(M)={ n, n x n=,,2, nd x is ny string} Theorem: The DFA Memership Prolem is solvle in liner time. Let M = (Q, Σ, δ, q, F) nd w = w...w m. Algorithm for DFA M: p := q ; for i := to m do p := δ(p,w i ); if p F then return Yes else return No. 3
Equivlence of two DFAs Definition: Two DFAs M nd M 2 over the sme lphet re equivlent if they ccept the sme lnguge: L(M ) = L(M 2 ). Given few equivlent mchines, we re nturlly interested in the smllest one with the lest numer of sttes. Union Theorem Given two lnguges, L nd L 2, define the union of L nd L 2 s L L 2 = { w w L or w L 2 } Theorem: The union of two regulr lnguges is lso regulr lnguge. Theorem: The union of two regulr lnguges is lso regulr lnguge Proof (Sketch): Let M = (Q, Σ, δ, q, F ) e finite utomton for L 2 nd M 2 = (Q 2, Σ, δ 2, q, F 2 ) e finite utomton for L 2 We wnt to construct finite utomton M = (Q, Σ, δ, q, F) tht recognizes L = L L 2 Ide: Run oth M nd M 2 t the sme time Q = pirs of sttes, one from M nd one from M 2 = { (q, q 2 ) q Q nd q 2 Q 2 } = Q Q 2 q = (q,q 2 ) δ ((q,q 2 ),σ) = (δ (q,σ), δ 2 (q 2,σ)) F = (F Q 2 ) (Q F 2 ) Esy to see tht this simultes oth mchines nd ccepts the union. QED Theorem: The union of two regulr lnguges is lso regulr lnguge Automton for Union q q p q p q p p p q p q 4
The Regulr Opertions Union: A B = { w w A or w B } Reverse Reverse: A R = { σ σ k σ k σ A } Intersection: A B = { w w A nd w B } Negtion: A = { w w A } Reverse: A R = { σ σ k σ k σ A } Conctention: A B = { vw v A nd w B } Str: A* = { w w k k nd ech w i A } How to construct DFA for the reversl of lnguge? The direction in which we red string should e irrelevnt. If we flip trnsitions round we might not get DFA. The Kleene closure: A* Str: A* = { w w k k nd ech w i A } From the definition of the conctention, we define A n, n =,, 2, recursively A = {ε} A n+ = A n A A* is set consisting of conctentions of ny numer of strings from A. A * = A k k< The Kleene closure: A* Wht is A* of A={,}? Wht is A* of A={}? All inry strings All inry strings of n even numer of s Regulr Lnguges Are Closed Under The Regulr Opertions We hve seen the proof for Union. You will prove some of these on your homework. Theorem: Any finite lnguge is regulr Clim : Let w e string over n lphet. Then {w} is regulr lnguge. Proof: Construct the utomton tht ccepts {w}. Clim 2: A lnguge consisting of n strings is regulr Proof: By induction on the numer of strings. If {} then L {} is regulr 5
Pttern Mtching Input: Text T of length t, string S of length n Prolem: Does string S pper inside text T? Nïve method:, 2, 3, 4, 5,, t Cost: Roughly nt comprisons Automt Solution Build mchine M tht ccepts ny string with S s consecutive sustring Feed the text to M Cost: t comprisons + time to uild M As luck would hve it, the Knuth, Morris, Prtt lgorithm uilds M quickly Rel-life Uses of DFAs Regulr Expressions Are ll lnguges regulr? Coke Mchines Thermostts (fridge) Elevtors Trin Trck Switches Lexicl Anlyzers for Prsers Consider the lnguge L = { n n n > } i.e., unch of s followed y n equl numer of s No finite utomton ccepts this lnguge Cn you prove this? n n is not regulr. No mchine hs enough sttes to keep trck of the numer of s it might encounter 6
Tht is firly wek rgument Consider the following exmple L = strings where the # of occurrences of the pttern is equl to the numer of occurrences of the pttern Cn t e regulr. No mchine hs enough sttes to keep trck of the numer of occurrences of L = strings where the # of occurrences of the pttern is equl to the numer of occurrences of the pttern Cn t e regulr. No mchine hs enough sttes to keep trck of the numer of occurrences of M ccepts only the strings with n equl numer of s nd s! Let me show you professionl strength proof tht n n is not regulr How to prove lnguge is not regulr Assume it is regulr, hence is ccepted y DFA M with n sttes. Show tht there re two strings s nd s 2 which oth rech some stte in M (usully y pigeonhole principle) Then show there is some string t such tht string s t is in the lnguge, ut s 2 t is not. However, M ccepts either oth or neither. 7
Theorem: L= { n n n > } is not regulr Proof (y contrdiction): Assume tht L is regulr, M=(Q,{,},δ,q,F) Consider δ(q, i ) for i =,2,3, There re infinitely mny i s ut finite numer of sttes. δ(q, n )=q nd δ(q, m ) =q, nd n m Since M ccepts n n δ(q, n )=q f The finite-stte utomt re deterministic, if for ech pir of stte nd input vlue there is unique next stte given y the trnsition function. There is nother type mchine in which there my e severl possile next sttes. Such mchines clled nondeterministic. δ(q, m n )=δ( δ(q, m ), n )=δ(q, n )= q f It follows tht M ccepts m n, nd n m Nondeterministic finite utomton (NFA) A NFA is defined using the sme nottions M = (Q, Σ, δ, q, F) s DFA except the trnsition function δ ssigns set of sttes to ech pir Q Σ of stte nd input. A string is ccepted iff there exists some set of choices tht leds to n ccepting stte Note, every DFA is utomticlly lso NFA. Nondeterministic finite utomton q k Allows trnsitions from q k on the sme symol to mny sttes NFA for { k k is multiple of 2 or 3} Wht does it men tht for NFA to recognize string x = x x 2 x k? s s s 3, s 2 s 4 Since ech input symol x j (for j>) tkes the previous stte to set of sttes, we shll use union of these sttes. 8
Wht does it men tht for NFA to recognize string? Here we re going formlly define this. For stte q nd string w, δ * (q, w) is the set of sttes tht the NFA cn rech when it reds the string w strting t the stte q. Thus for NFA= (Q, Σ, δ, q, F), the function δ * : Q x Σ* -> 2 Q is defined y δ * (q, y x k ) = p δ*(q,y) δ(p,x k ) Find the lnguge recognized y this NFA s s s 2 s 3 s 4, L = { n, n, n n =,, 2 } Find the lnguge recognized y this NFA Theorem: The lnguges ccepted y n NFA re regulr. s In other words: For ny NFA there is n equivlent DFA. L = * (,, ) ()* This theorem my prove useful on the homework. You should prove it if you wnt to use it. NFA vs. DFA NFA Richer nottion to represent lnguge. Sometimes exponentilly smller. DFA Implementle in low level hrdwre. Very fst to simulte. Study Bee DFAs Regulr Lnguges Regulr opertors n n is not regulr NFAs NFAs ccept regulr lnguges 9