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. 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 (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, and probabilistic automata. Besides the DFAs, other known special cases of NFAs are unambiguous finite automata (UFA) and 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 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 and lead to an accepting state, the input is rejected. 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. == Formal definition == For a more elementary introduction of the formal definition, see automata theory. === Automaton === An NFA is represented formally by a 5-tuple, ( Q , Σ , δ , q 0 , F ) {\displaystyle (Q,\Sigma ,\delta ,q_{0},F)} , consisting of a finite set of states Q {\displaystyle Q} , a finite set of input symbols called the alphabet Σ {\displaystyle \Sigma } , a transition function δ {\displaystyle \delta } : Q × Σ → P ( Q ) {\displaystyle Q\times \Sigma \rightarrow {\mathcal {P}}(Q)} , an initial (or start) state q 0 ∈ Q {\displaystyle q_{0}\in Q} , and a set of accepting (or final) states F ⊆ Q {\displaystyle F\subseteq Q} . Here, P ( Q ) {\displaystyle {\mathcal {P}}(Q)} denotes the power set of Q {\displaystyle Q} . === Recognized language === Given an NFA M = ( Q , Σ , δ , q 0 , F ) {\displaystyle M=(Q,\Sigma ,\delta ,q_{0},F)} , its recognized language is denoted by L ( M ) {\displaystyle L(M)} , and is defined as the set of all strings over the alphabet Σ {\displaystyle \Sigma } that are accepted by M {\displaystyle M} . Loosely corresponding to the above informal explanations, there are several equivalent formal definitions of a string w = a 1 a 2 . . . a n {\displaystyle w=a_{1}a_{2}...a_{n}} being accepted by M {\displaystyle M} : w {\displaystyle w} is accepted if a sequence of states, r 0 , r 1 , . . . , r n {\displaystyle r_{0},r_{1},...,r_{n}} , exists in Q {\displaystyle Q} such that: r 0 = q 0 {\displaystyle r_{0}=q_{0}} r i + 1 ∈ δ ( r i , a i + 1 ) {\displaystyle r_{i+1}\in \delta (r_{i},a_{i+1})} , for i = 0 , … , n − 1 {\displaystyle i=0,\ldots ,n-1} r n ∈ F {\displaystyle r_{n}\in F} . In words, the first condition says that the machine starts in the start state q 0 {\displaystyle q_{0}} . The second condition says that given each character of string w {\displaystyle w} , the machine will transition from state to state according to the transition function δ {\displaystyle \delta } . The last condition says that the machine accepts w {\displaystyle w} if the last input of w {\displaystyle w} causes the machine to halt in one of the accepting states. In order for w {\displaystyle w} to be accepted by M {\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 from q 0 {\displaystyle q_{0}} to a state from F {\displaystyle F} by following w {\displaystyle w} , it is said that the automaton rejects the string. The set of strings M {\displaystyle M} accepts is the language recognized by M {\displaystyle M} and this language is denoted by L ( M ) {\displaystyle L(M)} . Alternatively, w {\displaystyle w} is accepted if δ ∗ ( q 0 , w ) ∩ F ≠ ∅ {\displaystyle \delta ^{}(q_{0},w)\cap F\not =\emptyset } , where δ ∗ : Q × Σ ∗ → P ( Q ) {\displaystyle \delta ^{}:Q\times \Sigma ^{}\rightarrow {\mathcal {P}}(Q)} is defined recursively by: δ ∗ ( r , ε ) = { r } {\displaystyle \delta ^{}(r,\varepsilon )=\{r\}} where ε {\displaystyle \varepsilon } is the empty string, and δ ∗ ( r , x a ) = ⋃ r ′ ∈ δ ∗ ( r , x ) δ ( r ′ , a ) {\displaystyle \delta ^{}(r,xa)=\bigcup _{r'\in \delta ^{}(r,x)}\delta (r',a)} for all x ∈ Σ ∗ , a ∈ Σ {\displaystyle x\in \Sigma ^{},a\in \Sigma } . In words, δ ∗ ( r , x ) {\displaystyle \delta ^{}(r,x)} is the set of all states reachable from state r {\displaystyle r} by consuming the string x {\displaystyle x} . The string w {\displaystyle w} is accepted if some accepting state in F {\displaystyle F} can be reached from the start state q 0 {\displaystyle q_{0}} by consuming w {\displaystyle w} . === 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 == The following automaton M, with a binary alphabet, determines if the input ends with a 1. Let M = ( { p , q } , { 0 , 1 } , δ , p , { q } ) {\displaystyle M=(\{p,q\},\{0,1\},\delta ,p,\{q\})} where the transition function δ {\displaystyle \delta } can be defined by this state transition table (cf. upper left picture): State Input 0 1 p { 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 of M can be described by the regular language given by the regular expression (0|1)1. All possible state sequences for the input string "1011" are shown in the lower picture. The string is accepted by 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 above "lucky-run" explanation, each path in the picture denotes a sequence of choices of M. In terms of the "cloning" explanation, each vertical column shows all clones of 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 above formal definitions, "1011" is accepted since when reading it M may traverse the state sequence ⟨ r 0 , r 1 , r 2 , r 3 , r 4 ⟩ = ⟨ p , p , p , p , q ⟩ {\displaystyle \langle r_{0},r_{1},r_{2},r_{3},r_{4}\rangle =\langle p,p,p,p,q\rangle } , which satisfies conditions 1 to 3. Concerning the second formal definition, bottom-up computation shows that δ ∗ ( p , ε ) = { p } {\displaystyle \delta ^{}(p,\varepsilon )=\{p\}} , hence δ ∗ ( p , 1 ) = δ ( p , 1 ) = { p , q } {\displaystyle \delta ^{}(p,1)=\delta (p,1)=\{p,q\}} , hence δ ∗ ( p , 10 ) = δ ( p , 0 ) ∪ δ ( q , 0 ) = { p } ∪ { } {\displaystyle \delta ^{}(p,10)=\delta (p,0)\cup \delta (q,0)=\{p\}\cup \{\}} , hence δ ∗ ( p , 101 ) = δ ( p , 1 ) = { p , q } {\displaystyle \delta ^{}(p,101)=\delta (p,1)=\{p,q\}} , and hence δ ∗ ( p , 1011 ) = δ ( p , 1 ) ∪ δ ( q , 1 ) = { p , q } ∪ { } {\displaystyle \delta ^{}(p,1011)=\delta (p,1)\cup \delta (q,1)=\{p,q\}\cup \{\}} ; since that set is
Read more →