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.
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).
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]
Given an NFA, its recognized language is denoted by, and is defined as the set of all strings over the alphabet that are accepted by.
Loosely corresponding to theabove informal explanations, there are several equivalent formal definitions of a string being accepted by:
is accepted if a sequence of states,, exists in such that:
, for
.
In words, the first condition says that the machine starts in the start state. The second condition says that given each character of string, the machine will transition from state to state according to the transition function. The last condition says that the machine accepts if the last input of causes the machine to halt in one of the accepting states. In order for to be accepted by, 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 to a state from by following, it is said that the automatonrejects the string. The set of strings accepts is thelanguagerecognized by and this language is denoted by.[5]: 320 [8]
Alternatively, is accepted if, where is definedrecursively by:
where is the empty string, and
for all.
In words, is the set of all states reachable from state by consuming the string. The string is accepted if some accepting state in can be reached from the start state by consuming.[9][10]
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.
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
1
0
1
1
1
p
q
?
2
p
p
p
q
?
3
p
p
p
p
q
The following automatonM, with a binary alphabet, determines if the input ends with a 1.Let wherethe transition function can be defined by thisstate transition table (cf. upper left picture):
Since the set 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.
Considering the first of theabove formal definitions, "1011" is accepted since when reading itM may traverse the state sequence, which satisfies conditions 1 to 3.
Concerning the second formal definition, bottom-up computation shows that, hence, hence, hence, and hence; since that set is not disjoint from, the string "1011" is accepted.
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.
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.
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.
For a state, let denote the set of states that are reachable from by following ε-transitions in the transition function, i.e., if there is a sequence of states such that
,
for each, and
.
is known as theepsilon closure, (alsoε-closure) of.
The ε-closure of a set of states of an NFA is defined as the set of states reachable from any state in following ε-transitions. Formally, for, define.
Similar to NFA without ε-moves, the transition function of an NFA-ε can be extended to strings.Informally, denotes the set of all states the automaton may have reached when starting in state and reading the stringThe function can be defined recursively as follows.
, for each state and where denotes the epsilon closure;
Informally: Reading the empty string may drive the automaton from state to any state of the epsilon closure of
for each state each string and each symbol
Informally: Reading the string may drive the automaton from state to any state in the recursively computed set; after that, reading the symbol may drive it from to any state in the epsilon closure of
The automaton is said to accept a string if
that is, if reading may drive the automaton from its start state to some accepting state in[11]
Let 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 wherethe transition relation can be defined by thisstate transition table:
Input
State
0
1
ε
S0
{}
{}
{S1,S3}
S1
{S2}
{S1}
{}
S2
{S1}
{S2}
{}
S3
{S3}
{S4}
{}
S4
{S4}
{S3}
{}
can be viewed as the union of twoDFAs: one with states and the other with states. The language of can be described by theregular language given by thisregular expression.We define using ε-moves but can be defined without using ε-moves.
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 movesdefine an NFA where
and
for each state and each symbol using the extended transition function defined above.
One has to distinguish the transition functions of and viz. and and their extensions to strings, and respectively.By construction, has no ε-transitions.
One can prove that for each string, byinduction on the length of
Based on this, one can show that if, and only if, for each string
If this follows from the definition of
Otherwise, let with and
From and we have we still have to show the "" direction.
If contains a state in then contains the same state, which lies in.
If contains and then also contains a state in viz.
If contains and but then there exists a state in, and the same state must be in[12]
Since NFA is equivalent to DFA, NFA-ε is also equivalent to DFA.
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.
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 languageL1∪L2.
Intersection; similarly, fromA1 andA2 an NFAAi can be constructed that acceptsL1∩L2.
Concatenation
Negation; similarly, fromA1 an NFAAn can be constructed that accepts Σ*\L1.
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.
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]
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]
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.
^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.
^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.
^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]
Rabin, M. O.; Scott, D. (April 1959). "Finite Automata and Their Decision Problems".IBM Journal of Research and Development.3 (2):114–125.doi:10.1147/rd.32.0114.
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.