Download presentation
Presentation is loading. Please wait.
Published byIvan Sutedja Modified over 6 years ago
1
Regular Languages Finite Automata eg. Supermarket automatic door:
exit or entrance
2
Finite Automata Input signal NEITHER FRONT REAR BOTH CLOSED OPEN
State transition table Door state State diagram
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
4
M1 = (Q, , , q0, F) Q = {q1, q2, q3} = {0, 1} : 0 1
: 0 1 q1 q1 q2 q2 q3 q2 q3 q2 q2 q1: the start state F = {q2} L(M) = A M recognizes A or M accepts A the set of all strings that M accepts L(M1) = ?
5
M2: 100000 101011 : 0 1 q1 q1 q2 q2 q1 q2 L(M2) = {w | w ends in a 1}
Eg: M2: M2=({q1,q2}, {0,1}, , q1, {q2}) : 0 1 q1 q1 q2 q2 q1 q2 L(M2) = {w | w ends in a 1} L(M2) =?
6
M3: L(M3)={ or ends in 0} L(M3)=?
7
Starts and ends with the same symbol
8
={<RESET>, 0, 1, 2} M5: 1 0 <RESET> 2 2 <RESET> 0 1 2 What is the language accepted by the above automata?
9
={<RESET>, 0, 1, 2} M5: L(M5) = {w | the sum of the symbols in w is 0 modulo except the <RESET> resets to 0}
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
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
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
Designing finite automata
eg: Design a finite automaton to recognize all strings that contains 001 as a substring
14
Regular operations A, B : languages
Regular 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 }
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, ………}
16
eg. A1={w | w has odd number of 1}
Q1= {qeven, qodd} = {0,1} 1: qeven qeven qodd qodd qodd qeven qeven : start state qodd : accept state
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
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} : (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
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)
20
Pf : Let M1 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}
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)
22
Nondeterminism: eg. Deterministic Computation
Non Deterministic Computation
23
Input :
24
eg: A : the language consisting of all strings over {0, 1} containing a 1 in the 3rd position from the end
26
eg: What does the following automaton accept?
1. 2.
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
28
eg. Q = {q1, q2, q3, q4} = {0, 1} : 0 1 q {q1} {q1, q2} q {q3} {q3} q3 {q4} q {q4} {q4} q1 : start state q4 : accept state
29
N=(Q, , , q0, F ) ~NFA w : 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
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 A 1. 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}
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})
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}} N4 = ({1,2,3}, {a,b}, , 1, {1})
33
Construct a DFA D equivalent to N4
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}
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
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), qQ 2(q, a), qQ {q1, q2}, q=q0 and a= , q=q0 and a
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
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 qF 1(q, a), qF1 and a 1(q, a){q2}, qF1 and a= 2(q, a), qQ2 qQ, a
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 …
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 qF 1(q, a), qF1 and a 1(q, a){q1}, qF1 and a= {q1}, q=q0 and a= q=q0 and a
41
Regular Expressions eg. (01)0* = ({0}{1})。{0}* eg. (01)* = {0, 1}*
AWK, GREP in UNIX PERL, text editors. eg. (01)0* = ({0}{1})。{0}* eg. (01)* = {0, 1}*
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
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
eg. R : regular expression R = R R。 = R
R ?=R R。 ?= R R = {0} R。 =
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
Pf: Let R be a regular expression describing some language A
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*
47
eg. (ab a)* a b ab ab a (ab a)*
48
eg. (a b)*aba a b a b (a b)* aba
49
(a b)*aba
50
Generalized nondeterministic finite automaton (GNFA)
( Q, , , qstart, qaccept) : (Q - {qaccept}) x (Q - {qstart}) R Set of all regular expressions over (qi, qj) = R
51
Pf: a language A is regular There is a DFA M accepting A
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
DFA M -------------------- GNFA G
A GNFA accepts a string w in * if w = 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 an equivalent regular expression start state adding accept state transition arrows
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
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
Induction step ⇒ G accepts w
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: 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 w G’: G: or ⇒ G accepts w G ≅G’, by induction hypothesis, the claim is true
56
example eg:
57
example
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
59
proof P =|Q|: number of state of M s = s1 s2… sn∈A, n≥P
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 be 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
60
B={0n1n: n≥0} is not regular
proof: 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
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 = 0P1P∈C, By pumping lemma, s can be split 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
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 →←
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 = 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
64
E={0i1j : i >j } is nonregular
proof: 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)
65
D={1n2 : n ≥ 0} is not regular
proof: 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 =
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 = →←
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.