Sei sulla pagina 1di 43

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
Techniques

Using inverted indexes


Using finite automata

Keyword
K
Inverted indexing

Page p2
Page pn

Applications unsuitable to use inverted indexing:


Document repository change rapidly.
Documents to be searched cannot be catalogued.

2.4 An Application: Text Search


2.4.2 NFAs for Text Search
(supplemental 1/3) A simple example --- find the
DFA equivalent to the following NFA which searches
words of x1 = ab and x2 = b in long text strings
a, b

a, b

a
1

Start

$20
4

a, b

$20
3
4

2.4 An Application: Text Search


2.4.2 NFAs for Text Search
(supplemental 2/3) A simple example (cond) --- find
the DFA equivalent to the following NFA (note: state
12 means state {1, 2}, etc.)
b

a
1
Start

12

$20
13

a
13
4
$20

1234

$20

$20
123
5

2.4 An Application: Text Search


2.4.2 NFAs for Text Search
(supplemental 3/3) A simple example (cond) --compare!!!

a, b

a
1

Start

$2
4
0

a
1

a, b

$2
3
0

a, b

a, b

Start

12

13
4
$20

a, b

$20
13

2.4 An Application: Text Search


2.4.2 NFAs for Text Search
Example 2.14: use an NFA to search two keywords
web and eBay among text

= set of all
printable ASCII
characters

$20
4

1
Start

Figure 2.16 (Note: there are two final states; red parts are
necessary and missing in the textbook; correct your textbook!!!)

$20
8

2.4 An Application: Text Search


2.4.2 NFAs for Text Search
Example 2.14 (contd): how to implement NFA?
Write a simulation program like Fig. 2.10
q0

Fig. 2.10

q0

q0

q1
Stuck!

q1

q0

q0

q0

q1
q2 Stuck!

q2
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 q0 is start state of NFA, then {q0} is start state of DFA
If p is a state of NFA reachable (accessible) from a path x = a1a2am,
then
one state of the DFA is a set of NFA states consisting of
1.
2.
3.

q0
p
every other NFA states reachable from q0 by following every subpath
which is a suffix ( ) of x, like ajaj+1am.

In the above way, all states in the DFA may be constructed,


and then all transitions derived (see the next page).

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

$2
4
0

1
e

Start

e
w

12

$2
8
0

b
146

135
3

Start

15

18

17

16
b

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 {q0, p1, p2, , pn},
and for each input symbol x, let the reached states be
{r1, r2, , rm}, then the next state is {q0, r1, r2, , rm}.
If no ri as above can be collected, then if the reached
states from the q0 by x are {s1, s2, , sk}, then the next
state is {q0, s1, s2, , 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 34, state 56,
so state 135146
(2) for x=e, state 3, state 5, but state 15,
so state 13515

12

135
3

146

e
1
Start

15

16

17

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 ( 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 EpsilonTransitions


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 FAs)
15

2.5 Finite Automata with EpsilonTransitions


Example 2.16: an -NFA accepting decimal
numbers like 2.15, .125, +1.4, -0.501
0, 1, , 9

0, 1, , 9

start

q0

, + , -

q1

q2

q3

q5

0, 1, , 9
Figure 2.18

0, 1, , 9

q4

To accept a number like +5. (nothing after the


decimal point), we have to add q4.

16

2.5 Finite Automata with EpsilonTransitions


Example 2.17: a more intuitive -NFA for
Example 2.14

$2
4

9
Start

Figure 2.19 Design an NFA with -transitions.

17

2.5 Finite Automata with EpsilonTransitions


2.5.2 Formal Notation for an -NFA
Definition: an -NFA A is denoted by A = (Q, , ,
q0, F) where the transition function takes as
arguments:
a state in Q, and
a member of {}
18

2.5 Finite Automata with EpsilonTransitions


2.5.2 Formal Notation for an -NFA
Notes:

cannot be used as an input symbol, but can be


accepted to yield a transition!

(qi, ) is defined for every state qi (getting into


the dead state if there is no next state with as
input symbol originally).
19

2.5 Finite Automata with EpsilonTransitions


2.5.2 Formal Notation for an -NFA
Example 2.18: the -NFA of Fig. 2.18 is described as
E = ({q0, q1, ..., q5}, {., +, , 0, 1, ..., 9}, , q0, {q5})
The transitions, e.g., include
(q0, ) = q1
(q1, ) =

See Fig. 2.20 for the complete transition table of E


(next page).
20

2.5 Finite Automata with EpsilonTransitions


2.5.2 Formal Notation for an -NFA
Example 2.18 (contd):
Fig. 2.20 --- the complete transition table of E.

q0
q1
q2
q3
q4
q5

0, 1, , 9

{q1}

{q5}

{q1}

{q2}

{q3}

{q1, q4}
{q3}
{q3}

21

2.5 Finite Automata with EpsilonTransitions


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 .

22

2.5 Finite Automata with EpsilonTransitions


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);
If p is in ECLOSE(q), then all states accessible from p through
paths of s are also in ECLOSE(q).
( states )

23

2.5 Finite Automata with EpsilonTransitions


2.5.3 Epsilon-Closures
-closure for a set of states S:
ECLOSE(S) = qS ECLOSE(q)
2

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}

24

2.5 Finite Automata with EpsilonTransitions


2.5.4 Extended Transitions & Languages for NFAs
Recursive definition of extended transition function

:
Basis: (q, ) = ECLOSE(q).
Induction: if w = xa, then (q, w) is computed as:
If (q, x) = {p1, p2, , pk} and
k

U (pi, a) = {r1, r2, , rm},

i1

U(pi, a)) .
then (q, w) = ECLOSE({r1, r2, , rm})=ECLOSE( i
1
25

2.5 Finite Automata with EpsilonTransitions


2.5.4 Extended Transitions & Languages for NFAs
Induction: if w = xa, then (q, w) is computed as:
k

If (q, x) = {p1, p2, , pk} and U (pi, a) = {r1, r2, , rm},


i1

then (q, w) = ECLOSE({r1, r2, , rm}).

(q, x)=

{ p1
p2
.
.
.
pk}

a
a
a

{ r1
r2
.
.
.
rm}

ECLOSE

{ r1,
r2, ,
rm}
26

2.5 Finite Automata with EpsilonTransitions


Example 2.20 ---Computing (q0, 5.6) for -NFA of Fig.
0, 1, , 9
0, 1, , 9
2.18.
start

q0
Figure 2.18

, + , -

q1
0, 1, , 9

q2

0, 1, , 9

q3

q5

q4

Compute (q0, 5)= (q0, 5) first:


(q0, ) = ECLOSE(q0) = {q0, q1}
(q0, 5)(q1, 5) = {q1, q4} = {q1, q4}

(q0, 5) = (q0, 5) = ECLOSE((q0, 5)(q1, 5))


= ECLOSE({q1, q4}) = ECLOSE({q1})ECLOSE({q4})
= {q1, q4}

27

2.5 Finite Automata with EpsilonTransitions


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 = (QE, , E, q0, FE) be the given -NFA, the
equivalent DFA D = (QD, , D, qD, FD) is
constructed as follows (in the next page).
28

2.5 Finite Automata with EpsilonTransitions


QD is the set of subsets of QE, in which each
accessible is an -closed subset of QE, i.e., are sets
S QE 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.
qD = ECLOSE(q0) (initial state of D)
FD = {S | SQD and SFE}
(continued in the next page)

29

2.5 Finite Automata with EpsilonTransitions


D(S, a) is computed for each a in and each S in
QD in the following way:
Let S = {p1, p2, ..., pk}
k

Compute U (pi, a) and let this set be {r1, r2, , rm}


i1
Set D(S, a) = ECLOSE({r1, r2, , rm})
k

U (pi, a))
= ECLOSE( i
1

Technique to create accessible states in DFA D:


starting from the start state q0 of -NFA E, generate
ECLOSE(q0) as start state qD of D;
30

2.5 Finite Automata with Epsilon-Transitions


Example 2.21: eliminate -transitions of Fig. 2.18.
0, 1, , 9

0, 1, , 9

start

q0

, + , -

q1

q2

0, 1, , 9

q4
Start state qD = ECLOSE(q0) = {q0, q1}
D({q0, q1}, +) = ECLOSE(E(q0, +)E(q1, +))
= ECLOSE({q1}) = ECLOSE({q1}) = {q1}, ...

0, 1, , 9

q3

q5

.
Figure 2.18

D({q0, q1}, 0) = ECLOSE(E(q0, 0)E(q1, 0))


= ECLOSE ({q1, q4}) = ECLOSE({q1, q4}) = {q1, q4}, ...
D({q0, q1}, .) = ECLOSE(E(q0, .)E(q1, .)) = {q2}

31

2.5 Finite Automata with EpsilonTransitions


Example 2.21: (contd)
0, 1, , 9
start

{q0,
q1}

+,-

{q1, q4}

{q1
}

{q2}

Figure 2.22
32

2.5 Finite Automata with EpsilonTransitions


Example 2.21: (contd)
0, 1, , 9

0, 1, , 9

start

q0

, + , -

q1

q2

0, 1, , 9

q3

q5

0, 1, , 9

q4

Figure 2.18

D({q1}, 0) = ECLOSE(E(q1, 0)) = ECLOSE({q1, q4})


= {q1, q4}...

D({q1}, .) = ECLOSE(E(q1, .)) = ECLOSE({q2})


= {q2 }

33

2.5 Finite Automata with EpsilonTransitions


0, 1, , 9
start

{q0,
q1}

+,-

{q1
}

{q1, q4}
0, 1, , 9

.
{q2}

34

2.5 Finite Automata with Epsilon-Transitions


Example 2.21: (contd)
0, 1, , 9

0, 1, , 9

start

q0

, + , -

q1

q2

0, 1, , 9

q3

q5

0, 1, , 9

q4

Figure 2.18

D({q1, q4}, 0) = ECLOSE(E(q1, 0)E(q4, 0))


= ECLOSE({q1, q4}) = {q1, q4}...
D({q1, q4}, .) = ECLOSE(E(q1, .)E(q4, .)) =
ECLOSE({q2}{q3}) = ECLOSE(q2)ECLOSE (q3)
= {q2}{q3, q5} = {q2, q3, q5}

35

2.5 Finite Automata with EpsilonTransitions


0, 1, , 9

0, 1, , 9
start

{q0,
q1}

+,-

{q1
}

{q1, q4}

{q2, q3, q5}

0, 1, , 9

.
{q2}

36

2.5 Finite Automata with Epsilon-Transitions


Example 2.21: (contd)
0, 1, , 9

0, 1, , 9

start

q0

, + , -

q1

q2

0, 1, , 9

q3

q5

0, 1, , 9

q4

Figure 2.18

D({q2}, 0) = ECLOSE(E(q2, 0)) = ECLOSE({q3}) = {q3, q5}


D({q2, q3, q5}, 0) = ECLOSE(E(q2, 0)E(q3, 0)
E(q5, 0))=ECLOSE({q3}{q3})=ECLOSE(q3)={q3, q5}
37

2.5 Finite Automata with EpsilonTransitions


0, 1, , 9

0, 1, , 9
start

{q0,
q1}

+,-

{q1
}

{q1, q4}

{q2, q3, q5}

0, 1, , 9
0, 1, 9

.
{q2}

0, 1, 9

{q3, q5}

38

2.5 Finite Automata with Epsilon-Transitions


Example 2.21: (contd)

0, 1, , 9

0, 1, , 9

start

q0

, + , -

q1

q2

0, 1, , 9

q3

q5

0, 1, , 9

q4

Figure 2.18

D({q3, q5}, 0) = ECLOSE(E(q3, 0)E(q5, 0))


=ECLOSE({q3}) =ECLOSE(q3)={q3, q5}

39

2.5 Finite Automata with EpsilonTransitions


0, 1, , 9

0, 1, , 9
start

{q0,
q1}

+,-

{q1
}

{q1, q4}

{q2, q3, q5}

0, 1, , 9
0, 1, 9

.
{q2}

0, 1, 9

{q3, q5}

0, 1, , 9
40

2.5 Finite Automata with Epsilon-Transitions


Example 2.21: (contd)
The dead state need be shown.

0, 1, , 9

0, 1, , 9
start

{q0,
q1}

+,-

{q1
}

{q1, q4}
0, 1, , 9 +, -

+, -

+, -, .
0, 1, 9
+, -, .

+, -, .

{q2}

{q2, q3, q5}

all symbols

0, 1, , 9

{q3, q5}

But according to Section 2.3.7a, the diagram in the last page


may
0, 1, ,
9
be regarded as deterministic.
41

2.5 Finite Automata with EpsilonTransitions


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.

42

2.6 A Review
3 Types of Automata
DFA
NFA ( -NFA )
-NFA

---good for soft/hardware implementation


---intermediately intuitive
---most intuitive

-NFA
equivalent

DFA

-NFA
43

Potrebbero piacerti anche