Chapter 2 Finite Automata part a Hokkaido Japan

  • Slides: 43
Download presentation
Chapter 2 Finite Automata (part a) Hokkaido, Japan 1

Chapter 2 Finite Automata (part a) Hokkaido, Japan 1

Outline (part a --- in this PPT) 2. 0 Introduction 2. 1 An Informal

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) –

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

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

2. 2 Deterministic Finite Automata Recall the example of a vending machine selling 20 -dolllar food in Chapter 0 $5 Start $0 $10 $5 $5 $10 $15 $5 $10 $20 $5 transition diagram 5

2. 2 Deterministic Finite Automata 2. 2. 1 Definition of DFA – A DFA

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

2. 2 Deterministic Finite Automata 2. 2. 1 Definition of DFA – A DFA A consists of 5 -tuples (2/2): a start state q 0; a set of (nonempty) final or accepting states F. – A may be written as a “ 5 -tuple” A = (Q, S, d, q 0, F). 7

2. 2 Deterministic Finite Automata 2. 2. 1 Definition of DFA – graphic model

2. 2 Deterministic Finite Automata 2. 2. 1 Definition of DFA – graphic model for a DFA tape reader finite control 8

2. 2 Deterministic Finite Automata 2. 2. 2 How a DFA processes strings –

2. 2 Deterministic Finite Automata 2. 2. 2 How a DFA processes strings – Given an input string x = a 1 a 2…an, if d(q 0, a 1) = q 1, d(q 1, a 2) = q 2, …, d(qi 1, ai) = qi, . . . , d(qn 1, 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

2. 2 Deterministic Finite automata Example 2. 1 – Design an FA A to accept the language L = {x 01 y | 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(q 0, 1) =

2. 2 Deterministic Finite automata Example 2. 1 – Transitions: d(q 0, 1) = q 0, d(q 0, 0) = q 2, d(q 2, 1) = q 1, d(q 2, 0) = q 2, d(q 1, 0) = q 1, d(q 1, 1) = q 1 – 5 -Tuple: A = ({q 0, q 1, q 2}, {0, 1}, d, q 0, {q 1}) 11

2. 2 Deterministic Finite automata 2. 2. 3 Simpler Notations for DFA’s – Transition

2. 2 Deterministic Finite automata 2. 2. 3 Simpler Notations for DFA’s – Transition diagram --1 Start q 0 0 0 q 2 1 q 1 0, 1 12

2. 2 Deterministic Finite automata 2. 2. 3 Simpler Notations for DFA’s – Transition

2. 2 Deterministic Finite automata 2. 2. 3 Simpler Notations for DFA’s – Transition table --0 1 q 0 q 2 q 0 *q 1 q 1 q 2 q 1 : initial state; *: final state 13

2. 2 Deterministic Finite automata 2. 2. 4 Extending transition function to strings –

2. 2 Deterministic Finite automata 2. 2. 4 Extending transition function to strings – Extended transition function If x = a 1 a 2…an and d is such that d(p, a 1) = q 1, d(q 1, a 2) = q 2, …, d(qi 1, ai) = qi, . . . , d(qn 1, an) = q, then we define to be (p, x) = q. 14

2. 2 Deterministic Finite automata 2. 2. 4 Extending Transition Function to Strings –

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 Basis: (q, e) = q. Induction: if w = xa (a is the last symbol of w), then (q, w) = d( (q, x), a). 15

2. 2 Deterministic Finite automata 2. 2. 4 Extending Transition Function to Strings A

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 (q, w) = d ( (q, x), a). … x (q, x) a d q(q, 1 0, 1 w=xa) 16

2. 2 Deterministic Finite automata 2. 2. 5 The Language of a DFA –

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 | (q 0, 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 –

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 q 0 0 0 q 2 1 q 1 $20 0, 1 – An NFA version of above DFA (more intuitive!) 0, 1 Start q 0 0 q 2 1 q 1 $20 0, 1 18

2. 3 Nondeterministic Finite Automata Some properties of NFA’s 0, 1 Start q 0

2. 3 Nondeterministic Finite Automata Some properties of NFA’s 0, 1 Start q 0 0 q 2 1 q 1 $20 0, 1 – Some transitions may “die, ” like d(q 2, 0). – Some transitions have multiple choices, like d(q 0, 0) = q 0 and q 2. 19

2. 3 Nondeterministic Finite Automata Example 2. 6 – Design an NFA accepting the

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 q 0 0 q 1 1 q 2 $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 q 0

2. 3 Nondeterministic Finite Automata Example 2. 6 (cont’d) 0, 1 0 q 0 Start q 1 1 q 2 $20 – When input x = 00101, the NFA processes x in the following way: q 0 Fig. 2. 10 0 q 0 q 1 Stuck! q 1 0 q 0 q 1 1 q 2 Stuck! 0 1 q 2 Accept! 21

2. 3 Nondeterministic Finite Automata 2. 3. 2 Definition of NFA – An NFA

2. 3 Nondeterministic Finite Automata 2. 3. 2 Definition of NFA – An NFA A is a 5 -tuple A = (Q, S, d, q 0, F) where Q = a finite (nonempty) set of states; S = a finite (nonempty) set of input symbols; q 0 = 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

2. 3 Nondeterministic Finite Automata 2. 3. 2 Definition of NFA – An NFA A is a 5 -tuple A = (Q, S, d, q 0, F) where d = a (state) transition function such that d(q, a) = {p 1, p 2, …, pm} which means “automaton A, in state q, takes input a and enters one of the states p 1, p 2, …, pm. ” 23

2. 3 Nondeterministic Finite Automata Example 2. 7 – Transition table of NFA of

2. 3 Nondeterministic Finite Automata Example 2. 7 – Transition table of NFA of the last example --0, 1 Start 0 q 1 1 q 2 $20 0 1 {q 0, q 1} {q 0} q 1 {q 2} *q 2 q 0 24

2. 3 Nondeterministic Finite Automata 2. 3. 3 Extended Transition Function – Recursive definition

2. 3 Nondeterministic Finite Automata 2. 3. 3 Extended Transition Function – Recursive definition of (with string as input) Basis: (q, e) = {q}. Induction: if w = xa (a is the last symbol of w), (q, x) = {p 1, p 2, …, pk}, and d(pi, a) = {r 1, r 2, …, rm} then (q, w) = {r 1, r 2, …, rm}. 25

2. 3 Nondeterministic Finite Automata (supplemental) 2. 3. 3 Extended Transition Function – A

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), (q, x) = {p 1, p 2, …, pk}, and d(pi, a) = {r 1, r 2, …, rm} then (q, w) = {r 1, r 2, …, rm}. … x (q, x)= { p 1 p 2. . . pk} a a a d (q, w=xa) = {r 1 r 2. . . 26

2. 3 Nondeterministic Finite Automata Example 2. 8 --– For the input w =

2. 3 Nondeterministic Finite Automata Example 2. 8 --– For the input w = 00101, we have 1. (q 0, e) = {q 0}. 2. (q 0, e 0) = d(q 0, 0) = {q 0, q 1}. 3. (q 0, 00) = d(q 0, 0)∪d(q 1, 0) = {q 0, q 1}∪ ={q 0, q 1} 0, 1 … Start q 0 0 q 1 1 q 2 $20 27

2. 3 Nondeterministic Finite Automata 2. 3. 4 The Language of an NFA –

2. 3 Nondeterministic Finite Automata 2. 3. 4 The Language of an NFA – Definition --If A = (Q, S, d, q 0, F) is an NFA, then the language accepted by A is L(A) = {w | (q 0, 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 –

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 –

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 –

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, d. N, q 0, FN), we construct a DFA D = (QD, S, d. D, q 0', 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 –

2. 3 Nondeterministic Finite Automata 2. 3. 5 Equivalence of DFA and NFA – (cont’d) q 0' = {q 0}; 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 d. D(S, a) = d. N(p, a) 32

2. 3 Nondeterministic Finite Automata Example 2. 10 --– For the NFA of Example

2. 3 Nondeterministic Finite Automata Example 2. 10 --– For the NFA of Example 2. 6 below: 0, 1 Start q 0 0 q 1 1 q 2 $20 – The power set of QN = {q 0, q 1, q 2} is QD = { , {q 0}, {q 1}, {q 2}, {q 0, q 1}, {q 0, q 2}, {q 1, q 2}, {q 0, q 1, q 2}}. 33

2. 3 Nondeterministic Finite Automata Example 2. 10 (cont’d) 0, 1 Start q 0

2. 3 Nondeterministic Finite Automata Example 2. 10 (cont’d) 0, 1 Start q 0 0 – The transition table is q 1 1 {q 0} {q 1} *{q 2} {q 0, q 1} *{q 0, q 2} *{q 1, q 2} *{q 0, q 1, q 2} q 2 $20 0 1 {q 0, q 1} {q 0} {q 2} {q 0, q 2} {q 0} {q 2} {q 0, q 2} 34

2. 3 Nondeterministic Finite Automata Example 2. 10 (cont’d) – Change names of the

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

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

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 q 0 0 q 1 1 q 2 $20 – The transition table is {q 00} {q 11} *{q 22} {q 00, q 11} *{q 00, q 22} *{q 11, q 22} *{q 00, q 11, q 22} 0 {q 00, q 11} 1 {q 00} {q 22} {q 00, q 22} {q 00} {q 22} {q 00, q 22} 37

2. 3 Nondeterministic Finite Automata Example 2. 10 (cont’d) --- – solved by “Lazy

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 Figure 2. 14 {q 0} 0 0 {q 0, q 1} 1 1 0 {q 0, q 2} 38

2. 3 Nondeterministic Finite Automata Theorem 2. 11 If DFA D = (QD, S,

2. 3 Nondeterministic Finite Automata Theorem 2. 11 If DFA D = (QD, S, d. D, {q 0}, FD) is constructed from NFA N = (QN, S, d. N, q 0, 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

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

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 = 2 n 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. 7 a Regarding NFA’s as DFA’s –

2. 3 Nondeterministic Finite Automata 2. 3. 7 a 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. 7 a Regarding NFA’s as DFA’s –

2. 3 Nondeterministic Finite Automata 2. 3. 7 a 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