Deterministic Finite Automata COMP2600 Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2, 2016 Slides by Ranald Clouston and Katya Lebedeva. COMP 2600 Deterministic Finite Automata 1
A Deterministic Finite State Automaton (DFA) is a quintuple (Σ,S,s 0,F,δ) where 1. Σ is a finite set of input symbols (the alphabet) 2. S is a finite state of states 3. s 0 is the start (or initial) state: s 0 S 4. F is a set of final (or accepting) states: F S 5. δ : S Σ S is a transition function COMP 2600 Deterministic Finite Automata 2
Example alphabet - {0,1} S0 1 S 0 1 1 2 0 S 1 0 states - {S 0,S 1,S 2 } initial state - S 0 final states - {S 2 } transition function 0 1 S 0 S 1 S 0 S 1 S 1 S 2 S 2 S 1 S 0 Note that the actual names of states are irrelevant. COMP 2600 Deterministic Finite Automata 3
Transition Function as a diagram S0 1 S 0 1 1 2 0 S 1 0 as a transition table 0 1 S 0 S 1 S 0 S 1 S 1 S 2 S 2 S 1 S 0 δ(s 0,0) = S 1 δ(s 0,1) = S 0 δ(s 1,0) = S 1 δ(s 1,1) = S 2 δ(s 2,0) = S 1 δ(s 2,1) = S 0 COMP 2600 Deterministic Finite Automata 4
Extended Transition Function We want to extend δ to multiple transitions: δ : S Σ S δ : S Σ S S0 1 S 0 1 1 2 0 S 1 0 Input 0101 takes the DFA from S 0 to S 2. Hence δ (S 0,0101) = S 2. Input 1011 takes the DFA from S 1 to S 0. Hence δ (S 1,1011) = S 0. COMP 2600 Deterministic Finite Automata 5
δ (s,w) = q automaton to state q. In other words: If and denotes that starting at state s, the input string w will take the w = a 1 a 2...a n δ(s,a 1 ) = p 1 δ(p 1,a 2 ) = p 2... δ(p n 1,a n ) = q then δ (s,a 1 a 2...a n ) = q COMP 2600 Deterministic Finite Automata 6
Definition by induction Basis: δ (s,ε) = s (1) Induction: δ (s,aα) = δ (δ(s,a),α) (2) Note that a Σ (i.e. a is a symbol) and α is a string. Thus δ is defined by breaking the input string into a single symbol followed by a string. We exploit the fact that strings are defined inductively! Since we informally thinking about δ as operating on a string, it should not matter how we break the input string for δ. Indeed, we can show that for any state s and strings α and β the following holds: δ (s,αβ) = δ (δ (s,α),β) COMP 2600 Deterministic Finite Automata 7
Append Theorem δ (s,αβ) = δ (δ (s,α),β) Proof: By induction on the length of α. Base case: α = ε LHS = δ (s,εβ) = δ (s,β) RHS = δ (δ (s,ε),β) = δ (s,β) = LHS (by (1)) COMP 2600 Deterministic Finite Automata 8
Inductive case: Suppose δ (s,αβ) = δ (δ (s,α),β) (IH) LHS = δ (s,(aα)β) = δ (s,a(αβ)) = δ (δ(s,a),αβ) (by (2)) = δ (δ (δ(s,a),α),β) (by IH) RHS = δ (δ (s,aα),β) = δ (δ (δ(s,a),α),β) (by (2)) Corollary: when β is a single symbol b δ (s,αb) = δ(δ (s,α),b) COMP 2600 Deterministic Finite Automata 9
Example: S0 1 S 0 1 1 2 0 S 1 0 δ (S 1,1011) = δ (δ(s 1,1),011) = δ (S 2,011) = δ (S 1,11) = δ (S 2,1) = δ (S 0,ε) = S 0 COMP 2600 Deterministic Finite Automata 10
Language accepted by a DFA We say a DFA accepts a string if, starting from the start state, it terminates in one of the final states. More precisely, let A = (S,Σ,δ,s 0,F) be a DFA and w be a string in Σ. We say w is accepted by A if δ (s 0,w) F The language accepted by A is the set of all strings accepted by A: L(A) = {w Σ δ (s 0,w) F} That is, w L(A) iff δ (s 0,w) F. COMP 2600 Deterministic Finite Automata 11
A 1 : S0 1 S 0 1 1 2 0 S 1 0 0011101 takes the machine from state S 0 through the sequence of states S 1, S 1, S 2, S 0, S 0, S 1 to the final state S 2 δ (S 0,0011101) = δ (S 1,011101) = δ (S 1,11101) =... = δ (S 1,1) = S 2 COMP 2600 Deterministic Finite Automata 12
A 1 : S0 1 S 0 1 1 2 0 S 1 0 Strings accepted by A 1 : 01, 001, 101, 0001, 0101, 00101101... Strings not accepted by A 1 : ε, 0, 1, 00, 10, 11, 100... What is the difference between strings that are accepted by A 1 and that are not? How do we justify our guess at this answer? COMP 2600 Deterministic Finite Automata 13
To prove that a machine M accepts the language that is characterised by a predicate P: 1. prove that any string satisfying P is accepted by M 2. prove that any string accepted by M satisfies P Our claim: A 1 accepts the language with only words finishing with 01. We need to prove the following: 1. If a string ends in 01, then it is accepted by A 1. I.e.: for all α Σ, δ (S 0,α01) F 2. If a string is accepted by A 1, then it ends in 01. I.e.: for all w Σ, if δ (S 0,w) F then exists α Σ. w = α01 COMP 2600 Deterministic Finite Automata 14
Part 1: for all α Σ, δ (S 0,α01) = S 2 By the append theorem, δ (S 0,α01) = δ (δ (S 0,α),01) A 1 : Observe that δ (s,01) = S 2 holds for all s S. S0 1 S 0 1 1 2 0 S 1 0 Indeed, there exist only three possible cases: δ (S 0,01) = δ (S 1,1) = S 2 δ (S 1,01) = δ (S 1,1) = S 2 δ (S 2,01) = δ (S 1,1) = S 2 Hence, δ (S 0,α01) = δ (δ (S 0,α),01) = S 2 COMP 2600 Deterministic Finite Automata 15
Part 2: for all w if δ (S 0,w) = S 2 then exists α Σ. w = α01 w = ε δ (S 0,ε) = S 0 and S 0 is not equal to S 2. Hence, the antecedent is false and the claim is true by the rules of propositional logic. w = 1 (w is a single symbol) The antecedent is false because there is no single step path from S 0 to S 2. Hence, the claim is true. w > 1. There are paths from S 0 to S 2 of two or more steps. Suppose δ (S 0,αxy) = S 2. S0 1 S By the corollary to the append theorem, 0 1 δ(δ 2 1 (S 0,αx),y) = S 2. 0 S 1 0 By the definition of δ, y must be 1 and δ (S 0,αx) must be S 1. Similarly, since δ(δ (S 0,α),x) = S 1, x is 0 by the definition of δ. COMP 2600 Deterministic Finite Automata 16
Limitations of DFAs What class of languages can be recognised by DFAs? A very important example: Consider this language: L = { a n b n n N} That is, L = {ε,ab,aabb,aaabbb,a 4 b 4,a 5 b 5,...} This language cannot be recognised by any finite state automaton! This is because DFA cannot remember how many a s it has seen. COMP 2600 Deterministic Finite Automata 17
Proof by contradiction: Suppose A is a DFA that accepts L. That is L = L(A). Each of the following expressions denotes a state of A δ (S 0,a), δ (S 0,aa), δ (S 0,a 3 )... Since this list is infinite and the number of states in A is finite, some of these expressions must denote the same state. Choose distinct i and j such that δ (S 0,a i ) = δ (S 0,a j ). What we have done here is pick two initial string fragments that the automaton will not be able to distinguish in terms of what is allowed for the rest of the string COMP 2600 Deterministic Finite Automata 18
Since a i b i is accepted, we know δ (S 0,a i b i ) F By the append theorem δ (S 0,a i b i ) = δ (δ (S 0,a i ),b i ) F Since δ (S 0,a i ) = δ (S 0,a j ) δ (δ (S 0,a j ),b i ) F δ (δ (S 0,a j ),b i ) = δ (S 0,a j b i ) F Thus a j b i is accepted by A. But a j b i is not in L, contradicting the initial assumption. COMP 2600 Deterministic Finite Automata 19
Pigeon-Hole Principle We used the pigeon-hole principle in the preceding proof: If we have more pigeons than pigeon-holes, then at least two pigeons must be sharing a hole. In our example we have infinitely many pigeons (words of the language) crammed into finitely many holes (states of the automaton). This is a useful technique to prove that a certain infinite language cannot be recognised by a finite state automata. But be careful: there are infinite languages that can be recognised by DFAs, as we have seen. COMP 2600 Deterministic Finite Automata 20
Equivalent Automata Two automata are said to be equivalent if they accept the same language. A 2 : a b b S a S 1 S 2 a b 0 A 3 : b S 3 S a 4 b a a S a S 1 b a,b 0 S 2 b S 3 a b Can we simplify a DFA? Is there an equivalent DFA with fewer states? COMP 2600 Deterministic Finite Automata 21
Equivalent States Two states S j and S k of a DFA are equivalent if, for all input strings w δ (S j,w) F if and only if δ (S k,w) F Note that δ (S j,w) and δ (S k,w) may be different states - we only care that both are in, or not in, F. In the following example, S 2 is equivalent to S 4. a b b A 2 : a S 1 S 2 S a b 0 b S 3 S a 4 b a COMP 2600 Deterministic Finite Automata 22
Algorithm for Finding Equivalent States There is an iterative algorithm to compute a list of equivalence classes of states. The working data structure for the algorithm is a list of groups of states Each group contains states that appear to be equivalent, given the tests we have done so far On each iteration, we test one of the groups with a symbol from the alphabet. If we notice different behaviour, we split the group. COMP 2600 Deterministic Finite Automata 23
Informal Description of the Algorithm Initialise: Divide the states into two groups: a group for non-final states and a group for final states. We assume that the states in the same group are equivalent, and we try to distinguish them. Loop: Pick a group, {s 1,...s j } and a symbol x. If states δ(s i,x) for i = 1, j are all in the same group, then the group {s 1,...s j } is not (yet) split. Otherwise split the group according to the result. Terminate when we cannot any more split any group by any letter of the alphabet. COMP 2600 Deterministic Finite Automata 24
A 2 : a b [[s 0,s 1,s 3 ],[s 2,s 4 ]] b a S 1 S 2 a [[s S a 0,s 1 ],[s 3 ],[s 2,s 4 ]] b 0 b b S 3 S a 4 [[s 0 ],[s 1 ],[s 3 ],[s 2,s 4 ]] a b a [[s 0 ],[s 1 ],[s 3 ],[s 2,s 4 ]] b [[s 0 ],[s 1 ],[s 3 ],[s 2,s 4 ]] COMP 2600 Deterministic Finite Automata 25
Elimination of States Suppose A = (Σ,S,s 0,F,δ) is a DFA with state S k equivalent to state S j. (and S k is not s 0.) We can eliminate S k from this automaton by defining a new automaton A = (Σ,S,s 0,F,δ ) as follows: S is S without S k F is F without S k δ S j (s,w) = δ(s, w) if δ(s,w) = S k otherwise. COMP 2600 Deterministic Finite Automata 26
Example A 2 : a b b a S 1 S 2 S a b 0 b S 3 S a 4 b a Since S 2 S 4 in A 2, we eliminate one of them: S 4. New set of states is {S 0,S 1,S 2,S 3 } New set of final states is {S 2 } New transition function is: a a S 1 b a,b S 0 S 2 b S 3 a b COMP 2600 Deterministic Finite Automata 27
Minimisation of a DFA Consider the DFA below: a a,b b S 1 S 2 a b S 3 None of S 1, S 2, S 3 are equivalent... But S 3 is inaccessible from the start state. That is why it can be deleted (along with the transitions emerging from it). Deleting equivalent and inaccessible states will give a minimal DFA. COMP 2600 Deterministic Finite Automata 28