Great Theoretical Ideas In Computer Science John Lafferty Lecture 22 CS 15 251 November 9 2006 Carnegie Mellon University One Minute To Learn Programming Finite Automata a b a a b a b b Fall 2006 b a Today we ll talk about a programmin g language so simple that you can learn it in less than a minute Steven Rudich www cs cmu edu Meet ABA The Automaton a a b b Input String aba aabb aabba b a b a b a Result Accept Reject Accept Accept Steven Rudich www cs cmu edu The Simplest Interesting Machine Finite State Machine OR Finite Automaton Steven Rudich www cs cmu edu Finite Automaton Finite set of states Q qo q1 q2 K qk A start state qo A set of accepting states A finite alphabet State transition instructions F qi1 qi2 K qir a b x 1 a qi qj Q Q qi a q j Steven Rudich www cs cmu edu How Machine M operates M reads one letter at a time from the input string going from left to right M starts in state q0 If M is in state qi reads the letter a then If qi a is undefined then CRASH Otherwise M moves to state qi a Steven Rudich www cs cmu edu Let M Q F be a finite automaton M accepts the string x if when M reads x it ends in an accepting state M rejects the string x if when M reads x it ends in a nonaccepting state M crashes on x if M crashes while reading x Steven Rudich www cs cmu edu The set or language accepted by M is LM x M accepts x k All length k strings over the alphabet 0 1 2 3 Notice that this is Steven Rudich www cs cmu edu Back to ABA The Automaton a a b b Input String aba aabb aabba b a b a b a Result Accept Reject Accept Accept Steven Rudich www cs cmu edu What is the language accepted by this machine a b L a b all finite strings of a s and b s Steven Rudich www cs cmu edu What is the language accepted by this machine a b a b Steven Rudich www cs cmu edu What is the language accepted by this machine a b a b L all even length strings of a s and b s Steven Rudich www cs cmu edu What machine accepts this language L all strings in a b that contain at least one a b a Steven Rudich www cs cmu edu a b What machine accepts this language L strings with an odd number of b s and any number of a s a b b Steven Rudich www cs cmu edu a What is the language accepted by this machine a b b a L any string ending with a b Steven Rudich www cs cmu edu What is the language accepted by this machine b a b a Steven Rudich www cs cmu edu a b What is the language accepted by this machine b a b a a b L any string with at least two a s Steven Rudich www cs cmu edu What machine accepts this language L any string with an a and a b Steven Rudich www cs cmu edu What machine accepts this language L any string with an a and a b a a b a b b Steven Rudich www cs cmu edu a b What machine accepts this language L strings with an even number of ab pairs a b a b a b a Steven Rudich www cs cmu edu b L all strings containing ababb as a consecutive substring Steven Rudich www cs cmu edu L all strings containing ababb as a consecutive substring b a a b a a b b a a a ab b b aba abab Invariant I am state s exactly when s is the longest suffix of the input so far that forms a prefix of ababb Steven Rudich www cs cmu edu The grep Problem Problem Input Does the string text T of length t S appear string S of length n inside the text n symbols T 6 4 44 7 4 4 48 Na ve method a1 a2 a3 K K K K K K at Cost O nt comparisons Steven Rudich www cs cmu edu Automata Solution Build a machine M that accepts any string with S as a consecutive substring Feed the text to M Cost t comparisons time to build M As luck would have it the Knuth Morris Pratt algorithm builds M quickly Steven Rudich www cs cmu edu Real life uses of finite state machines grep coke machines thermostats fridge elevators train track switches lexical analyzers for parsers Steven Rudich www cs cmu edu Any L is defined to be a language L is just a set of strings It is called a language for historical reasons Steven Rudich www cs cmu edu Let L be a language L is called a regular language if there is some finite automaton that accepts L In this lecture we have seen many regular languages even length strings strings containing ababb Steven Rudich www cs cmu edu Theorem Any finite langage is regular Proof Make a machine with a path for each string in the language sharing prefixes Example L a bcd ac bb a c b b c d Steven Rudich www cs cmu edu Are all languages regular Steven Rudich www cs cmu edu Consider the language a b ab aabb aaabbb K n n i e a bunch of a s followed by an equal number of b s No finite automaton accepts this language Can you prove this Steven Rudich www cs cmu edu anbn is not regular No machine has enough states to keep track of the number of a s it might encounter Steven Rudich www cs cmu edu That is a fairly weak argument Consider the following example Steven Rudich www cs cmu edu 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 Steven Rudich www cs cmu edu Remember ABA a a b b b a b a b a ABA accepts only the strings with an equal number of ab s and ba s Steven Rudich www cs cmu edu Let me show you a professional strength proof that anbn is not regular Steven Rudich www cs cmu edu Pigeonhole principle Given n boxes and m n objects at least one box must contain more than one object Letterbox principle If the average number of letters per box is a then some box will have at least a letters Similarly some box has at most a Steven Rudich www cs cmu edu Theorem anbn is not regular Steven Rudich www cs cmu edu Theorem anbn is not regular Proof Assume that it is Then M with k …
View Full Document
Unlocking...