Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Regular Languages Finite Automata eg. Supermarket automatic door: exit or entrance.

Similar presentations


Presentation on theme: "1 Regular Languages Finite Automata eg. Supermarket automatic door: exit or entrance."— Presentation transcript:

1 1 Regular Languages Finite Automata eg. Supermarket automatic door: exit or entrance

2 2 Finite Automata NEITHERFRONTREARBOTH CLOSED OPENCLOSED OPENCLOSEDOPEN Door state Input signal State transition table State diagram

3 3 Definition A finite automaton is a 5-tuple (Q, , , q 0, F) Q : a finite set called the states  : a finite set called the alphabet  : Qx   Q is the transition function q 0  Q is the start state F  Q is the set of accept states final states

4 4 M 1 = (Q, , , q 0, F) Q = {q 1, q 2, q 3 }  = {0, 1}  : 01 q 1 q 1 q 2 q 2 q 3 q 2 q 3 q 2 q 2 q 1 : the start state F = {q 2 } L(M) = A M recognizes A or M accepts A the set of all strings that M accepts L(M 1 ) = ?

5 5 q 2 q 3 q 2 q 3 q 2 q 3 100000 101011 M 2 =({q 1,q 2 }, {0,1}, , q 1, {q 2 }) Eg: :01q1q1q2q2q1q2:01q1q1q2q2q1q2 L(M 2 ) =? M2:M2: L(M 2 ) = {w | w ends in a 1}

6 6 M3:M3: L(M 3 )=? L(M 3 )={  or ends in 0}

7 7 M4:M4: Starts and ends with the same symbol

8 8 What is the language accepted by the above automata? 1 0 2 2 0 1 2 M5:M5:  ={, 0, 1, 2}

9 9 L(M 5 ) = {w | the sum of the symbols in w is 0 modulo 3 except the resets to 0} M5:M5:  ={, 0, 1, 2}

10 10 Formal definition of computation M = (Q, , , q 0, F) w : a string over , w i  , w = w 1 w 2 … w n M accepts w if a sequence of states r 0, r 1, …, r n exists in Q and satisfies the following 3 conditions: r 0 = q 0  ( r i, w i+1 ) = r i+1, for i = 0, …, n-1 r n  F

11 11 We say that M recognizes language A if A = { w | M accepts w } Def: A language is called a regular language if some finite automata recognizes it.

12 12 Designing finite automata Let A = { w | w is 0-1 string and w has odd number of 1} Is A a regular language ? What is the automata accepting A ?

13 13 Designing finite automata eg: Design a finite automaton to recognize all strings that contains 001 as a substring

14 14 Regular operations A, B : languages Regular operations : union, concatenation, star Union : A  B = { x | x  A or x  B} Concatenation : A 。 B = { x y | x  A and y  B} Star : A* = { x 1 x 2 … x k | k  0 and each x i  A }

15 15 Regular operations eg:  = { a, b, c, …, z} (26 letters) A = { good, bad} B = { boy, girl} A  B = { good, bad, boy, girl} A 。 B= {goodboy, goodgirl, badboy,badgirl} A* = { , good, bad, goodbad, badgood, goodgood, badbad, goodgoodgood, goodgoodbad, ……… }

16 16 eg. A 1 ={ w | w has odd number of 1} Q 1 = {q even, q odd }  = {0,1}  1 :01 q even q even q odd q odd q odd q even q even : start state q odd : accept state

17 17 eg. A 2 ={ w | w has a 1} Q 2 = {q 1, q 2 }  = {0,1}  2 :01 q 1 q 1 q 2 q 2 q 2 q 2 q 1 : start state q 2 : accept state

18 18 A 1  A 2 ={ w | w has odd number of 1 or w has a 1} Q= {(q even,q 1 ), (q even,q 2 ), (q odd,q 1 ), (q odd,q 2 )}  = {0, 1}  : 01 (q even, q 1 )(q even, q 1 )(q odd, q 2 ) (q even, q 2 )(q even, q 2 )(q odd, q 2 ) (q odd, q 1 )(q odd, q 1 )(q even, q 2 ) (q odd, q 2 )(q odd, q 2 )(q even, q 2 ) (q even, q 1 ): start state (q odd, q 1 )(q odd, q 2 )(q even, q 2 ): accept state

19 19 Theorem: The class of regular languages is closed under the union operation (in other words, if A 1 and A 2 are regular languages, so is A 1  A 2 )

20 20 Pf : Let M 1 recognize A 1, where M 1 = (Q 1, ,  1, q 1, F 1 ) M 2 recognize A 2, where M 2 = (Q 2, ,  2, q 2, F 2 ) Goal: Construct M=(Q, , , q 0, F) to recognize A 1  A 2 Q={(r 1,r 2 )|r 1  Q 1 and r 2  Q 2 }  is the same in M 1, M 2 For each (r 1, r 2 )  Q and each a  , let  ((r 1,r 2 ), a)=(  1 (r 1,a),  2 (r 2,a)) q 0 =(q 1, q 2 ), (q 0 is a new state  Q 1  Q 2 ) F={(r 1, r 2 )|r 1  F 1 or r 2  F 2 }

21 21 Theorem: The class of regular languages is close under the concatenation (in other words, if A 1 and A 2 are regular languages, so is A 1 。 A 2 )

22 22 Nondeterminism: eg. Deterministic Computation Non Deterministic Computation

23 23 Input : 0 1 0 1 1 0

24 24 eg: A : the language consisting of all strings over {0, 1} containing a 1 in the 3rd position from the end

25 25 0 1 1 0 0 1

26 26 eg: What does the following automaton accept? 1.2.   

27 27 Formal definition of a nondeterministic finite automaton   =  {  }, P (Q) : the collection of all subset of Q A nondeterministic finite automaton is a 5-tuple ( Q, , , q 0, F), where Q : a finite set of states  : a finite set of alphabet  : Qx    P (Q) transition function q 0  Q : start state F  Q : the set of accept states

28 28 eg. Q = {q 1, q 2, q 3, q 4 }  = {0, 1}  : 01  q 1 {q 1 } {q 1, q 2 }  q 2 {q 3 }  {q 3 } q 3  {q 4 }  q 4 {q 4 } {q 4 }  q 1 : start state q 4 : accept state

29 29 N=(Q, , , q 0, F )~NFA w : string over , w= y 1 y 2 y 3 … y m, y i   r 0,r 1,r 2,…,r m  Q N accepts w if exists r 0,r 1,r 2,…,r m such that r 0 =q 0 r i+1   (r i, y i+1 ), for i =0, … m-1 r m  F

30 30 Equivalence of NFA and DFA Theorem : Every nondeterministic finite automaton has an equivalent deterministic finite automaton Pf: Let N=(Q, , , q 0, F) be the NFA recognizing A. Goal: Construct a DFA recognizing A 1. First we don ’ t consider . Construct M= ( Q’, ,  ’, q 0 ’, F’) 1. Q’= P (Q) 2. For R  Q’ and a  let 3. q 0 ’= {q 0 } 4. F’ = {R  Q’ | R contains an accept state of N}

31 31 Equivalence of NFA and DFA 2. Consider  : For any R  M, define E(R)={q | q can be reached from R by traveling along 0 or more  } Replace  (r,a) by E(  (r,a)) Thus,  ’(R, a)={q  Q : q  E(  (r,a)) for some r  R} Change q 0 ’ to be E({q 0 })

32 32 Corollary: A language is regular if and only if some NFA recognizes it eq. D ’ s state : { , {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3}} N 4 = ({1,2,3}, {a,b}, , 1, {1})

33 33 Construct a DFA D equivalent to N 4

34 34 eg. Convert the following NFA to an equivalent DFA E({q 0 }) = {q 0 } E(  ({q 0 },a)) = {q 1,q 2 } E(  ({q 0 },b)) =  E(  ({q 1,q 2 },a) = {q 1,q 2 } E(  ({q 1,q 2 },b) = {q 0 }

35 35 Closure under the regular operations Theorem : The class of regular language is closed under the union operation Pf: A 1 : N 1 : A 2 : N 2 : N 1 =(Q 1, ,  1, q 1, F 1 ) for A 1 N 2 =(Q 2, ,  2, q 2, F 2 ) for A 2 A1  A2

36 36 Construct N=(Q, , , q 0, F) to recognize A1  A2 Q : {q 0 }  Q 1  Q 2 q 0 : start state F : F 1  F 2 For q  Q and a    (q, a)=  1 (q, a),q  Q 1  2 (q, a),q  Q 2 {q 1, q 2 },q=q 0 and a=  ,q=q 0 and a 

37 37 Theorem : The class of regular language is closed under the concatenation operation Pf: N 1 : N 2 : N 1 =(Q 1, ,  1, q 1, F 1 ) recognize A 1 N 2 =(Q 2, ,  2, q 2, F 2 ) recognize A 2 A1 。 A2

38 38 Construct N=(Q, , , q 1, F 2 ) to recognize A1 。 A2 Q : Q 1  Q 2 q 1 : start state F 2 : accept state  (q, a) =  1 (q, a), q  Q 1 and q  F 1  1 (q, a), q  F 1 and a   1 (q, a)  {q 2 },q  F 1 and a=   2 (q, a),q  Q 2 q  Q, a 

39 39 Theorem : The class of regular language is closed under the star operation Pf: A: A*: N 1 =(Q 1, ,  1, q 1, F 1 ) recognize A A = {a, b} A* = { , a, b, … } , A A 。 A = {aa, ab, ba, bb} A 。 A 。 A …

40 40 Construct N=(Q, , , q 0, F) to recognize A* Q : { q 0 }  Q q 0 : start state F : { q 0 }  F  (q, a) =  1 (q, a), q  Q 1 and q  F 1  1 (q, a), q  F 1 and a   1 (q, a)  {q 1 },q  F 1 and a=  {q 1 },q=q 0 and a=   q=q 0 and a 

41 41 Regular Expressions AWK, GREP in UNIX PERL, text editors. eg. (0  1)0* = ({0}  {1}) 。 {0}* eg. (0  1)* = {0, 1}*

42 42 Formal definition of a regular expression Definition: R is a regular expression if R is a     (R 1  R 2 ), R 1 and R 2 are regular (R 1 。 R 2 ), R 1 and R 2 are regular (R 1 * ), R 1 is regular expression Inductive definition

43 43 eg.  = {0, 1} 0*10* = { w : w has exactly a single 1}  *1  *  *001  * (  )* 01  10 0  *0  1  *1  0  1 (0  )1*=01*  1* (0  )(1  ) = { , 0, 1, 01} 1*  =   * = {  }

44 44 eg. R : regular expression R  = R R 。  = R R   ?=R R 。  ?= R R = {0} R 。  = 

45 45 Equivalence with finite automata Theorem: A language is regular if and only if some regular expression describes it Lemma: (  ) If a language is described by a regular expression then it is regular

46 46 Pf: Let R be a regular expression describing some language A Goal: Convert R into an NFA N. R=a  , L(R)={a} R= , L(R)={  } R= , L(R)=  R = R 1  R 2 R = R 1 。 R 2 R = R 1 *

47 47 eg. (ab  a)* a b ab ab  a (ab  a)*

48 48 eg. (a  b)*aba a b a  b (a  b)* aba

49 49 (a  b)*aba

50 50 Generalized nondeterministic finite automaton (GNFA) ( Q, , , q start, q accept )  : (Q - {q accept }) x (Q - {q start })  R Set of all regular expressions over   (q i, q j ) = R

51 51 Lemma: (  ) If a language is regular, then it is described by a regular expression Pf: a language A is regular  There is a DFA M accepting A Goal: Convert DFAs into equivalent regular expressions

52 52 A GNFA accepts a string w in  * if w = w 1 w 2 …w k, where each w i is in  * and a sequence of states q 0 q 1 q 2 …q k exists s.t. q 0 = q start is the start state q k = q accept is the accept state for each i, we have w i  L(R i ), where R i =  (q i-1, q i ) DFA M --------------------  GNFA G Convert (G) : takes a GNFA and return an equivalent regular expression start state addingaccept state transition arrows

53 53 Convert(G) Let k be the number of states of G If k=2, return R If k>2, select any q rip  Q (  q start,q accept ), Let G ’ = GNFA(Q’, ,  ’, q start, q accept ), where Q’=Q-{q rip }, and for any q i  Q’-{q accept } and any q j  Q’-{q start }, let  ’(q i, q j ) = (R 1 )(R 2 )*(R 3 )  (R 4 ) Compute CONVERT(G’) and return this value

54 54 Claim: For any GNFA G, CONVERT(G) is equivalent to G. Pf: By induction on k, the number of states of the GNFA Basis: It is clear for k=2

55 55 Induction step Assume that the claim is true for k-1 states Suppose G accepts an input w, Then in an accepting branch of the computation G enters a sequence of states: q start, q 1, q 2, …, q accept If none of them is q rip, G’ accepts w. If q rip does appear in the above states, removing each run of consecutive q rip states forms an accepting computation for G’ Suppose G’ accepts an input w G’: G: or ⇒ G accepts w G ≅ G’, by induction hypothesis, the claim is true

56 56 example eg:

57 57 example

58 58 Pumping lemma for regular languages Is {0 n 1 n : n ≥ 0} regular? How can we prove a language non-regular? Theorem: (Pumping Lemma) If A is a regular language, then there is a number P (the pumping length) where, if s is any string in A of length ≥ P, then s may be divided into 3 pieces, s = x yz, satisfying the following conditions: for each i ≥ 0, x y i z ∈ A | y | > 0 | x y | ≤ P

59 59 proof M=(Q, Σ, δ,q 1, F): a DFA recognizing A P =|Q|: number of state of M s = s 1 s 2 … s n ∈ A, n ≥ P r 1, r 2, …, r n+1 : the seq. of states M enters while processing r i+1 = δ (r i, s i ) for 1 ≤ i ≤ n. By pigeonhole principle, there must be 2 identical states, say r j =r l, x = s 1 … s j-1, y = s j …s l-1, z = s l …s n x takes M from r 1 to r j, y takes M from r j to r j, and z takes M from r j to r n+1 M must accept x y i z for i ≥ 0 ∵ j ≠ l, | y |=| s j …s l-1 |>0, l ≤ P +1, so | x y | ≤ P

60 60 B={0 n 1 n : n≥0} is not regular proof: Suppose B is regular Let P be the pumping length Choose s = 0 P 1 P = 0 … 01 … 1 ∈ B Let s = x yz, By pumping lemma, for any i ≥0 x y i z ∈ B. But 0…001…1 : y has 0 only ⇒ →← 0…01…1 : y has 1 only ⇒ →← 0…01…1 : y has both 0 and 1 ⇒ x yyz ∉ B

61 61 C={ w | w has an equal number of 0s and 1s} is not regular proof: (By pumping lemma) Let P be the pumping length, Suppose C is regualr Let s = 0 P 1 P ∈ C, By pumping lemma, s can be split into 3 pieces, s = x yz, and x y i z ∈ C for any i ≥0 By condition 3 in the lemma: | x y | ≤ P Thus y must have only 0s. Then x yyz ∉ C

62 62 proof: (Not by pumping lemma) Suppose C is regular It is clear that 0*1* is regualr Then C ∩ 0*1*={0 n 1 n : n≥0} is regular →←

63 63 F={ ww | w ∈ {0,1}* } is nonregular proof: Suppose F is regular Let P be the pumping length given by the pumping lemma Let s = 0 P 10 P 1 ∈ F Split s into 3 pieces, s = x yz By condition 3 in the lemma: | x y | ≤ P Thus y must have 0 only. ⇒ x yyz ∉ F 0…010…01 →← w y

64 64 E={0 i 1 j : i > j } is nonregular proof: Assume E is regular Let P be the pumping length Let s = 0 P+1 1 P ∈ E Split s into 3 pieces, s = x yz By pumping lemma: x y i z ∈ E for any i ≥ 0 | y |>0, y have 0 only. x z ∈ E. But x z has #(0) ≤ #(1)

65 65 D={1 n 2 : n ≥ 0 } is not regular proof: Assume D is regular Let P be the pumping length Let s = 1 P 2 ∈ D Split s into 3 pieces, s = x yz ⇒ x y i z ∈ D, i ≥ 0 Consider x y i z ∈ D and x y i+1 z ∈ D ⇒ | x y i z | and | x y i+1 z | are perfect squre for any i ≥ 0 If m=n 2, (n+1) 2 - n 2 =2n+1 = 2 +1

66 66 Let m=| x y i z | | y | ≤ | s |= P 2 Let i = P 4 | y |= | x y i+1 z |-| x y i z | ≤ P 2 = ( P 4 ) 1/2 < 2( P 4 ) 1/2 +1 ≤ 2(| x y i z |) 1/2 +1 = 2 +1 → ←


Download ppt "1 Regular Languages Finite Automata eg. Supermarket automatic door: exit or entrance."

Similar presentations


Ads by Google