Finite Automata Seungjin Choi Department of Computer Science and Engineering Pohang University of Science and Technology 77 Cheongam-ro, Nam-gu, Pohang 37673, Korea seungjin@postech.ac.kr 1 / 28
Outline Examples of finite automata (=finite state machine, good models for computers with extremely limited amount of memory) Deterministic finite automata (DFA) Nondeterministic finite automata (NFA) Equivalence between DFA and NFA More details and applications 2 / 28
An Example of FA: Automatic Door Two states: open or closed Four inputs: front, rear, both, neither. Front pad Rear pad rear both neither closed front open front rear both Door (a) Automatic Door neither (b) FA 3 / 28
An Example of FA: e-commerce Protocol for e-commerce using e-money Allowed events: 1. The customer can pay the store (=send he money-file to the store). 2. The customer can cancel the money (like putting a stop on a check). 3. The store can ship the goods to the customer. 4. The store can redeem the money (=cash the check). 5. The bank can transfer the money to the store. 4 / 28
5 / 28
6 / 28
7 / 28
Deterministic Finite Automata (DFA) Definition (DFA) A deterministic finite automaton is a quintuple (5-tuple), M = (Q, Σ, δ, q, F ), where Q: a finite set of states, Σ: a finite set of input symbols (input alphabet), δ : Q Σ Q: a transition function, q Q: a start state (an initial state), F Q: a set of final or accepting states. 8 / 28
1 q q 1 q 2 1 1 The DFA associated with this transition diagram is given by M = ({q, q 1, q 2 }, {, 1}, δ, q, {q 1 }), δ(q, ) = q, δ(q, 1) = q 1, δ(q 1, ) = q, δ(q 1, 1) = q 2, δ(q 2, ) = q 2, δ(q 2, 1) = q 1. M accepts 1, 11, 111, 111 and does not accept,1, 11. 9 / 28
Notations for DFA Transition diagram Transition table See Sec. 2.2.3 1 / 28
Extended Transition Function The extended transition function, δ : Q Σ Q, describes what happens when we start in any state and follow any sequence of inputs. For example, given δ(q, a) = q 1 and δ(q 1, b) = q 2, we have We can define δ recursively by δ (q, ɛ) = q, δ (q, ab) = q 2. δ (q, wa) = δ(δ (q, w), a), q Q, w Σ, a Σ. For example, δ (q, a) = δ(δ (q, ɛ), a) = δ(q, a) = q 1. Thus, δ (q, ab) = δ(δ (q, a), b) = δ(q 1, b) = q 2. 11 / 28
The Language of DFA Definition The language accepted by a DFA, M = (Q, Σ, δ, q, F ), is the set of all strings on Σ accepted by M, L(M) = {w Σ δ (q, w) F }. Example: L = { n 1 n }, the language accepted by the DFA shown below: 1, 1 q q 1 q 2, 1 12 / 28
Example: Find a DFA that recognizes the set of all strings on Σ = {a, b} starting with the prefix ab. Need 4 states: (1) a start state; (2) 2 states for recognizing ab ending in a final trap state; (3) one non-final trap state. a b q q 1 q 2 a, b q 3 a, b 13 / 28
Example: Find a DFA that accepts all the strings on Σ = {, 1}, except those containing the substring 1. 1 ǫ 1 1, 1 1 14 / 28
Regular Language Definition (Regular) A language L is called regular if and only if there exits a DFA M such that L = L(M). Regular language DFA What you need to do to show that L is regular, is to find a DFA M such that L = L(M). 15 / 28
Example: Show that the language, L = {awa w {a, b} } is regular. q a b b q 2 q 3 a b a q 1 a,b 16 / 28
Example: Show that L 2 is regular, where L 2 = {aw 1 aaw 2 a w 1, w 2 {a, b} }. q a b b b b a q 2 q 3 q 4 q 5 a b a a q 1 a,b 17 / 28
Nondeterministic Finite Automata (NFA) Definition (NFA) A nondeterministic finite automaton is a quintuple (5-tuple), M = (Q, Σ, δ, q, F ), where Q: a finite set of states, Σ: a finite set of input symbols (input alphabet), δ : Q (Σ {ɛ}) 2 Q : a transition function, q Q: a start state (an initial state), F Q: a set of final or accepting states. 18 / 28
Differences between DFA and NFA In NFA, the range of the transition function δ is the power set 2 Q, implying that several different movements are allowed. For example, δ(q 1, a) = {q, q 2 }. NFA can make a transition without consuming an input symbol. For example, δ(q, ɛ) = q 1. The set δ(q i, a) may be empty. In other words, no transition may be defined for a specific situation. 19 / 28
An Example of NFA, 1 q q 1 q 2 1 ǫ Several edges with the same label originate from one vertex ɛ-transition δ(q 2, ) = φ 2 / 28
NFA with Extended Transition Functions ǫ q q 1 ǫ q 2 For an NFA, the extended transition function is defined so that δ (q i, w) contains q j if and only if there is a walk in the transition graph from q i to q j labeled w. δ (q 1, ) = {q, q 1, q 2 } and δ (q 2, ɛ) = {q, q 2 }. 21 / 28
The Language of NFA Definition The language L accepted by an NFA, M = (Q, Σ, δ, q, F ), is defined by L(M) = {w Σ δ (q, w) F φ}. Example: L = {(1) n n } is accepted by the NFA shown below:, 1 q q 1 q 2 1 ǫ 22 / 28
Why Nondeterminism? Nondeterministic machines can serve as models of search-and-backtrack algorithms. Sometimes helpful in solving problems easily. Certain results are more easily established for NFA s than for DFA s. 23 / 28
Equivalence Definition Two finite automata M 1 and M 2 are said to be equivalent if L(M 1 ) = L(M 2 ), that is, they accept the same language., 1 q q 1 q 2 1 ǫ (a) NFA 1 q q 1 q 2 1 (b) DFA, 1 These two FAs are equivalent. 24 / 28
Equivalence of DFA and NFA Theorem For any NFA M N there exists a DFA M D such that and vice versa. L(M D ) = L(M N ) This involves the subset construction, an important example how an automaton M B can be generically constructed from another automaton M A. Given an NFA M N = (Q N, Σ, δ N, q, F N ), we will construct a DFA M D = (Q D, Σ, δ D, {q }, F D ) such that L(M D ) = L(M N ). 25 / 28
Subset Construction Q D = {S S Q N }, i.e., Q D = 2 Q N. Note that Q D = 2 Q N, although most states in Q D are likely to be garbage. F D = {S Q N S F N φ}. For every S Q N and a Σ, δ D (S, a) = q S δ N (q, a). 26 / 28
Example: Subset Construction, 1 1 q q1 q2 1 φ φ φ {q } {q, q 1 } {q } {q 1 } φ {q 2 } {q 2 } φ φ {q, q 1 } {q, q 1 } {q, q 2 } {q, q 2 } {q, q 1 } {q } {q 1, q 2 } φ {q 2 } {q, q 1, q 2 } {q, q 1 } {q, q 2 } 27 / 28
Example: An Equivalent DFA Determine states accessible from the start state and draw a transition graph: 1 {q } {q, q 1 } 1 {q, q 2 } 1 28 / 28