Upload
elisa
View
49
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Chapter 2. Finite Automata (part a). Hokkaido, Japan. 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 - PowerPoint PPT Presentation
Citation preview
11
Chapter 2 Chapter 2 Finite AutomataFinite Automata
(part a)(part a)
Hokkaido, Japan
22
OutlineOutline
(part a --- in this PPT)(part a --- in this PPT)2.0 Introduction2.0 Introduction2.1 An Informal Picture of Finite Automata 2.1 An Informal Picture of Finite Automata 2.2 Deterministic Finite automata2.2 Deterministic Finite automata2.3 Nondeterministic Finite Automata2.3 Nondeterministic Finite Automata
(part b --- in another PPT)(part b --- in another PPT)2.4 An Application: Text Search2.4 An Application: Text Search2.5 Finite Automata with Epsilon-Transitions2.5 Finite Automata with Epsilon-Transitions
33
2.0 Introduction2.0 IntroductionTwo types of finite automata (FA):Two types of finite automata (FA):– Deterministic FA (DFA)Deterministic FA (DFA)– Nondeterministic FA (NFA)Nondeterministic FA (NFA)
Both types are of the same power, Both types are of the same power, – The former is easier for hardware and software The former is easier for hardware and software
implementationsimplementations..– The latter is more efficient for The latter is more efficient for descriptionsdescriptions of of
applications of FA.applications of FA.
44
2.1 An Informal Picture of Finite 2.1 An Informal Picture of Finite Automata Automata
A complete application example of A complete application example of
finite automata for protocol design finite automata for protocol design
and verificationand verification– Read by yourself!Read by yourself!
– Involving a concept of “product of two Involving a concept of “product of two
automata”automata”
55
2.2 Deterministic Finite Automata2.2 Deterministic Finite Automata
Recall the example of a vending machine Recall the example of a vending machine selling 20-dolllar food in Chapter 0selling 20-dolllar food in Chapter 0
$0StartStart
$5 $5
$10 $10
$5
$20
$15
$5
$10
$5
$10
$10$10
$20 transition transition diagramdiagram
66
2.2 Deterministic Finite Automata2.2 Deterministic Finite Automata
2.2.1 Definition of DFA2.2.1 Definition of DFA– A DFA A DFA AA consists of 5-tuples (1/2): consists of 5-tuples (1/2):
a finite (nonempty) set of states a finite (nonempty) set of states QQ;; a finite (nonempty) set of input symbols a finite (nonempty) set of input symbols ;; a (state) transition function a (state) transition function such that such that
qq, , aa) = ) = pp
meansmeans “ “automaton automaton AA, in state , in state qq, takes input , takes input aa and enters state and enters state pp;”;”
77
2.2 Deterministic Finite Automata2.2 Deterministic Finite Automata
2.2.1 Definition of DFA2.2.1 Definition of DFA– A DFA A DFA AA consists of 5-tuples (2/2): consists of 5-tuples (2/2):
a start state a start state qq00;;
a set of (nonempty) a set of (nonempty) finalfinal or or acceptingaccepting states states FF..
– AA may be written as a “5-tuple” may be written as a “5-tuple” AA = ( = (QQ, , , , , , qq00, , FF))..
88
2.2 Deterministic Finite Automata2.2 Deterministic Finite Automata
2.2.1 Definition of DFA2.2.1 Definition of DFA– graphic model for a DFAgraphic model for a DFA
0 11 0 1 0
有限
控制器
讀取頭
磁帶
…
圖 4.2 一部有限自動機的圖形模式。
0
tape
tape reader
finitecontrol
99
2.2 Deterministic Finite Automata2.2 Deterministic Finite Automata
2.2.2 How a DFA processes strings2.2.2 How a DFA processes strings
– Given an input string Given an input string xx = = aa11aa22……aann, if , if
((qq00, , aa11) = ) = qq11, , ((qq11, , aa22) = ) = qq22, …, , …,
qqii11, , aaii) = ) = qqii, ..., , ..., ((qqnn11, , aann) = ) = qqnn, ,
and and qqnn FF,,
then then xx is “ is “acceptedaccepted”; otherwise, “”; otherwise, “rejectedrejected.”.”
– Every transition is Every transition is deterministicdeterministic..
1010
2.2 Deterministic Finite automata2.2 Deterministic Finite automata
Example 2.1Example 2.1– Design an FA Design an FA AA to accept the language to accept the language
LL = { = {xx0101yy | | xx and and yy are any strings of 0’s and 1’s} are any strings of 0’s and 1’s}..
– Examples of strings in Examples of strings in LL::
0101, 11, 1101010, 1000, 10001011…1…
1111
2.2 Deterministic Finite automata2.2 Deterministic Finite automata
Example 2.1Example 2.1– Transitions: Transitions:
qq00, 1) = , 1) = qq00, , ((qq00, 0) = , 0) = qq22, , ((qq22, 1) = , 1) = qq11, ,
((qq22, 0) = , 0) = qq22, , qq11, 0) = , 0) = qq11, , qq11, 1) = , 1) = qq11
– 5-Tuple:5-Tuple:
AA = ({ = ({qq00, , qq11, , qq22}, {0, 1}, }, {0, 1}, , , qq00, {, {qq11})})
1212
2.2.3 Simpler Notations for DFA’s2.2.3 Simpler Notations for DFA’s
– Transition diagram ---Transition diagram ---
2.2 Deterministic Finite automata2.2 Deterministic Finite automata
1 0
0, 1q0StartStart q2 0 q1
1
1313
2.2.3 Simpler Notations for DFA’s2.2.3 Simpler Notations for DFA’s
– Transition table ---Transition table ---
: initial state; *: final state
2.2 Deterministic Finite automata2.2 Deterministic Finite automata
0 1
q0 q2 q0
*q1 q1 q1
q2 q2 q1
1414
2.2 Deterministic Finite automata2.2 Deterministic Finite automata
2.2.4 Extending transition function to strings2.2.4 Extending transition function to strings– Extended transition functionExtended transition function
If If xx = = aa11aa22……aann and and is such thatis such that
((pp, , aa11) = ) = qq11, , ((qq11, , aa22) = ) = qq22, …, , …,
qqii11, , aaii) = ) = qqii, ..., , ..., ((qqnn11, , aann) = ) = qq,,
then we define to bethen we define to be
((pp, , xx) = ) = qq..
1515
2.2 Deterministic Finite automata2.2 Deterministic Finite automata
2.2.4 Extending Transition Function to String2.2.4 Extending Transition Function to Stringss– Also may be defined Also may be defined recursively recursively as in the textbook.as in the textbook.– Recursive definition for Recursive definition for
BasisBasis: (: (qq, , ) = ) = qq. .
InductionInduction: if : if ww = = xaxa ( (aa is the last symbol of is the last symbol of ww), then ), then
((qq, , ww) = ) = ( (( (qq, , xx), ), aa).).
1616
2.2 Deterministic Finite automata2.2 Deterministic Finite automata2.2.4 Extending Transition Function to Str2.2.4 Extending Transition Function to Stringsings
A graphic diagram for the following concept:A graphic diagram for the following concept:
InductionInduction: :
if if ww = = xaxa ( (aa is the last symbol of is the last symbol of ww), then ), then
((qq, , ww) = ) = ( (( (qq, , xx), ), aa).).
0, 1…… q1 a (q, x) x
(q, w=xa)
1717
2.2 Deterministic Finite automata2.2 Deterministic Finite automata
2.2.5 The Language of a DFA2.2.5 The Language of a DFA– The language of a DFA The language of a DFA AA is defined as is defined as
LL((AA) = {) = {ww | ( | (qq00, , ww) is in ) is in FF}.}.
– If If LL is is LL((AA) for some DFA ) for some DFA AA, then we say , then we say LL is a is a
regular languageregular language. .
1818
0, 1
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
2.3.1 An informal view of NFA’s2.3.1 An informal view of NFA’s– Review of a previous example of DFA (of ExamplReview of a previous example of DFA (of Exampl
e 2.1)e 2.1)
– An NFA version of above DFA (An NFA version of above DFA (more intuitivemore intuitive!)!)
q0StartStart q2
0$20 q1
1
1 0
0, 1
q0StartStart q2
0$20 q1
1 0, 1
1919
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
Some properties of NFA’sSome properties of NFA’s
– Some transitions may “Some transitions may “diedie,” like ,” like qq22, 0), 0)..
– Some transitions have Some transitions have multiplemultiple choices, like choices, like
((qq00, 0) = , 0) = qq00 and and qq22..
q0StartStart q2
0$20 q1
1
0, 1
0, 1
2020
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
Example 2.6 Example 2.6 – Design an NFA accepting the following languageDesign an NFA accepting the following language
LL = { = {ww | | ww{0, 1}* and ends in 01}.{0, 1}* and ends in 01}.
– NondeterminismNondeterminism creates many transition paths, but creates many transition paths, but if there is if there is one path leading to a final stateone path leading to a final state, then the , then the input is accepted.input is accepted.
q0StartStart q1
0$20 q2
1
0, 1
2121
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
Example 2.6 (cont’d)Example 2.6 (cont’d)
– When input When input xx = 00101, the NFA processes = 00101, the NFA processes xx in the in the following way:following way:
0 0 1 0 10 0 1 0 1
q0StartStart q1
0$20 q2
1
0, 1
q0
q1
Stuck!
q0
q1
q2 Stuck!
q0 q0
q1
q2Accept!
q0 q0q0 q0 q0
q1
q2
Fig. 2.10
2222
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
2.3.2 Definition of NFA2.3.2 Definition of NFA
– An NFA An NFA AA is a 5-tuple is a 5-tuple AA = ( = (QQ, , , , , , qq00, , FF)) where where
Q = Q = a finite (nonempty) set of states;a finite (nonempty) set of states;
a finite (nonempty) set of input symbols;a finite (nonempty) set of input symbols;
qq00 = a start state; = a start state;
F = F = a set of (nonempty) final or accepting states;a set of (nonempty) final or accepting states;
2323
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
2.3.2 Definition of NFA2.3.2 Definition of NFA– An NFA An NFA AA is a 5-tuple is a 5-tuple AA = ( = (QQ, , , , , , qq00, , FF)) where where
a (state) transition functiona (state) transition functionsuch that such that
qq, , aa) = ) = {{pp11, , pp22, …, , …, ppmm} }
which which meansmeans
“ “automaton automaton AA, in state , in state qq, takes input , takes input aa and enters and enters one of the states one of the states pp11, , pp22, …, , …, ppmm.”.”
2424
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
Example 2.7 Example 2.7 – Transition table of NFA of the last example ---Transition table of NFA of the last example ---
q0StartStart q1
0$20 q2
1
0, 1
0 1 q0 {q0,
q1}{q0}
q1 {q2}
*q2
2525
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
2.3.3 Extended Transition Function2.3.3 Extended Transition Function– Recursive definition of Recursive definition of (with string as input)(with string as input)
BasisBasis: (: (qq, , ) = {) = {qq}. }.
InductionInduction: if : if ww = = xaxa ( (aa is the last symbol of is the last symbol of ww), ), ((qq, , xx) = {) = {pp11, , pp22, …, , …, ppkk},}, and and
((ppii, , aa) = {) = {rr11, , rr22, …, , …, rrmm}}
then then ((qq, , ww) = {) = {rr11, , rr22, …, , …, rrmm}.}.
1
k
iU
2626
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite Automata Automata (supplemental)(supplemental)
2.3.3 Extended Transition Function2.3.3 Extended Transition Function– A graphic diagram for the following concept:A graphic diagram for the following concept:
InductionInduction: if : if ww = = xaxa ( (aa is the last symbol of is the last symbol of ww), ), ((qq, , xx) = {) = {pp11, , pp22, …, , …, ppkk},}, and and ((ppii, , aa) = {) = {rr11, , rr22, …, , …, rrmm}}
then then ((qq, , ww) = {) = {rr11, , rr22, …, , …, rrmm}.}.
……
a (q, x)= { p1 p2 . . . pk}
x
a
a
(q, w=xa) = {r1 r2 . . . rm}
2727
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
Example 2.8 ---Example 2.8 ---– For the input For the input ww = 00101, we have = 00101, we have
1.1. ((qq00, , ) = {) = {qq00}.}.
2. (2. (qq00, , 0) = 0) = ((qq00, , 00) = {) = {qq00, , qq11}.}.
3. (3. (qq00, , 0000) = ) = ((qq00, , 0)0)∪∪((qq11, , 00)) = {= {qq00, , qq11}}∪∪{{qq00, , qq11}}
… …
q0StartStart q1
0$20 q2
1
0, 1
2828
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
2.3.4 The Language of an NFA2.3.4 The Language of an NFA– Definition ---Definition ---
If If AA = ( = (QQ, , , , , , qq00, , FF)) is an NFA, then the language is an NFA, then the language
accepted by accepted by AA is is
LL((AA) = {) = {ww | ( | (qq00, , ww))∩∩FF }.}.
– That is, as long as a final state is reached, which is That is, as long as a final state is reached, which is among possibly many, the input is accepted.among possibly many, the input is accepted.
2929
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
2.3.5 Equivalence of DFA and NFA2.3.5 Equivalence of DFA and NFA
– NFA’s are more intuitive to construct for many lanNFA’s are more intuitive to construct for many lan
guages.guages.
– DFA’s are easier for use in software and hardware DFA’s are easier for use in software and hardware
implementations.implementations.
3030
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
2.3.5 Equivalence of DFA and NFA2.3.5 Equivalence of DFA and NFA– Every NFA Every NFA NN has an has an equivalentequivalent DFA DFA DD, ,
i.e.,i.e., LL((DD) = ) = LL((NN).).
((DefinitionDefinition: “Two models are said : “Two models are said to be to be equivalentequivalent
if they have identical languages.”)if they have identical languages.”)
– The proof needs “The proof needs “subset constructionsubset construction.”.”
3131
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
2.3.5 Equivalence of DFA and NFA2.3.5 Equivalence of DFA and NFA– Subset constructionSubset construction: each state of DFA is a : each state of DFA is a subsetsubset
of NFAof NFA
– Given an NFA Given an NFA NN = ( = (QQNN, , , , , , qq00, , FFNN),), we construc we construc
t a DFA t a DFA DD = ( = (QQDD, , , , DD, , qq00'', , FFDD) such that:) such that:
The two alphabets are identical;The two alphabets are identical;
QQDD is the power set of is the power set of QQN N (i.e., set of all subsets of (i.e., set of all subsets of QQNN););
InaccessibleInaccessible states in states in QQDD should be deleted; should be deleted;
(continued in the next page)(continued in the next page)
3232
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
2.3.5 Equivalence of DFA and NFA2.3.5 Equivalence of DFA and NFA– (cont’d)(cont’d)
qq00'' = { = {qq00}};;
Each subset Each subset SS of of QQN N is in final state set is in final state set FFDD if if SS in includes at least one accepting state in cludes at least one accepting state in FFNN (i.e., (i.e., SS∩∩FFNN ). ).
For each subset For each subset SS of of QQN N and for each input symand for each input symbol bol aa, define , define
DD((S, aS, a)) = = NN((p, ap, a))p SU
3333
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
Example 2.10 ---Example 2.10 ---– For the NFA of Example 2.6 below:For the NFA of Example 2.6 below:
– The power set of The power set of QQN N = = {{qq00,, q q11, , qq22} is } is
QQDD == , {, {qq00}, {}, {qq11}, {}, {qq22}, {}, {qq00, , qq11}, {}, {qq00, , qq22}, },
{{qq11, , qq22}, {}, {qq00,, q q11, , qq22}}}}..
q0StartStart q1
0$20 q2
1
0, 1
3434
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
Example 2.10 (cont’d)Example 2.10 (cont’d)
– The transition table isThe transition table is
q0StartStart q1
0$20 q2
1
0, 1
0 1
{q0}
{q1}*{q2}
{q0, q1}*{q0, q2}*{q1, q2}
*{q0, q1, q2}
{q0, q1}
{q0, q1}{q0, q1}
{q0, q1}
{q0}{q2}
{q0, q2}{q0}{q2}
{q0, q2}
3535
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
Example 2.10 (cont’d)Example 2.10 (cont’d)– Change names of the states, we getChange names of the states, we get
0 1A
BC
*DE
*F*G*H
AEAAEEAE
ABDAFBDF
3636
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
Example 2.10 (cont’d)Example 2.10 (cont’d)– Checking Checking accessibleaccessible states from the start state as states from the start state as
redred ones; the others are inaccessible. ones; the others are inaccessible.
0 1A
BC
*DE
*F*G*H
AEAAEEAE
ABDAFBDF
Figure 2.13
3737
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
Example 2.10 (cont’d) Example 2.10 (cont’d) (supplemental)(supplemental)– solved by “solved by “Lazy evaluationLazy evaluation” using table: ” using table:
starting from the start state, only accessible states are evaluated, in order to avoid generation of inaccessible states.
– The transition table isThe transition table is
q0StartStart q1
0$20 q2
1
0, 1
0 1
{q0}
{q1}*{q2}
{q0, q1}*{q0, q2}*{q1, q2}
*{q0, q1, q2}
{q0, q1}
{q0, q1}{q0, q1}
{q0, q1}
{q0}{q2}
{q0, q2}{q0}{q2}
{q0, q2}
0 1
{q0}
{q1}*{q2}
{q0, q1}*{q0, q2}*{q1, q2}
*{q0, q1, q2}
{q0, q1}
{q0, q1}{q0, q1}
{q0, q1}
{q0}{q2}
{q0, q2}{q0}{q2}
{q0, q2}
0 1
{q0}
{q1}*{q2}
{q0, q1}*{q0, q2}*{q1, q2}
*{q0, q1, q2}
{q0, q1}
{q0, q1}{q0, q1}
{q0, q1}
{q0}{q2}
{q0, q2}{q0}{q2}
{q0, q2}
3838
1
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
{q0}StartStart
0
0 1{q0, q1}
{q0, q2}
0
1
Example 2.10 (cont’d) --- Example 2.10 (cont’d) --- – solved by “solved by “Lazy evaluationLazy evaluation” using diagram: ” using diagram: starting from the start state, only accessible states are
evaluated, in order to avoid generation of inaccessible states.
Figure 2.14
3939
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
Theorem 2.11Theorem 2.11
If DFA If DFA DD = ( = (QQDD, , , , DD, {, {qq00}, }, FFDD) is constructed fr) is constructed fr
omom NFA NFA NN = ( = (QQNN, , , , , , qq00, , FFNN)) by subset constr by subset constr
uction, then uction, then LL((DD) = ) = LL((NN).).
– Proof. See the textbook.Proof. See the textbook.
4040
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
Theorem 2.12 Theorem 2.12 (equivalence of DFA and (equivalence of DFA and
NFA):NFA):
A language A language LL is accepted by some DFA is accepted by some DFA if and only ifif and only if
LL is accepted by some NFA. is accepted by some NFA.
– Proof. See the textbook. Proof. See the textbook.
4141
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
2.3.6 A Bad Case of Subset Construction2.3.6 A Bad Case of Subset Construction
– The DFA constructed from an NFA usually has The DFA constructed from an NFA usually has the the samesame number of accessible states of the NFA. number of accessible states of the NFA.
– But the worse case is But the worse case is mm = = 22nn
wherewherem m is the number of states in the DFA.is the number of states in the DFA.nn is the number of states in the NFA. is the number of states in the NFA.
4242
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
2.3.7a Regarding NFA’s as DFA’s2.3.7a Regarding NFA’s as DFA’s– In each state of a DFA, for each input there In each state of a DFA, for each input there mustmust bb
e ae a next state. next state.
– In an NFA, for a certain input there In an NFA, for a certain input there mightmight bebe nono ne next state. So the following is an NFA. xt state. So the following is an NFA.
startstart t t h th e the n then
4343
2.3 Nondeterministic Finite 2.3 Nondeterministic Finite AutomataAutomata
2.3.7a Regarding NFA’s as DFA’s2.3.7a Regarding NFA’s as DFA’s– But it is But it is deterministicdeterministic in nature --- either getting into in nature --- either getting into
a next state or becoming “a next state or becoming “deaddead” (getting into a “dea” (getting into a “dead” state).d” state).
– Such a kind of NFA has Such a kind of NFA has at most one transition out oat most one transition out of any state on any symbolf any state on any symbol, and may be regarded as a , and may be regarded as a DFA.DFA.
startstart t t h th e the n then