of Regulr Lnguges Dr. Neil T. Dntm CSCI-561, Colordo School of Mines Fll 2018 Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 1 / 50
Outline Introduction Closure Properties Stte Minimiztion Hopcroft s Algorithm Brzozowski s Algorithm Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 2 / 50
Introduction Outline Introduction Closure Properties Stte Minimiztion Hopcroft s Algorithm Brzozowski s Algorithm Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 3 / 50
Introduction Lnguge Clsses nd Properties Lnguge Clss: set of lnguges The regulr lnguges, lso clled the regulr set Lnguge Properties Decision Properties Closure Properties Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 4 / 50
Introduction Decision Properties Decision Property: property on lnguge L B Exmple: Is lnguge L empty? L = Need n lgorithm to check property Lets us reson out lnguge / modeled system Does the system terminte? Does the system generte undesired ehvior? Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 5 / 50
Introduction Closure Properties Closure Property: n opertor on set outputs memer of the set + : Z Z Z : R R R Lets us compose lnguges Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 6 / 50
Outline Introduction Closure Properties Stte Minimiztion Hopcroft s Algorithm Brzozowski s Algorithm Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 7 / 50
Conctention, Union, Repetition Conctention Union Repetition ɛ strt ɛ strt N1. ɛ ɛ ɛ strt N2. ɛ ɛ ɛ strt ɛ ɛ strt strt N1 N2.. ɛ ɛ ɛ ɛ ɛ ɛ strt ɛ strt N1 ɛ ɛ. ɛ ɛ ɛ ɛ From the sic regex opertors Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 8 / 50
Reverse Outline Given: Finite Automton A Find: Finite Automton R where L (R) = {σ 0... σ n σ n... σ 0 L (A)} Solution: Reverse edges of A Strt stte of A ecomes new ccept stte. New strt stte with ɛ trnsitions to old ccept sttes. reverse strt.. strt Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 9 / 50
Exmple: Reverse L () strt q 0 q 1 q 2 L (() R) q 0 q 1 q ɛ 2 q 0 strt Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 10 / 50
Reverse Algorithm Function f-reverse(a) Input: A = (Q, Σ, E, q 0, F ) Output: A = (Q, Σ, E, q 0, F ) 1 q 0 newstte(); 2 Q Q {q 0 }; 3 F {q 0 }; 4 Σ Σ; 5 E q j σ qi q σ i q j E }{{} Reverse Edges ɛ q q 0 ; q F }{{} New strt to old ccept Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 11 / 50
Exercise: Reverse ( c) d L (( c) d), c strt q 0 q d 1 q 2 L ((( c) d) R) Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 12 / 50
Regex Reverse Given: Regulr Expression A Find: Reverse of A, denoted s A R Solution: Inductively: Bsis: For symol α eing Σ, ɛ, or : α R = α Induction: For α R in: conctention: (βγ) R = γ R β R union: (β γ) R = β R γ R Kleene-closure: (β ) R = ( β R) Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 13 / 50
Exmple: Regex Reverse 01 Infix Given: A = (01) Find: A R Solution: A R = (01) R A R = 1 R 0 R A R = 10 Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 14 / 50
Exmple: Regex Reverse S-Expression (01) R ( r e v e r s e ( conctention 0 1 ) ) 1 R 0 R ( conctention ( r e v e r s e 1) ( reverse 0 ) ) 10 ( conctention 1 0) Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 15 / 50
Regex Reverse Algorithm Function regex-reverse(e) 1 if e (Σ {ɛ, }) then // Recursive cse 2 switch cr(e) do 3 cse UNION do 4 cons(union, mp (regex-reverse, cdr (e))) 5 cse CONCATENATION do 6 cons(concatenation, mp (regex-reverse, reverse(cdr (e)))) 7 cse KLEENE-CLOSURE do 8 list(kleene-closure, regex-reverse(second(e))) 9 else // Bse cse 10 return e; Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 16 / 50
Regex Reverse Code 1 ( defun r e g e x r e v e r s e ( e ) 2 ( typecse e 3 ( l i s t ( cse ( c r e ) 4 ( : union ( cons : union 5 (mp l i s t # r e g e x r e v e r s e 6 ( cdr e ) ) ) ) 7 ( : c o n c t e n t i o n ( cons : c o n c t e n t i o n 8 (mp l i s t # r e g e x r e v e r s e 9 ( r e verse ( cdr e ) ) ) ) ) 10 ( : k l e e n e c l o s u r e ( l i s t : k l e e n e c l o s u r e 11 ( r e g e x r e v e r s e ( second e ) ) ) ) ) ) 12 ( t e ) ) ) Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 17 / 50
Exercise: Regex Reverse 01 10 Given: A = (01 10 ) Find: A R Solution: Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 18 / 50
Exercise: S-Expression Regex Reverse Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 19 / 50
Exercise: S-Expression Regex Reverse (continued) Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 20 / 50
Complement Given: Regulr Lnguges A. Find: A = {σ σ A} Solution: Flip ccept / non-ccept sttes complement strt. strt. Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 21 / 50
Explicit Ded Stte strt L ( + + ) q 0 q 1 q 2 Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 22 / 50
Exmple: Complement complement strt q 0 q 1 q 2 q ded, strt q 0 q 1 q 2 q ded, Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 23 / 50
Complement Algorithm Function df-complement(a) Input: A = (Q, Σ, E, q 0, F ) Output: A = (Q, Σ, E, q 0, F ) 1 Σ Σ; 2 q 0 q 0 ; 3 q ded newstte(); 4 Q Q {q ded } ; // Flip ccept / non-ccept sttes 5 F (Q \ F ) {q ded }; { } 6 E σ σ E q i qded qi qj E ; } {{ } trnsitions to ded stte Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 24 / 50
Intersection Given: Regulr Lnguges A nd B Find: C = {σ (σ A) (σ B)} Solution: Use the product DFA C ccepts when oth A nd B ccept F C = {(q i, q j ) Q C q i F A q j F B } Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 25 / 50
Exmple: Intersection of L (L) nd L (M) L (L) strt 0 1 0,1 L (M) strt 1 0 0 c 1 d Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 26 / 50
Intersection Pseudo-lgorithm Function df-intersect(m 1, M 2 ) Input: M 1 = (Q 1, Σ 1, E 1, q (0,1), F 1 ) Input: M 2 = (Q 2, Σ 2, E 2, q (0,2), F 2 ) Output: M = (Q, Σ, E, q 0, F ) 1 Σ Σ 1 Σ 2 ; 2 Q Q 1 Q 2 ; // Crtesin product of sttes 3 q 0 ( q (0,1), q (0,2) ) ; 4 F {(q i, q j ) Q q i F 1 q j F 2 } ; // ccept in oth M 1 nd M 2 5 E ( ) σ ( ) ) ) σ σ q(i,1), q (i,2) q(j,1), q (j,2) (q }{{} (i,1) q(j,1) E 1 (q (i,2) q(j,2) E 2 ; }{{}}{{} Crtesin product edge σ-edge in M 1 σ-edge in M 2 Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 27 / 50
Intersection Visit Algorithm Function df-intersect-visit(q 1,q 2,Q, E ) Output: (Q, E ) 1 forll σ Σ do 2 q 1 E 1 (q 1, σ) ; // Successor stte on σ in M 1 3 q 2 E 2 (q 2, σ) ; // Successor stte on σ in M 2 4 q (q 1, q 2 ); 5 if (q 1 ) (q 2 ) (q Q ) then 6 Q Q {q }; } 7 E E {(q 1, q 2 ) σ q ; 8 (Q, E ) df-intersect-visit(q 1, q 2, Q, E ) ; // Recurse on successor 9 return (Q, E ) ; Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 28 / 50
Exmple: Intersection Visit visit q 1 σ 1 σ 2 1 c 1 1 σ 3, q 2 σ 1 σ 2 2 σ 4 c 2 2 Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 29 / 50
Difference Given: Finite Automton L nd M Find: Finite Automton R where L (R) = L (L) \ L (M) Solution: Use product DFA Finl sttes where L ccepts nd M does not F R = {(q l, q m ) Q R (q l F L ) (q m F M )} Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 30 / 50
Exmple: FA Difference L (L) 0 1 0 strt 0,1 L (L) \ L (M) strt 0 (, c) (, d) L (M) strt 1 0 0 c 1 d 0 1 1 0 1 (, c) (, d) 1 Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 31 / 50
Exercise: Intersection nd Difference Product strt L 0 1 1 0 L M strt M 1 0 0 c 1 d Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 32 / 50
Exercise: Intersection nd Difference Intersection strt L 0 1 1 0 L (L) L (M) strt M 1 0 0 c 1 d Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 33 / 50
Exercise: Intersection nd Difference Difference strt L 0 1 1 0 L (L) \ L (M) strt M 1 0 0 c 1 d Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 34 / 50
Stte Minimiztion Outline Introduction Closure Properties Stte Minimiztion Hopcroft s Algorithm Brzozowski s Algorithm Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 35 / 50
Stte Minimiztion Cnonicl DFA Theorem: Cnonicl DFA The minimum stte DFA ccepting ny regulr lnguge L is unique up to n isomorphism (renming of sttes). Proof Outline For ech stte q A in Q A nd q B in Q B, there must exist σ Σ such tht q A = ˆδ(q 0,A, σ) nd q B = ˆδ(q 0,B, σ) Without such σ, we could remove q A or q B to find smller utomton q A will correspond with q B Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 36 / 50
Stte Minimiztion Two Minimiztion Algorithms John Hopcroft Jnusz (John) Brzozowski Cornell Wterloo Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 37 / 50
Stte Minimiztion Hopcroft s Algorithm Fixed Point Definition: Fixed Point (fixpoint) The fixed point of function is vlue where the function s input nd output re equl. For f : X X, the fixpoint is some vlue x X where f (x) = x. f : Z Z f (x) = x 2 Exmples 0 is fixpoint: f (0) = 0 1 is fixpoint: f (1) = 1 2 is NOT fixpoint: f (2) = 4 g : R R g(σ) = σ R is fixpoint: g() = is NOT fixpoint: g() = Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 38 / 50
Stte Minimiztion Hopcroft s Algorithm Hopcroft s Algorithm Outline 1. Prtition sttes initilly into A nd Q \ A 2. Repetedly refine prtitions: 2.1 If prtition p contins sttes tht trnsition to different successor prtitions on symol s, 2.2 Split p into new su-prtitions where ll sttes in ech su-prtition trnsition to the sme successor prtition on s 3. Repet the refinement until reching the fixpoint (no further refinements possile) Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 39 / 50
Stte Minimiztion Hopcroft s Algorithm Hopcroft s Algorithm Illustrtion Initil Prtitioning Non-ccept Sttes Accept Sttes.......... Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 40 / 50
Stte Minimiztion Hopcroft s Algorithm Hopcroft s Algorithm Illustrtion Refinement z z q q s q q s q q c q r refine q q c q r z z z q y z q y q x q z q x q z Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 41 / 50
Stte Minimiztion Hopcroft s Algorithm Hopcroft s Algorithm Detils Algorithm 1: Hopcroft s Algorithm Input: Q, Σ, E, s, ; Output: Q, Σ, E, s, ; 1 Q {, Q \ } ; // Initil Prtitioning 2 T ; // Work list 3 while T do 4 q pop(t ) ; // FA sttes, tokens, edges, strt, ccept // Minimum DFA sttes, tokens, edges, strt, ccept 5 forll z Σ do { ( ) } 6 x p Q p z r E, r q // All z predecessor sttes of prtition q 7 if x then 8 Q = ; 9 forll y Q do 10 i = y x ; // Suset of prtition y trnsitioning on z to q 11 j = y \ x; // Suset of prtition y trnsitioning on z to q 12 if i j then 13 Q Q i j; // Replce prtition y with i nd j 14 if y T then T (T \ y) i j ; 15 else if i < j then T T i ; 16 else T T j ; 17 else Q Q y ; // Don t split y 18 Q Q ; Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 42 / 50
Stte Minimiztion Hopcroft s Algorithm Hopcroft s Algorithm Illustrtion Refinement, Redux y x z q i = y x z q q s j = y \ x q q s q q c q r refine q q c q r z z z q y z q y q x q z q x q z Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 43 / 50
Stte Minimiztion Hopcroft s Algorithm Exmple: Hopcroft s Algorithm Initil DFA strt 0 Add Reject strt 0, 1 3 1 D 3 2, 2 Initil Prtition {0,1,3,D} {2},, {1,3}, {2}, {0,D}, {1,3}, {0} {D} {1,3} strt, {0} {2},, {2} Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 44 / 50
Stte Minimiztion Hopcroft s Algorithm Exercise: Hopcroft s Algorithm Initil DFA strt 0 1 2 5 3 4 Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 45 / 50
Stte Minimiztion Hopcroft s Algorithm Exercise: Hopcroft s Algorithm (continued) Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 46 / 50
Stte Minimiztion Brzozowski s Algorithm Brzozowski s Algorithm Algorithm 2: Brzozowski s Algorithm Input: A ; Output: M ; 1 M nf-to-df(reverse(nf-to-df(reverse(a)))); // FA // Minimum stte DFA Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 47 / 50
Stte Minimiztion Brzozowski s Algorithm Exmple: Brzozowski s Algorithm NFA to DFA Initil DFA strt 0 1 3 2 strt, {0} {1,3} {s,2} strt Reverse Reverse 0 1 3 2 ɛ strt s s strt ɛ, {0} NFA to DFA, {0,s } {1,3} {1,3} {s,2} {s,2} Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 48 / 50
Stte Minimiztion Brzozowski s Algorithm Exercise: Brzozowski s Algorithm Initil DFA 0 1 strt 2 5 4 3 Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 49 / 50
Stte Minimiztion Brzozowski s Algorithm Exmple: Brzozowski s Algorithm (continued) Dntm (Mines CSCI-561) Closure Properties of Regulr Lnguges Fll 2018 50 / 50