Regular Languages Mark Greenstreet, CpSc 421, Term 1, 2006/07 8 September 2008 – p.1/14
Lecture Outline Regular Languages ✈ Definition of regular languages ✈ Closure properties 8 September 2008 – p.2/14
Lecture Outline Regular Languages ✈ Definition of regular languages ✈ Regular languages are recognized by finite automata ✈ Examples ✈ Closure properties 8 September 2008 – p.2/14
Languages (review) A language is a set of strings. ✈ Let Σ be a finite set, called an alphabet. ✈ Σ ∗ is the set of all strings of Σ , i.e. sequences of zero or more symbols from Σ . ✈ A language is a subset of Σ ∗ . Examples: ✈ Example, Σ = { a , b } , and L 1 is the set of all strings that of length at most two: { ǫ, a , b , aa , ab , ba , bb } L 1 = ✈ With Σ as above, let L 2 be the set of all strings where every a is followed immediately by a b : { ǫ, b , ab , bb , abb , bab , bbb , . . . } L 2 = ✈ With Σ as above, let L 3 be the set of all strings that have more a ’s than b ’s: { a , aa , aaa , aab , aba , aab , . . . } = L 3 8 September 2008 – p.3/14
Deterministic Finite Automata (review) ✈ A deterministic finite automaton (DFA) is a 5-tuple, ( Q, Σ , δ, q 0 , F ) where: Q is a finite set of states. Σ is a finite set of symbols. δ : Q × Σ → Q is the next state function. q 0 is the initial state. F is the set of accepting states. ✈ Let M = ( Q, Σ , δ, q 0 , F ) be a DFA. For s ∈ Σ ∗ , δ ( q, s ) = q, if s = ǫ if s = x · c for c ∈ Σ = δ ( δ ( q, x ) , c ) , The language accepted by M is { s ∈ Σ ∗ | δ ( q 0 , s ) ∈ F } L ( M ) = 8 September 2008 – p.4/14
DFA examples b,c a,b,c a a c b Every a in s is followed by a b without � � � � L ( M 1 ) = s ∈ Σ ∗ an intervening c . � � 8 September 2008 – p.5/14
DFA examples b,c a a a a b,c b,c b,c { s ∈ Σ ∗ | s ends with three consecutive a ’s. } L ( M 2 ) = 8 September 2008 – p.5/14
DFA examples b b a a c c b c a c c a a b b the difference between the number of � � � � a ’s in s and the number of b ’s is a mul- L ( M 3 ) = s ∈ Σ ∗ � � tiple of 5. � 8 September 2008 – p.5/14
Regular Languages (Definition) A language, B , is a regular language iff there is some DFA M such that L ( M ) = B . In other words, the regular languages are the languages that are recognized by DFAs. ✈ To show that a language is regular, we can construct a DFA that recognizes is. ✈ Conversely, we can show that a language is not regular by proving that there can be no DFA that accepts it. 8 September 2008 – p.6/14
Regular Languages (Properties) The regular languages are closed under: Complement: If B is a regular language, then so is B . ✈ A string is in B iff it is not in B . Intersection: If B 1 and B 2 are regular languages, then so is B 1 ∩ B 2 . ✈ A string is in B 1 ∩ B 2 iff it is in both B 1 and B 2 . ✈ Because we have complement and intersection, we can conclude that the union, difference, symmetric difference, etc. of regular langauges is regular. Concatenation: If B 1 and B 2 are regular languages, then so is B 1 · B 2 . ✈ A string, s , is in B 1 · B 2 iff there are strings x and y such that x ∈ B 1 , y ∈ B 2 , and s = x · y . Note that x and/or y may be ǫ . Kleene star: B is a regular language, then so is B ∗ . ✈ A string, s , is in B ∗ iff s = ǫ or there are strings x and y such that x ∈ B ∗ , y ∈ B , and s = x · y . ✈ Note that even if B = ∅ , ǫ ∈ B ∗ . Thus, for any language B , B ∗ � = ∅ . 8 September 2008 – p.7/14
Complement example M ′ M b,c a,b,c b,c a,b,c a a a c a c b b � s ends with an a or has an a followed s ∈ Σ ∗ � � � L ( M ′ ) = � immediately by a c . 8 September 2008 – p.8/14
Closure under Complement Let B ⊆ Σ ∗ be a regular language. Let M = ( Q, Σ , δ, q 0 , F ) be a DFA that recognizes B . Let M ′ = ( Q, Σ , δ, q 0 , F ) . M ′ recognizes B . Proof: let s ∈ Σ ∗ be a string. ✈ If s ∈ B , then δ ( q 0 , s ) ∈ F . Thus, δ ( q 0 , s ) �∈ F . Thus s �∈ L ( M ′ ) . ✈ If s �∈ B , then δ ( q 0 , s ) �∈ F . Thus, δ ( q 0 , s ) ∈ F . Thus s ∈ L ( M ′ ) . B is recognized by a DFA; therefore, B is regular. � 8 September 2008 – p.9/14
Closure under Intersection ✈ Let B 1 , B 2 ⊆ Σ ∗ be regular languages. ✈ Let M 1 = ( Q 1 , Σ , δ 1 , q 1 , 0 , F 1 ) and M 2 = ( Q 2 , Σ , δ 2 , q 2 , 0 , F 2 ) be DFAs that recognize B 1 and B 2 respectively. ✈ Let M ∩ = ( Q 1 × Q 2 , Σ , δ, q 0 , F 1 × F 2 ) where q 0 = ( q 1 , 0 , q 2 , 0 ) δ (( q 1 , q 2 ) , c ) = ( δ 1 ( q 1 , c ) , δ 2 ( q 2 , c )) for any q 1 ∈ Q 1 , q 2 ∈ Q 2 and c ∈ Σ . M ∩ recognizes B 1 ∩ B 2 . Proof on next slide. 8 September 2008 – p.10/14
Proof that L ( M ∩ ) = B 1 ∩ B 2 Let s ∈ Σ ∗ be a string. First, we note that for any string s ∈ Σ ∗ , δ (( q 1 , q 2 ) , s ) = ( δ ( q 1 , s ) , δ ( q 2 , s )) . This can be proven by induction (see slide 15). If s ∈ B 1 ∩ B 1 , then s ∈ B 1 and s ∈ B 2 . Thus, δ 1 ( q 0 , 1 , s ) ∈ F 1 and δ 2 ( q 0 , 2 , s ) ∈ F 2 . Thus, δ ( q 0 , s ) = δ (( q 0 , 1 , q 0 , 2 ) , s ) , def. q 0 = ( δ 1 ( q 0 , 1 , s ) , δ 2 ( q 0 , 2 ) , s )) , def. δ ∈ F 1 × F 2 , ( s ∈ B 1 ) ⇒ δ 1 ( q 0 , 1 , s ) ∈ F 1 ( s ∈ B 2 ) ⇒ δ 1 ( q 0 , 2 , s ) ∈ F 2 ∴ = L ( M ∩ ) s If s �∈ B 1 , then . . . 8 September 2008 – p.11/14
Proof that L ( M ∩ ) = B 1 ∩ B 2 Let s ∈ Σ ∗ be a string. First, we note that for any string s ∈ Σ ∗ , δ (( q 1 , q 2 ) , s ) = ( δ ( q 1 , s ) , δ ( q 2 , s )) . This can be proven by induction (see slide 15). If s ∈ B 1 ∩ B 1 , then s ∈ B 1 and s ∈ B 2 . Thus, δ 1 ( q 0 , 1 , s ) ∈ F 1 and δ 2 ( q 0 , 2 , s ) ∈ F 2 . Thus, s ∈ L ( M ∩ ) . If s �∈ B 1 , then δ ( q 0 , s ) = ( q 1 , q 2 ) with q 1 �∈ F 1 – just work out δ ( q 0 , s ) as above. Thus, ( q 1 , q 2 ) �∈ F 1 and s �∈ L ( M ∩ ) . If s �∈ B 2 , then s �∈ L ( M ∩ ) by an argument equivalent to the one for s �∈ B 1 . Thus, δ ( q 0 , s ) ∈ F . Thus s ∈ L ( M ′ ) . ∴ s ∈ L ( M ∩ iff s ∈ B 1 ∩ B 2 . 8 September 2008 – p.11/14
Closure under Intersection (cont.) ✈ Let B 1 , B 2 ⊆ Σ ∗ be a regular language. ✈ Let M 1 = ( Q 1 , Σ , δ 1 , q 1 , 0 , F 1 ) and M 2 = ( Q 2 , Σ , δ 2 , q 2 , 0 , F 2 ) be DFAs that recognize B 1 and B 2 respectively. ✈ Let M ∩ = ( Q 1 × Q 2 , Σ , δ, q 0 F 1 × F 2 ) where q 0 = ( q 1 , 0 , q 2 , 0 ) δ (( q 1 , q 2 ) , c ) = ( δ 1 ( q 1 , c ) , δ 2 ( q 2 , c )) for any q 1 ∈ Q 1 , q 2 ∈ Q 2 and c ∈ Σ . M ∩ recognizes B 1 ∩ B 2 . ✈ B 1 ∩ B 2 is recognized by a DFA; therefore, B is regular. ✈ � ✈ Note: M ∩ is called a product machine because of the use of cartesian cross-product to define the set of states. 8 September 2008 – p.12/14
Intersection Example c a c a b b,c a,b,c a c a b a c b c a a b b � b a b a c c c ≡ b b c c a c c b b b a b a a b a b b a c a a c c b b c c a a c c a b a b a c b 8 September 2008 – p.13/14
This week Reading: September 10 (Today): Sipser 1.1 (continued). Lecture will cover the rest of the section (i.e. pages 40–47). September 12 (Friday): Sipser 1.2. Lecture will cover through Example 1.35 (i.e. pages 47–52). Homework: September 12 (Friday): Homework 0 due. Homework 1 goes out (due Sept. 19). 8 September 2008 – p.14/14
Proof that δ (( q 1 , q 2 ) , s ) = . . . By induction on s : case s = ǫ : δ (( q 1 , q 2 ) , ǫ ) = ( q 1 , q 2 ) , def. δ for strings = ( δ 1 ( q 1 , ǫ ) δ 2 ( q 2 , ǫ )) , � ” case s = x · c : δ (( q 1 , q 2 ) , x · c ) = δ ( δ (( q 1 , q 2 ) , x ) , c ) , def. δ for strings = δ (( δ 1 ( q 1 , x ) , δ 2 ( q 2 , x )) , c ) ind. hyp. = ( δ 1 ( δ 1 ( q 1 , x ) , c ) , δ 2 ( δ 2 ( q 2 , x ) , c )) def. δ = ( δ 1 ( q 1 , s ) , δ 2 ( q 2 , s )) , def. δ 1 , δ 2 for strings � 8 September 2008 – p.15/14
Recommend
More recommend