Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 2 Finite Automata (part b) Windmills in Holland.

Similar presentations


Presentation on theme: "1 Chapter 2 Finite Automata (part b) Windmills in Holland."— Presentation transcript:

1 1 Chapter 2 Finite Automata (part b) Windmills in Holland

2 2 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

3 3 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 –Techniques Using inverted indexes Using finite automata –Applications unsuitable to use inverted indexing: Document repository change rapidly. Documents to be searched cannot be catalogued. Keyword K Page p 1 Page p 2...... Page p n Inverted indexing

4 4 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 2 Start b $20 4 a 1 a, b $20 3 b

5 5 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.) Start b a 12 $20 b 13 4 a 1 b $20 1313 b a 123 4 a b a $20 12 3 ab

6 6 2.4 An Application: Text Search 2.4.2 NFA’s for Text Search –(supplemental 3/3) A simple example (con’d) --- compare!!! Start b a 12 $20 a, b 13 4 a 1 a, b $20 1313 b a, b 2 Start b $2 0 4 a 1 a, b $2 0 3 b

7 7 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 “eBay” among text Figure 2.16 (Note: there are two final states; red parts are necessary and missing in the textbook; correct your textbook!!!)    2 Start 3 e $20 4 b w 1 6 7 8 5 e b a y  = set of all printable ASCII characters

8 8 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 Convert the NFA to an equivalent DFA using subset construction and simulate the DFA. q0q0 q1q1 Stuck! q0q0 q1q1 q2q2 q0q0 q0q0 q1q1 q2q2 Accept! q0q0 q0q0 q0q0 q0q0 q0q0 q1q1 q2q2 Fig. 2.10

9 9 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 …a m, then one state of the DFA is a set of NFA states consisting of one state of the DFA is a set of NFA states consisting of 1. q 0 2. p 3. every other NFA states reachable from q 0 by following every subpath which is a suffix ( 尾段 ) of x, like a j a j+1 …a m. –In the above way, all states in the DFA may be constructed, and then all transitions derived (see the next page).

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

11 11 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    2 Start 3 e $2 0 4 b w 1 6 7 8 5 e b a y  12 Start 3 e b w e b a y 135 1 16 17 18 18 15  146

12 12 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, …, p n }, and for each input symbol x, let the reached states be {r 1, r 2, …, r m }, then the next state is {q 0, r 1, r 2, …, r m }. For any state of the DFA of the form {q 0, p 1, p 2, …, p n }, and for each input symbol x, let the reached states be {r 1, r 2, …, r m }, then the next state is {q 0, r 1, r 2, …, r m }. If no r i as above can be collected, then if the reached states from the q 0 by x are {s 1, s 2, …, s k }, then the next state is {q 0, s 1, s 2, …, s k }.

13 13 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 so state 135  146 (2) for x=e, state 3 , state 5 , but state 1  5, (2) for x=e, state 3 , state 5 , but state 1  5, so state 135  15 so state 135  15  12 Start 3 e w e b a y 135 b 1 16 17 18 18 15  146 e

14 14 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 (   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!

15 15 2.5 Finite Automata with Epsilon- Transitions 2.5.1 Use of  -transitions –We allow the automaton to accept the empty string . –This means that a transition is allowed to occur without reading in a symbol. –The resulting NFA is called  -NFA. –It adds “programming (design) convenience” (more intuitive for use in designing FA’s)

16 16 Example 2.16: an  -NFA accepting decimal numbers like 2.15,.125, +1.4, -0.501… –To accept a number like “+5.” (nothing after the decimal point), we have to add q 4. q5q5q5q5  0, 1, …, 9 2.5 Finite Automata with Epsilon- Transitions start q0q0q0q0 , +, - q1q1q1q1 q2q2q2q2. 0, 1, …, 9 q3q3q3q3 q4q4q4q4. Figure 2.18

17 17   2.5 Finite Automata with Epsilon- Transitions Example 2.17: a more intuitive  -NFA for Example 2.14 Figure 2.19 Design an NFA with  -transitions.  2 Start 3 e $2 4 b 9 6 7 8 0  b a y 1 w 5 e

18 18 2.5 Finite Automata with Epsilon- Transitions 2.5.2 Formal Notation for an  -NFA –Definition: an  -NFA A is denoted by A = (Q, , , q 0, F) where the transition function  takes as arguments: a state in Q, and a state in Q, and a member of  ∪ {  } a member of  ∪ {  }

19 19 2.5 Finite Automata with Epsilon- Transitions 2.5.2 Formal Notation for an  -NFA –Notes:  cannot be used as an input symbol, but can be accepted to yield a transition!  (q i,  ) is defined for every state q i (getting into the dead state  if there is no next state with  as input symbol originally).  (q i,  ) is defined for every state q i (getting into the dead state  if there is no next state with  as input symbol originally).

20 20 2.5 Finite Automata with Epsilon- Transitions 2.5.2 Formal Notation for an  -NFA –Example 2.18: the  -NFA of Fig. 2.18 is described as E = ({q 0, q 1,..., q 5 }, {., +, , 0, 1,..., 9}, , q 0, {q 5 }) E = ({q 0, q 1,..., q 5 }, {., +, , 0, 1,..., 9}, , q 0, {q 5 }) The transitions, e.g., include The transitions, e.g., include –  (q 0,  ) =  q 1  –  (q 1,  ) =  See Fig. 2.20 for the complete transition table of E (next page). See Fig. 2.20 for the complete transition table of E (next page).

21 21 2.5 Finite Automata with Epsilon- Transitions 2.5.2 Formal Notation for an  -NFA –Example 2.18 (cont’d): Fig. 2.20 --- the complete transition table of E. Fig. 2.20 --- the complete transition table of E.   0, 1, …, 9 q0q1q2q3q4q5q0q1q2q3q4q5 {q1}{q5}{q1}{q5} {q1}{q1} {q2}{q3}{q2}{q3}  {q 1, q 4 } {q 3 } 

22 22 2.5 Finite Automata with Epsilon- Transitions 2.5.3 Epsilon-Closures (  -closures) –We have to define the  -closure to define the extended transition function for the  -NFA. –We “  -closure” a state q by following all transitions out of q that are labeled .

23 23 2.5 Finite Automata with Epsilon- Transitions 2.5.3 Epsilon-Closures (  -closures) –Formal recursive definition of the set ECLOSE(q) for q: State q is in ECLOSE(q) (including the state itself); 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  ’s are also in ECLOSE(q). If p is in ECLOSE(q), then all states accessible from p through paths of  ’s are also in ECLOSE(q). (“ 經  式快速道路路段可到的所有 states 車站皆算在內 ”) (“ 經  式快速道路路段可到的所有 states 車站皆算在內 ”)

24 24 2.5 Finite Automata with Epsilon- Transitions 2.5.3 Epsilon-Closures –  -closure for a set of states S: ECLOSE(S) = ∪ q  S ECLOSE(q) –Example 2.19 ECLOSE(1) = {1, 2, 3, 4, 6} ECLOSE({3, 5}) = ECLOSE(3) ∪ ECLOSE(5) = {3, 6} ∪ {5, 7} = {3, 5, 6, 7} 3 6  1 7 4   2  5 a b

25 25 2.5 Finite Automata with Epsilon- Transitions 2.5.4 Extended Transitions & Languages for  - NFA’s –Recursive definition of extended transition function : Basis: (q,  ) = ECLOSE(q). Basis: (q,  ) = ECLOSE(q). Induction: if w = xa, then (q, w) is computed as: Induction: if w = xa, then (q, w) is computed as: If (q, x) = {p 1, p 2, …, p k } and  (p i, a) = {r 1, r 2, …, r m }, then (q, w) = ECLOSE( {r 1, r 2, …, r m })= ECLOSE(  (p i, a)).

26 26 2.5 Finite Automata with Epsilon- Transitions 2.5.4 Extended Transitions & Languages for  - NFA’s –Induction: if w = xa, then (q, w) is computed as: If (q, x) = {p 1, p 2, …, p k } and  (p i, a) = {r 1, r 2, …, r m }, then (q, w) = ECLOSE( {r 1, r 2, …, r m }). … a (q, x)= { p 1 p 2. p k } x  a a { r 1 r 2. r m }  ECLOSE { r 1, r 2, …, r m }

27 27 2.5 Finite Automata with Epsilon- Transitions Example 2.20 ---Computing (q 0, 5.6) for  -NFA of Fig. 2.18. –Compute (q 0, 5)= (q 0,  5) first: – (q 0,  ) = ECLOSE(q 0 ) = {q 0, q 1 } –  (q 0, 5) ∪  (q 1, 5) =  ∪ {q 1, q 4 } = {q 1, q 4 } – (q 0, 5) = (q 0,  5) = ECLOSE(  (q 0, 5) ∪  (q 1, 5)) = ECLOSE({q 1, q 4 }) = E CLOSE({q 1 }) ∪ ECLOSE({q 4 }) = ECLOSE({q 1, q 4 }) = E CLOSE({q 1 }) ∪ ECLOSE({q 4 }) = {q 1, q 4 } = {q 1, q 4 } (complete the rest of computations by yourself!) - (complete the rest of computations by yourself!) - q5q5q5q5  0, 1, …, 9 start q0q0q0q0 , +, - q1q1q1q1 q2q2q2q2. 0, 1, …, 9 q3q3q3q3 q4q4q4q4. Figure 2.18

28 28 2.5 Finite Automata with Epsilon- Transitions 2.5.5 Eliminating  -Transitions –The  -transition is good for design of FA, but for implementation, they have to be eliminated. –Given an  -NFA, we can find an equivalent DFA (a theorem seen later). –Let E = (Q E, ,  E, q 0, F E ) be the given  -NFA, the equivalent DFA D = (Q D, ,  D, q D, F D ) is constructed as follows (in the next page).

29 29 2.5 Finite Automata with Epsilon- Transitions –Q D is the set of subsets of Q E, in which each accessible is an  -closed subset of Q E, i.e., are sets S  Q E such that S = ECLOSE(S). In other words, each  -closed set of states, S, includes those states such that any  -transition out of one of the states in S leads to a state that is also in S. In other words, each  -closed set of states, S, includes those states such that any  -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) –F D = {S | S  Q D and S ∩ F E  } (continued in the next page) (continued in the next page)

30 30 2.5 Finite Automata with Epsilon- Transitions –  D (S, a) is computed for each a in  and each S in Q D in the following way: Let S = {p 1, p 2,..., p k } Let S = {p 1, p 2,..., p k } Compute  (p i, a) and let this set be {r 1, r 2, …, r m } Compute  (p i, a) and let this set be {r 1, r 2, …, r m } Set  D (S, a) = ECLOSE({r 1, r 2, …, r m }) Set  D (S, a) = ECLOSE({r 1, r 2, …, r m }) = ECLOSE(  (p i, a)) = ECLOSE(  (p i, a)) –Technique to create accessible states in DFA D: starting from the start state q 0 of  -NFA E, generate ECLOSE(q 0 ) as start state q D of D; starting from the start state q 0 of  -NFA E, generate ECLOSE(q 0 ) as start state q D of D; from the generated states to derive other states. from the generated states to derive other states.

31 31 2.5 Finite Automata with Epsilon-Transitions Example 2.21: eliminate  -transitions of Fig. 2.18. Example 2.21: eliminate  -transitions of Fig. 2.18. – Start state q D = ECLOSE(q 0 ) = {q 0, q 1 } –  D ({q 0, q 1 }, +) = ECLOSE(  E (q 0, +) ∪  E (q 1, +)) = ECLOSE({q 1 } ∪  ) = ECLOSE({q 1 }) = {q 1 },... = ECLOSE({q 1 } ∪  ) = ECLOSE({q 1 }) = {q 1 },... –  D ({q 0, q 1 }, 0) = ECLOSE(  E (q 0, 0) ∪  E (q 1, 0)) = ECLOSE (  ∪ {q 1, q 4 }) = ECLOSE({q 1, q 4 }) = {q 1, q 4 },... = ECLOSE (  ∪ {q 1, q 4 }) = ECLOSE({q 1, q 4 }) = {q 1, q 4 },... –  D ({q 0, q 1 },.) = ECLOSE(  E (q 0,.) ∪  E (q 1,.)) = {q 2 } q5q5q5q5  0, 1, …, 9 start q0q0q0q0 , +, - q1q1q1q1 q2q2q2q2. 0, 1, …, 9 q3q3q3q3 q4q4q4q4. Figure 2.18

32 32 2.5 Finite Automata with Epsilon- Transitions Example 2.21: (cont’d) Example 2.21: (cont’d) +, - +, - {q1}{q1}{q1}{q1} start {q 0, q 1 } Figure 2.22 {q 1, q 4 } 0, 1, …, 9 {q2}{q2}{q2}{q2}.

33 33 2.5 Finite Automata with Epsilon- Transitions Example 2.21: (cont’d) Example 2.21: (cont’d) –  D ({q 1 }, 0) = ECLOSE(  E (q 1, 0)) = ECLOSE({q 1, q 4 }) = {q 1, q 4 }... = {q 1, q 4 }... –  D ({q 1 },.) = ECLOSE(  E (q 1,.)) = ECLOSE({q 2 }) = {q 2 } = {q 2 } q5q5q5q5  0, 1, …, 9 start q0q0q0q0 , +, - q1q1q1q1 q2q2q2q2. 0, 1, …, 9 q3q3q3q3 q4q4q4q4. Figure 2.18

34 34 2.5 Finite Automata with Epsilon- Transitions +, - +, - {q1}{q1}{q1}{q1} start {q 0, q 1 } {q 1, q 4 } 0, 1, …, 9 {q2}{q2}{q2}{q2}..

35 35 Example 2.21: (cont’d) Example 2.21: (cont’d) –  D ({q 1, q 4 }, 0) = ECLOSE(  E (q 1, 0) ∪  E (q 4, 0)) = ECLOSE({q 1, q 4 } ∪  ) = {q 1, q 4 }... = ECLOSE({q 1, q 4 } ∪  ) = {q 1, q 4 }... –  D ({q 1, q 4 },.) = ECLOSE(  E (q 1,.) ∪  E (q 4,.)) = ECLOSE({q 2 } ∪ {q 3 }) = ECLOSE(q 2 ) ∪ ECLOSE (q 3 ) ECLOSE({q 2 } ∪ {q 3 }) = ECLOSE(q 2 ) ∪ ECLOSE (q 3 ) = {q 2 } ∪ {q 3, q 5 } = {q 2, q 3, q 5 } = {q 2 } ∪ {q 3, q 5 } = {q 2, q 3, q 5 } q5q5q5q5  0, 1, …, 9 start q0q0q0q0 , +, - q1q1q1q1 q2q2q2q2. 0, 1, …, 9 q3q3q3q3 q4q4q4q4. Figure 2.18 2.5 Finite Automata with Epsilon-Transitions

36 36 0, 1, …, 9 2.5 Finite Automata with Epsilon- Transitions +, - +, - {q1}{q1}{q1}{q1} start {q 0, q 1 } {q 1, q 4 } 0, 1, …, 9 {q2}{q2}{q2}{q2}.. {q 2, q 3, q 5 }.

37 37 Example 2.21: (cont’d) Example 2.21: (cont’d) –  D ({q 2 }, 0) = ECLOSE(  E (q 2, 0)) = ECLOSE({q 3 }) = {q 3, q 5 } … –  D ({q 2, q 3, q 5 }, 0) = ECLOSE(  E (q 2, 0) ∪  E (q 3, 0) ∪  E (q 5, 0))=ECLOSE({q 3 } ∪ {q 3 } ∪  )=ECLOSE(q 3 )={q 3, q 5 } … q5q5q5q5  0, 1, …, 9 start q0q0q0q0 , +, - q1q1q1q1 q2q2q2q2. 0, 1, …, 9 q3q3q3q3 q4q4q4q4. Figure 2.18 2.5 Finite Automata with Epsilon-Transitions

38 38 2.5 Finite Automata with Epsilon- Transitions 0, 1, …, 9 +, - +, - {q1}{q1}{q1}{q1} start {q 0, q 1 } {q 1, q 4 } 0, 1, …, 9 {q2}{q2}{q2}{q2}.. {q 2, q 3, q 5 }. {q 3, q 5 } 0, 1, …9

39 39 Example 2.21: (cont’d) Example 2.21: (cont’d) –  D ({q 3, q 5 }, 0) = ECLOSE(  E (q 3, 0) ∪  E (q 5, 0)) =ECLOSE({q 3 } ∪  ) =ECLOSE(q 3 )={q 3, q 5 } … q5q5q5q5  0, 1, …, 9 start q0q0q0q0 , +, - q1q1q1q1 q2q2q2q2. 0, 1, …, 9 q3q3q3q3 q4q4q4q4. Figure 2.18 2.5 Finite Automata with Epsilon-Transitions

40 40 0, 1, …, 9 2.5 Finite Automata with Epsilon- Transitions 0, 1, …, 9 +, - +, - {q1}{q1}{q1}{q1} start {q 0, q 1 } {q 1, q 4 } 0, 1, …, 9 {q2}{q2}{q2}{q2}.. {q 2, q 3, q 5 }. {q 3, q 5 } 0, 1, …9

41 41 Example 2.21: (cont’d) Example 2.21: (cont’d) –The dead state  need be shown. –But according to Section 2.3.7a, the diagram in the last page may be regarded as deterministic. 0, 1, …, 9 +, - +, - {q1}{q1}{q1}{q1} start {q 0, q 1 } {q 1, q 4 } 0, 1, …, 9 {q2}{q2}{q2}{q2}.. {q 2, q 3, q 5 }. {q 3, q 5 } all symbols 0, 1, …9  2.5 Finite Automata with Epsilon-Transitions +, -,. +, - +, -,. +, -

42 42 2.5 Finite Automata with Epsilon- Transitions Theorem 2.22 –A language L accepted by some  -NFA if and only if L is accepted by some DFA. –Proof: see the textbook yourself.

43 43 2.6 A Review 3 Types of Automata – DFA --- good for soft/hardware implementation – NFA (  -NFA ) ---intermediately intuitive –  -NFA ---most intuitive  -NFA DFA equivalent


Download ppt "1 Chapter 2 Finite Automata (part b) Windmills in Holland."

Similar presentations


Ads by Google