CMSC 330: Orgniztion of Progrmming Lnguges Finite Automt 2 Types of Finite Automt Deterministic Finite Automt () Exctly one sequence of steps for ech string All exmples so fr Nondeterministic Finite Automt (NFA) My hve mny sequences of steps for ech string Accepts if ny pth ends in finl stte t end of string More compct thn CMSC 330 1 Compring s nd NFAs NFA for ( )* NFAs cn hve more thn one trnsition leving stte on the sme symol s llow only one trnsition per symol I.e., trnsition function must e vlid function is specil cse of NFA Hs pths to either S0 or S1 Neither is finl, so rejected Hs pths to different sttes One pth leds to S3, so ccepts string
NFA for ( )* Another exmple Hs pths to sttes S0, S1 Hs pths to S0, S1 Need to use -trnsition Lnguge? ( )* Compring s nd NFAs (cont.) NFAs my hve trnsitions with empty string lel My move to new stte without consuming chrcter -trnsition Relting REs to s nd NFAs Regulr expressions, NFAs, nd s ccept the sme lnguges! cn trnsform NFA trnsition must e leled with symol is specil cse of NFA cn trnsform RE cn trnsform
Forml Definition A deterministic finite utomton () is 5-tuple (Σ, Q, q 0, F, δ) where Σ is n lphet Q is nonempty set of sttes q 0 Q is the strt stte F Q is the set of finl sttes δ : Q x Σ Q specifies the 's trnsitions! Wht's this definition sying tht δ is? A ccepts s if it stops t finl stte on s Forml Definition: Exmple Σ = {0, 1} Q = {S0, S1} q 0 = S0 F = {S1} δ 0 1 S0 S0 S1 S1 S0 S1 or s { (S0,0,S01),(S0,1,S1),(S1,0,S0),(S1,1,S1) } Nondeterministic Finite Automt (NFA) An NFA is 5-tuple (Σ, Q, q 0, F, δ) where Σ is n lphet Q is nonempty set of sttes Difference from q 0 Q is the strt stte F Q is the set of finl sttes δ Q x (Σ {}) Q specifies the NFA's trnsitions! Trnsitions on re llowed cn optionlly tke these trnsitions without consuming ny input! Cn hve more thn one trnsition for given stte nd symol An NFA ccepts s if there is t lest one pth from its strt to finl stte on s Reducing Regulr Expressions to NFAs Gol: Given regulr expression e, construct NFA: <e> = (Σ, Q, q 0, F, δ) Rememer regulr expressions re defined recursively from primitive RE lnguges Invrint: F = 1 in our NFAs! Recll F = set of finl sttes Bse cse: <> = ({}, {S0, S1}, S0, {S1}, {(S0,, S1)} )
Reduction (cont.) Bse cse: Reduction: Conctention Induction: AB <> = (, {S0}, S0, {S0}, ) Bse cse: <A> <B> < > = (, {S0, S1}, S0, {S1}, ) Reduction: Conctention (cont.) Induction: AB Reduction: Union Induction: (A B) <A> <B> <A> = (Σ A, Q A, q A, {f A }, δ A ) <B> = (Σ B, Q B, q B, {f B }, δ B ) <AB> = (Σ A Σ B, Q A Q B, q A, {f B }, δ A δ B {(f A,,q B )} )
Reduction: Union (cont.) Induction: (A B) Reduction: Closure Induction: A* <A> = (Σ A, Q A, q A, {f A }, δ A ) <B> = (Σ B, Q B, q B, {f B }, δ B ) <(A B)> = (Σ A Σ B, Q A Q B {S0,S1}, S0, {S1}, δ A δ B {(S0,,q A ), (S0,,q B ), (f A,,S1), (f B,,S1)}) Reduction: Closure (cont.) Induction: A* <A> = (Σ A, Q A, q A, {f A }, δ A ) <A*> = (Σ A, Q A {S0,S1}, S0, {S1}, δ A {(f A,,S1), (S0,,q A ), (S0,,S1), (S1,,S0)}) Reduction Complexity Given regulr expression A of size n... Size = # of symols + # of opertions How mny sttes does <A> hve? 2 dded for ech, 2 dded for ech * O(n) Tht s pretty good!
Prctice Drw NFAs for the following regulr expressions nd lnguges (0 1)*110* 101* 111 ll inry strings ending in 1 (odd numers) ll lphetic strings which come fter hello in lphetic order (*c d* )d Recp Finite utomt Alphet, sttes (Σ, Q, q 0, F, δ) Types Deterministic () Non-deterministic (NFA) Reducing RE to NFA Conctention Union Closure CMSC 330 22 Next How NFA Works Reducing NFA to -closure Suset lgorithm Minimizing Hopcroft reduction Complementing Implementing When NFA processes string NFA my e in severl possile sttes! Multiple trnsitions with sme lel! -trnsitions Exmple After processing! NFA my e in sttes S1 S2 S3 S1 S2 S3 CMSC 330 23 CMSC 330 24
Reducing NFA to NFA my e reduced to By explicitly trcking the set of NFA sttes Intuition Build where! Ech stte represents set of NFA sttes Exmple S1 S2 NFA S3 CMSC 330 25 S1 S1, S2, S3 Reducing NFA to (cont.) Reduction pplied using the suset lgorithm stte is suset of set of ll NFA sttes Algorithm Input! NFA (Σ, Q, q 0, F n, δ) Output! (Σ, R, r 0, F d, δ) Using two suroutines! -closure(p)! move(p, ) CMSC 330 26 -trnsitions nd -closure We sy p q If it is possile to go from stte p to stte q y tking only -trnsitions If p, p 1, p 2, p n, q Q such tht! {p,,p 1 } δ, {p 1,,p 2 } δ,, {p n,,q} δ -closure(p) Set of sttes rechle from p using -trnsitions lone! Set of sttes q such tht p q! -closure(p) = {q p q } Note! -closure(p) lwys includes p! -closure( ) my e pplied to set of sttes (tke union) -closure: Exmple 1 Following NFA contins S1 S2 S2 S3 S1 S3! Since S1 S2 nd S2 S3 S1 S2 S3 -closures -closure(s1) = { S1, S2, S3 } -closure(s2) = { S2, S3 } -closure(s3) = { S3 } -closure( { S1, S2 } ) = { S1, S2, S3 } { S2, S3 } CMSC 330 27 CMSC 330 28
-closure: Exmple 2 Following NFA contins S1 S3 S3 S2 S1 S2! Since S1 S3 nd S3 S2 S1 S2 S3 -closure: Prctice Find -closures for following NFA -closures -closure(s1) = { S1, S2, S3 } -closure(s2) = { S2 } -closure(s3) = { S2, S3 } -closure( { S2,S3 } ) = { S2 } { S2, S3 } Find -closures for the NFA you construct for The regulr expression (0 1*)111(0* 1) CMSC 330 29 CMSC 330 30 Clculting move(p,) move(,p) : Exmple 1 move(p,) Set of sttes rechle from p using exctly one trnsition on! Set of sttes q such tht {p,, q} δ! move(p,) = {q {p,, q} δ} Note: move(p,) my e empty Ø! If no trnsition from p with lel Following NFA Σ = {, } Move move(s1, ) = move(s1, ) = move(s2, ) = move(s2, ) = move(s3, ) = move(s3, ) = { S2, S3 } Ø Ø { S3 } Ø Ø S1 S2 S3 CMSC 330 31 CMSC 330 32
move(,p) : Exmple 2 Following NFA Σ = {, } Move move(s1, ) = move(s1, ) = move(s2, ) = move(s2, ) = move(s3, ) = move(s3, ) = { S2 } { S3 } { S3 } Ø Ø Ø S1 S2 S3 NFA Reduction Algorithm Input NFA (Σ, Q, q 0, F n, δ), Output (Σ, R, r 0, F d, δ) Algorithm Let r 0 = -closure(q 0 ), dd it to R // strt stte While n unmrked stte r R // process stte r Mrk r // ech stte visited once For ech Σ // for ech letter Let S = {s q r & move(q,) = s} // sttes reched vi Let e = -closure(s) // sttes reched vi If e R // if stte e is new Let R = R {e} // dd e to R (unmrked) Let δ = δ {r,, e} // dd trnsition r e Let F d = {r s r with s F n } // finl if include stte in F n CMSC 330 33 CMSC 330 34 NFA Exmple 1 Strt = -closure(s1) = { {S1,S3} } NFA R = { {S1,S3} } r R = {S1,S3} S1 S2 S3 Move({S1,S3},) = {S2}! e = -closure({s2}) = {S2}! R = R {{S2}} = { {S1,S3}, {S2} }! δ = δ {{S1,S3},, {S2}} Move({S1,S3},) = Ø {1,3} {2} NFA Exmple 1 (cont.) R = { {S1,S3}, {S2} } r R = {S2} Move({S2},) = Ø Move({S2},) = {S3}! e = -closure({s3}) = {S3}! R = R {{S3}} = { {S1,S3}, {S2}, {S3} }! δ = δ {{S2},, {S3}} S1 S2 S3 {1,3} NFA {2} {3} CMSC 330 35 CMSC 330 36
NFA Exmple 1 (cont.) NFA Exmple 2 R = { {S1,S3}, {S2}, {S3} } r R = {S3} Move({S3},) = Ø Move({S3},) = Ø Mrk {S3}, exit loop F d = {{S1,S3}, {S3}}! Since S3 F n Done! S1 S2 S3 {1,3} NFA {2} {3} NFA {A} {B,D} {C,D} R = { {A}, {B,D}, {C,D} } CMSC 330 37 CMSC 330 38 NFA Exmple 3 Anlyzing the reduction NFA {B,D,E} Any string from {A} to either {D} or {CD} Represents pth from A to D in the originl NFA {A,E} {C,D} {E} R = { {A,E}, {B,D,E}, {C,D}, {E} } NFA CMSC 330 39 CMSC 330 40
Anlyzing the reduction (cont d) Cn reduce ny NFA to using suset lg. How mny sttes in the? Ech stte is suset of the set of NFA sttes Given NFA with n sttes, my hve 2 n sttes! Since set with n items my hve 2 n susets Corollry! Reducing NFA with n sttes my e O(2 n ) CMSC 330 41 Minimizing Result from CS theory Every regulr lnguge is recognizle y minimum-stte tht is unique up to stte nmes In other words For every, there is unique with minimum numer of sttes tht ccepts the sme lnguge Two minimum-stte s hve sme underlying shpe S1 S2 S3 c CMSC 330 42 S1 c S3 S2 1 1.1. 1, 1 7 1 1 9 Minimizing : Hopcroft Reduction Intuition Look to distinguish sttes from ech other! End up in different ccept / non-ccept stte with identicl input Algorithm Construct initil prtition! Accepting & non-ccepting sttes Itertively refine prtitions (until prtitions remin fixed)! Split prtition if memers in prtition hve trnsitions to different prtitions for sme input Two sttes x, y elong in sme prtition if nd only if for ll symols in Σ they trnsition to the sme prtition Updte trnsitions & remove ded sttes CMSC 330 43 Splitting Prtitions No need to split prtition {S,T,U,V} All trnsitions on led to identicl prtition P2 Even though trnsitions on led to different sttes S U V T P1 CMSC 330 44 X Z Y P2
Splitting Prtitions (cont.) Need to split prtition {S,T,U} into {S,T}, {U} Trnsitions on from S,T led to prtition P2 Trnsition on from U led to prtition P3 P4 S U T P1 CMSC 330 45 X Z Y P2 P3 Resplitting Prtitions Need to reexmine prtitions fter splits Initilly no need to split prtition {S,T,U} After splitting prtition {X,Y} into {X}, {Y} Need to split prtition {S,T,U} into {S,T}, {U} P4 S U T P1 CMSC 330 46 P3 X P2 Y Minimiztion Algorithm (1) Input (Σ, Q, q 0, F n, δ), Output (Σ, R, r 0, F d, δ) Algorithm Let p 0 = F n, p 1 = Q F // initil prtitions = finl, nonfinl sttes Let R = { p p {p 0,p 1 } nd p!= Ø }, P = Ø // dd p to R if nonempty While P!= R do // while prtitions chnged on prev itertion Let P = R, R = Ø // P = prev prtitions, R = current prtitions For ech p P // for ech prtition from previous itertion {p 0,p 1 } = split(p,p) // split prtition, if necessry R = R { p p {p 0,p 1 } nd p!= Ø } // dd p to R if nonempty r 0 = p R where q 0 p // prtition w/ strting stte F d = { p p R nd exists s p such tht s F n } // prtitions w/ finl sttes δ(p,c) = q when δ(s,c) = r where s p nd r q // dd trnsitions Minimiztion Algorithm (2) Algorithm for split(p,p) Choose some r p, let q = p {r}, m = { } // pick some stte r in p For ech s q // for ech stte in p except for r For ech c Σ // for ech symol in lphet If δ(r,c) = q 0 nd δ(s,c) = q 1 nd // q s = sttes reched for c there is no p 1 P such tht q 0 p 1 nd q 1 p 1 then m = m {s} // dd s to m if q s not in sme prtition Return p m, m // m = sttes tht ehve differently thn r // m my e Ø if ll sttes ehve the sme // p m = sttes tht ehve the sme s r CMSC 330 47 CMSC 330 48
Minimizing : Exmple 1 Minimizing : Exmple 2 P2 S T R P1 P2 S T R P1 After Initil prtitions clenup Accept { R } = P1 P2 P1 Reject { S, T } = P2 Split prtition? Not required, minimiztion done move(s,) = T P2 move(s,) = R P1 move(t,) = T P2 move (T,) = R P1 CMSC 330 49 After Initil prtitions clenup Accept { R } = P1 P2 P1 Reject { S, T } = P2 Split prtition? Not required, minimiztion done move(s,) = T P2 move(s,) = R P1 move(t,) = S P2 move (T,) = R P1 CMSC 330 50 Minimizing : Exmple 3 P2 S T R P3 Initil prtitions lredy Accept { R } = P1 miniml Reject { S, T } = P2 Split prtition? Yes, different prtitions for B move(s,) = T P2 move(s,) = T P2 move(t,) = T P2 move (T,) = R P1 CMSC 330 51 P1 Complement of Given ccepting lnguge L How cn we crete ccepting its complement? Exmple! Σ = {,} CMSC 330 52
Complement of (cont.) Algorithm Add explicit trnsitions to ded stte Chnge every ccepting stte to non-ccepting stte & every non-ccepting stte to n ccepting stte Note this only works with s Why not with NFAs? Prctice Mke the which ccepts the complement of the lnguge ccepted y the elow. S3 CMSC 330 53 CMSC 330 54 Reducing s to REs Generl ide Remove sttes one y one, leling trnsitions with regulr expressions When two sttes re left (strt nd finl), the trnsition lel is the regulr expression for the Relting REs to s nd NFAs Why do we wnt to convert etween these? Cn mke it esier to express ides Cn e esier to implement CMSC 330 55 CMSC 330 56
Implementing s (one-off) It's esy to uild progrm which mimics cur_stte = 0; while (1) { } } symol = getchr(); switch (cur_stte) { cse 0: switch (symol) { cse '0': cur_stte = 0; rek; cse '1': cur_stte = 1; rek; cse '\n': printf("rejected\n"); return 0; defult: printf("rejected\n"); return 0; } rek; cse 1: switch (symol) { cse '0': cur_stte = 0; rek; cse '1': cur_stte = 1; rek; cse '\n': printf("ccepted\n"); return 1; defult: printf("rejected\n"); return 0; } rek; defult: printf("unknown stte; I'm confused\n"); rek; CMSC 330 57 Implementing s (generic) More generlly, use generic tle-driven given components (Σ, Q, q 0, F, δ) of : let q = q 0 while (there exists nother symol s of the input string) q := δ(q, s); if q F then ccept else reject q is just n integer Represent δ using rrys or hsh tles Represent F s set CMSC 330 58 Run Time of How long for to decide to ccept/reject string s? Assume we cn compute δ(q, c) in constnt time Then time to process s is O( s )! Cn t get much fster! Constructing for RE A my tke O(2 A ) time But usully not the cse in prctice So there s the initil overhed But then processing strings is fst CMSC 330 59 Regulr Expressions in Prctice Regulr expressions re typiclly compiled into tles for the generic lgorithm Cn think of this s simple yte code interpreter But relly just representtion of (Σ, Q A, q A, {f A }, δ A ), the components of the produced from the RE Regulr expression implementtions often hve extr constructs tht re non-regulr I.e., cn ccept more thn the regulr lnguges Cn e useful in certin cses Disdvntges! Nonstndrd, plus cn hve higher complexity CMSC 330 60
Prctice Convert to Convert to n NFA nd then to (0 1)*11 0* Strings of lternting 0 nd 1 * ( ) Summry of Regulr Expression Theory Finite utomt, NFA Equivlence of RE, NFA, RE NFA! Conctention, union, closure NFA! -closure & suset lgorithm Minimiztion, complement Implementtion CMSC 330 61 CMSC 330 62