Chapter 2 Finite Automata part b Windmills in
Chapter 2 Finite Automata (part b) Windmills in Holland 1
Outline (part a --- in another 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 this PPT) 2. 4 An Application: Text Search 2. 5 Finite Automata with Epsilon-Transitions 2
2. 4 An Application: Text Search 2. 4. 1 Finding Strings in Text – Searching Google for a set of words is equivalent to just finding strings in documents Page p 1 – Techniques Using inverted indexes Using finite automata Keyword K . . . Inverted indexing Page p 2 Page pn – Applications unsuitable to use inverted indexing: Document repository change rapidly. Documents to be searched cannot be catalogued. 3
2. 4 An Application: Text Search 2. 4. 2 NFA’s for Text Search – (supplemental 1/3) A simple example --- find the DFA equivalent to the following NFA which searches words of x 1 = ab and x 2 = b in long text strings a, b a 1 Start 2 b $20 4 a, b b $20 3 4
2. 4 An Application: Text Search 2. 4. 2 NFA’s for Text Search – (supplemental 2/3) A simple example (con’d) --- find the DFA equivalent to the following NFA (note: state 12 means state {1, 2}, etc. ) a 1 Start a b 12 13 4$20 b a b b $20 13 a a a 123 b 4$20 b $20 123 5
2. 4 An Application: Text Search 2. 4. 2 NFA’s for Text Search – (supplemental 3/3) A simple example (con’d) --compare!!! a a, b 1 Start 2 b $2 4 0 a 1 a, b b $2 3 0 a, b a a, b Start 12 b 134 $20 a, b b $20 13 6
2. 4 An Application: Text Search 2. 4. 2 NFA’s for Text Search – Example 2. 14: use an NFA to search two keywords “web” and “e. Bay” among text S S = set of all printable ASCII characters S w 2 e 3 b $20 4 1 Start S e 5 b 6 a 7 y Figure 2. 16 (Note: there are two final states; red parts are necessary and missing in the textbook; correct your textbook!!!) $20 8 7
2. 4 An Application: Text Search 2. 4. 2 NFA’s for Text Search – Example 2. 14 (cont’d): how to implement NFA? Write a simulation program like Fig. 2. 10 q 0 q 0 q 1 Stuck! q 1 q 0 q 0 q 1 q 2 Stuck! q 2 Accept! Convert the NFA to an equivalent DFA using subset construction and simulate the DFA. 8
2. 4 An Application: Text Search 2. 4. 3 A DFA to Recognize a Set of Keywords – – The converted DFA has an equal number of states of the original NFA (an observation). The DFA states may be constructed as (may be proved): If q 0 is start state of NFA, then {q 0} is start state of DFA If p is a state of NFA reachable (accessible) from a path x = a 1 a 2…am, then one state of the DFA is a set of NFA states consisting of 1. 2. 3. – q 0 p every other NFA states reachable from q 0 by following every subpath which is a suffix (尾段) of x, like ajaj+1…am. In the above way, all states in the DFA may be constructed, and then all transitions derived (see the next page). 9
2. 4 An Application: Text Search 2. 4. 3 A DFA to Recognize a Set of Keywords – Example 2. 15 (a continuation of Example 2. 14) E. g. , state 3 of the NFA becomes state 135 of the DFA because start state 1 is included; state 3 itself is reachable by the path x=we, so state 3 itself is included; x has a suffix e by which states 1 and 5 are reachable from start state 1. (for the transition diagram, see the next page) 10
2. 4 An Application: Text Search 2. 4. 3 A DFA to Recognize a Set of Keywords – Example 2. 15 (a continuation of Example 2. 14) E. g. , state 3 of the NFA becomes state 135 of the DFA S S w 2 e b 3 $2 4 0 S 1 e Start 5 b 6 e w 12 7 a S b 135 3 $2 8 0 y 146 1 S Start e 15 18 17 16 b a y 11
2. 4 An Application: Text Search 2. 4. 3 A DFA to Recognize a Set of Keywords – How to derive transitions --For any state of the DFA of the form {q 0, p 1, p 2, …, pn}, and for each input symbol x, let the reached states be {r 1, r 2, …, rm}, then the next state is {q 0, r 1, r 2, …, rm}. If no ri as above can be collected, then if the reached states from the q 0 by x are {s 1, s 2, …, sk}, then the next state is {q 0, s 1, s 2, …, sk}. 12
2. 4 An Application: Text Search 2. 4. 3 A DFA to Recognize a Set of Keywords – How to derive transitions --e. g. , (1) for x=b, state 3 4, state 5 6, so state 135 146 (2) for x=e, state 3 , state 5 , but state 1 5, so state 135 15 w e 12 135 3 b S 146 e 1 Start e S 15 b 16 a 17 y 18 13
2. 4 An Application: Text Search 2. 4. 3 A DFA to Recognize a Set of Keywords – A complete NFA to DFA conversion is shown in Fig. 2. 17 (S b e w, e. g. , means all ASCII characters other than b, e, w). – Derive it by yourself. Can you write it down directly? ? ? – The above rules for constructing the DFA directly may be thought as theorems! 14
2. 5 Finite Automata with Epsilon. Transitions 2. 5. 1 Use of e-transitions – We allow the automaton to accept the empty string e. – This means that a transition is allowed to occur without reading in a symbol. – The resulting NFA is called e-NFA. – It adds “programming (design) convenience” (more intuitive for use in designing FA’s) 15
2. 5 Finite Automata with Epsilon. Transitions Example 2. 16: an e-NFA accepting decimal numbers like 2. 15, . 125, +1. 4, -0. 501… 0, 1, …, 9 start q 0 e, + , - q 1 . q 2 q 3 e q 5 . 0, 1, …, 9 Figure 2. 18 0, 1, …, 9 q 4 – To accept a number like “+5. ” (nothing after the decimal point), we have to add q 4. 16
2. 5 Finite Automata with Epsilon. Transitions Example 2. 17: a more intuitive e-NFA for Example 2. 14 S 1 e S w 2 e 3 b $2 4 S 9 Start e 0 e 5 b 6 a 7 y 8 Figure 2. 19 Design an NFA with e-transitions. 17
2. 5 Finite Automata with Epsilon. Transitions 2. 5. 2 Formal Notation for an e-NFA – Definition: an e-NFA A is denoted by A = (Q, S, d, q 0, F) where the transition function d takes as arguments: a state in Q, and a member of S∪{e} 18
2. 5 Finite Automata with Epsilon. Transitions 2. 5. 2 Formal Notation for an e-NFA – Notes: e cannot be used as an input symbol, but can be accepted to yield a transition! d(qi, e) is defined for every state qi (getting into the dead state if there is no next state with e as input symbol originally). 19
2. 5 Finite Automata with Epsilon. Transitions 2. 5. 2 Formal Notation for an e-NFA – Example 2. 18: the e-NFA of Fig. 2. 18 is described as E = ({q 0, q 1, . . . , q 5}, {. , +, , 0, 1, . . . , 9}, d, q 0, {q 5}) The transitions, e. g. , include – d (q 0, e) = {q 1} – d(q 1, e) = See Fig. 2. 20 for the complete transition table of E 20
2. 5 Finite Automata with Epsilon. Transitions 2. 5. 2 Formal Notation for an e-NFA – Example 2. 18 (cont’d): Fig. 2. 20 --- the complete transition table of E. q 0 q 1 q 2 q 3 q 4 q 5 e +, . 0, 1, …, 9 {q 1} {q 5} {q 1} {q 2} {q 3} {q 1, q 4} {q 3} 21
2. 5 Finite Automata with Epsilon. Transitions 2. 5. 3 Epsilon-Closures (e-closures) – We have to define the e-closure to define the extended transition function for the e-NFA. – We “e-closure” a state q by following all transitions out of q that are labeled e. 22
2. 5 Finite Automata with Epsilon. Transitions 2. 5. 3 Epsilon-Closures (e-closures) – Formal recursive definition of the set ECLOSE(q) for q: State q is in ECLOSE(q) (including the state itself); If p is in ECLOSE(q), then all states accessible from p through paths of e’s are also in ECLOSE(q). (“經e式快速道路路段可到的所有states車站皆算在內”) 23
2. 5 Finite Automata with Epsilon. Transitions 2. 5. 3 Epsilon-Closures – e-closure for a set of states S: ECLOSE(S) = ∪q S ECLOSE(q) 2 e – Example 2. 19 e e 3 6 b 1 e 4 a 5 e 7 ECLOSE(1) = {1, 2, 3, 4, 6} ECLOSE({3, 5}) = ECLOSE(3)∪ECLOSE(5) = {3, 6}∪{5, 7} = {3, 5, 6, 7} 24
2. 5 Finite Automata with Epsilon. Transitions 2. 5. 4 Extended Transitions & Languages for e. NFA’s – Recursive definition of extended transition function : Basis: (q, e) = ECLOSE(q). Induction: if w = xa, then (q, w) is computed as: If (q, x) = {p 1, p 2, …, pk} and d(pi, a) = {r 1, r 2, …, rm}, then (q, w) = ECLOSE({r 1, r 2, …, rm})=ECLOSE( d(pi, a)). 25
2. 5 Finite Automata with Epsilon. Transitions 2. 5. 4 Extended Transitions & Languages for e. NFA’s – Induction: if w = xa, then (q, w) is computed as: If (q, x) = {p 1, p 2, …, pk} and d(pi, a) = {r 1, r 2, …, rm}, then (q, w) = ECLOSE({r 1, r 2, …, rm}). … x (q, x)= { p 1 p 2. . . pk} a a a d { r 1 r 2. . . rm } e. . . ECLOSE { r 1, r 2, …, rm } 26
2. 5 Finite Automata with Epsilon. Transitions Example 2. 20 ---Computing (q 0, 5. 6) for e-NFA of Fig. 2. 1 0, 1, …, 9 start q 0 Figure 2. 18 e, + , - q 1 . 0, 1, …, 9 q 2 0, 1, …, 9 q 3 e q 5 . – Compute (q 0, 5)= (q 0, e 5) first: q 4 – (q 0, e) = ECLOSE(q 0) = {q 0, q 1} – d(q 0, 5)∪d(q 1, 5) = ∪{q 1, q 4} = {q 1, q 4} – (q 0, 5) = (q 0, e 5) = ECLOSE(d(q 0, 5)∪d(q 1, 5)) = ECLOSE({q 1, q 4}) = ECLOSE({q 1})∪ECLOSE({q 4}) = {q 1, q 4} (complete the rest of computations by yourself!) 27
2. 5 Finite Automata with Epsilon. Transitions 2. 5. 5 Eliminating e-Transitions – The e-transition is good for design of FA, but for implementation, they have to be eliminated. – Given an e-NFA, we can find an equivalent DFA (a theorem seen later). – Let E = (QE, S, d. E, q 0, FE) be the given e-NFA, the equivalent DFA D = (QD, S, d. D, q. D, FD) is constructed as follows (in the next page). 28
2. 5 Finite Automata with Epsilon. Transitions – QD is the set of subsets of QE, in which each accessible is an e-closed subset of QE, i. e. , are sets S QE such that S = ECLOSE(S). In other words, each e-closed set of states, S, includes those states such that any e-transition out of one of the states in S leads to a state that is also in S. – q. D = ECLOSE(q 0) (initial state of D) – FD = {S | S QD and S∩FE } (continued in the next page) 29
2. 5 Finite Automata with Epsilon. Transitions – d. D(S, a) is computed for each a in S and each S in QD in the following way: Let S = {p 1, p 2, . . . , pk} Compute d(pi, a) and let this set be {r 1, r 2, …, rm} Set d. D(S, a) = ECLOSE({r 1, r 2, …, rm}) = ECLOSE( d(pi, a)) – Technique to create accessible states in DFA D: starting from the start state q 0 of e-NFA E, generate ECLOSE(q 0) as start state q. D of D; from the generated states to derive other states. 30
2. 5 Finite Automata with Epsilon-Transitions Example 2. 21: eliminate e-transitions of Fig. 2. 18. 0, 1, …, 9 start q 0 e, + , - q 1 . q 2 0, 1, …, 9 q 3 e q 5 . 0, 1, …, 9 q 4 Figure 2. 18 – Start state q. D = ECLOSE(q 0) = {q 0, q 1} – d. D({q 0, q 1}, +) = ECLOSE(d. E(q 0, +)∪d. E(q 1, +)) = ECLOSE({q 1}∪ ) = ECLOSE({q 1}) = {q 1}, . . . – d. D({q 0, q 1}, 0) = ECLOSE(d. E(q 0, 0)∪d. E(q 1, 0)) = ECLOSE ( ∪{q 1, q 4}) = ECLOSE({q 1, q 4}) = {q 1, q 4}, . . . – d. D({q 0, q 1}, . ) = ECLOSE(d. E(q 0, . )∪d. E(q 1, . )) = {q 2} 31
2. 5 Finite Automata with Epsilon. Transitions Example 2. 21: (cont’d) 0, 1, …, 9 start {q 0, q 1} +, - {q 1, q 4} {q 1 } . {q 2} Figure 2. 22 32
2. 5 Finite Automata with Epsilon. Transitions Example 2. 21: (cont’d) 0, 1, …, 9 start q 0 e, + , - q 1 . q 2 0, 1, …, 9 q 3 e q 5 . 0, 1, …, 9 q 4 Figure 2. 18 – d. D({q 1}, 0) = ECLOSE(d. E(q 1, 0)) = ECLOSE({q 1, q 4}) = {q 1, q 4}. . . – d. D({q 1}, . ) = ECLOSE(d. E(q 1, . )) = ECLOSE({q 2}) = {q 2} 33
2. 5 Finite Automata with Epsilon. Transitions 0, 1, …, 9 start {q 0, q 1} +, - {q 1 } . {q 1, q 4} 0, 1, …, 9 . {q 2} 34
2. 5 Finite Automata with Epsilon-Transitions Example 2. 21: (cont’d) 0, 1, …, 9 start q 0 e, + , - q 1 . q 2 0, 1, …, 9 q 3 e q 5 . 0, 1, …, 9 q 4 Figure 2. 18 – d. D({q 1, q 4}, 0) = ECLOSE(d. E(q 1, 0)∪d. E(q 4, 0)) = ECLOSE({q 1, q 4}∪ ) = {q 1, q 4}. . . – d. D({q 1, q 4}, . ) = ECLOSE(d. E(q 1, . )∪d. E(q 4, . )) = ECLOSE({q 2}∪{q 3}) = ECLOSE(q 2)∪ECLOSE (q 3) = {q 2}∪{q 3, q 5} = {q 2, q 3, q 5} 35
2. 5 Finite Automata with Epsilon. Transitions 0, 1, …, 9 start {q 0, q 1} +, - {q 1 } . {q 1, q 4} . {q 2, q 3, q 5} 0, 1, …, 9 . {q 2} 36
2. 5 Finite Automata with Epsilon-Transitions Example 2. 21: (cont’d) 0, 1, …, 9 start q 0 e, + , - q 1 . q 2 0, 1, …, 9 q 3 e q 5 . 0, 1, …, 9 q 4 Figure 2. 18 – d. D({q 2}, 0) = ECLOSE(d. E(q 2, 0)) = ECLOSE({q 3}) = {q 3, q 5} … – d. D({q 2, q 3, q 5}, 0) = ECLOSE(d. E(q 2, 0)∪d. E(q 3, 0)∪ d. E(q 5, 0))=ECLOSE({q 3}∪ )=ECLOSE(q 3)={q 3, q 5} … 37
2. 5 Finite Automata with Epsilon. Transitions 0, 1, …, 9 start {q 0, q 1} +, - {q 1 } . {q 1, q 4} . {q 2, q 3, q 5} 0, 1, …, 9 0, 1, … 9 . {q 2} 0, 1, … 9 {q 3, q 5} 38
2. 5 Finite Automata with Epsilon-Transitions Example 2. 21: (cont’d) 0, 1, …, 9 start q 0 e, + , - q 1 . q 2 0, 1, …, 9 q 3 e q 5 . 0, 1, …, 9 q 4 Figure 2. 18 – d. D({q 3, q 5}, 0) = ECLOSE(d. E(q 3, 0)∪d. E(q 5, 0)) =ECLOSE({q 3}∪ ) =ECLOSE(q 3)={q 3, q 5} … 39
2. 5 Finite Automata with Epsilon. Transitions 0, 1, …, 9 start {q 0, q 1} +, - {q 1 } . {q 1, q 4} . {q 2, q 3, q 5} 0, 1, …, 9 0, 1, … 9 . {q 2} 0, 1, … 9 {q 3, q 5} 0, 1, …, 9 40
2. 5 Finite Automata with Epsilon-Transitions Example 2. 21: (cont’d) – The dead state need be shown. 0, 1, …, 9 start {q 0, q 1} +, - {q 1 } . . {q 1, q 4} 0, 1, …, 9 +, - f +, -, . 0, 1, … 9 +, -, . {q 2} {q 2, q 3, q 5} all symbols 0, 1, …, 9 {q 3, q 5} 0, 1, …, 9 – But according to Section 2. 3. 7 a, the diagram in the last page may be regarded as deterministic. 41
2. 5 Finite Automata with Epsilon. Transitions Theorem 2. 22 – A language L accepted by some e-NFA if and only if L is accepted by some DFA. – Proof: see the textbook yourself. 42
2. 6 A Review 3 Types of Automata – DFA – NFA ( e-NFA ) – e-NFA ---good for soft/hardware implementation ---intermediately intuitive ---most intuitive e-NFA equivalent DFA e-NFA 43
- Slides: 43