660 likes | 705 Views
Regular Languages. Finite Automata eg. Supermarket automatic door:. exit or entrance. Finite Automata. Input signal. State transition table. Door state. State diagram. Definition. A finite automaton is a 5-tuple (Q, , , q 0 , F) Q : a finite set called the states
E N D
Regular Languages • Finite Automata • eg. Supermarket automatic door: exit or entrance
Finite Automata Input signal Statetransitiontable Doorstate Statediagram
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
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) = ?
q2q3q2q3q2q3 100000101011 Eg: M2: M2=({q1,q2}, {0,1}, , q1, {q2}) : 0 1q1 q1 q2 q2 q1 q2 L(M2) =? L(M2) = {w | w ends in a 1}
M3: L(M3)={ or ends in 0} L(M3)=?
={<RESET>, 0, 1, 2} M5: 1 0 <RESET> 2 2 <RESET> 0 1 2 What is the language accepted by the above automata?
={<RESET>, 0, 1, 2} M5: L(M5) = {w | the sum of the symbols in w is 0 modulo 3 except the <RESET> resets to 0}
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
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.
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 ?
Designing finite automata • eg: Design a finite automaton to recognize all strings that contains 001 as a substring
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 iA }
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, ………}
eg. A1={w | w has odd number of 1} • Q1= {qeven, qodd} • = {0,1} • 1:0 1 qeven qeven qoddqodd qodd qeven • qeven : start state • qodd : accept state
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
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
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)
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 F1or r2 F2}
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)
Nondeterminism: • eg. DeterministicComputation Non DeterministicComputation
eg: A : the language consisting of all strings over {0, 1} containing a 1 in the 3rd position from the end
eg: What does the following automaton accept? 1. 2.
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
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
N=(Q, , , q0, F ) ~NFAw : string over , w= y1 y2 y3…ym , yir0,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
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}
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})
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})
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}
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
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
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
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
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…
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
Regular Expressions • AWK, GREP in UNIXPERL, text editors. • eg.(01)0* = ({0}{1})。{0}* • eg.(01)* = {0, 1}*
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
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* = • * = {}
eg.R : regular expressionR = RR。 = R • R ?=R R。 ?= R R = {0}R。 =
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
Pf:Let R be a regular expression describing some language AGoal: Convert R into an NFA N. • R=a , L(R)={a} • R=, L(R)={} • R=, L(R)= • R = R1R2 • R = R1。R2 • R = R1*
eg. (ab a)* • a • b • ab • ab a • (ab a)*
eg. (a b)*aba • a • b • a b • (a b)* • aba
Generalized nondeterministic finite automaton (GNFA) • ( Q, , , qstart, qaccept) : (Q - {qaccept}) x (Q - {qstart}) R Set of all regular expressions over (qi, qj) = R