Regular Languages and Finite State Machines Plan for the Day: Mathematical preliminaries - some review One application formal definition of finite automata Examples 1
Sets A set is an unordered collection of objects without duplicates. Objects in the set are called elements or members. denotes the empty set, the set with no elements. Note: { }. Definition: For any sets S and T, S is a subset of T, denoted S T, if x[x S x T]. Example: N Z. Note: One way of proving S = T: show S T and T S. Definition: For any sets S, T: S T = {w w S or w T } S T = {w w S and w T } S T = {w w S and w T } 2
Review of Sets Recall: To prove a universally quantified implication with a direct proof, e.g.: x y[p(x, y) Q(x, y)] First: Let x and y be arbitrary elements of the domain. Next: Assume P(x, y) is true. Show: Q(x, y) is true. Prove: For any sets A and B, A (B C) A B c. Proof: in class Exercise: Prove: For all sets A, B and C, A (B C) = (A B) (A C). 3
Strings and Languages Definition: An alphabet is a finite set of symbols. Example: Σ = {0, 1}, Σ = {a, b, c,..., z}. Definition: A string over alphabet Σ is a finite sequence of symbols from Σ. Note: The string of zero symbols is ǫ. Definition: Σ is the set of all strings over Σ. Definition: A language is a set of strings over some alphabet Σ, i.e., a subset of Σ. Example: Let alphabet A = {a, b, c}. Here are some languages over A: L 1 = {a, aab} L 2 = L 3 = {xy x, y {a, b, c}, x = y and the first character in x is a} L 4 = {ǫ, aa, aaaa, aaaaaa,...} 4
L 5 = {w {a, b, c} # a (w) = [# b (w)] 2 } Some strings in L 5 : ǫ, c, cc, cccc, ab, aaaabb, baabaa, abc,... Describe this language in English: L = {0 i 1 j 0 i j} 5
Automatic door controller Consider an automatic door placed at the entrance or exit of a grocery store. The door swings open when its sensors detect an approaching person, and then stays open until the person has passed through and beyond the door. It has two sensor pads - one in front of the door and one to the rear. The controller is always in one of two states: open or closed. The controller moves from state to state, depending on the input it receives. Four possible inputs to the door: front - a person is on the front pad (in front of the door) rear - a person is on the rear pad both - people are standing on both pads neither - no one is standing on either pad 6
Finite Automaton - Automatic Door Controller Transition table for controller: neither front rear both closed closed open closed closed open closed open open open Notes: The door will not open if people are standing on both pads - it might knock over someone standing behind the door. If the door is open, the only input that closes it is neither - that is, if no one is on either pad. 7
Finite Automaton - Door Controller neither both rear front front rear both closed open neither Figure 1: state diagram for door controller 8
One Model of Computation: finite automata Finite automata (or finite state machines) are simple computing machines with severely restricted memories. But a computer with just a little memory can do a lot. Example: A state diagram for finite automaton M. 1 0 0 q 0 q 1 1 M has two states, q 0 and q 1. q 0 is the start state. Arrows represent transitions from one state to another on the specified input symbol. Final (or accepting) states are indicated with a double circle. 9
Processing Strings with a FA Question: Does M accept input string 110? 1. Processing begins in start state q 0. 2. M reads symbols in 110 from left to right - after reading each symbol, M moves to another state. 3. If M is in a final state after processing the last symbol, then M accepts the string. Answer: Yes, 110 is accepted by M. 10
Finite Automaton/Finite State Machine A finite automaton (FA) consists of: set of states start state final states input alphabet (the set of symbols on the transition arrows) rules for going from one state to another, depending on input symbol - defined by a transition function Transition function example: in-class 11
Formal Definition of a Deterministic Finite Automaton (DFA) Definition: A deterministic finite automaton (DFA) is a 5-tuple (Q, Σ, δ, q 0, F) where 1. Q is a finite set of states 2. Σ is a finite input alphabet 3. δ : Q Σ Q is the transition function 4. q 0 Q is the start state 5. F Q is the set of final states Formal definition of M: M = ({q 0, q 1 }, {0, 1}, δ, q 0, {q 1 }), where δ is given by: 0 1 q 0 q 1 q 0 q 1 q 1 q 0 12
The language of a FA Definition: If S is the set of all strings the FA M accepts, then S is the language of M, denoted S = L(M). Terminology: We say that M recognizes S. Question: What is L(M)? Example: FA M 2 a b a b b a s q r Exercise: Write down the formal definition of M 2. Does M 2 accept the following strings: aaba? aaab? Question: What is L(M 2 )? 13
Finite Automata Example: DFA M 3 : b a a s q r b a b Exercise: Write down the formal definition of M 3. What is L(M 3 )? 14
Another FA Example: M 4 1 q 1 r 0 0 0 0 1 t 1 s Question: Does M 4 accept 011? 1011? 001001? Question: What is L(M 4 )? 15
The Language of a DFA Definition: Let M = (Q, Σ, δ, q 0, F) be a DFA, and let w = w 1 w 2...w n be a string over Σ. Then M accepts w if there exists a sequence of states r 0, r 1,..., r n Q such that 1. r 0 = q 0 2. δ(r i, w i+1 ) = r i+1 for i = 0, 1,..., n 1 3. r n F We say M recognizes language L = {w M accepts w}. Definition: A language is regular if some DFA recognizes it. 16
Designing DFAs Problem: Given a language L, define a DFA M that recognizes L. Note: What do you need to remember about a string w as you read it to determine if it s in L? This gives you the states. For a regular language, there will only be a finite amount of information you need to remember. Example: L = {w {0, 1} w ends in 1} As you read a string, one symbol at a time, left to right, what do you need to remember in order to determine if the string is in L? Remember: Last symbol seen was a 1 Last symbol seen not 1 State diagram: (in class) 17