NFA DFA Exmple 3 CMSC 330: Orgniztion of Progrmming Lnguges NFA {B,D,E {A,E {C,D {E Finite Automt, con't. R = { {A,E, {B,D,E, {C,D, {E 2 Equivlence of DFAs nd NFAs Any string from {A to either {D or {CD represents pth from A to D in the originl NFA 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 ) NFA DFA 3 4
Implementing DFAs Implementing DFAs (Alterntive) 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; Alterntively, use generic tle-driven DFA given components (Σ, Q, q 0, F, δ) of DFA: 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 5 CMSC 330 6 Run Time of Algorithm Regulr Expressions in Prctice Given string s, how long does lgorithm tke to decide whether s is ccepted? ssume we cn compute δ(q0, c) in constnt time then the time per string s to determine cceptnce is O( s ) cn t get much fster! But recll tht constructing the DFA from the NFA constructed from the regulr expression A my tke O(2 A ) time ut this is usully not the cse in prctice So there s the initil overhed, ut then ccepting strings is fst CMSC 330 7 Regulr expressions re typiclly compiled into tles for the generic lgorithm cn think of this s simple yte code interpreter ut relly just representtion of (Σ, Q A, q A, {f A, δ A ), the components of the DFA produced from the r.e. 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 8
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 9 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 nd 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 nd remove ded sttes J. Hopcroft, An n log n lgorithm for minimizing sttes in finite utomton, 1971 10 Splitting Prtitions No need to split prtition {S,T,U,V All trnsitions on led to identicl prtition Even though trnsitions on led to different sttes Splitting Prtitions (cont.) Need to split prtition {S,T,U into {S,T, {U Trnsitions on from S,T led to prtition Trnsition on from U led to prtition P3 S U V T X Z Y P4 S U T X Z Y P3 11 12
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 P3 X Y 13 DFA Minimiztion Algorithm (1) Input DFA (Σ, Q, q 0, F n, δ n ), output DFA (Σ, R, r 0, F d, δ 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 itertion Let P = R, R = Ø prtitions For ech p P previous itertion // while prtitions chnged on prev // P = prev prtitions, R = current // for ech prtition from 14 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 c Σ // for ech stte in p except for r // for ech symol in lphet If δ d (r,c) = q 0 nd δ d (s, c) = q 1 nd // q s = sttes reched for c Return p m, m differently thn r 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 // m = sttes tht ehve 15 Minimizing DFA: Exmple 1 Initil prtitions Accept Reject S T R Split prtition? Not required, minimiztion done move(s, ) = T move(s, ) move(t, ) = T move(t, ) { R { S, T After clenup = R = R 16
Minimizing DFA: Exmple 2 Minimizing DFA: Exmple 3 S T R S T R Initil prtitions Accept Reject { R { S, T Split prtition? Not required, minimiztion done move(s, ) = T move(s, ) = R move(t, ) = S move (T, ) = R After clenup 17 Initil prtitions Accept Reject Split prtition? Yes, different prtitions for move(s, ) = T move(s, ) move(t, ) = T move(t, ) { R { S, T P3 DFA lredy miniml = T = R 18 Complement of DFA Given DFA ccepting lnguge L, how cn we crete DFA ccepting its complement? Exmple DFA Σ = {, Complement of DFA (cont.) Algorithm: Add explicit trnsitions to ded stte Chnge every finl stte to nonfinl stte nd every nonfinl stte to finl stte Note this only works with DFAs Why not with NFAs? 19 20
Prctice Mke the DFA which ccepts the complement of the lnguge ccepted y the DFA elow. 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 S3 21 22 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 23