Chapter 2 Finite Automata (part a) Hokkaido, Japan 1 Outline (part a --- in this PPT) 2.0 Introduction 2.1 An Informal Picture of Finite Automata 2.2 Deterministic Finite automata 2.3 Nondeterministic Finite Automata (part b --- in another PPT) 2.4 An Application: Text Search 2.5 Finite Automata with Epsilon-Transitions 2 2.0 Introduction Two types of finite automata (FA): – Deterministic FA (DFA) – Nondeterministic FA (NFA) Both types are of the same power, – The former is easier for hardware and software implementations. – The latter is more efficient for descriptions of applications of FA. 3 2.1 An Informal Picture of Finite Automata A complete application example of finite automata for protocol design and verification – Read by yourself! – Involving a concept of “product of two automata” 4 2.2 Deterministic Finite Automata Recall the example of a vending machine selling 20-dolllar food in Chapter 0 $10 $5 Start $0 $15 $5 $5 $10 $10 $5 $10 $10 $20 $5 transition diagram 5 2.2 Deterministic Finite Automata 2.2.1 Definition of DFA – A DFA A consists of 5-tuples (1/2): a finite (nonempty) set of states Q; a finite (nonempty) set of input symbols S; a (state) transition function d such that d(q, a) = p means “automaton A, in state q, takes input a and enters state p;” 6 2.2 Deterministic Finite Automata 2.2.1 Definition of DFA – A DFA A consists of 5-tuples (2/2): a start state q0; a set of (nonempty) final or accepting states F. – A may be written as a “5-tuple” A = (Q, S, d, q0, F). 7 2.2 Deterministic Finite Automata 2.2.1 Definition of DFA – graphic model for a DFA tape 磁帶 0 1 1 0 1 0 0 … 讀取頭 tape reader 有限 finite control 控制器 圖 4.2 一部有限自動機的圖形模式。 8 2.2 Deterministic Finite Automata 2.2.2 How a DFA processes strings – Given an input string x = a1a2…an, if d(q0, a1) = q1, d(q1, a2) = q2, …, d(qi1, ai) = qi, ..., d(qn1, an) = qn, and qn F, then x is “accepted”; otherwise, “rejected.” – Every transition is deterministic. 9 2.2 Deterministic Finite automata Example 2.1 – Design an FA A to accept the language L = {x01y | x and y are any strings of 0’s and 1’s}. – Examples of strings in L: 01, 11010, 100011… 10 2.2 Deterministic Finite automata Example 2.1 – Transitions: d(q0, 1) = q0, d(q0, 0) = q2, d(q2, 1) = q1, d(q2, 0) = q2, d(q1, 0) = q1, d(q1, 1) = q1 – 5-Tuple: A = ({q0, q1, q2}, {0, 1}, d, q0, {q1}) 11 2.2 Deterministic Finite automata 2.2.3 Simpler Notations for DFA’s – Transition diagram --1 Start q0 0 0 q2 1 q1 0, 1 12 2.2 Deterministic Finite automata 2.2.3 Simpler Notations for DFA’s – Transition table --0 1 q0 q2 q0 *q1 q1 q1 q2 q2 q1 : initial state; *: final state 13 dˆ 2.2 Deterministic Finite automata 2.2.4 Extending transition function to strings – Extended transition function dˆ If x = a1a2…an and d is such that d(p, a1) = q1, d(q1, a2) = q2, …, d(qi1, ai) = qi, ..., d(qn1, an) = q, then we define dˆ to be dˆ (p, x) = q. 14 dˆ 2.2 Deterministic Finite automata 2.2.4 Extending Transition Function to Strings – Also may be defined recursively as in the textbook. – Recursive definition for dˆ Basis:dˆ (q, e) = q. Induction: if w = xa (a is the last symbol of w), then dˆ (q, w) = d(dˆ(q, x), a). 15 dˆ 2.2 Deterministic Finite automata 2.2.4 Extending Transition Function to Strings A graphic diagram for the following concept: Induction: if w = xa (a is the last symbol of w), then dˆ(q, w) = d (dˆ(q, x), a). … x dˆ (q, x) a d 0, 1 1 w=xa) dˆq(q, 16 dˆ 2.2 Deterministic Finite automata 2.2.5 The Language of a DFA – The language of a DFA A is defined as L(A) = {w | dˆ(q0, w) is in F}. – If L is L(A) for some DFA A, then we say L is a regular language. 17 2.3 Nondeterministic Finite Automata 2.3.1 An informal view of NFA’s – Review of a previous example of DFA (of Example 2.1) 1 Start q0 0 0 q2 1 q1 $20 0, 1 – An NFA version of above DFA (more intuitive!) 0, 1 Start q0 0 q2 1 q1 $20 0, 1 18 2.3 Nondeterministic Finite Automata Some properties of NFA’s 0, 1 Start q0 0 q2 1 q1 $20 0, 1 – Some transitions may “die,” like d(q2, 0). – Some transitions have multiple choices, like d(q0, 0) = q0 and q2. 19 2.3 Nondeterministic Finite Automata Example 2.6 – Design an NFA accepting the following language L = {w | w{0, 1}* and ends in 01}. 0, 1 Start q0 0 q1 1 q2 $20 – Nondeterminism creates many transition paths, but if there is one path leading to a final state, then the input is accepted. 20 2.3 Nondeterministic Finite Automata Example 2.6 (cont’d) 0, 1 0 q0 Start q1 1 q2 $20 – When input x = 00101, the NFA processes x in the following way: q0 Fig. 2.10 0 q0 q0 q1 Stuck! q1 q0 q0 q0 q1 q2 Stuck! 0 1 0 1 q2 Accept! 21 2.3 Nondeterministic Finite Automata 2.3.2 Definition of NFA – An NFA A is a 5-tuple A = (Q, S, d, q0, F) where Q = a finite (nonempty) set of states; S = a finite (nonempty) set of input symbols; q0 = a start state; F = a set of (nonempty) final or accepting states; 22 2.3 Nondeterministic Finite Automata 2.3.2 Definition of NFA – An NFA A is a 5-tuple A = (Q, S, d, q0, F) where d = a (state) transition function such that d(q, a) = {p1, p2, …, pm} which means “automaton A, in state q, takes input a and enters one of the states p1, p2, …, pm.” 23 2.3 Nondeterministic Finite Automata Example 2.7 – Transition table of NFA of the last example --0, 1 Start 0 q0 q1 1 q2 $20 0 1 {q0, q1} {q0} q1 {q2} *q2 q0 24 2.3 Nondeterministic Finite Automata 2.3.3 Extended Transition Function – Recursive definition of dˆ (with string as input) Basis: dˆ(q, e) = {q}. Induction: if w = xa (a is the last symbol of w), dˆ(q, x) = {p1, p2, …, pk}, and k U d(pi, a) = {r1, r2, …, rm} then i=1 dˆ (q, w) = {r1, r2, …, rm}. 25 dˆ 2.3 Nondeterministic Finite Automata (supplemental) 2.3.3 Extended Transition Function – A graphic diagram for the following concept: Induction: if w = xa (a is the last symbol of w), dˆ (q, x) = {p1, p2, …, pk}, and d(pi, a) = {r1, r2, …, rm} then dˆ (q, w) = {r1, r2, …, rm}. dˆ (q, x)= … x { p1 p2 . . . pk} a a a d dˆ (q, w=xa) = {r1 r2 . . . rm} 26 2.3 Nondeterministic Finite Automata Example 2.8 --– For the input w = 00101, we have 1. dˆ(q0, e) = {q0}. 2. dˆ(q0, e 0) = d(q0, 0) = {q0, q1}. 3. dˆ(q0, 00) = d(q0, 0)∪d(q1, 0) = {q0, q1}∪ ={q0, q1} 0, 1 … Start q0 0 q1 1 q2 $20 27 2.3 Nondeterministic Finite Automata 2.3.4 The Language of an NFA – Definition --If A = (Q, S, d, q0, F) is an NFA, then the language accepted by A is L(A) = {w d| ˆ (q0, w)∩F }. – That is, as long as a final state is reached, which is among possibly many, the input is accepted. 28 2.3 Nondeterministic Finite Automata 2.3.5 Equivalence of DFA and NFA – NFA’s are more intuitive to construct for many languages. – DFA’s are easier for use in software and hardware implementations. 29 2.3 Nondeterministic Finite Automata 2.3.5 Equivalence of DFA and NFA – Every NFA N has an equivalent DFA D, i.e., L(D) = L(N). (Definition: “Two models are said to be equivalent if they have identical languages.”) – The proof needs “subset construction.” 30 2.3 Nondeterministic Finite Automata 2.3.5 Equivalence of DFA and NFA – Subset construction: each state of DFA is a subset of NFA – Given an NFA N = (QN, S, dN, q0, FN), we construct a DFA D = (QD, S, dD, q0', FD) such that: The two alphabets are identical; QD is the power set of QN (i.e., set of all subsets of QN); Inaccessible states in QD should be deleted; (continued in the next page) 31 2.3 Nondeterministic Finite Automata 2.3.5 Equivalence of DFA and NFA – (cont’d) q0' = {q0}; Each subset S of QN is in final state set FD if S includes at least one accepting state in FN (i.e., S∩FN ). For each subset S of QN and for each input symbol a, define dD(S, a) = U dN(p, a) pS 32 2.3 Nondeterministic Finite Automata Example 2.10 --– For the NFA of Example 2.6 below: 0, 1 Start q0 0 q1 1 q2 $20 – The power set of QN = {q0, q1, q2} is QD = {, {q0}, {q1}, {q2}, {q0, q1}, {q0, q2}, {q1, q2}, {q0, q1, q2}}. 33 2.3 Nondeterministic Finite Automata Example 2.10 (cont’d) 0, 1 Start q0 0 – The transition table is q1 1 {q0} {q1} *{q2} {q0, q1} *{q0, q2} *{q1, q2} *{q0, q1, q2} q2 $20 0 1 {q0, q1} {q0, q1} {q0, q1} {q0, q1} {q0} {q2} {q0, q2} {q0} {q2} {q0, q2} 34 2.3 Nondeterministic Finite Automata Example 2.10 (cont’d) – Change names of the states, we get A B C *D E *F *G *H 0 A E A A E E A E 1 A B D A F B D F 35 2.3 Nondeterministic Finite Automata Example 2.10 (cont’d) – Checking accessible states from the start state as red ones; the others are inaccessible. Figure 2.13 A B C *D E *F *G *H 0 A E A A E E A E 1 A B D A F B D F 36 2.3 Nondeterministic Finite Automata Example 2.10 (cont’d) (supplemental) – solved by “Lazy evaluation” using table: starting from the start state, only accessible states are evaluated, in order to avoid generation of inaccessible states. 0, 1 Start q0 0 q1 1 q2 $20 – The transition table is {q00} {q11} *{q22} {q00, q11} *{q00, q22} *{q11, q22} *{q00, q11, q22} 0 {q00, q11} {q00, q11} {q00, q11} {q00, q11} 1 {q00} {q22} {q00, q22} {q00} {q22} {q00, q22} 37 2.3 Nondeterministic Finite Automata Example 2.10 (cont’d) --- – solved by “Lazy evaluation” using diagram: starting from the start state, only accessible states are evaluated, in order to avoid generation of inaccessible states. 1 Start {q0} 0 0 {q0, q1} 1 {q0, q2} 0 Figure 2.14 1 38 2.3 Nondeterministic Finite Automata Theorem 2.11 If DFA D = (QD, S, dD, {q0}, FD) is constructed from NFA N = (QN, S, dN, q0, FN) by subset construction, then L(D) = L(N). – Proof. See the textbook. 39 2.3 Nondeterministic Finite Automata Theorem 2.12 (equivalence of DFA and NFA): A language L is accepted by some DFA if and only if L is accepted by some NFA. – Proof. See the textbook. 40 2.3 Nondeterministic Finite Automata 2.3.6 A Bad Case of Subset Construction – The DFA constructed from an NFA usually has the same number of accessible states of the NFA. – But the worse case is m = 2n where m is the number of states in the DFA. n is the number of states in the NFA. 41 2.3 Nondeterministic Finite Automata 2.3.7a Regarding NFA’s as DFA’s – In each state of a DFA, for each input there must be a next state. – In an NFA, for a certain input there might be no next state. So the following is an NFA. start t t h th e the n then 42 2.3 Nondeterministic Finite Automata 2.3.7a Regarding NFA’s as DFA’s – But it is deterministic in nature --- either getting into a next state or becoming “dead” (getting into a “dead” state). start t t h th e the n then – Such a kind of NFA has at most one transition out of any state on any symbol, and may be regarded as a DFA. 43