9/2/28 Stereotypical computer CISC 49 Theory of Computation Finite state machines & Regular languages Professor Daniel Leeds dleeds@fordham.edu JMH 332 Central processing unit (CPU) performs all the instructions Memory stores data and instructions for CPU Input collects information from the world Output provides information to the world Input Output CPU 2 Super-simple computers Small number of potential inputs Small number of potential outputs/actions Thermostat Elevator Vending machine Automatic door Automatic door Desired behavior Person approaches entryway, door opens Person goes through entryway, door stays open Person is no longer near entryway, door closes Nobody near entryway, door stays closed Two states: Open, Closed Two inputs: Front-sensor, Back-sensor 3 Finite state machine 4
9/2/28 Graph and table representations Neither Front, Back, Both Front, Back, Both More finite state machine applications Text parsing Closed Neither Open Front Back Neither Both Closed Open Open Closed Open Open Open Open Closed Open 5 Traffic light Pac-Man Electronic locks 6 Coding a combination lock q q 3, A finite automaton M with 3 states Start state q; accept state q2 (double circle) Example accepted string: What are all strings that this model will accept? String ending with or string end with followed by even number of s 8 Formal definition of Finite State Automaton Finite state automaton is a 5-tuple Q, Σ, δ, q, F Q is a finite set called states Σ is a finite set called the alphabet δ: Q Σ Q is the transition function q Q is the start state F Q is the set of accept states 9 2
9/2/28 Describe M using formal definition q q 3 M = Q, Σ, δ, q, F, Q = q,, q 3 δ = Σ =, Start state: q F = q q 3 q q 3 Language of M If A is set of all strings accepted by M, A is language of M L(M)=A A machine may accept many strings, but only one language M accepts a string M recognizes a language Describe L(M)=A A={w w ends with or w end with one followed by even number of s} 3 Describe M2 using formal definition Describe M2 using formal definition q q M2 = Q,,, δ, q, M2 = Q,,, δ, q, Q = Start state: δ = Q = {q, } Start state: q δ = q q q2 q2 q q2 4 5 3
9/2/28 What is the language of M2? L(M2)={w } What is the language of M2? L(M2)={w w ends with at least one } 6 7 CFG practice G -> AB A -> xxa y B -> wbw z What is the language of M4? (page 38, Ex..) a q a s b r b L(M4)={w } a b b a G 2 -> D D -> nd o b r 2 a 8 9 4
9/2/28 Perform modulo arithmetic Let Σ={RESET,,, 2} Construct M5 to accept a string only if the sum of each input symbol is multiple of 3, and RESET sets the sum back to (.3, page 39) More modulo arithmetic Generalize M5 to accept if sum of symbols is a multiple of i instead of 3 q, q, q2, q3,, q i,,,2, RESET, δ, q, F 2 23 Definition of M accepting a string Let M = Q, Σ, δ, q, F be a finite automaton and let w = w w 2 w n Then M accepts w if a sequence of states r, r,, r n in Q exists with 3 conditions r =q δ r i, w i+ = r i+ for i =,,, n r n F Regular languages Definition: a language is called a regular language if some finite automaton recognizes it equivalently All of the strings in a regular language are accepted by some finite automaton 25 26 5
9/2/28 Designing finite automata (FAs) Determine what you need to remember How many states needed for your task? Set start and finish states Assign transitions Check your solution Should accept w L Should reject w L Be careful about ε! 27 FA design practice! FA to accept language where number of s is odd (page 43) FA to accept string with as substring (page 44) FA to accept string with substring abab 28 Regular operations Let A and B be languages. We define 3 regular operations: Union: A B = x x A or x B Concatenation: A B = xy x A and y B Star: A = x x 2 x k k and each x i A Repeat a string or more times Examples of regular operations Let A = good, bad and B = boy, girl What is: A B = A B = A = 32 33 6
9/2/28 Express CFG as RegEx? G 2 -> D D -> nd o Closure A collection of objects is closed under an operation if applying that operation to members of the collection returns an object in the collection G -> AB A -> xxa y B -> wbw z Regular languages are closed under,, 35 36 Closure of Concatenation Theorem.26: The class of regular languages is closed under the concatenation operation If A and A2 are regular languages, then so is A A2 Challenge: How do we know when M ends and M2 begins? Determinism vs. non-determinism Determinism: Single transition allowed given current state and given input Non-determinism: multiple transitions allowed for current state and given input transition permitted for null input ε,,,ε q q 3 q 4 37 38 7
9/2/28 NFA in action When there is a choice, follow all paths like cloning If there is no forward arrow, path terminates and clone dies (no accept) NFA will accept if at least one path terminates at accept Alternative thought: Magically pick best path from the set of options The language of M,,,ε q q 3 q 4 List some accepted strings What is L(M)? 39 4 NFA construction practice Build an NFA that accepts all strings over {,} with in the third position from the end NFA -> DFA Build an NFA that accepts all strings over {,} with in the third position from the end Can we construct a DFA for this? 42 44 8
9/2/28 Formal definition of Nondeterministic Finite Automaton Similar to DFA: a 5-tuple Q, Σ, δ, q, F Q is a finite set called states Σ is a finite set called the alphabet δ: Q Σε P(Q) is the transition function q Q is the start state F Q is the set of accept states 45 Describe M using formal definition, M = Q, Σ, δ, q, F Q = Σ = Start state: F = q q δ =,, q 3 q q q 3 ε 46 Consider NFA N q q Convert NFA N to DFA M q q N Language: L(N)={w w begins with, ends with, every in w is preceded by a } 47 9
9/2/28 Equivalence of NFAs and DFAs NFAs and DFAs recognize the same class of languages Two machines are equivalent if they recognize the same language Every NFA has an equivalent DFA Union Closure with NFAs Proofs by construction fewer states! Any NFA proof applies to DFA Given two regular languages A and A 2 recognized by N and N2 respectively, construct N to recognize A A 2 52 54 Let s consider two languages L: start with, end with L2: start with, end with Let s consider two languages L: start with, end with N, q q Construct machines for each languages Construct machines N3 to recognize L U L2 L2: start with, end with N2, r r r 2 55 56
9/2/28 N3 recognizes L U L2 ε N, q q Closure under concatenation Given two regular languages A and A 2 recognized by N and N2 respectively, construct N to recognize A A 2 s ε N2, r r r 2 57 59 Closure of Union Theorem.25: The class of regular languages is closed under the union operation Proof by construction 62