Regular Languages n Finite Automata n eg Supermarket

  • Slides: 66
Download presentation
Regular Languages n Finite Automata n eg. Supermarket automatic door: exit or entrance 1

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

Finite Automata Input signal NEITHER Door CLOSED state OPEN CLOSED FRONT REAR BOTH OPEN

Finite Automata Input signal NEITHER Door CLOSED state OPEN CLOSED FRONT REAR BOTH OPEN CLOSED OPEN State transition table State diagram 2

Definition n A finite automaton is a 5 -tuple (Q, , , q 0,

Definition n A finite automaton is a 5 -tuple (Q, , , q 0, F) n n n 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 3

n M 1 = (Q, , , q 0, F) n n n Q

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

q 2 q 3 100000 101011 Eg: M 2=({q 1, q 2}, {0, 1},

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

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

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

M 4: Starts and ends with the same symbol 7

M 4: Starts and ends with the same symbol 7

M 5: ={<RESET>, 0, 1, 2} 1 0 <RESET> 2 2 <RESET> 0 1

M 5: ={<RESET>, 0, 1, 2} 1 0 <RESET> 2 2 <RESET> 0 1 2 What is the language accepted by the above automata? 8

M 5: ={<RESET>, 0, 1, 2} L(M 5) = {w | the sum of

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

Formal definition of computation M = (Q, , , q 0, F) w :

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

n n We say that M recognizes language A if A = {w |

n n 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. 11

Designing finite automata n Let A = {w | w is 0 -1 string

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

Designing finite automata n eg: Design a finite automaton to recognize all strings that

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

Regular operations n A, B : languages Regular operations : union, concatenation, star n

Regular operations n A, B : languages Regular operations : union, concatenation, star n n n 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 } 14

Regular operations n n eg: = { a, b, c, …, z} (26 letters)

Regular operations n n 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, badbad, goodgood, 15

n eg. A 1={w | w has odd number of 1} n n n

n eg. A 1={w | w has odd number of 1} n n n Q 1= {qeven, qodd} = {0, 1} 1 : 0 1 qeven qodd qeven : start state qodd : accept state 16

n eg. A 2={w | w has a 1} n n n Q 2

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

n A 1 A 2={w | w has odd number of 1 or w

n A 1 A 2={w | w has odd number of 1 or w has a 1} n n n Q= {(qeven, q 1), (qeven, q 2), (qodd, q 1), (qodd, q 2)} = {0, 1} : 0 1 (qeven, q 1) (qeven, q 2) (qodd, q 1) (qodd, q 2) (qeven, q 2) (qeven, q 1): start state (qodd, q 1)(qodd, q 2)(qeven, q 2): accept state 18

n Theorem: The class of regular languages is closed under the union operation (in

n 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) 19

n Pf : Let M 1 recognize A 1, where M 1= (Q 1,

n 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 n Q={(r 1, r 2)|r 1 Q 1 and r 2 Q 2} n n 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} 20

n Theorem: The class of regular languages is close under the concatenation (in other

n 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) 21

Nondeterminism: n eg. Deterministic Computation Non Deterministic Computation 22

Nondeterminism: n eg. Deterministic Computation Non Deterministic Computation 22

Input : 0 1 1 0 23

Input : 0 1 1 0 23

n eg: A : the language consisting of all strings over {0, 1} containing

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

011001 25

011001 25

n eg: What does the following automaton accept? 1. 2. 26

n eg: What does the following automaton accept? 1. 2. 26

Formal definition of a nondeterministic finite automaton n n = { }, P (Q)

Formal definition of a nondeterministic finite automaton n n = { }, P (Q) : the collection of all subset of Q A nondeterministic finite automaton is a 5 -tuple ( Q, , , q 0, F), where n n n 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 27

n eg. n n n Q = {q 1, q 2, q 3, q

n eg. n n n Q = {q 1, q 2, q 3, q 4} = {0, 1} : 0 1 q 2 q 3 q 4 n n q 1 : q 4 : {q 1} {q 1, q 2} {q 3} {q 4} start state accept state 28

n n N=(Q, , , q 0, F ) ~NFA w : string over

n n N=(Q, , , q 0, F ) ~NFA w : string over , w= y 1 y 2 y 3 … ym , yi r 0, r 1, r 2, …, rm Q N accepts w if exists r 0, r 1, r 2, …, rm such that n r 0=q 0 n r i+1 (ri , yi+1), for i =0, …m-1 n rm F 29

Equivalence of NFA and DFA n n Theorem : Every nondeterministic finite automaton has

Equivalence of NFA and DFA n n 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} 30

Equivalence of NFA and DFA 2. Consider : For any R M, define E(R)={q

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}) 31

n Corollary: A language is regular if and only if some NFA recognizes it

n 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}) 32

n Construct a DFA D equivalent to N 4 33

n Construct a DFA D equivalent to N 4 33

n n eg. Convert the following NFA to an equivalent DFA E({q 0}) =

n n 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} 34

Closure under the regular operations n n Theorem : The class of regular language

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

n Construct N=(Q, , , q 0, A 1 A 2 n Q :

n Construct N=(Q, , , q 0, A 1 A 2 n Q : {q 0} Q 1 Q 2 n q 0 : start state n F : F 1 F 2 n For q Q and a (q, a)= 1(q, a), 2(q, a), {q 1, q 2}, , F) to recognize q Q 1 q Q 2 q=q 0 and a= q=q 0 and a 36

n n Theorem : The class of regular language is closed under the concatenation

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

Construct N=(Q, , , q 1, F 2) to recognize A 1。A 2 n

Construct N=(Q, , , q 1, F 2) to recognize A 1。A 2 n Q : Q 1 Q 2 n q 1 : start state n F 2 : accept state n (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= q Q, a 2(q, a), q Q 2 n 38

n n Theorem : The class of regular language is closed under the star

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

n Construct N=(Q, , , q 0, F) to recognize A* n Q :

n Construct N=(Q, , , q 0, F) to recognize A* n Q : { q 0} Q n q 0 : start state n F : { q 0} F n (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 40

Regular Expressions n n n AWK, GREP in UNIX PERL, text editors. eg. (0

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

Formal definition of a regular expression n Definition: R is a regular expression if

Formal definition of a regular expression n Definition: R is a regular expression if R is n n n 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 42

n eg. = {0, 1} n n n n n 0*10* = {w :

n eg. = {0, 1} n n n n n 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* = { } 43

n n eg. R : regular expression R = R R。 = R R

n n eg. R : regular expression R = R R。 = R R ? =R R。 ? = R R = {0} R。 = 44

Equivalence with finite automata n n Theorem: A language is regular if and only

Equivalence with finite automata n n 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 45

n Pf: Let R be a regular expression describing some language A Goal: Convert

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

n eg. (ab a)* n a b ab n ab a n (ab a)*

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

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

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

n (a b)*aba 49

n (a b)*aba 49

Generalized nondeterministic finite automaton (GNFA) n ( Q, , , qstart, qaccept) : (Q

Generalized nondeterministic finite automaton (GNFA) n ( Q, , , qstart, qaccept) : (Q - {qaccept}) x (Q - {qstart}) R Set of all regular expressions over (qi, qj) = R 50

n n Lemma: ( ) If a language is regular, then it is described

n n 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 n Goal: Convert DFAs into equivalent regular expressions 51

n A GNFA accepts a string w in * if w = w 1

n A GNFA accepts a string w in * if w = w 1 w 2…wk , where each wi is in * and a sequence of states q 0 q 1 q 2…qk exists s. t. n n q 0 = qstart is the start state qk = qaccept is the accept state for each i , we have wi L(Ri ), where Ri = (qi-1 , qi ) DFA M ---------- GNFA G adding n start state accept state transition arrows Convert (G) : takes a GNFA and return an equivalent regular expression 52

Convert(G) n n n n Let k be the number of states of G

Convert(G) n n n n Let k be the number of states of G If k=2, return R If k>2, select any qrip Q ( qstart , qaccept), Let G’ = GNFA(Q’, , ’, qstart, qaccept), where Q’=Q-{qrip}, and for any qi Q’-{qaccept} and any qj Q’-{qstart}, let ’(qi, qj) = (R 1)(R 2)*(R 3) (R 4) Compute CONVERT(G’) and return this value 53

n n Claim: For any GNFA G, CONVERT(G) is equivalent to G. Pf: By

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

Induction step Assume that the claim is true for k-1 states n Suppose G

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

example n eg: 56

example n eg: 56

example 57

example 57

Pumping lemma for regular languages n n Is {0 n 1 n: n≥ 0}

Pumping lemma for regular languages n n 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: i n for each i ≥ 0, x y z ∈A n |y | > 0 n |x y | ≤ P 58

n proof n n n M=(Q, Σ, δ, q 1, F): a DFA recognizing

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

n B={0 n 1 n: n≥ 0} is not regular proof: n Suppose B

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

n C={w | w has an equal number of 0 s and 1 s}

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

proof: (Not by pumping lemma) n n n Suppose C is regular It is

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

n F={ww | w∈{0, 1}* } is nonregular proof: n Suppose F is regular

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

n E={0 i 1 j : i >j } is nonregular proof: n n

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

2 n n D={1 : n ≥ 0} is not regular proof: n n

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

n Let m=|x yiz| n |y| ≤ |s |= P 2 n Let i

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