Nondeterministic finite automaton
In automata theory, a finite-state machine is called a deterministic finite automaton, 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.
Using the subset construction algorithm, each NFA can be translated to an equivalent DFA; i.e., a DFA recognizing the same formal language.
Like DFAs, NFAs only recognize regular languages.
NFAs were introduced in 1959 by Michael O. Rabin and Dana Scott, who also showed their equivalence to DFAs. NFAs are used in the implementation of regular 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.
NFAs have been generalized in multiple ways, e.g., nondeterministic finite automaton with ε-moves, finite-state transducers, pushdown automata, alternating automata, ω-automata, and probabilistic automata.
Besides the DFAs, other known special cases of NFAs
are unambiguous finite automata
and self-verifying finite automata.
Informal introduction
There are several informal explanations around, which are equivalent.- An NFA, similar to a DFA, consumes a string of input symbols. For each input symbol, it transitions to a new state until all input symbols have been consumed. In each step, the automaton arbitrarily chooses one of the applicable transitions. If there exists some "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 and lead to an accepting state, the input is rejected.
- Again, an 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.
Formal definition
Automaton
An NFA is represented formally by a 5-tuple,, consisting of
- a finite set of states.
- a finite set of input symbols.
- a transition function :.
- an initial state.
- a set of states distinguished as accepting states.
Recognized language
Given an NFA, its recognized language is denoted by, and is defined as set of all strings over the alphabet that are accepted by.Loosely corresponding to the [|above] 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
- #.
- Alternatively, is accepted if, where is defined recursively by:
- # where is the empty string, and
- # for all.
Initial state
Example
The following automaton, with a binary alphabet, determines if the input ends with a 1.Let where
the transition function can be defined by this state transition table :
0 | 1 | |
Since the set contains more than one state, is nondeterministic.
The language of can be described by the regular language given by the regular expression
*1
.All possible state sequences for the input string "1011" are shown in the lower picture.
The string is accepted by since one state sequence satisfies the above definition; it doesn't matter that other sequences fail to do so.
The picture can be interpreted in a couple of ways:
- In terms of the above "lucky-run" explanation, each path in the picture denotes a sequence of choices of.
- In terms of the "cloning" explanation, each vertical column shows all clones of 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.
- Considering the first of the above formal definitions, "1011" is accepted since when reading it may traverse the state sequence, which satisfies conditions 1 to 3.
- Concerning the second formal definition, bottom-up comutation shows that, hence, hence, hence, and hence ; since that set is not disjoint from, the string "1011" is accepted.
Equivalence to DFA
A Deterministic finite automaton can be seen as a special kind of NFA, in which for each state and alphabet, 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 a 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 2n states, which sometimes makes the construction impractical for large NFAs.
NFA with ε-moves
Nondeterministic finite automaton with ε-moves is a further generalization to NFA. This automaton replaces the transition function with the one that allows the empty string ε as a possible input. The transitions without consuming an input symbol are called ε-transitions. In the state diagrams, they are usually labeled with the Greek letter ε. ε-transitions provide a convenient way of modeling the 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 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,, consisting of- a finite set of states
- a finite set of input symbols called the alphabet
- a transition function
- an initial state
- a set of states distinguished as accepting states.
ε-closure of a state or set of states
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
- .
ε-closure is also defined for a set of states. 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.
Accepting states
Let be a string over the alphabet. The automaton accepts the string if a sequence of states,, exists in with the following conditions:
- where for each, and
- .
at the state that is reachable from the start state via ε-transitions. The second condition says that after reading,
the machine takes a transition of from to,
and then takes any number of ε-transitions according to to move from to.
The last condition says that the machine accepts if the last input of causes the machine to halt in one of the accepting states. Otherwise, it is said that the automaton rejects the string. The set of strings accepts is the language recognized by and this language is denoted by.
Example
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 where
the transition relation can be defined by this state transition table:
0 | 1 | ε | |
S0 | |||
S1 | |||
S2 | |||
S3 | |||
S4 |
can be viewed as the union of two DFAs: one with states and the other with states.
The language of can be described by the regular language given by this regular expression ∪.
We define using ε-moves but 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.Let be a NFA-ε.
The NFA is equivalent to, where for each and,.
Thus NFA-ε is equivalent to NFA. Since NFA is equivalent to DFA, NFA-ε is also equivalent to DFA.
Closure properties
NFAs are said to be closed under a operatorif NFAs recognize the languages
that are obtained by applying the operation on the NFA recognizable languages.
The NFAs are closed under the following operations.
- Union
- Intersection
- Concatenation
- Negation
- Kleene closure
NFAs can be constructed from any regular expression using Thompson's construction algorithm.
Properties
The machine starts in the specified initial state and reads in a string of symbols from its 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". 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 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 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.
- Keep a set 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. Each step requires at most s2 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 O, and space O.
- Create multiple copies. For each n way decision, the NFA creates up to 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.
- 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.
Application of NFA