Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Nondeterministic finite automaton

From Wikipedia, the free encyclopedia
Type of finite-state machine in automata theory
NFA for(0|1)* 1 (0|1)3.
ADFA for thatlanguage has at least 16 states.

Inautomata theory, afinite-state machine is called adeterministic finite automaton (DFA), if

  • each of its transitions isuniquely determined by its source state and input symbol, and
  • reading an input symbol is required for each state transition.

Anondeterministic finite automaton (NFA), ornondeterministic finite-state machine, does not need to obey these restrictions. In particular, every DFA is also an NFA. Sometimes the termNFA is used in a narrower sense, referring to an NFA that isnot a DFA, but not in this article.

Using thesubset construction algorithm, each NFA can be translated to an equivalent DFA; i.e., a DFA recognizing the sameformal language.[1]Like DFAs, NFAs only recognizeregular languages.

NFAs were introduced in 1959 byMichael O. Rabin andDana Scott,[2] who also showed their equivalence to DFAs. NFAs are used in the implementation ofregular expressions:Thompson's construction is an algorithm for compiling a regular expression to an NFA that can efficiently perform pattern matching on strings. Conversely,Kleene's algorithm can be used to convert an NFA into a regular expression (whose size is generally exponential in the input automaton).

NFAs have been generalized in multiple ways, e.g.,nondeterministic finite automata with ε-moves,finite-state transducers,pushdown automata,alternating automata,ω-automata, andprobabilistic automata.Besides the DFAs, other known special cases of NFAsareunambiguous finite automata (UFA)andself-verifying finite automata (SVFA).

Informal introduction

[edit]

There are at least two equivalent ways to describe the behavior of an NFA. The first way makes use of thenondeterminism in the name of an NFA. For each input symbol, the NFA transitions to a new state until all input symbols have been consumed. In each step, the automaton nondeterministically "chooses" one of the applicable transitions. If there exists at least one "lucky run", i.e. some sequence of choices leading to an accepting state after completely consuming the input, it is accepted. Otherwise, i.e. if no choice sequence at all can consume all the input[3] and lead to an accepting state, the input is rejected.[4][5]: 319 [6]

In the second way, the NFA consumes a string of input symbols, one by one. In each step, whenever two or more transitions are applicable, it "clones" itself into appropriately many copies, each one following a different transition. If no transition is applicable, the current copy is in a dead end, and it "dies". If, after consuming the complete input, any of the copies is in an accept state, the input is accepted, else, it is rejected.[4][7][6]

Formal definition

[edit]

For a more elementary introduction of the formal definition, seeautomata theory.

Automaton

[edit]

AnNFA is represented formally by a 5-tuple,(Q,Σ,δ,q0,F){\displaystyle (Q,\Sigma ,\delta ,q_{0},F)}, consisting of

Here,P(Q){\displaystyle {\mathcal {P}}(Q)} denotes thepower set ofQ{\displaystyle Q}.

Recognized language

[edit]

Given an NFAM=(Q,Σ,δ,q0,F){\displaystyle M=(Q,\Sigma ,\delta ,q_{0},F)}, its recognized language is denoted byL(M){\displaystyle L(M)}, and is defined as the set of all strings over the alphabetΣ{\displaystyle \Sigma } that are accepted byM{\displaystyle M}.

Loosely corresponding to theabove informal explanations, there are several equivalent formal definitions of a stringw=a1a2...an{\displaystyle w=a_{1}a_{2}...a_{n}} being accepted byM{\displaystyle M}:

In words, the first condition says that the machine starts in the start stateq0{\displaystyle q_{0}}. The second condition says that given each character of stringw{\displaystyle w}, the machine will transition from state to state according to the transition functionδ{\displaystyle \delta }. The last condition says that the machine acceptsw{\displaystyle w} if the last input ofw{\displaystyle w} causes the machine to halt in one of the accepting states. In order forw{\displaystyle w} to be accepted byM{\displaystyle M}, it is not required that every state sequence ends in an accepting state, it is sufficient if one does. Otherwise,i.e. if it is impossible at all to get fromq0{\displaystyle q_{0}} to a state fromF{\displaystyle F} by followingw{\displaystyle w}, it is said that the automatonrejects the string. The set of stringsM{\displaystyle M} accepts is thelanguagerecognized byM{\displaystyle M} and this language is denoted byL(M){\displaystyle L(M)}.[5]: 320 [8]
In words,δ(r,x){\displaystyle \delta ^{*}(r,x)} is the set of all states reachable from stater{\displaystyle r} by consuming the stringx{\displaystyle x}. The stringw{\displaystyle w} is accepted if some accepting state inF{\displaystyle F} can be reached from the start stateq0{\displaystyle q_{0}} by consumingw{\displaystyle w}.[9][10]

Initial state

[edit]

The above automaton definition uses asingle initial state, which is not necessary. Sometimes, NFAs are defined with a set of initial states. There is an easy construction that translates an NFA with multiple initial states to an NFA with a single initial state, which provides a convenient notation.

Example

[edit]
Thestate diagram forM. It is not deterministic since in statep reading a 1 can lead top or toq.
All possible runs ofM on input string "10"
All possible runs ofM on input string "1011".
Arc label: input symbol,node label: state,green: start state,red: accepting state(s).
Input
State sequence
1011
1pq?
2pppq?
3ppppq

The following automatonM, with a binary alphabet, determines if the input ends with a 1.LetM=({p,q},{0,1},δ,p,{q}){\displaystyle M=(\{p,q\},\{0,1\},\delta ,p,\{q\})} wherethe transition functionδ{\displaystyle \delta } can be defined by thisstate transition table (cf. upper left picture):

StateInput01p{p}{p,q}q{\displaystyle {\begin{array}{|c|cc|}{\bcancel {{}_{\text{State}}\quad {}^{\text{Input}}}}&0&1\\\hline p&\{p\}&\{p,q\}\\q&\emptyset &\emptyset \end{array}}}

Since the setδ(p,1){\displaystyle \delta (p,1)} contains more than one state,M is nondeterministic.The language ofM can be described by theregular language given by theregular expression(0|1)*1.

All possible state sequences for the input string "1011" are shown in the lower picture.

The string is accepted byM since one state sequence satisfies the above definition; it does not matter that other sequences fail to do so.The picture can be interpreted in a couple of ways:

  • In terms of theabove "lucky-run" explanation, each path in the picture denotes a sequence of choices ofM.
  • In terms of the "cloning" explanation, each vertical column shows all clones ofM at a given point in time, multiple arrows emanating from a node indicate cloning, a node without emanating arrows indicating the "death" of a clone.

The feasibility to read the same picture in two ways also indicates the equivalence of both above explanations.

In contrast, the string "10" is rejected byM (all possible state sequences for that input are shown in the upper right picture), since there is no way to reach the only accepting state,q, by reading the final 0 symbol. Whileq can be reached after consuming the initial "1", this does not mean that the input "10" is accepted; rather, it means that an input string "1" would be accepted.

Equivalence to DFA

[edit]

Adeterministic finite automaton (DFA) can be seen as a special kind of NFA, in which for each state and symbol, the transition function has exactly one state. Thus, it is clear that everyformal language that can be recognized by a DFA can be recognized by an NFA.

Conversely, for each NFA, there is a DFA such that it recognizes the same formal language. The DFA can be constructed using thepowerset construction.

This result shows that NFAs, despite their additional flexibility, are unable to recognize languages that cannot be recognized by some DFA. It is also important in practice for converting easier-to-construct NFAs into more efficiently executable DFAs. However, if the NFA hasn states, the resulting DFA may have up to 2n states, which sometimes makes the construction impractical for large NFAs.

NFA with ε-moves

[edit]

Nondeterministic finite automaton with ε-moves (NFA-ε) is a further generalization to NFA. In this kind of automaton, the transition function is additionally defined on theempty string ε. A transition without consuming an input symbol is called an ε-transition and is represented in state diagrams by an arrow labeled "ε". ε-transitions provide a convenient way of modeling systems whose current states are not precisely known: i.e., if we are modeling a system and it is not clear whether the current state (after processing some input string) should be q or q', then we can add an ε-transition between these two states, thus putting the automaton in both states simultaneously.

Formal definition

[edit]

AnNFA-ε is represented formally by a 5-tuple,(Q,Σ,δ,q0,F){\displaystyle (Q,\Sigma ,\delta ,q_{0},F)}, consisting of

Here,P(Q){\displaystyle {\mathcal {P}}(Q)} denotes thepower set ofQ{\displaystyle Q} andε{\displaystyle \varepsilon } denotes empty string.

ε-closure of a state or set of states

[edit]

For a stateqQ{\displaystyle q\in Q}, letE(q){\displaystyle E(q)} denote the set of states that are reachable fromq{\displaystyle q} by following ε-transitions in the transition functionδ{\displaystyle \delta }, i.e.,pE(q){\displaystyle p\in E(q)} if there is a sequence of statesq1,...,qk{\displaystyle q_{1},...,q_{k}} such that

E(q){\displaystyle E(q)} is known as theepsilon closure, (alsoε-closure) ofq{\displaystyle q}.

The ε-closure of a setP{\displaystyle P} of states of an NFA is defined as the set of states reachable from any state inP{\displaystyle P} following ε-transitions. Formally, forPQ{\displaystyle P\subseteq Q}, defineE(P)=qPE(q){\displaystyle E(P)=\bigcup \limits _{q\in P}E(q)}.

Extended transition function

[edit]

Similar to NFA without ε-moves, the transition functionδ{\displaystyle \delta } of an NFA-ε can be extended to strings.Informally,δ(q,w){\displaystyle \delta ^{*}(q,w)} denotes the set of all states the automaton may have reached when starting in stateqQ{\displaystyle q\in Q} and reading the stringwΣ.{\displaystyle w\in \Sigma ^{*}.}The functionδ:Q×ΣP(Q){\displaystyle \delta ^{*}:Q\times \Sigma ^{*}\rightarrow {\mathcal {P}}(Q)} can be defined recursively as follows.

Informally: Reading the empty string may drive the automaton from stateq{\displaystyle q} to any state of the epsilon closure ofq.{\displaystyle q.}
Informally: Reading the stringw{\displaystyle w} may drive the automaton from stateq{\displaystyle q} to any stater{\displaystyle r} in the recursively computed setδ(q,w){\displaystyle \delta ^{*}(q,w)}; after that, reading the symbola{\displaystyle a} may drive it fromr{\displaystyle r} to any state in the epsilon closure ofδ(r,a).{\displaystyle \delta (r,a).}

The automaton is said to accept a stringw{\displaystyle w} if

δ(q0,w)F,{\displaystyle \delta ^{*}(q_{0},w)\cap F\neq \emptyset ,}

that is, if readingw{\displaystyle w} may drive the automaton from its start stateq0{\displaystyle q_{0}} to some accepting state inF.{\displaystyle F.}[11]

Example

[edit]
Thestate diagram forM

LetM{\displaystyle M} be a NFA-ε, with a binary alphabet, that determines if the input contains an even number of 0s or an even number of 1s. Note that 0 occurrences is an even number of occurrences as well.

In formal notation, letM=({S0,S1,S2,S3,S4},{0,1},δ,S0,{S1,S3}){\displaystyle M=(\{S_{0},S_{1},S_{2},S_{3},S_{4}\},\{0,1\},\delta ,S_{0},\{S_{1},S_{3}\})} wherethe transition relationδ{\displaystyle \delta } can be defined by thisstate transition table:

Input
State
01ε
S0{}{}{S1,S3}
S1{S2}{S1}{}
S2{S1}{S2}{}
S3{S3}{S4}{}
S4{S4}{S3}{}

M{\displaystyle M} can be viewed as the union of twoDFAs: one with states{S1,S2}{\displaystyle \{S_{1},S_{2}\}} and the other with states{S3,S4}{\displaystyle \{S_{3},S_{4}\}}. The language ofM{\displaystyle M} can be described by theregular language given by thisregular expression(10101)(01010){\displaystyle (1^{*}01^{*}01^{*})^{*}\cup (0^{*}10^{*}10^{*})^{*}}.We defineM{\displaystyle M} using ε-moves butM{\displaystyle M} can be defined without using ε-moves.

Equivalence to NFA

[edit]

To show NFA-ε is equivalent to NFA, first note that NFA is a special case of NFA-ε, so it remains to show for every NFA-ε, there exists an equivalent NFA.

Given an NFA with epsilon movesM=(Q,Σ,δ,q0,F),{\displaystyle M=(Q,\Sigma ,\delta ,q_{0},F),}define an NFAM=(Q,Σ,δ,q0,F),{\displaystyle M'=(Q,\Sigma ,\delta ',q_{0},F'),} where

F={F{q0} if E(q0)F{}F otherwise {\displaystyle F'={\begin{cases}F\cup \{q_{0}\}&{\text{ if }}E(q_{0})\cap F\neq \{\}\\F&{\text{ otherwise }}\\\end{cases}}}

and

δ(q,a)=δ(q,a){\displaystyle \delta '(q,a)=\delta ^{*}(q,a)} for each stateqQ{\displaystyle q\in Q} and each symbolaΣ,{\displaystyle a\in \Sigma ,} using the extended transition functionδ{\displaystyle \delta ^{*}} defined above.

One has to distinguish the transition functions ofM{\displaystyle M} andM,{\displaystyle M',} viz.δ{\displaystyle \delta } andδ,{\displaystyle \delta ',} and their extensions to strings,δ{\displaystyle \delta } andδ,{\displaystyle \delta '^{*},} respectively.By construction,M{\displaystyle M'} has no ε-transitions.

One can prove thatδ(q0,w)=δ(q0,w){\displaystyle \delta '^{*}(q_{0},w)=\delta ^{*}(q_{0},w)} for each stringwε{\displaystyle w\neq \varepsilon }, byinduction on the length ofw.{\displaystyle w.}

Based on this, one can show thatδ(q0,w)F{}{\displaystyle \delta '^{*}(q_{0},w)\cap F'\neq \{\}} if, and only if,δ(q0,w)F{},{\displaystyle \delta ^{*}(q_{0},w)\cap F\neq \{\},} for each stringwΣ:{\displaystyle w\in \Sigma ^{*}:}

Fromδ(q0,w)=δ(q0,w){\displaystyle \delta '^{*}(q_{0},w)=\delta ^{*}(q_{0},w)} andFF,{\displaystyle F\subseteq F',} we haveδ(q0,w)F{}δ(q0,w)F{};{\displaystyle \delta '^{*}(q_{0},w)\cap F'\neq \{\}\;\Leftarrow \;\delta ^{*}(q_{0},w)\cap F\neq \{\};} we still have to show the "{\displaystyle \Rightarrow }" direction.

Since NFA is equivalent to DFA, NFA-ε is also equivalent to DFA.

Closure properties

[edit]
Composed NFA accepting the union of the languages of some given NFAsN(s) andN(t). For an input stringw in the language union, the composed automaton follows an ε-transition fromq to the start state (left colored circle) of an appropriate subautomaton —N(s) orN(t) — which, by followingw, may reach an accepting state (right colored circle); from there, statef can be reached by another ε-transition. Due to the ε-transitions, the composed NFA is properly nondeterministic even if bothN(s) andN(t) were DFAs; vice versa, constructing a DFA for the union language (even of two DFAs) is much more complicated.

The set of languages recognized by NFAs isclosed under the following operations. These closure operations are used inThompson's construction algorithm, which constructs an NFA from anyregular expression. They can also be used to prove that NFAs recognize exactly theregular languages.

  • Union (cf. picture); that is, if the languageL1 is accepted by some NFAA1 andL2 by someA2, then an NFAAu can be constructed that accepts the languageL1L2.
  • Intersection; similarly, fromA1 andA2 an NFAAi can be constructed that acceptsL1L2.
  • Concatenation
  • Negation; similarly, fromA1 an NFAAn can be constructed that accepts Σ*\L1.
  • Kleene closure

Since NFAs are equivalent to nondeterministic finite automaton with ε-moves (NFA-ε), the above closures are proved using closure properties of NFA-ε.

Properties

[edit]

The machine starts in the specified initial state and reads in a string of symbols from itsalphabet. The automaton uses thestate transition function Δ to determine the next state using the current state, and the symbol just read or the empty string. However, "the next state of an NFA depends not only on the current input event, but also on an arbitrary number of subsequent input events. Until these subsequent events occur it is not possible to determine which state the machine is in".[13] If, when the automaton has finished reading, it is in an accepting state, the NFA is said to accept the string, otherwise it is said to reject the string.

The set of all strings accepted by an NFA is the language the NFA accepts. This language is aregular language.

For every NFA adeterministic finite automaton (DFA) can be found that accepts the same language. Therefore, it is possible to convert an existing NFA into a DFA for the purpose of implementing a (perhaps) simpler machine. This can be performed using thepowerset construction, which may lead to an exponential rise in the number of necessary states. For a formal proof of the powerset construction, please see thePowerset construction article.

Implementation

[edit]

There are many ways to implement a NFA:

  • Convert to the equivalent DFA. In some cases this may cause exponential blowup in the number of states.[14]
  • Keep aset data structure of all states which the NFA might currently be in. On the consumption of an input symbol,unite the results of the transition function applied to all current states to get the set of next states; if ε-moves are allowed, include all states reachable by such a move (ε-closure). Each step requires at mosts2 computations, wheres is the number of states of the NFA. On the consumption of the last input symbol, if one of the current states is a final state, the machine accepts the string. A string of lengthn can be processed in timeO(ns2),[15] and spaceO(s).
  • Create multiple copies. For eachn way decision, the NFA creates up ton−1 copies of the machine. Each will enter a separate state. If, upon consuming the last input symbol, at least one copy of the NFA is in the accepting state, the NFA will accept. (This, too, requires linear storage with respect to the number of NFA states, as there can be one machine for every NFA state.)
  • Explicitly propagate tokens through the transition structure of the NFA and match whenever a token reaches the final state. This is sometimes useful when the NFA should encode additional context about the events that triggered the transition. (For an implementation that uses this technique to keep track of object references have a look at Tracematches.)[16]

Complexity

[edit]
  • One can solve in linear time theemptiness problem for NFA, i.e., check whether the language of a given NFA is empty. To do this, we can simply perform adepth-first search from the initial state and check if some final state can be reached.
  • It isPSPACE-complete to test, given an NFA, whether it isuniversal, i.e., if there is a string that it does not accept.[17] As a consequence, the same is true of theinclusion problem, i.e., given two NFAs, is the language of one a subset of the language of the other.
  • Given as input an NFAA and an integer n, thecounting problem of determining how many words of lengthn are accepted byA is intractable; it is#P-hard. In fact, this problem is complete (underparsimonious reductions) for the complexity classSpanL.[18]

Application of NFA

[edit]

NFAs and DFAs are equivalent in that if a language is recognized by an NFA, it is also recognized by a DFA and vice versa. The establishment of such equivalence is important and useful. It is useful because constructing an NFA to recognize a given language is sometimes much easier than constructing a DFA for that language. It is important because NFAs can be used to reduce the complexity of the mathematical work required to establish many important properties in thetheory of computation. For example, it is much easier to proveclosure properties ofregular languages using NFAs than DFAs.

See also

[edit]

Notes

[edit]
  1. ^Martin, John (2010).Introduction to Languages and the Theory of Computation. McGraw Hill. p. 108.ISBN 978-0071289429.
  2. ^Rabin & Scott 1959.
  3. ^A choice sequence may lead into a "dead end" where no transition is applicable for the current input symbol; in this case it is considered unsuccessful.
  4. ^abHopcroft & Ullman 1979, pp. 19–20.
  5. ^abAlfred V. Aho and John E. Hopcroft and Jeffrey D. Ullman (1974).The Design and Analysis of Computer Algorithms. Reading/MA: Addison-Wesley.ISBN 0-201-00029-6.
  6. ^abHopcroft, Motwani & Ullman 2006, pp. 55–6.
  7. ^Sipser 1997, p. 48.
  8. ^Sipser 1997, p. 54.
  9. ^Hopcroft & Ullman 1979, p. 21.
  10. ^Hopcroft, Motwani & Ullman 2006, p. 59.
  11. ^Hopcroft & Ullman 1979, p. 25.
  12. ^Hopcroft & Ullman 1979, pp. 26–27.
  13. ^FOLDOC Free Online Dictionary of Computing,Finite-State Machine
  14. ^Chris Calabro (February 27, 2005)."NFA to DFA blowup"(PDF).cseweb.ucsd.edu. Retrieved6 March 2023.
  15. ^Hopcroft, Motwani & Ullman 2006, pp. 154–5.
  16. ^Allan, C., Avgustinov, P., Christensen, A. S., Hendren, L., Kuzins, S., Lhoták, O., de Moor, O., Sereni, D., Sittampalam, G., and Tibble, J. 2005.Adding trace matching with free variables to AspectJArchived 2009-09-18 at theWayback Machine. In Proceedings of the 20th Annual ACM SIGPLAN Conference on Object Oriented Programming, Systems, Languages, and Applications (San Diego, CA, USA, October 16–20, 2005). OOPSLA '05. ACM, New York, NY, 345-364.
  17. ^Historically shown in:Meyer, A. R.; Stockmeyer, L. J. (1972-10-25). "The equivalence problem for regular expressions with squaring requires exponential space".13th Annual Symposium on Switching and Automata Theory (Swat 1972). USA: IEEE Computer Society. pp. 125–129.doi:10.1109/SWAT.1972.29. For a modern presentation, see[1]
  18. ^Álvarez, Carme; Jenner, Birgit (1993-01-04)."A very hard log-space counting class".Theoretical Computer Science.107 (1):3–30.doi:10.1016/0304-3975(93)90252-O.ISSN 0304-3975.

References

[edit]
Each category of languages, except those marked by a*, is aproper subset of the category directly above it.Any language in each category is generated by a grammar and by an automaton in the category in the same line.
String metric
String-searching algorithm
Multiple string searching
Regular expression
Sequence alignment
Data structure
Other
Retrieved from "https://en.wikipedia.org/w/index.php?title=Nondeterministic_finite_automaton&oldid=1310539696"
Category:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp