Regular Languages Finite Automata eg. Supermarket automatic door:

Slides:



Advertisements
Similar presentations
Complexity and Computability Theory I Lecture #4 Rina Zviel-Girshin Leah Epstein Winter
Advertisements

FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
Applied Computer Science II Chapter 1 : Regular Languages Prof. Dr. Luc De Raedt Institut für Informatik Albert-Ludwigs Universität Freiburg Germany.
1 Introduction to Computability Theory Lecture3: Regular Expressions Prof. Amos Israeli.
Finite Automata Great Theoretical Ideas In Computer Science Anupam Gupta Danny Sleator CS Fall 2010 Lecture 20Oct 28, 2010Carnegie Mellon University.
1 Introduction to Computability Theory Lecture4: Regular Expressions Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture3: Regular Expressions Prof. Amos Israeli.
1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY (For next time: Read Chapter 1.3 of the book)
Finite Automata and Non Determinism
CS 310 – Fall 2006 Pacific University CS310 Finite Automata Sections:1.1 page 44 September 8, 2006.
Introduction to the Theory of Computation John Paxton Montana State University Summer 2003.
CS 310 – Fall 2006 Pacific University CS310 Finite Automata Sections: September 1, 2006.
Automata & Formal Languages, Feodor F. Dragan, Kent State University 1 CHAPTER 1 Regular Languages Contents Finite Automata (FA or DFA) definitions, examples,
CS5371 Theory of Computation Lecture 6: Automata Theory IV (Regular Expression = NFA = DFA)
Lecture 3 Goals: Formal definition of NFA, acceptance of a string by an NFA, computation tree associated with a string. Algorithm to convert an NFA to.
CS5371 Theory of Computation Lecture 4: Automata Theory II (DFA = NFA, Regular Language)
FSA Lecture 1 Finite State Machines. Creating a Automaton  Given a language L over an alphabet , design a deterministic finite automaton (DFA) M such.
1 Regular Languages Finite Automata eg. Supermarket automatic door: exit or entrance.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
Great Theoretical Ideas in Computer Science.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
REGULAR LANGUAGES.
Theory of Languages and Automata
Theory of Computation, Feodor F. Dragan, Kent State University 1 Regular expressions: definition An algebraic equivalent to finite automata. We can build.
CHAPTER 1 Regular Languages
Formal Definition of Computation Let M = (Q, ∑, δ, q 0, F) be a finite automaton and let w = w 1 w w n be a string where each wi is a member of the.
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
CSCI 2670 Introduction to Theory of Computing September 13.
CS 203: Introduction to Formal Languages and Automata
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 1 Regular Languages Some slides are in courtesy.
Finite Automata & Regular Languages Sipser, Chapter 1.
Finite Automata Great Theoretical Ideas In Computer Science Victor Adamchik Danny Sleator CS Spring 2010 Lecture 20Mar 30, 2010Carnegie Mellon.
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen.
 2004 SDU Lecture4 Regular Expressions.  2004 SDU 2 Regular expressions A third way to view regular languages. Say that R is a regular expression if.
1 Introduction to the Theory of Computation Regular Expressions.
CSCI 2670 Introduction to Theory of Computing September 11, 2007.
Regular Languages Chapter 1 Giorgi Japaridze Theory of Computability.
Complexity and Computability Theory I Lecture #5 Rina Zviel-Girshin Leah Epstein Winter
Finite Automata A simple model of computation. 2 Finite Automata2 Outline Deterministic finite automata (DFA) –How a DFA works.
Theory of Languages and Automata By: Mojtaba Khezrian.
1/29/02CSE460 - MSU1 Nondeterminism-NFA Section 4.1 of Martin Textbook CSE460 – Computability & Formal Language Theory Comp. Science & Engineering Michigan.
Theory of Computation Automata Theory Dr. Ayman Srour.
Dr. Gary Weiss, January Computer Language Theory Chapter 1: Regular Languages.
Theory of Languages and Automata By: Mojtaba Khezrian.
Foundations of Computing Science
Non Deterministic Automata
CSE 105 theory of computation
Regular Expressions Sections:1.3 page 63 September 17, 2008
Nondeterministic Finite Automata
Finite Automata & Regular Languages
Jaya Krishna, M.Tech, Assistant Professor
CSC 4170 Theory of Computation Nondeterminism Section 1.2.
Hierarchy of languages
Lecture3 DFA vs. NFA, properties of RL
CS 154, Lecture 3: DFANFA, Regular Expressions.
فصل دوم Context-Free Languages
CSE 2001: Introduction to Theory of Computation Fall 2009
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
Chapter 4 Properties of Regular Languages
CS21 Decidability and Tractability
Computer Language Theory
Chapter 1 Regular Language - 02
CS21 Decidability and Tractability
CSC 4170 Theory of Computation Nondeterminism Section 1.2.
Chapter 1 Regular Language
CSCI 2670 Introduction to Theory of Computing
CHAPTER 1 Regular Languages
Intro to Theory of Computation
Presentation transcript:

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

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

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

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) = ?

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) =?

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

Starts and ends with the same symbol

={<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 if A = {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 : 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 }

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, ………}

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

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

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

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)

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)|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  F1 or 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. Deterministic Computation Non Deterministic Computation

Input : 0 1 0 1 1 0

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

0 1 1 0 0 1

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  : QxP (Q) transition function q0Q : 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 ) ~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

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}

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

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

Construct a DFA D equivalent to N4

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

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

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), 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

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), 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

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

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

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 expression R = R R。 = 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 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 = R1R2 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

(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

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

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

Convert(G) Convert(G) 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) = (R1)(R2)*(R3)(R4) Compute CONVERT(G’) and return this value

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

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

example eg:

example

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

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

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

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

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 →←

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

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)

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 = 2 +1

→← 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 →←