Designing finite utomt II Prolem: Design DFA A such tht L(A) consists of ll strings of nd which re of length 3n, for n = 0, 1, 2, (1) Determine wht to rememer out the input string Assign stte to ech of the possiilities: q 3n, q 3n+1, q 3n+2 (2) Add the trnsitions telling how the possiilities rerrnge, select the initil stte nd the fvourle sttes (3) Test the utomton > q 3n q 3n+1 q 3n+2 Fundmentls of Computing (5) 1
Pttern mtching Prolem: Design DFA A such tht L(A) consists of ll strings of s nd s which contin s sustring There re four possiilities to rememer: hven t seen ny symols of the pttern: (initil) stte s hve just seen n : stte q hve just seen : stte q or hve just seen the entire pttern : (fvourle) stte q > s q q q Fundmentls of Computing (5) 2
DFA s theoreticl model for progrmming unusul, different from conventionl progrmming lnguges mjor concepts of impertive progrmming (eg, sequences, rnching, loops) cn e simulted elegnt, simple to use, hs thoroughly developed theory ll pttern mtching (eg, WWW serch engines) cn e descried limited expressive power (the utomton is not cple of incresing ny of its resources during computtion) Wht out suprogrm clls? Fundmentls of Computing (5) 3
Comining two utomt Prolem: Design n utomton tht ccepts ll the strings tht hve either two consecutive s or two consecutive s On pge 24 (4), there is DFA A 4 ccepting ll the strings tht hve two consecutive s This utomton cn esily e trnsformed into n utomton A 4 tht ccepts ll the strings with two consecutive s Since we lredy hve progrms tht solve the prolem for s nd, respectively, s, n ovious ide would e to design progrm tht would just cll the given progrms s suprogrms A possile solution would e to comine the initil sttes of A 4 nd A 4 Fundmentls of Computing (5) 4
Comining two utomt (cont) Given: L(A 4 ) = ll words hving two consecutive s L(A 4 ) = ll words hving two consecutive s Need: n utomton A such tht L(A) = L(A 4 ) L(A 4 ) q 2 r 2 > s q 1 r 1 Fundmentls of Computing (5) 5
Nondeterminism Oserve tht the stte trnsition digrm on the previous slide no longer represents DFA: there is more thn one rrow coming out of the initil stte s with identicl lels (sy, ) We sy tht this utomton is nondeterministic ecuse, eing in stte s nd reding from the input tpe, the device now hs choice: it cn choose to move either to stte q 1 or to stte s Wht cn ffect this choice? We my imgine tht the utomton just tries to guess which wy will led it finlly to fvourle stte Fundmentls of Computing (5) 6
But rel computers cnnot guess This rgument cn hrdly e refuted However, one cn justify this vrint of finite utomt y the following: Nondeterministic finite utomt re much esier to design thn deterministic ones They llow us to model how progrms re uilt from suprogrms Whtever cn e done y nondeterministic finite utomton cn lso e done (though usully in it more complicted wy) y deterministic one In other words, nondeterminism does not increse the computtionl power of finite utomt Fundmentls of Computing (5) 7
More nondeterminism There re other fetures of nondeterministic finite utomt (NFA): Just s from some sttes with some input symols there my e severl different possile next sttes, so with other comintions of sttes nd input symols there my e no possile moves t ll (like in the exmple elow, there is no -rrow coming out of s) We lso llow the mchine to jump from stte to nother stte without consuming ny input symol These jumps will e indicted on the stte trnsition digrms y rrows lelled with the empty word ε (Trnsition tles re no longer dequte to descrie these utomt) > s q r ε Fundmentls of Computing (5) 8
Acceptnce of words y NFAs A word w is ccepted y n NFA A if there exists computtion of A on input w ending up with configurtion (q, ε), for some fvourle stte q Otherwise, A rejects the input word So n NFA cn reject n input word ecuse every computtion on this input is either stuck or ends up with configurtion (q, ε), ut q is not fvourle stte There cn e more thn one computtion on n input word!! Fundmentls of Computing (5) 9
Exmple: how n NFA works > s q r ε Computtions on input : (s, ), (q, ) (s, ), (r, ), (r, ), (q, ), (s, ε) stuck ccepted Computtions on input : (s, ), (q, ), (s, ), (q, ), (s, ε) ccepted Computtions on input : (s, ), (q, ) (s, ), (r, ), (r, ), (q, ) stuck stuck (s, ), (r, ), (q, ) stuck, no more rejected Fundmentls of Computing (5) 10
0 NFA: nother exmple > s 1 q 1 p 0 0 ε Computtions on input 11: (s, 11), (q, 1) stuck (s, 11), (p, 11), (q, 1) stuck, no more rejected Computtions on input 00: (s, 00), (s, 0), (s, ε); (s, 00), (s, 0), (p, ε) stuck (s, 00), (s, 0), (p, 0); (s, 00), (p, 0) stuck (s, 00), (p, 00) stuck, no more rejected Computtions on input 001: (s, 001), (s, 01), (s, 1), (q, ε) ccepted Fundmentls of Computing (5) 11
Nondeterministic Finite Automton (NFA) Forml definition: A nondeterministic finite utomton (NFA) is quintuple A = (Q, Σ,, s, F ) where Q is finite set of sttes Σ is finite set, the input lphet s Q is the initil stte F Q is the set of fvourle (or ccepting) sttes Q (Σ {ε}) Q is ternry reltion, the trnsition reltion (If A, eing in stte q Q, hs red the symol Σ, it enters one of the sttes q for which (q,, q ) is in For ny stte q such tht (q, ε, q ) is in, A cn jump from stte q to stte q without reding ny input symol) Fundmentls of Computing (5) 12
Configurtions of NFAs Let A = (Q, Σ,, s, F ) e n NFA A configurtion of A is ny pir of the form ( stte in Q, word over Σ ) Configurtion (q, w) yields configurtion (q, w ) in one step if either w = σw for some σ in Σ nd the triple (q, σ, q ) is in or w = w nd the triple (q, ε, q ) is in ( jump ) Oserve tht in generl configurtion cn yield severl (or no) configurtions in one step In other words, configurtion does not determine uniquely the configurtion it yields in one step Tht is why the utomton A is clled nondeterministic Fundmentls of Computing (5) 13
Computtions of NFAs A computtion of A on input word w is ny sequence of configurtions such tht (q 1, w 1 ), (q 2, w 2 ),, (q k, w k ) (q 1, w 1 ) = (s, w) where s is the initil stte of A every (q i, w i ) yields (q i+1, w i+1 ) in one step, nd either w k is the empty word ε, or the configurtion (q k, w k ) yields no configurtion in one step (it gets stuck) There cn e more thn one computtion on n input word Fundmentls of Computing (5) 14
NFAs nd prllelism Nondeterminism cn e viewed s kind of prllel computtion wherein severl processes cn e running concurrently Suppose we re running n NFA on n input string nd come to stte nd red symol with multiple wys to proceed Then the mchine splits into multiple copies of itself nd follows ll the possiilities in prllel If there re susequent choices, the mchine splits gin If the next input symol does not pper on ny rrows exiting the stte occupied y copy of the mchine, tht copy dies, the rnch of computtion ssocited to it is stuck If stte with some exiting ε-rrows is encountered then, without reding ny input, the mchine splits into multiple copies: one following ech exiting ε-rrow, nd one stying t the current stte Fundmentls of Computing (5) 15
Differences etween NFAs nd DFAs > s q r ε There cn e more thn one rrow lelled y the sme symol coming out of stte (eg, there re two -rrows out of r) There cn e no rrow lelled y some symol coming out of stte (eg, there is no -rrow out of q) There cn e rrows lelled y not symols from the input lphet ut y the empty word ε These rrows represent jumps (eg there is jump from s to q) Every DFA is n NFA s well! Fundmentls of Computing (5) 16
Pttern mtching revisited Prolem: Design finite utomton A such tht L(A) consists of ll strings of s nd s which contin s sustring > It is much esier to design n NFA thn DFA We will show now tht every NFA cn e converted to DFA ccepting precisely the sme words Fundmentls of Computing (5) 17
Equivlence of DFAs nd NFAs Two utomt A nd A re sid to e equivlent if they ccept the sme lnguge Clim: For every NFA A, there is DFA A equivlent to A Moreover, there is mechnicl method, n lgorithm, tht crries out this conversion from nondeterministic utomton to its deterministic equivlent Fundmentls of Computing (5) 18
Tsk: given n NFA The Suset Construction > s q r ε construct n equivlent DFA (deterministic!) wtch out: in DFA no choice, no getting stuck, no ε-jumps re llowed Things to define: new sttes? new initil stte? new fvourle sttes? new trnsitions (rrows)? Fundmentls of Computing (5) 19
Suset construction: the new sttes NFA: > s q r ε The new sttes: ll the susets of the NFA s sttes ( stte of DFA represents ll the sttes of NFA tht cn e reched from the initil stte on the input consumed so fr) The new initil stte: the set contining the NFA s initil stte plus ll those sttes tht re rechle from it y some ε-jumps The new fvourle sttes: those susets tht contin t lest one of the NFA s fvourle sttes {s} {r} {s, r} {q} {s, q} {q, r} {s, q, r} Fundmentls of Computing (5) 20
Suset construction: the new trnsition function Recll: Being in stte nd reding n input symol, the trnsition function gives the utomton s next stte And the new sttes re susets of the old sttes now Generl rule: P? the set of ll those sttes tht re rechle from some stte in the set P either y n -rrow, or y n -rrow followed y possily one or more ε-jumps Fundmentls of Computing (5) 21
NFA: Exmple: computing the new trnsition function > s q r ε DFA: -rrow leving stte {s, q, r} goes to {s, q, r} -rrow leving stte {s, q, r} goes to {q, r} s s q ε q r r s q q r r Fundmentls of Computing (5) 22
Suset construction: the new trnsition function NFA: > s q r ε DFA: {s} {r} {s, r} {q} {s, q} {q, r} {s, q, r} Fundmentls of Computing (5) 23
Removing the unrechle sttes Oserve tht there is no computtion going through the sttes {s} nd {s, r}, so we cn sfely remove them together with their outgoing rrows: {r} {q} {s, q} {q, r} {s, q, r} The initil stte is never removle! (ll computtions strt there) Fundmentls of Computing (5) 24
Stte minimistion prolem We sw tht it is very esy to design nondeterministic finite utomt for, sy, pttern mtching prolems However, n NFA cnnot e directly implemented s computer progrm One must convert it into DFA But the numer of sttes of the resulting DFA my increse significntly 2 numer of sttes of the originl NFA in the worst cse An importnt field where finite utomt re eing used is hrdwre design: some components of hrdwre re sed on simultion of DFA An importnt ojective here is to use finite utomt tht hve minimum possile numer of sttes There re methods for doing this (The suset construction is just the initil phse of design) Fundmentls of Computing (5) 25