CMSC 330: Orgniztion of Progrmming Lnguges Finite Automt 2 CMSC 330 1 Types of Finite Automt Deterministic Finite Automt (DFA) 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 DFA 1
Compring DFAs nd NFAs NFAs cn hve more thn one trnsition leving stte on the sme symol DFAs llow only one trnsition per symol I.e., trnsition function must e vlid function DFA is specil cse of NFA NFA for ( )* 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 2
Another exmple DFA Lnguge? ( )* Compring DFAs nd NFAs (cont.) NFAs my hve trnsitions with empty string lel My move to new stte without consuming chrcter -trnsition DFA trnsition must e leled with symol DFA is specil cse of NFA 3
NFA for ( )* Hs pths to sttes S0, S1 Hs pths to S0, S1 Need to use -trnsition Relting REs to DFAs nd NFAs Regulr expressions, NFAs, nd DFAs ccept the sme lnguges! DFA cn trnsform NFA cn trnsform cn trnsform RE 4
Forml Definition A deterministic finite utomton (DFA) is 5-tuple (Σ, Q, q 0, F, δ) where Σ is n lphet the strings recognized y the DFA re over this set Q is nonempty set of sttes q 0 Q is the strt stte F Q is the set of finl sttes How mny cn there e? δ : Q x Σ Q specifies the DFA's trnsitions Wht's this definition sying tht δ is? A DFA 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) } 5
Nondeterministic Finite Automt (NFA) An NFA 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 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)} ) 6
Reduction (cont.) Bse cse: <> = (, {S0}, S0, {S0}, ) Bse cse: < > = (, {S0, S1}, S0, {S1}, ) Reduction: Conctention Induction: AB <A> <B> 7
Reduction: Conctention (cont.) Induction: AB <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 Induction: (A B) 8
Reduction: Union (cont.) Induction: (A B) <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 Induction: A* 9
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! 10
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 (DFA) Non-deterministic (NFA) Reducing RE to NFA Conctention Union Closure 22 11
Next Reducing NFA to DFA -closure Suset lgorithm Minimizing DFA Hopcroft reduction Complementing DFA Implementing DFA 23 How NFA Works 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 24 12
Reducing NFA to DFA NFA my e reduced to DFA By explicitly trcking the set of NFA sttes Intuition Build DFA where Ech DFA stte represents set of NFA sttes Exmple S1 S2 S3 S1 S1, S2, S3 NFA DFA 25 Reducing NFA to DFA (cont.) Reduction pplied using the suset lgorithm DFA stte is suset of set of ll NFA sttes Algorithm Input NFA (Σ, Q, q 0, F n, δ) Output DFA (Σ, R, r 0, F d, δ) Using -closure(p) move(p, ) 26 13
-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) 27 -closure: Exmple 1 Following NFA contins S1 S2 S2 S3 S1 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 } 28 14
-closure: Exmple 2 Following NFA contins S1 S3 S3 S2 S1 S2 S1 S2 S3 -closures -closure(s1) = { S1, S2, S3 } -closure(s2) = { S2 } -closure(s3) = { S2, S3 } -closure( { S2,S3 } ) = { S2 } { S2, S3 } 29 -closure: Prctice Find -closures for following NFA Find -closures for the NFA you construct for The regulr expression (0 1*)111(0* 1) 30 15
Clculting move(p,) 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 31 move(,p) : Exmple 1 Following NFA Σ = {, } Move move(s1, ) = move(s1, ) = move(s2, ) = move(s2, ) = move(s3, ) = move(s3, ) = { S2, S3 } Ø Ø { S3 } Ø Ø S1 S2 S3 32 16
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 33 NFA DFA Reduction Algorithm Input NFA (Σ, Q, q 0, F n, δ), Output DFA (Σ, R, r 0, F d, δ) Algorithm Let r 0 = -closure(q 0 ), dd it to R While n unmrked stte r R Mrk r For ech Σ Let S = {s q r & move(q,) = s} Let e = -closure(s) If e R Let R = e R Let δ = δ {r,, e} Let F d = {r s r with s F n } // DFA strt stte // process DFA stte r // ech stte visited once // for ech letter // sttes reched vi // sttes reched vi // if stte e is new // dd e to R (unmrked) // dd trnsition r e // finl if include stte in F n 34 17
NFA DFA 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} DFA {2} 35 NFA DFA 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 DFA {2} {3} 36 18
NFA DFA Exmple 1 (cont.) R = { {S1,S3}, {S2}, {S3} } r R = {S3} Move({S3},) = Ø Move({S3},) = Ø F d = {{S1,S3}, {S3}} Since S3 F n Done! S1 S2 S3 {1,3} NFA DFA {2} {3} 37 NFA DFA Exmple 2 NFA DFA {B,D} {A} {C,D} R = { {A}, {B,D}, {C,D} } 38 19
NFA DFA Exmple 3 NFA DFA {A,E} {B,D,E} {C,D} {E} R = { {A,E}, {B,D,E}, {C,D}, {E} } 39 Equivlence of DFAs nd NFAs Any string from {A} to either {D} or {CD} Represents pth from A to D in the originl NFA NFA DFA 40 20
Equivlence of DFAs nd NFAs (cont.) Cn reduce ny NFA to DFA using suset lg. How mny sttes in the DFA? Ech DFA stte is suset of the set of NFA sttes Given NFA with n sttes, DFA 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 ) 41 Minimizing DFA Result from CS theory Every regulr lnguge is recognizle y minimum-stte DFA tht is unique up to stte nmes In other words For every DFA, there is unique DFA with minimum numer of sttes tht ccepts the sme lnguge Two minimum-stte DFAs hve sme underlying shpe S1 S2 S3 c S1 c S3 S2 42 21
J. Hopcroft, An n log n lgorithm for minimizing sttes in finite utomton, 1971 Minimizing DFA: Hopcroft Reduction Intuition Look for sttes tht cn e distinguish 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 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 P1 X P2 T Y U V Z 44 22
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 S P1 X P2 P4 U T Z Y P3 45 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 P3 X P2 Y 46 23
DFA Minimiztion Algorithm (1) Input DFA (Σ, Q, q 0, F n, δ), Output DFA (Σ, 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 47 DFA 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 48 24
Minimizing DFA: Exmple 1 DFA P2 S T R P1 Initil prtitions Accept { R } P1 P2 P1 Reject { S, T } P2 Split prtition? Not required, minimiztion done move(s,) = T P2 move(t,) = T P2 move(s,) move (T,) After clenup = R P1 = R P1 49 Minimizing DFA: Exmple 2 DFA P2 S T R P1 Initil prtitions Accept { R } P1 P2 P1 Reject { S, T } P2 Split prtition? Not required, minimiztion done move(s,) = T P2 move(t,) = S P2 move(s,) move (T,) After clenup = R P1 = R P1 50 25
Minimizing DFA: Exmple 3 DFA P2 S T R P1 Initil prtitions Accept Reject { R } { S, T } P1 P2 Split prtition? Yes, different prtitions for B move(s,) = T P2 move(t,) = T P2 P3 move(s,) move (T,) DFA lredy miniml = T P2 = R P1 51 Complement of DFA Given DFA ccepting lnguge L How cn we crete DFA ccepting its complement? Exmple DFA Σ = {,} 52 26
Complement of DFA (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 DFAs Why not with NFAs? 53 Prctice Mke the DFA which ccepts the complement of the lnguge ccepted y the DFA elow. S3 54 27
Reducing DFAs 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 DFA 55 Relting REs to DFAs nd NFAs Why do we wnt to convert etween these? Cn mke it esier to express ides Cn e esier to implement 56 28
Implementing DFAs It's esy to uild progrm which mimics DFA 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; 57 Implementing DFAs (Alterntive) Alterntively, use generic tle-driven DFA given components (Σ, Q, q 0, F, δ) of DFA: 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 58 29
Run Time of DFA How long for DFA 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 DFA 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 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 DFA 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 60 30
Prctice Convert to DFA Convert to n NFA nd then to DFA (0 1)*11 0* Strings of lternting 0 nd 1 * ( ) 61 Summry of Regulr Expression Theory Finite utomt DFA, NFA Equivlence of RE, NFA, DFA RE NFA Conctention, union, closure NFA DFA -closure & suset lgorithm DFA Minimiztion, complement Implementtion 62 31