Presentation is loading. Please wait.

Presentation is loading. Please wait.

Deterministic Finite Automata (DFA) Borrowed from CMU / COMPSCI 102.

Similar presentations


Presentation on theme: "Deterministic Finite Automata (DFA) Borrowed from CMU / COMPSCI 102."— Presentation transcript:

1 Deterministic Finite Automata (DFA) Borrowed from CMU / COMPSCI 102

2 0 0, The machine accepts a string if the process ends in a double circle

3 0 0, The machine accepts a string if the process ends in a double circle Anatomy of a Deterministic Finite Automaton states q0q0 q1q1 q2q2 q3q3 start state (q 0 ) accept states (F)

4 Anatomy of a Deterministic Finite Automaton 0 0, q0q0 q1q1 q2q2 q3q3 The alphabet of a finite automaton is the set where the symbols come from: The language of a finite automaton is the set of strings that it accepts {0,1}

5 0,1 q0q0 L(M) = All strings of 0s and 1s  The Language of Machine M

6 q0q0 q1q L(M) = { w | w has an even number of 1s}

7 An alphabet Σ is a finite set (e.g., Σ = {0,1}) A string over Σ is a finite-length sequence of elements of Σ For x a string, |x| isthe length of x The unique string of length 0 will be denoted by ε and will be called the empty or null string Notation A language over Σ is a set of strings over Σ

8 Q is the set of states Σ is the alphabet  : Q  Σ → Q is the transition function q 0  Q is the start state F  Q is the set of accept states A finite automaton is a 5-tuple M = (Q, Σ, , q 0, F) L(M) = the language of machine M = set of all strings machine M accepts

9 Q = {q 0, q 1, q 2, q 3 } Σ = {0,1}  : Q  Σ → Q transition function * q 0  Q is start state F = {q 1, q 2 }  Q accept states M = (Q, Σ, , q 0, F) where  01 q0q0 q0q0 q1q1 q1q1 q2q2 q2q2 q2q2 q3q3 q2q2 q3q3 q0q0 q2q2 * q2q2 0 0, q0q0 q1q1 q3q3 M

10 qq q0q0 q ,1 Build an automaton that accepts all and only those strings that contain 001

11 A language is regular if it is recognized by a deterministic finite automaton L = { w | w contains 001} is regular L = { w | w has an even number of 1s} is regular

12 Union Theorem Given two languages, L 1 and L 2, define the union of L 1 and L 2 as L 1  L 2 = { w | w  L 1 or w  L 2 } Theorem: The union of two regular languages is also a regular language

13 Proof Sketch: Let M 1 = (Q 1, Σ,  1, q 0, F 1 ) be finite automaton for L 1 and M 2 = (Q 2, Σ,  2, q 0, F 2 ) be finite automaton for L 2 We want to construct a finite automaton M = (Q, Σ, , q 0, F) that recognizes L = L 1  L 2 1 2

14 Idea: Run both M 1 and M 2 at the same time! Q = pairs of states, one from M 1 and one from M 2 = { (q 1, q 2 ) | q 1  Q 1 and q 2  Q 2 } = Q 1  Q 2

15 Theorem: The union of two regular languages is also a regular language q0q0 q1q p0p0 p1p

16 q 0,p 0 q 1,p q 0,p 1 q 1,p Automaton for Union

17 q 0,p 0 q 1,p q 0,p 1 q 1,p Automaton for Intersection

18 Theorem: The union of two regular languages is also a regular language Corollary: Any finite language is regular

19 The Regular Operations Union: A  B = { w | w  A or w  B } Intersection: A  B = { w | w  A and w  B } Negation:  A = { w | w  A } Reverse: A R = { w 1 …w k | w k …w 1  A } Concatenation: A  B = { vw | v  A and w  B } Star: A* = { w 1 …w k | k ≥ 0 and each w i  A } note: can be different w i -s

20 Regular Languages Are Closed Under The Regular Operations We have seen part of the proof for Union. The proof for intersection is very similar. The proof for negation is easy.

21 Input: Text T of length t, string S of length n The “Grep” Problem Problem: Does string S appear inside text T? a 1, a 2, a 3, a 4, a 5, …, a t Naïve method: Cost:Roughly nt comparisons

22 Automata Solution Build a machine M that accepts any string with S as a consecutive substring Feed the text to M Cost: As luck would have it, the Knuth-Morris- Pratt (KMP) algorithm builds M quickly t comparisons + time to build M

23 Grep Coke Machines Train Track Switches Lexical Analyzers for Parsers... Real-life Uses of DFAs

24 Are all languages regular?

25 i.e., a bunch of a’s followed by an equal number of b’s Consider the language L = { a n b n | n > 0 } No finite automaton accepts this language Can you prove this?

26 a n b n is not regular. No machine has enough states to keep track of the number of a’s it might encounter

27 That is a fairly weak argument Consider the following example…

28 L = strings where the # of occurrences of the pattern ab is equal to the number of occurrences of the pattern ba Can’t be regular. No machine has enough states to keep track of the number of occurrences of ab

29 M accepts only the strings with an equal number of ab’s and ba’s! b b a b a a a b a b

30 Let me show you a professional strength proof that a n b n is not regular…

31 Pigeonhole principle: Given n boxes and m > n objects, at least one box must contain more than one object

32 Theorem: L= {a n b n | n > 0 } is not regular Proof (by contradiction): Assume that L is regular Then there exists a machine M with k states that accepts L For each 0  i, let S i be the state M is in after reading a i  i,j such that S i = S j, but i  j Why? after reading a n for some n > k this must hold because there are not enough states

33 L= {a n b n | n > 0 } is not regular (proof continued...) M will do the same thing on a i b i and a j b i But a valid M must reject a j b i and accept a i b i  i,j such that S i = S j, but i  j

34 Summary Deterministic Finite Automata Definition Testing if they accept a string Building automata Regular Languages Definition Closed Under Union, Intersection, Negation Using Pigeonhole Principle to show language ain’t regular


Download ppt "Deterministic Finite Automata (DFA) Borrowed from CMU / COMPSCI 102."

Similar presentations


Ads by Google