1Finite AutomataGreat Theoretical Ideas In Computer ScienceVictor AdamchikDanny SleatorCS 15-251 Spring 2010Lecture 20 Mar 30, 2010 Carnegie Mellon UniversityA machine so simple that you can understand it in less than one minuteWishful thinking…Deterministic Finite Automata00,1001110111 111111The machine accepts a string if the process ends in a double circle00,1001110111 111111The machine accepts a string if the process ends in a double circlestatesstart state (q0)accept states (F)transitionsAnatomy of a Deterministic Finite AutomatonThe alphabet of a finite automaton is the set where the symbols come from, for example {0,1}The language of a finite automaton is the set of strings that it acceptsThe singular of automata is automaton.L(M) = All strings of 0s and 1sThe Language L(M) of Machine M0,1q0The language of a finite automaton is the set of strings that it accepts2001L(M) ={ w | w has an even number of 1s}q00q111The Language L(M) of Machine MAn 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| is the length of xThe unique string of length 0 will be denoted by ε and will be called the empty or null stringNotationA language over Σ is a set of strings over ΣQ is the finite set of statesΣ is the alphabet : Q Σ → Q is the transition functionq0 Q is the start stateF Q is the set of accept statesA finite automaton is M = (Q, Σ, , q0, F) L(M) = the language of machine M= set of all strings machine M acceptsQ = {q0, q1, q2, q3}Σ = {0,1} : Q Σ → Q transition functionq0 Q is start stateF = {q1, q2} Q accept statesM = (Q, Σ, , q0, F) where 0 1q0q0q1q1q2q2q2q3q2q3q0q2q200,100111q0q1q3MThe finite-state automata are deterministic, if for each pair Q Σ of state and input value there is a unique next state given by the transition function.There is another type machine in which there may be several possible next states. Such machines called nondeterministic.Build an automaton that accepts all and only those strings that contain 001{0}01{00}01{001}100,1EXAMPLE3Build an automaton that accepts all binary numbers that are divisible by 3,i.e, L = 0, 11, 110, 1001, 1100, 1111, 10010, 10101…100110A language is regular if it is recognized by a deterministic finite automatonL = { w | w contains 001} is regularL = { w | w has an even number of 1s} is regularA language over Σ is a set of strings over ΣDetermine the language recognized by010,1L(M)={1n| n = 0, 1, 2, …}Determine the language recognized byL(M)={1, 01}00,10,1110Determine the language recognized byL(M)={0n, 0n10x | n=0,1,2…, and x is any string}10,10,1100DFA Membership problemDetermine whether someword belongs to the language.Theorem: The DFA Membership Problem is solvable in linear time.Let M = (Q, Σ, , q0, F) and w = w1...wm. Algorithm for DFA M:p := q0;for i := 1 to m do p := (p,wi);if pF then return Yes else return No.4Equivalence of two DFAsGiven a few equivalent machines, we are naturally interested in the smallest one with the least number of states.Definition: Two DFAs M1and M2over the same alphabet are equivalent if theyaccept the same language: L(M1) = L(M2).Union TheoremGiven two languages, L1and L2, define the union of L1and L2as L1 L2= { w | w L1or w L2} Theorem: The union of two regular languages is also a regular language.Theorem: The union of two regular languages is also a regular languageProof (Sketch): Let M1 = (Q1, Σ, 1, q0, F1) be finite automaton for L1and M2= (Q2, Σ, 2, q0, F2) be finite automaton for L2We want to construct a finite automaton M = (Q, Σ, , q0, F) that recognizes L = L1 L212Idea: Run both M1and M2at the same time!Q= pairs of states, one from M1and one from M2= { (q1, q2) | q1 Q1and q2 Q2}= Q1 Q2Theorem: The union of two regular languages is also a regular language00q00q11101p01p100Automaton for Union0p0 q01100011p0 q1p1 q0p1 q1005The Regular OperationsUnion: A B = { w | w A or w B } Intersection: A B = { w | w A and w B } Negation: A = { w | w A } Reverse: AR= { w1 …wk| wk …w1 A }Concatenation: A B = { vw | v A and w B }Star: A* = { w1 …wk| k ≥ 0 and each wi A }ReverseReverse: AR= { w1 …wk| wk …w1 A }How to construct a DFA for the reversal of a language?The direction in which we read a string should be irrelevant. If we flip transitions around we might not get a DFA.The Kleene closure: A*From the definition of the concatenation, we definite An, n =0, 1, 2, … recursivelyA0= {ε}An+1= AnAStar: A* = { w1 …wk| k ≥ 0 and each wi A }A* is a set consisting of concatenations of arbitrary many strings from A.U0kkAA*The Kleene closure: A*What is A* of A={0,1}?All binary stringsWhat is A* of A={11}?All binary strings of an even number of 1sRegular Languages Are Closed Under The Regular OperationsWe have seen part of the proof for Union. The proof for intersection is very similar. The proof for negation is easy.Theorem: Any finite language is regularClaim 1: Let w be a string over an alphabet. Then {w} is a regular language. Proof: By induction on the number of characters. If {a} and {b} are regular then {ab} is regularClaim 2: A language consisting of n strings is regular Proof: By induction on the number of strings. If {a} then L{a} is regular6Input: Text T of length t, string S of length nPattern MatchingProblem: Does string S appear inside text T?a1, a2, a3, a4, a5, …, atNaïve method: Cost: Roughly nt comparisonsAutomata SolutionBuild a machine M that accepts any string with S as a consecutive substringFeed the text to MCost:As luck would have it, the Knuth, Morris, Pratt algorithm builds M quicklyt comparisons + time to build MGrepCoke MachinesThermostats (fridge)ElevatorsTrain Track SwitchesLexical Analyzers for ParsersReal-life Uses of DFAsAre all languages regular?i.e., a bunch of a’s followed by an equal number of b’sConsider the language L = { anbn| n > 0 }No finite automaton accepts this languageCan you prove this?anbnis not regular. No machine has enough states to keep track of the number of a’s it might encounter7That is a fairly weak argument Consider the following example…L = strings where the # of occurrences of the pattern ab is equal to the number of occurrences of the pattern baCan’t be regular. No machine has enough states to keep track of the number of occurrences of abM accepts only the strings with
View Full Document