✬ ✩ The University of Melbourne Dept. of Computer Science and Software Eng. 433–330 Theory of Computation Harald Søndergaard Lecture 2 Finite Automata ✫ ✪ 2-0
� � � � � ✬ ✩ An Example Automaton Imagine a vending machine selling tea or coffee for $2. It accepts 1- and 2-dollar coins. If we let ‘1’ (‘2’) stand for the event that a 1-dollar (2-dollar) coin enters the coin slot, and C ( T ) stand for the push of button ‘C’ (‘T’) and subsequent delivery of a cup of coffee (tea), then the following automaton describes the acceptable event sequences: � ���� ���� ���� ���� ���� ���� 2 1 , 2 � � � � � C,T � � � � ���� ���� � � 1 1 , 2 � � � � � That’s “acceptable” from a greedy owner’s point of view, for example, 2 T 11 C 22 C is accepted, but 111 C 1 T is not. ✫ ✪ 2-1
� � � � � � ✬ ✩ Example 2 Here is an automaton for recognising unsigned number literals in some programming language: d d � ���� ���� ���� � ���� ���� ���� ���� ���� � ���� ���� ���� ���� d . d 1 2 3 4 � �������������� E E ���� ���� ���� ���� ���� � ���� ���� ���� �� �� d 5 6 7 + , − � d d d is an abbreviation for 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 (that is, the digits). ✫ ✪ 2-2
✬ ✩ Formal Definition A finite automaton is a 5-tuple ( Q, Σ , δ, q 0 , F ), where • Q is a finite set of states , • Σ is a finite alphabet , • δ : Q × Σ → Q is the transition function , • q 0 ∈ Q is the start state , and • F ⊆ Q are the accept states . Here δ is a total function. ✫ ✪ 2-3
� � � ✬ ✩ Example 3 The automaton M 1 : 0 1 � ���� � ���� ���� � ���� ���� ���� ���� ���� 0 1 q 1 q 2 q 3 0 , 1 can be described precisely as M 1 = ( { q 1 , q 2 , q 3 } , { 0 , 1 } , δ, q 1 , { q 2 } ) , 0 1 q 1 q 1 q 2 where δ is given by: q 2 q 3 q 2 q 3 q 2 q 2 The language recognised by M 1 , � w contains at least one 1, � � � L ( M 1 ) = w and an even number of 0s � � � follow the last 1 � ✫ ✪ 2-4
� � � � � � � � ✬ ✩ Example 4 a b � ���� ���� ���� ���� ���� ���� b q 1 q 2 � a � a � � ���� � ���� � � � � s � � � � � ���� � ���� ���� ���� ���� ���� � � b � a r 1 r 2 b a b Which language is recognised by this machine? ✫ ✪ 2-5
✬ ✩ Exercise Consider the alphabet Σ = { 0 , 1 } . We can interpret strings in Σ ∗ as numbers in binary representation. Construct an automaton over Σ to recognise exactly those numbers that are multiples of 5. Hint: Consider five states: ���� ���� ���� ���� q 1 q 3 � ���� ���� ���� ���� q 0 ���� ���� ���� ���� q 2 q 4 ✫ ✪ 2-6
✬ ✩ Acceptance, Recognition We can say precisely what it means for an automaton to accept a string. Let M = ( Q, Σ , δ, q 0 , F ) and let w = v 1 v 2 · · · v n be a string from Σ ∗ . M accepts w iff there is a sequence of states r 0 , r 1 , . . . , r n , with each r i ∈ Q , such that 1. r 0 = q 0 2. δ ( r i , v i +1 ) = r i +1 for i = 0 , . . . , n − 1 3. r n ∈ F M recognises language A iff A = { w | M accepts w } ✫ ✪ 2-7
✬ ✩ Regular Languages A language is regular iff there is a finite automaton that recognises it. We shall soon see that there are languages which are not regular. ✫ ✪ 2-8
✬ ✩ Regular Operations Recall that for our purposes, a language is simply a set of strings. Let A and B be languages. The regular operations are: • Union: A ∪ B • Concatenation: A ◦ B = { xy | x ∈ A, y ∈ B } • Kleene star: A ∗ = { x 1 x 2 · · · x k | k ≥ 0 , each x i ∈ A } Note that the empty string, ǫ , is always in A ∗ . ✫ ✪ 2-9
✬ ✩ Regular Operations (cont.) Let A = { aa, abba } and B = { a, ba, bba, bbba, . . . } . A ∪ B = { a, aa, abba, ba, bba, bbba, . . . } A ◦ B = { aaa, abbaa, aaba, abbaba, aabba, abbabba, . . . } A ∗ = { ǫ, aa, abba, aaaa, aaabba, abbaaa, abbaabba, aaaaaa, aaaaabba, aaabbaaa, aaabbaabba, . . . } The regular languages are closed under the regular operations. It will be easier to show this after we have considered non-deterministic automata. ✫ ✪ 2-10
� ✬ ✩ Nondeterminism The type of machine we have seen so far is called a deterministic finite automaton, or DFA. We now turn to non-deterministic finite automata, or NFAs. Here is an NFA for recognising the language � w ∈ { 0 , 1 } ∗ has length 3 or more, � � w � and the third last symbol in w is 1 � � 0 , 1 � ���� ���� � ���� ���� 0 , 1 � ���� ���� 0 , 1 � ���� ���� ���� ���� 1 q 1 q 2 q 3 q 4 Note: No transitions from q 4 , and two possible transitions when we meet a 1 in state q 1 . ✫ ✪ 2-11
� � � � � � � � � � � � � ✬ ✩ Nondeterminism (cont.) The NFA is much easier to design and understand than a DFA for the same language: �� �� 0 0 � ���� ���� ���� ���� ���� ���� ���� ���� ���� ���� ���� ���� 0 0 000 100 010 110 1 � �������� � � 1 � � 0 1 1 0 ���� ���� ���� ���� ���� ���� � ���� ���� � ���� ���� ���� ���� � � 0 � �� �� � 0 001 101 011 111 1 1 1 1 This is the simplest DFA that will do the job! ✫ ✪ 2-12
� � ✬ ✩ Epsilon Transitions NFAs may also be allowed to move from one state to another without consuming input. Such a transition is called an ǫ transition. Amongst other things this is useful for constructing machines to recognise unions A ∪ B of languages: machine for A � � ǫ � � ���� ���� � � � � � � � � � ǫ � � machine for B ✫ ✪ 2-13
✬ ✩ Formal Definition For any alphabet Σ let Σ ǫ denote Σ ∪ { ǫ } . An NFA is a 5-tuple ( Q, Σ , δ, q 0 , F ), where • Q is a finite set of states , • Σ is a finite alphabet , • δ : Q × Σ ǫ → P ( Q ) is the transition function , • q 0 ∈ Q is the start state , and • F ⊆ Q are the accept states . ✫ ✪ 2-14
✬ ✩ Acceptance, Recognition The definition of what it means for an NFA N to accept a string says that it has to be possible to make the necessary transitions. Let N = ( Q, Σ , δ, q 0 , F ) be an NFA and let w = v 1 v 2 · · · v n where each v i is a member of Σ ǫ . N accepts w iff there is a sequence of states r 0 , r 1 , . . . , r n , with each r i ∈ Q , such that 1. r 0 = q 0 2. r i +1 ∈ δ ( r i , v i +1 ) for i = 0 , . . . , n − 1 3. r n ∈ F N recognises language A iff A = { w | N accepts w } ✫ ✪ 2-15
Recommend
More recommend