1 / 66

Regular Languages

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

Download Presentation

Regular Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Finite Automata Input signal Statetransitiontable Doorstate Statediagram

  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 •  : QxQ is the transition function • q0Q 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 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) = ?

  5. 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}

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

  7. Starts and ends with the same symbol M4:

  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 3 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 ifA = {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 : AB = {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} • AB = { 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:0 1 qeven qeven qoddqodd 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. A1A2={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

  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 A1A2)

  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)|r1Q1 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  Q1Q2) • F={(r1, r2)|r1 F1or 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. DeterministicComputation Non DeterministicComputation

  23. Input : 0 1 0 1 1 0

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

  25. 0 1 1 0 0 1

  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 •  : QxP (Q) transition function • q0Q : start state • F  Q : the set of accept states

  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

  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

  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 RQ’ and a let 3. q0’= {q0} 4. F’ = {RQ’ | R contains an accept state of N}

  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 q0’to be E({q0})

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

  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 A1A2

  36. Construct N=(Q, , , q0, F) to recognize A1A2 • Q : {q0}  Q1  Q2 • q0 : start state • F : F1  F2 • For qQ and a(q, a)= 1(q, a), qQ1 2(q, a), qQ2 {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), qQ1 and qF1 1(q, a), qF1 and a 1(q, a){q2}, qF1 and a= 2(q, a), qQ2 qQ, 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), qQ1 and qF1 1(q, a), qF1 and a 1(q, a){q1}, qF1 and a= {q1}, q=q0 and a=  q=q0 and a

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

  42. Formal definition of a regular expression • Definition:R is a regular expression if R is • a   •  •  • (R1R2), 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 expressionR = RR。 = 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 AGoal: Convert R into an NFA N. • R=a  , L(R)={a} • R=, L(R)={} • R=, L(R)= • R = R1R2 • 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

More Related