0227 regularlanguages

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

Upload: issbp

Post on 29-Nov-2014

937 views

Category:

Documents


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: 0227 regularlanguages

1

Regular Languages

Finite Automata eg. Supermarket automatic door:

exit or entrance

Page 2: 0227 regularlanguages

2

Finite Automata

NEITHER FRONT

REAR BOTH

CLOSED

CLOSED OPEN CLOSED

CLOSED

OPEN CLOSED OPEN OPEN OPEN

Doorstate

Input signal

Statetransitiontable

Statediagram

Page 3: 0227 regularlanguages

3

Definition

A finite automaton is a 5-tuple (Q, , , q0, F) Q : a finite set called the states : a finite set called the alphabet : QxQ is the transition function q0Q is the start state F Q is the set of accept states

final states

Page 4: 0227 regularlanguages

4

M1 = (Q, , , q0, F) Q = {q1, q2, q3} = {0, 1} : 0 1

q1 q1 q2

q2 q3 q2

q3 q2 q2

q1: the start state F = {q2}

L(M) = AM recognizes A orM accepts Athe set of all strings that M accepts

L(M1) = ?

Page 5: 0227 regularlanguages

5

q2q3q2q3q2q3

100000101011

M2=({q1,q2}, {0,1}, , q1, {q2})

Eg:

: 0 1q1 q1 q2

q2 q1 q2L(M2) =?

M2:

L(M2) = {w | w ends in a 1}

Page 6: 0227 regularlanguages

6

M3:

L(M3)=?L(M3)={ or ends in 0}

Page 7: 0227 regularlanguages

7

M4:Starts and ends with the same symbol

Page 8: 0227 regularlanguages

8What is the language accepted by the above automata?1 0 <RESET> 2 2 <RESET> 0 1 2

M5:={<RESET>, 0, 1, 2}

Page 9: 0227 regularlanguages

9

L(M5) = {w | the sum of the symbols in w is 0 modulo 3 except the <RESET> resets to 0}

M5:={<RESET>, 0, 1, 2}

Page 10: 0227 regularlanguages

10

Formal definition of computation

M = (Q, , , q0, F)w : a string over , wi, w =w1w2 …wn

M accepts w if a sequence of states r0, r1, …, rn exists in Q and satisfies the following 3 conditions: r0 = q0

( ri, wi+1) = ri+1, for i = 0, …, n-1 rn F

Page 11: 0227 regularlanguages

11

We say that M recognizes language A ifA = {w | M accepts w}

Def:A language is called a regular language if some finite automata recognizes it.

Page 12: 0227 regularlanguages

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 ?

Page 13: 0227 regularlanguages

13

Designing finite automata

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

Page 14: 0227 regularlanguages

14

Regular operations

A, B : languagesRegular operations : union, concatenation, star Union :

AB = {x | x A or x B} Concatenation :

A。 B = {x y | x A and y B} Star :

A* = {x 1x 2…x k | k 0 and each x i

A }

Page 15: 0227 regularlanguages

15

Regular operations

eg: = { a, b, c, …, z} (26 letters)A = { good, bad}B = { boy, girl}

AB = { good, bad, boy, girl}A 。 B= {goodboy, goodgirl, badboy,badgirl}A* = {, good, bad, goodbad, badgood, goodgood, badbad, goodgoodgood, goodgoodbad, ………}

Page 16: 0227 regularlanguages

16

eg. A1={w | w has odd number of 1} Q1= {qeven, qodd} = {0,1} 1: 0 1

qeven qeven qodd qodd qodd qeven

qeven : start state qodd : accept state

Page 17: 0227 regularlanguages

17

eg. A2={w | w has a 1} Q2 = {q1, q2} = {0,1} 2: 0 1

q1 q1 q2

q2 q2 q2 q1: start state q2: accept state

Page 18: 0227 regularlanguages

18

A1A2={w | w has odd number of 1 or w has a 1}

Q= {(qeven,q1), (qeven,q2), (qodd,q1), (qodd,q2)} = {0, 1} : 0 1

(qeven, q1) (qeven, q1) (qodd, q2)(qeven, q2) (qeven, q2) (qodd, q2)(qodd, q1) (qodd, q1) (qeven, q2)(qodd, q2) (qodd, q2) (qeven, q2)

(qeven, q1): start state (qodd, q1)(qodd, q2)(qeven, q2): accept state

Page 19: 0227 regularlanguages

19

Theorem:The class of regular languages is closed under the union operation(in other words, if A1 and A2 are regular languages, so is A1A2)

Page 20: 0227 regularlanguages

20

Pf : LetM1 recognize A1, where M1= (Q1, , 1, q1, F1)

M2 recognize A2, where M2= (Q2, , 2, q2, F2)Goal: Construct M=(Q, , , q0, F) to recognize A1 A2

Q={(r1,r2)|r1Q1 and r2 Q2}

is the same in M1, M2

For each (r1, r2)Q and each a, let

((r1,r2), a)=(1(r1,a), 2(r2,a)) q0=(q1, q2), (q0 is a new state Q1Q2) F={(r1, r2)|r1 F1 or r2 F2}

Page 21: 0227 regularlanguages

21

Theorem:The class of regular languages is close under the concatenation(in other words, if A1 and A2 are regular languages, so is A1。 A2)

Page 22: 0227 regularlanguages

22

Nondeterminism: eg.

DeterministicComputation

Non DeterministicComputation

Page 23: 0227 regularlanguages

23

Input : 0 1 0 1 1 0

Page 24: 0227 regularlanguages

24

eg:

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

Page 25: 0227 regularlanguages

25

0 1 1 0 0 1

Page 26: 0227 regularlanguages

26

eg: What does the following automaton accept?

1. 2.

Page 27: 0227 regularlanguages

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, , , q0, F), where Q : a finite set of states : a finite set of alphabet : QxP (Q) transition function q0Q : start state F Q : the set of accept states

Page 28: 0227 regularlanguages

28

eg.

Q = {q1, q2, q3, q4} = {0, 1} : 0 1

q1 {q1} {q1, q2} q2 {q3} {q3}q3 {q4}

q4 {q4} {q4} q1 : start state q4 : accept state

Page 29: 0227 regularlanguages

29

N=(Q, , , q0, F ) ~NFAw : string over , w= y1 y2 y3 … ym , yi

r0,r1,r2,…,rm Q

N accepts w if exists r0,r1,r2,…,rm such that r0=q0

ri+1 (ri ,yi+1), for i =0,…m-1 rm F

Page 30: 0227 regularlanguages

30

Equivalence of NFA and DFA Theorem : Every nondeterministic finite

automaton has an equivalent deterministic finite automaton

Pf: Let N=(Q, , , q0, F) be the NFA recognizing A.Goal: Construct a DFA recognizing A1. First we don’t consider . Construct M= ( Q’, , ’, q0’, F’)

1. Q’= P (Q) 2. For RQ’ and a let

3. q0’= {q0} 4. F’ = {RQ’ | R contains an accept state of N}

Rr

ar

RrarqQqaR

),(

}),,(|{),('

Page 31: 0227 regularlanguages

31

Equivalence of NFA and DFA

2. Consider : For any RM, 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)={qQ : qE((r,a)) for some rR} Change q0’ to be E({q0})

Page 32: 0227 regularlanguages

32

Corollary: A language is regular if and only if some NFA recognizes iteq.

D’s state :{, {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3}}

N4 = ({1,2,3}, {a,b}, , 1, {1})

Page 33: 0227 regularlanguages

33

Construct a DFA D equivalent to N4

Page 34: 0227 regularlanguages

34

eg. Convert the following NFA to an equivalent DFA

E({q0}) = {q0}E(({q0},a)) = {q1,q2}E(({q0},b)) = E(({q1,q2},a) = {q1,q2}E(({q1,q2},b) = {q0}

Page 35: 0227 regularlanguages

35

Closure under the regular operations

Theorem : The class of regular language is closed under the union operation

Pf:A1: N1:

A2: N2:

N1=(Q1, , 1, q1, F1) for A1

N2=(Q2, , 2, q2, F2) for A2

A1A2

Page 36: 0227 regularlanguages

36

Construct N=(Q, , , q0, F) to recognize A1A2 Q : {q0} Q1 Q2

q0 : start state F : F1 F2

For qQ and a

(q, a)= 1(q, a), qQ1 2(q, a), qQ2 {q1, q2}, q=q0 and a= , q=q0 and a

Page 37: 0227 regularlanguages

37

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

Pf: N1: N2:

N1=(Q1, , 1, q1, F1) recognize A1

N2=(Q2, , 2, q2, F2) recognize A2

A1。 A2

Page 38: 0227 regularlanguages

38

Construct N=(Q, , , q1, F2) to recognize A1 。 A2 Q : Q1 Q2

q1 : start state F2 : accept state (q, a) = 1(q, a), qQ1 and qF1

1(q, a), qF1 and a 1(q, a){q2}, qF1 and a=

2(q, a), qQ2

qQ, a

Page 39: 0227 regularlanguages

39

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

Pf: A:

A*:

N1=(Q1, , 1, q1, F1) recognize A

A = {a, b}

A* = {, a, b, …}

, A A 。 A = {aa, ab, ba, bb} A 。 A 。 A …

Page 40: 0227 regularlanguages

40

Construct N=(Q, , , q0, F) to recognize A* Q : {q0} Q q0 : start state F : {q0} F (q, a) = 1(q, a), qQ1 and qF1

1(q, a), qF1 and a 1(q, a){q1}, qF1 and a=

{q1}, q=q0 and a= q=q0 and a

Page 41: 0227 regularlanguages

41

Regular Expressions AWK, GREP in UNIX

PERL, text editors. eg.

(01)0* = ({0}{1}) 。 {0}*

eg.(01)* = {0, 1}*

Page 42: 0227 regularlanguages

42

Formal definition of a regular expression

Definition:R is a regular expression if R is a (R1R2), R1 and R2 are regular (R1。 R2), R1 and R2 are regular (R1* ), R1 is regular expression

Inductive definition

Page 43: 0227 regularlanguages

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* = * = {}

Page 44: 0227 regularlanguages

44

eg.R : regular expressionR = RR 。 = R

R ?=R R 。 ?= R R = {0}R 。 =

Page 45: 0227 regularlanguages

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

Page 46: 0227 regularlanguages

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 = R1R2

R = R1。 R2

R = R1*

Page 47: 0227 regularlanguages

47

eg. (ab a)* a b ab

ab a

(ab a)*

Page 48: 0227 regularlanguages

48

eg. (a b)*aba a b

a b

(a b)*

aba

Page 49: 0227 regularlanguages

49

(a b)*aba

Page 50: 0227 regularlanguages

50

Generalized nondeterministic finite automaton (GNFA)

( Q, , , qstart, qaccept) : (Q - {qaccept}) x (Q - {qstart}) R

Set of all regular expressions over

(qi, qj) = R

Page 51: 0227 regularlanguages

51

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

Pf: a language A is regularThere is a DFA M accepting A Goal: Convert DFAs into equivalent regul

ar expressions

Page 52: 0227 regularlanguages

52

A GNFA accepts a string w in * ifw = w1w2…wk , where each wi is in * and a sequence of states q0q1q2…qk exists s.t.

q0 = 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

Convert (G) : takes a GNFA and return anequivalent regular expressi

on

start stateadding accept state

transition arrows

Page 53: 0227 regularlanguages

53

Convert(G) Convert(G)

Let k be the number of states of G If k=2, return R

If k>2, select any qripQ (qstart ,qaccept), Let G’ = GNFA(Q’, , ’, qstart, qaccept), where Q’=Q-{qrip},and for any qiQ’-{qaccept} and any qjQ’-{qstart}, let ’(qi, qj) = (R1)(R2)*(R3)(R4)

Compute CONVERT(G’) and return this value

Page 54: 0227 regularlanguages

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

Page 55: 0227 regularlanguages

55

Induction stepAssume that the claim is true for k-1 states Suppose G accepts an input w, Then in an acceptin

g branch of the computation G enters a sequence of states: qstart, q1, q2, …, qaccept

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 wG’: G: or

⇒ G accepts w G ≅G’, by induction hypothesis, the claim is true

Page 56: 0227 regularlanguages

56

example

eg:

Page 57: 0227 regularlanguages

57

example

Page 58: 0227 regularlanguages

58

Pumping lemma for regular languages Is {0n1n: 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 yi z ∈A |y| > 0 |x y| ≤ P

Page 59: 0227 regularlanguages

59

proof M=(Q,Σ,δ,q1, F): a DFA recognizing A P =|Q|: number of state of M s = s1 s2… sn∈A, n≥P r1, r2, …, rn+1: the seq. of states M enters while processing ri+1=δ(ri ,si) for 1≤i ≤n. By pigeonhole principle, there must b

e 2 identical states, say rj=rl , x = s1…sj-1 , y =sj…sl-1 , z =sl …sn

x takes M from r1 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

Page 60: 0227 regularlanguages

60

B={0n1n: n≥0} is not regularproof:

Suppose B is regular Let P be the pumping length Choose s = 0P1P = 0…01…1∈B Let s = x yz, By pumping lemma, for any i ≥0

x yiz∈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

Page 61: 0227 regularlanguages

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 regual

r Let s = 0P1P∈C, By pumping lemma, s can be spli

t into 3 pieces, s =x yz, and x yiz∈C for any i ≥0 By condition 3 in the lemma: |x y| ≤ P

Thus y must have only 0s.Then x yyz ∉C

Page 62: 0227 regularlanguages

62

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

→←

Page 63: 0227 regularlanguages

63

F={ww | w∈{0,1}* } is nonregularproof:

Suppose F is regular Let P be the pumping length given by the pumpin

g lemma Let s = 0P10P1∈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

Page 64: 0227 regularlanguages

64

E={0i1j : i >j } is nonregularproof:

Assume E is regular Let P be the pumping length Let s = 0P+11P∈E Split s into 3 pieces, s =x yz 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)

Page 65: 0227 regularlanguages

65

D={1n2 : n ≥ 0} is not regularproof:

Assume D is regular Let P be the pumping length Let s = 1P2∈D Split s into 3 pieces, s =x yz ⇒ x yiz∈D, i ≥ 0 Consider x yiz∈D and x yi+1z∈D

⇒|x yiz| and |x yi+1z| are perfect squre for any i ≥0

If m=n2, (n+1)2 - n2 =2n+1 = 2 +1

Page 66: 0227 regularlanguages

66

Let m=|x yiz| |y| ≤ |s |= P2

Let i = P4

|y|= |x yi+1z|-|x yiz| ≤ P2 = (P4)1/2

< 2(P4)1/2+1 ≤ 2(|x yiz|)1/2+1 = 2 +1

→←