Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Nondeterministic finite automaton
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Type of finite-state machine in automata theory}} [[File:Relatively small NFA.svg|100px|thumb|NFA for [[regular expression|(0<nowiki>|</nowiki>1)<sup>*</sup> 1 (0<nowiki>|</nowiki>1)<sup>3</sup>]].<br />A [[deterministic finite automaton|DFA]] for that [[formal language|language]] has at least 16 states.]] In [[automata theory]], a [[finite-state machine]] is called a [[deterministic finite automaton]] (DFA), if * each of its transitions is ''uniquely'' determined by its source state and input symbol, and * reading an input symbol is required for each state transition. A '''nondeterministic finite automaton''' ('''NFA'''), or '''nondeterministic finite-state machine''', does not need to obey these restrictions. In particular, every DFA is also an NFA. Sometimes the term '''NFA''' is used in a narrower sense, referring to an NFA that is ''not'' a DFA, but not in this article. Using the [[subset construction algorithm]], each NFA can be translated to an equivalent DFA; i.e., a DFA recognizing the same [[formal language]].<ref>{{cite book |title=Introduction to Languages and the Theory of Computation |last1=Martin |first1=John |year=2010 |page=108 |publisher=McGraw Hill |isbn= 978-0071289429}}</ref> Like DFAs, NFAs only recognize [[regular language]]s. NFAs were introduced in 1959 by [[Michael O. Rabin]] and [[Dana Scott]],{{sfn|Rabin|Scott|1959}} who also showed their equivalence to DFAs. NFAs are used in the implementation of [[regular expression]]s: [[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., [[#NFA with ε-moves|nondeterministic finite automata with ε-moves]], [[finite-state transducer]]s, [[pushdown automaton|pushdown automata]], [[Alternating finite automaton|alternating automata]], [[ω-automaton|ω-automata]], and [[probabilistic automaton|probabilistic automata]]. Besides the DFAs, other known special cases of NFAs are [[unambiguous finite automaton|unambiguous finite automata]] (UFA) and [[self-verifying finite automaton|self-verifying finite automata]] (SVFA). ==Informal introduction== There are at least two equivalent ways to describe the behavior of an NFA. The first way makes use of the [[Nondeterministic algorithm|nondeterminism]] 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<ref>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.</ref> and lead to an accepting state, the input is rejected.{{sfn|Hopcroft|Ullman|1979|pp=19-20}}<ref name="Aho.Hopcroft.Ullman.1974">{{cite book | isbn=0-201-00029-6 | author=Alfred V. Aho and John E. Hopcroft and Jeffrey D. Ullman | title=The Design and Analysis of Computer Algorithms | url=https://archive.org/details/designanalysisof00ahoarich | url-access=registration | location=Reading/MA | publisher=Addison-Wesley | year=1974 }}</ref>{{rp|319}}{{sfn|Hopcroft|Motwani|Ullman|2006|pp=55-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.{{sfn|Hopcroft|Ullman|1979|pp=19–20}}<!---not an error: Hopcroft and Ullman use both informal explanations--->{{sfn|Sipser|1997|p=48}}{{sfn|Hopcroft|Motwani|Ullman|2006|pp=55-6}} ==Formal definition== For a more elementary introduction of the formal definition, see [[automata theory]]. ===Automaton=== An ''NFA'' is represented formally by a 5-[[tuple]], <math>(Q, \Sigma, \delta, q_0, F)</math>, consisting of * a finite [[Set (mathematics)|set]] of [[State (computer science)|states]] <math>Q</math>, * a finite set of input symbols called the [[Alphabet (computer science)|alphabet]] <math>\Sigma</math>, * a transition [[function (mathematics)|function]] <math>\delta</math> : <math>Q\times \Sigma \rightarrow \mathcal{P}(Q)</math>, * an initial (or start) state <math>q_0 \in Q</math>, and * a set of accepting (or final) states <math>F \subseteq Q</math>. Here, <math>\mathcal{P}(Q)</math> denotes the [[power set]] of <math>Q</math>. ===Recognized language=== Given an NFA <math>M = (Q, \Sigma, \delta, q_0, F)</math>, its recognized language is denoted by <math>L(M)</math>, and is defined as the set of all strings over the alphabet <math>\Sigma</math> that are accepted by <math>M</math>. Loosely corresponding to the [[#Informal introduction|above]] informal explanations, there are several equivalent formal definitions of a string <math>w = a_1 a_2 ... a_n</math> being accepted by <math>M</math>: *<math>w</math> is accepted if a sequence of states, <math>r_0, r_1, ..., r_n</math>, exists in <math>Q</math> such that: *# <math>r_0 = q_0</math> *# <math>r_{i+1} \in \delta (r_i, a_{i+1})</math>, for <math>i = 0, \ldots, n-1</math> *# <math>r_n \in F</math>. :In words, the first condition says that the machine starts in the start state <math>q_0</math>. The second condition says that given each character of string <math>w</math>, the machine will transition from state to state according to the transition function <math>\delta</math>. The last condition says that the machine accepts <math>w</math> if the last input of <math>w</math> causes the machine to halt in one of the accepting states. In order for <math>w</math> to be accepted by <math>M</math>, 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 from <math>q_0</math> to a state from <math>F</math> by following <math>w</math>, it is said that the automaton ''rejects'' the string. The set of strings <math>M</math> accepts is the [[Formal language|language]] ''recognized'' by <math>M</math> and this language is denoted by <math>L(M)</math>.<ref name="Aho.Hopcroft.Ullman.1974"/>{{rp|320}}<!---using "deduction" relation "\vdash"--->{{sfn|Sipser|1997|p=54}} *Alternatively, <math>w</math> is accepted if <math>\delta^*(q_0, w) \cap F \not = \emptyset</math>, where <math>\delta^*: Q \times \Sigma^* \rightarrow \mathcal{P}(Q)</math> is defined [[recursion (computer science)|recursively]] by: *# <math>\delta^*(r, \epsilon) = \{r\}</math> where <math>\epsilon</math> is the empty string, and *# <math>\delta^*(r, xa)= \bigcup_{r' \in \delta^*(r, x)} \delta(r', a)</math> for all <math>x \in \Sigma^*, a \in \Sigma</math>. :In words, <math>\delta^*(r, x)</math> is the set of all states reachable from state <math>r</math> by consuming the string <math>x</math>. The string <math>w</math> is accepted if some accepting state in <math>F</math> can be reached from the start state <math>q_0</math> by consuming <math>w</math>.{{sfn|Hopcroft|Ullman|1979|p=21}}{{sfn|Hopcroft|Motwani|Ullman|2006|p=59}} ===Initial state=== The above automaton definition uses a ''single 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== {| style="float: right; border-spacing: 0;" |- | VALIGN=TOP | [[File:NFASimpleExample.svg|thumb|250px|The [[state diagram]] for ''M''. It is not deterministic since in state ''p'' reading a 1 can lead to ''p'' or to ''q''.]] | VALIGN=TOP | [[File:NFASimpleExample Runs10.gif|thumb|250px|All possible runs of ''M'' on input string "10"]] |- | COLSPAN=2 | [[File:NFASimpleExample Runs1011.gif|thumb|530px|All possible runs of ''M'' on input string "1011".<br />''Arc label:'' input symbol, ''node label'': state, ''{{color|#00c000|green}}:'' start state, ''{{color|#c00000|red}}:'' accepting state(s). {| class="wikitable" |- ! {{diagonal split header|State sequence|Input}} ! || 1 || || 0 || || 1 || || 1 || |- | 1 || ''p'' || || ''q'' || ||'''?'''|| || || || |- | 2 || ''p'' || || ''p'' || || ''p'' || || ''q'' || || '''?''' |- | 3 || ''p'' || || ''p'' || || ''p'' || || ''p'' || || ''q'' |} ]] |} The following automaton {{mvar|M}}, with a binary alphabet, determines if the input ends with a 1. Let <math>M = (\{p, q\}, \{0, 1\}, \delta, p, \{q\})</math> where the transition function <math>\delta</math> can be defined by this [[state transition table]] (cf. upper left picture): :<math>\begin{array}{|c|cc|} \bcancel{{}_\text{State}\quad {}^\text{Input}} & 0 & 1 \\ \hline p & \{p\} & \{p,q\} \\ q & \emptyset & \emptyset \end{array}</math> Since the set <math>\delta(p,1)</math> contains more than one state, {{mvar|M}} is nondeterministic. The language of {{mvar|M}} can be described by the [[regular language]] given by the [[regular expression]] <code>(0|1)*1</code>. All possible state sequences for the input string "1011" are shown in the lower picture. The string is accepted by {{mvar|M}} 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 the [[#Informal introduction|above]] "lucky-run" explanation, each path in the picture denotes a sequence of choices of {{mvar|M}}. * In terms of the "cloning" explanation, each vertical column shows all clones of {{mvar|M}} 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. * Considering the first of the [[#Recognized language|above]] formal definitions, "1011" is accepted since when reading it {{mvar|M}} may traverse the state sequence <math>\langle r_0,r_1,r_2,r_3,r_4 \rangle = \langle p, p, p, p, q \rangle</math>, which satisfies conditions 1 to 3. * Concerning the second formal definition, bottom-up computation shows that <math>\delta^*(p,\epsilon) = \{ p \}</math>, hence <math>\delta^*(p,1) = \delta(p,1) = \{ p,q \}</math>, hence <math>\delta^*(p,10) = \delta(p,0) \cup \delta(q,0) = \{ p \} \cup \{\}</math>, hence <math>\delta^*(p,101) = \delta(p,1) = \{ p,q \}</math>, and hence <math>\delta^*(p,1011) = \delta(p,1) \cup \delta(q,1) = \{ p,q \} \cup \{\}</math>; since that set is not disjoint from <math>\{ q \}</math>, the string "1011" is accepted. In contrast, the string "10" is rejected by {{mvar|M}} (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, {{mvar|q}}, by reading the final 0 symbol. While {{mvar|q}} 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== A [[deterministic 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 every [[formal 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 the [[powerset 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 has ''n'' states, the resulting DFA may have up to 2<sup>''n''</sup> states, which sometimes makes the construction impractical for large NFAs. ==NFA with ε-moves== Nondeterministic finite automaton with ε-moves (NFA-ε) is a further generalization to NFA. In this kind of automaton, the transition function is additionally defined on the [[empty 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=== An ''NFA-ε'' is represented formally by a 5-[[tuple]], <math>(Q, \Sigma, \delta, q_0, F)</math>, consisting of * a finite [[Set (mathematics)|set]] of [[State (computer science)|states]] <math>Q</math> * a finite set of [[input symbol]]s called the [[Alphabet (computer science)|alphabet]] <math>\Sigma</math> * a transition [[Function (mathematics)|function]] <math>\delta : Q \times (\Sigma \cup \{\epsilon\}) \rightarrow \mathcal{P}(Q)</math> * an ''initial'' (or [[Finite-state machine#Start state|''start'']]) state <math>q_0 \in Q</math> * a set of states <math>F</math> distinguished as [[Finite-state machine#Accept .28or final.29 states|''accepting'' (or ''final'') ''states'']] <math>F \subseteq Q</math>. Here, <math>\mathcal{P}(Q)</math> denotes the [[power set]] of <math>Q</math> and <math>\epsilon</math> denotes empty string. ===ε-closure of a state or set of states=== For a state <math>q \in Q</math>, let <math>E(q)</math> denote the set of states that are reachable from <math>q</math> by following ε-transitions in the transition function <math>\delta</math>, i.e., <math>p \in E(q)</math> if there is a sequence of states <math>q_1,..., q_k</math> such that * <math>q_1 = q</math>, * <math>q_{i+1} \in \delta(q_i, \varepsilon)</math> for each <math>1 \le i < k</math>, and * <math>q_k = p</math>. <math>E(q)</math> is known as the '''epsilon closure''', (also '''ε-closure''') of <math>q</math>. The ε-closure of a set <math>P</math> of states of an NFA is defined as the set of states reachable from any state in <math>P</math> following ε-transitions. Formally, for <math>P \subseteq Q</math>, define <math>E(P) = \bigcup\limits_{q\in P} E(q)</math>. ===Extended transition function=== Similar to NFA without ε-moves, the transition function <math>\delta</math> of an NFA-ε can be extended to strings. Informally, <math>\delta^*(q,w)</math> denotes the set of all states the automaton may have reached when starting in state <math>q \in Q</math> and reading the string <math>w \in \Sigma^* .</math> The function <math>\delta^*: Q \times \Sigma^* \rightarrow \mathcal{P}(Q)</math> can be defined recursively as follows. * <math>\delta^*(q,\varepsilon) = E(q)</math>, for each state <math>q \in Q ,</math> and where <math>E</math> denotes the epsilon closure; :''Informally:'' Reading the empty string may drive the automaton from state <math>q</math> to any state of the epsilon closure of <math>q .</math> * <math display=inline>\delta^*(q,wa) = \bigcup_{r \in \delta^*(q,w)} E(\delta(r,a)) ,</math> for each state <math>q \in Q ,</math> each string <math>w \in \Sigma^*</math> and each symbol <math>a \in \Sigma .</math> :''Informally:'' Reading the string <math>w</math> may drive the automaton from state <math>q</math> to any state <math>r</math> in the recursively computed set <math>\delta^*(q,w)</math>; after that, reading the symbol <math>a</math> may drive it from <math>r</math> to any state in the epsilon closure of <math>\delta(r,a) .</math> The automaton is said to accept a string <math>w</math> if :<math>\delta^*(q_0,w) \cap F \neq \emptyset ,</math> that is, if reading <math>w</math> may drive the automaton from its start state <math>q_0</math> to some accepting state in <math>F .</math>{{sfn|Hopcroft|Ullman|1979|p=25}} ===Example=== [[Image:NFAexample.svg|thumb|250px|The [[state diagram]] for ''M'']] Let <math>M</math> 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, let <math display=block>M = (\{S_0, S_1, S_2, S_3, S_4\}, \{0, 1\}, \delta, S_0, \{S_1, S_3\})</math> where the transition relation <math>\delta</math> can be defined by this [[state transition table]]: {| class="wikitable" style="margin-left:auto;margin-right:auto; text-align:center;" ! {{diagonal split header|State|Input}} ! 0 ! 1 ! ε |- ! ''S''<sub>0</sub> | {} | {} | {''S''<sub>1</sub>, ''S''<sub>3</sub>} |- ! ''S''<sub>1</sub> | {''S''<sub>2</sub>} | {''S''<sub>1</sub>} | {} |- ! ''S''<sub>2</sub> | {''S''<sub>1</sub>} | {''S''<sub>2</sub>} | {} |- ! ''S''<sub>3</sub> | {''S''<sub>3</sub>} | {''S''<sub>4</sub>} | {} |- ! ''S''<sub>4</sub> | {''S''<sub>4</sub>} | {''S''<sub>3</sub>} | {} |} <math>M</math> can be viewed as the union of two [[deterministic finite automaton|DFA]]s: one with states <math>\{S_1, S_2\}</math> and the other with states <math>\{S_3, S_4\}</math>. The language of <math>M</math> can be described by the [[regular language]] given by this [[regular expression]] <math>(1^{*}01^{*}01^{*})^{*} \cup (0^{*}10^{*}10^{*})^{*}</math>. We define <math>M</math> using ε-moves but <math>M</math> can be defined without using ε-moves. ===Equivalence to NFA=== 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 moves <math>M = (Q, \Sigma, \delta, q_0, F) ,</math> define an NFA <math>M' = (Q, \Sigma, \delta', q_0, F') ,</math> where :<math>F' = \begin{cases} F \cup \{ q_0 \} & \text{ if } E(q_0) \cap F \neq \{\} \\ F & \text{ otherwise } \\ \end{cases} </math> and :<math>\delta'(q,a) = \delta^*(q,a) </math> for each state <math>q \in Q</math> and each symbol <math>a \in \Sigma ,</math> using the extended transition function <math>\delta^*</math> defined above. One has to distinguish the transition functions of <math>M</math> and <math>M' ,</math> viz. <math>\delta</math> and <math>\delta' ,</math> and their extensions to strings, <math>\delta</math> and <math>\delta'^* ,</math> respectively. By construction, <math>M'</math> has no ε-transitions. One can prove that <math>\delta'^*(q_0,w) = \delta^*(q_0,w)</math> for each string <math>w \neq \varepsilon</math>, by [[mathematical induction|induction]] on the length of <math>w .</math> Based on this, one can show that <math>\delta'^*(q_0,w) \cap F' \neq \{\}</math> if, and only if, <math>\delta^*(q_0,w) \cap F \neq \{\},</math> for each string <math>w \in \Sigma^* :</math> * If <math>w = \varepsilon ,</math> this follows from the definition of <math>F' .</math> * Otherwise, let <math>w = va</math> with <math>v \in \Sigma^*</math> and <math>a \in \Sigma .</math> :From <math>\delta'^*(q_0,w) = \delta^*(q_0,w)</math> and <math>F \subseteq F' ,</math> we have <math display=block>\delta'^*(q_0,w) \cap F' \neq \{\} \;\Leftarrow\; \delta^*(q_0,w) \cap F \neq \{\} ;</math> we still have to show the "<math>\Rightarrow</math>" direction. :*If <math>\delta'^*(q_0,w)</math> contains a state in <math>F' \setminus \{ q_0 \} ,</math> then <math>\delta^*(q_0,w)</math> contains the same state, which lies in <math>F</math>. :*If <math>\delta'^*(q_0,w)</math> contains <math>q_0 ,</math> and <math>q_0 \in F ,</math> then <math>\delta^*(q_0,w)</math> also contains a state in <math>F ,</math> viz. <math>q_0 .</math> :*If <math>\delta'^*(q_0,w)</math> contains <math>q_0 ,</math> and <math>q_0 \not\in F ,</math> but <math>q_0\in F',</math> then there exists a state in <math>E(q_0)\cap F</math>, and the same state must be in <math display=inline>\delta^*(q_0,w) = \bigcup_{r \in \delta^*(q,v)} E(\delta(r,a)) .</math>{{sfn|Hopcroft|Ullman|1979|pp=26-27}} Since NFA is equivalent to DFA, NFA-ε is also equivalent to DFA. ==Closure properties== [[File:Thompson-or.svg|thumb|Composed NFA accepting the union of the languages of some given NFAs {{color|#800000|''N''(''s'')}} and {{color|#008000|''N''(''t'')}}. For an input string ''w'' in the language union, the composed automaton follows an ε-transition from ''q'' to the start state (left colored circle) of an appropriate subautomaton — {{color|#800000|''N''(''s'')}} or {{color|#008000|''N''(''t'')}} — which, by following ''w'', may reach an accepting state (right colored circle); from there, state ''f'' can be reached by another ε-transition. Due to the ε-transitions, the composed NFA is properly nondeterministic even if both {{color|#800000|''N''(''s'')}} and {{color|#008000|''N''(''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 is [[closed under]] the following operations. These closure operations are used in [[Thompson's construction algorithm]], which constructs an NFA from any [[regular expression]]. They can also be used to prove that NFAs recognize exactly the [[regular languages]]. *Union (cf. picture); that is, if the language ''L''<sub>1</sub> is accepted by some NFA ''A''<sub>1</sub> and ''L''<sub>2</sub> by some ''A''<sub>2</sub>, then an NFA ''A''<sub>u</sub> can be constructed that accepts the language ''L''<sub>1</sub>∪''L''<sub>2</sub>. *Intersection; similarly, from ''A''<sub>1</sub> and ''A''<sub>2</sub> an NFA ''A''<sub>i</sub> can be constructed that accepts ''L''<sub>1</sub>∩''L''<sub>2</sub>. *Concatenation *Negation; similarly, from ''A''<sub>1</sub> an NFA ''A''<sub>n</sub> can be constructed that accepts Σ<sup>*</sup>\''L''<sub>1</sub>. *[[Kleene closure]] Since NFAs are equivalent to nondeterministic finite automaton with ε-moves (NFA-ε), the above closures are proved using closure properties of NFA-ε. ==Properties== The machine starts in the specified initial state and reads in a string of symbols from its [[Alphabet (computer science)|alphabet]]. The automaton uses the [[state 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".<ref>FOLDOC Free Online Dictionary of Computing, ''[http://foldoc.org/nfa Finite-State Machine]''</ref> 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 a [[regular language]]. For every NFA a [[deterministic 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 the [[powerset construction]], which may lead to an exponential rise in the number of necessary states. For a formal proof of the powerset construction, please see the [[Powerset construction]] article. ==Implementation== 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.<ref>{{cite web|url=https://cseweb.ucsd.edu//~ccalabro/essays/fsa.pdf|title=NFA to DFA blowup|website=cseweb.ucsd.edu|date=February 27, 2005|access-date=6 March 2023|author=Chris Calabro}}</ref> * Keep a [[set data structure]] of all states which the NFA might currently be in. On the consumption of an input symbol, [[set union|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 most ''s''<sup>2</sup> computations, where ''s'' 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 length ''n'' can be processed in time [[Big O notation#Formal definition|''O'']](''ns''<sup>2</sup>),{{sfn|Hopcroft|Motwani|Ullman|2006|pp=154-5}} and space ''O''(''s''). * Create multiple copies. For each ''n'' way decision, the NFA creates up to ''n''−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.)<ref>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. [http://abc.comlab.ox.ac.uk/papers#oopsla2005 Adding trace matching with free variables to AspectJ] {{Webarchive|url=https://web.archive.org/web/20090918053718/http://abc.comlab.ox.ac.uk/papers#oopsla2005 |date=2009-09-18 }}. 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.</ref> == Complexity == * One can solve in linear time the [[emptiness problem]] for NFA, i.e., check whether the language of a given NFA is empty. To do this, we can simply perform a [[depth-first search]] from the initial state and check if some final state can be reached. * It is [[PSPACE]]-complete to test, given an NFA, whether it is ''universal'', i.e., if there is a string that it does not accept.<ref>Historically shown in: {{Cite journal |last1=Meyer |first1=A. R. |last2=Stockmeyer |first2=L. J. |date=1972-10-25 |title=The equivalence problem for regular expressions with squaring requires exponential space |journal=Proceedings of the 13th Annual Symposium on Switching and Automata Theory (SWAT) |location=USA |publisher=IEEE Computer Society |pages=125–129 |doi=10.1109/SWAT.1972.29}} For a modern presentation, see [http://www.lsv.fr/~jacomme/1718/ca/td4_sol.pdf]</ref> As a consequence, the same is true of the ''inclusion problem'', i.e., given two NFAs, is the language of one a subset of the language of the other. * Given as input an NFA ''A'' and an integer n, the [[counting problem (complexity)|counting problem]] of determining how many words of length ''n'' are accepted by ''A'' is intractable; it is [[♯P|'''#P'''-hard]]. In fact, this problem is complete (under [[parsimonious reduction]]s) for the complexity class [[SpanL]].<ref>{{Cite journal |last1=Álvarez |first1=Carme |last2=Jenner |first2=Birgit |date=1993-01-04 |title=A very hard log-space counting class |url=https://dx.doi.org/10.1016/0304-3975%2893%2990252-O |journal=Theoretical Computer Science |volume=107 |issue=1 |pages=3–30 |doi=10.1016/0304-3975(93)90252-O |issn=0304-3975}}</ref> ==Application of NFA== 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 the [[theory of computation]]. For example, it is much easier to prove [[Regular languages#Closure properties|closure properties]] of [[regular language]]s using NFAs than DFAs. ==See also== * [[Deterministic finite automaton]] * [[Two-way nondeterministic finite automaton]] * [[Pushdown automaton]] * [[Nondeterministic Turing machine]] == Notes == {{Reflist}} == References == *{{cite journal |doi=10.1147/rd.32.0114 |last1=Rabin |first1=M. O. |last2=Scott |first2=D. |title=Finite Automata and Their Decision Problems |journal=IBM Journal of Research and Development |volume=3 |issue=2 |pages=114–125 |date=April 1959 }} * {{Sipser 1997}} ''(See §1.2: Nondeterminism, pp. 47–63.)''{{sfn whitelist|CITEREFSipser1997}} * {{Hopcroft and Ullman 1979|author-link=no|title-link=no}}{{sfn whitelist|CITEREFHopcroftUllman1979}} * {{Hopcroft, Motwani, and Ullman 2006}} ''(See chapter 2, "Finite Automata".)''{{sfn whitelist|CITEREFHopcroftMotwaniUllman2006}} {{Formal languages and grammars}} {{Strings |state=collapsed}} {{DEFAULTSORT:Nondeterministic Finite-State Machine}} [[Category:Finite-state machines]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Cite book
(
edit
)
Template:Cite journal
(
edit
)
Template:Cite web
(
edit
)
Template:Color
(
edit
)
Template:Diagonal split header
(
edit
)
Template:Formal languages and grammars
(
edit
)
Template:Hopcroft, Motwani, and Ullman 2006
(
edit
)
Template:Hopcroft and Ullman 1979
(
edit
)
Template:Ifsubst
(
edit
)
Template:Mvar
(
edit
)
Template:Reflist
(
edit
)
Template:Rp
(
edit
)
Template:Sfn
(
edit
)
Template:Sfn whitelist
(
edit
)
Template:Short description
(
edit
)
Template:Sipser 1997
(
edit
)
Template:Strings
(
edit
)
Template:Webarchive
(
edit
)