Nondeterministic Finite Automaton
Get Nondeterministic Finite Automaton essential facts below. View Videos or join the Nondeterministic Finite Automaton discussion. Add Nondeterministic Finite Automaton to your PopFlock.com topic list for future reference or share this resource on social media.
Nondeterministic Finite Automaton
NFA for (0|1)* 1 (0|1)3.
A DFA for that 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.[1] Like DFAs, NFAs only recognize regular languages.

NFAs were introduced in 1959 by Michael O. Rabin and Dana Scott,[2] 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 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 (UFA) and self-verifying finite automata (SVFA).

## 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[3] and lead to an accepting state, the input is rejected.[4]:19[5]:319
• 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.[4]:19-20[6]:48[7]:56

## Formal definition

For a more elementary introduction of the formal definition see automata theory.

### Automaton

An NFA is represented formally by a 5-tuple, ${\displaystyle (Q,\Sigma ,\Delta ,q_{0},F)}$, consisting of

• a finite set of states ${\displaystyle Q}$.
• a finite set of input symbols ${\displaystyle \Sigma }$.
• a transition function ${\displaystyle \Delta }$ : ${\displaystyle Q\times \Sigma \rightarrow P(Q)}$.
• an initial (or start) state ${\displaystyle q_{0}\in Q}$.
• a set of states ${\displaystyle F}$ distinguished as accepting (or final) states ${\displaystyle F\subseteq Q}$.

Here, ${\displaystyle P(Q)}$ denotes the power set of ${\displaystyle Q}$.

### Recognized language

Given an NFA ${\displaystyle M=(Q,\Sigma ,\Delta ,q_{0},F)}$, its recognized language is denoted by ${\displaystyle L(M)}$, and is defined as set of all strings over the alphabet ${\displaystyle \Sigma }$ that are accepted by ${\displaystyle M}$.

Loosely corresponding to the above informal explanations, there are several equivalent formal definitions of a string ${\displaystyle w=a_{1}a_{2}...a_{n}}$ being accepted by ${\displaystyle M}$:

• ${\displaystyle w}$ is accepted if a sequence of states, ${\displaystyle r_{0},r_{1},...,r_{n}}$, exists in ${\displaystyle Q}$ such that:
1. ${\displaystyle r_{0}=q_{0}}$
2. ${\displaystyle r_{i+1}\in \Delta (r_{i},a_{i+1})}$, for ${\displaystyle i=0,\ldots ,n-1}$
3. ${\displaystyle r_{n}\in F}$.
In words, the first condition says that the machine starts in the start state ${\displaystyle q_{0}}$. The second condition says that given each character of string ${\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 ${\displaystyle w}$ if the last input of ${\displaystyle w}$ causes the machine to halt in one of the accepting states. In order for ${\displaystyle w}$ to be accepted by ${\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 ${\displaystyle q_{0}}$ to a state from ${\displaystyle F}$ by following ${\displaystyle w}$, it is said that the automaton rejects the string. The set of strings ${\displaystyle M}$ accepts is the language recognized by ${\displaystyle M}$ and this language is denoted by ${\displaystyle L(M)}$.[5]:320[6]:54
• Alternatively, ${\displaystyle w}$ is accepted if ${\displaystyle \Delta ^{*}(q_{0},w)\cap F\not =\emptyset }$, where ${\displaystyle \Delta ^{*}:Q\times \Sigma ^{*}\rightarrow P(Q)}$ is defined recursively by:
1. ${\displaystyle \Delta ^{*}(r,\epsilon )=\{r\}}$ where ${\displaystyle \epsilon }$ is the empty string, and
2. ${\displaystyle \Delta ^{*}(r,xa)=\bigcup _{r'\in \Delta ^{*}(r,x)}\Delta (r',a)}$ for all ${\displaystyle x\in \Sigma ^{*},a\in \Sigma }$.
In words, ${\displaystyle \Delta ^{*}(r,x)}$ is the set of all states reachable from state ${\displaystyle r}$ by consuming the string ${\displaystyle x}$. The string ${\displaystyle w}$ is accepted if some accepting state in ${\displaystyle F}$ can be reached from the start state ${\displaystyle q_{0}}$ by consuming ${\displaystyle w}$.[4]:21[7]: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 a NFA with multiple initial states to a NFA with single initial state, which provides a convenient notation.

## Example

 The state diagram for M. It is not deterministic since in state p reading a 1 can lead to p or to q. All possible runs of M on input string "10". All possible runs of M on input string "1011".Arc label: input symbol, node label: state, green: start state, red: accepting state(s).

The following automaton ${\displaystyle M}$, with a binary alphabet, determines if the input ends with a 1. Let ${\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):

Input
State
0 1
${\displaystyle p}$ ${\displaystyle \{p\}}$ ${\displaystyle \{p,q\}}$
${\displaystyle q}$ ${\displaystyle \emptyset }$ ${\displaystyle \emptyset }$

Since the set ${\displaystyle \Delta (p,1)}$ contains more than one state, ${\displaystyle M}$ is nondeterministic. The language of ${\displaystyle 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 ${\displaystyle M}$ 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 ${\displaystyle M}$.
• In terms of the "cloning" explanation, each vertical column shows all clones of ${\displaystyle 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 ${\displaystyle M}$ may traverse the state sequence ${\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 comutation shows that ${\displaystyle \Delta ^{*}(p,\epsilon )=\{p\}}$, hence ${\displaystyle \Delta ^{*}(p,1)=\Delta (p,1)=\{p,q\}}$, hence ${\displaystyle \Delta ^{*}(p,10)=\Delta (p,0)\cup \Delta (q,0)=\{p\}\cup \{\}}$, hence ${\displaystyle \Delta ^{*}(p,101)=\Delta (p,1)=\{p,q\}}$, and hence ${\displaystyle \Delta ^{*}(p,1011)=\Delta (p,1)\cup \Delta (q,1)=\{p,q\}\cup \{\}}$; since that set is not disjoint from ${\displaystyle \{q\}}$, the string "1011" is accepted.

In contrast, the string "10" is rejected by ${\displaystyle 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, ${\displaystyle q}$, by reading the final 0 symbol. While ${\displaystyle 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 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 (NFA-?) 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 (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, ${\displaystyle (Q,\Sigma ,\Delta ,q_{0},F)}$, consisting of

• a finite set of states ${\displaystyle Q}$
• a finite set of input symbols called the alphabet ${\displaystyle \Sigma }$
• a transition function ${\displaystyle \Delta :Q\times (\Sigma \cup \{\epsilon \})\rightarrow P(Q)}$
• an initial (or start) state ${\displaystyle q_{0}\in Q}$
• a set of states ${\displaystyle F}$ distinguished as accepting (or final) states ${\displaystyle F\subseteq Q}$.

Here, ${\displaystyle P(Q)}$ denotes the power set of ${\displaystyle Q}$ and ? denotes empty string.

### ?-closure of a state or set of states

For a state ${\displaystyle q\in Q}$, let ${\displaystyle E(q)}$ denote the set of states that are reachable from ${\displaystyle q}$ by following ?-transitions in the transition function ${\displaystyle \Delta }$, i.e., ${\displaystyle p\in E(q)}$ if there is a sequence of states ${\displaystyle q_{1},...,q_{k}}$ such that

• ${\displaystyle q_{1}=q}$,
• ${\displaystyle q_{i+1}\in \Delta (q_{i},\epsilon )}$ for each 1
• ${\displaystyle q_{k}=p}$.

${\displaystyle E(q)}$ is known as the ?-closure of ${\displaystyle q}$.

?-closure is also defined for a set of states. The ?-closure of a set of states, ${\displaystyle P}$, of an NFA is defined as the set of states reachable from any state in ${\displaystyle P}$ following ?-transitions. Formally, for ${\displaystyle P\subseteq Q,E(P)=}$?q?PE(q).

### Accepting states

Let ${\displaystyle w=a_{1}a_{2}...a_{n}}$ be a string over the alphabet ${\displaystyle \Sigma }$. The automaton ${\displaystyle M}$ accepts the string ${\displaystyle w}$ if a sequence of states, ${\displaystyle r_{0},r_{1},...,r_{n}}$, exists in ${\displaystyle Q}$ with the following conditions:

1. ${\displaystyle r_{0}\in E(q_{0})}$
2. ${\displaystyle r_{i+1}\in E(r')}$ where ${\displaystyle r'\in \Delta (r_{i},a_{i+1})}$ for each ${\displaystyle i=0,...,n-1}$, and
3. ${\displaystyle r_{n}\in F}$.

In words, the first condition says that the machine starts at the state that is reachable from the start state ${\displaystyle q_{0}}$ via ?-transitions. The second condition says that after reading ${\displaystyle a_{i}}$, the machine takes a transition of ${\displaystyle \Delta }$ from ${\displaystyle r_{i}}$ to ${\displaystyle r'}$, and then takes any number of ?-transitions according to ${\displaystyle \Delta }$ to move from ${\displaystyle r'}$ to ${\displaystyle r_{i+1}}$. The last condition says that the machine accepts ${\displaystyle w}$ if the last input of ${\displaystyle w}$ 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 ${\displaystyle M}$ accepts is the language recognized by ${\displaystyle M}$ and this language is denoted by ${\displaystyle L(M)}$.

### Example

The state diagram for M

Let ${\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, let ${\displaystyle M=(\{S_{0},S_{1},S_{2},S_{3},S_{4}\},\{0,1\},\Delta ,S_{0},\{S_{1},S_{3}\})}$ where the transition relation ${\displaystyle \Delta }$ can be defined by this state transition table:

Input
State
0 1 ?
S0 {} {} {S1, S3}
S1 {S2} {S1} {}
S2 {S1} {S2} {}
S3 {S3} {S4} {}
S4 {S4} {S3} {}

${\displaystyle M}$ can be viewed as the union of two DFAs: one with states ${\displaystyle \{S_{1},S_{2}\}}$ and the other with states ${\displaystyle \{S_{3},S_{4}\}}$. The language of ${\displaystyle M}$ can be described by the regular language given by this regular expression (1*(01*01*)*) ? (0*(10*10*)*). We define ${\displaystyle M}$ using ?-moves but ${\displaystyle M}$ 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 ${\displaystyle A=(Q,\Sigma ,\Delta ,q_{0},F)}$ be a NFA-?. The NFA ${\displaystyle A'=(Q,\Sigma ,\Delta ',E(q_{0}),F)}$ is equivalent to ${\displaystyle A}$, where for each ${\displaystyle a\in \Sigma }$ and ${\displaystyle q\in Q}$, ${\displaystyle \Delta '(q,a)=E(\Delta (q,a))}$.

Thus NFA-? is equivalent to NFA. Since NFA is equivalent to DFA, NFA-? is also equivalent to DFA.

## Closure properties

Composed NFA accepting the union of the languages of some given NFAs N(s) and 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 — N(s) or 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 N(s) and N(t) were DFAs; vice versa, constructing a DFA for the union language (even of two DFAs) is much more complicated.

NFAs are said to be closed under a (binary/unary) operator if 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 (cf. picture)
• Intersection
• Concatenation
• Negation
• Kleene closure

Since NFAs are equivalent to nondeterministic finite automaton with ?-moves (NFA-?), the above closures are proved using closure properties of NFA-?. The above closure properties imply that NFAs only recognize regular languages.

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".[8] 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.[9]
• 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 (?-closure). 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(ns2),[7]:153 and space O(s).
• Create multiple copies. For each n way decision, the NFA creates up to ${\displaystyle 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.[10])

## 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 closure properties of regular languages using NFAs than DFAs.

## Notes

1. ^ Martin, John (2010). Introduction to Languages and the Theory of Computation. McGraw Hill. p. 108. ISBN 978-0071289429.
2. ^ 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.
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. ^ a b c John E. Hopcroft and Jeffrey D. Ullman (1979). Introduction to Automata Theory, Languages, and Computation. Reading/MA: Addison-Wesley. ISBN 0-201-02988-X.
5. ^ a b Alfred 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. ^ a b Michael Sipser (1997). Introduction to the Theory of Computation. Boston/MA: PWS Publishing Co. ISBN 0-534-94728-X.
7. ^ a b c John E. Hopcroft and Rajeev Motwani and Jeffrey D. Ullman (2003). Introduction to Automata Theory, Languages, and Computation (PDF). Upper Saddle River/NJ: Addison Wesley. ISBN 0-201-44124-1.
8. ^ FOLDOC Free Online Dictionary of Computing, Finite State Machine
9. ^ http://cseweb.ucsd.edu/~ccalabro/essays/fsa.pdf
10. ^ 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 AspectJ. 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.

## References

• M. O. Rabin and D. Scott, "Finite Automata and their Decision Problems", IBM Journal of Research and Development, 3:2 (1959) pp. 115-125.
• Michael Sipser, Introduction to the Theory of Computation. PWS, Boston. 1997. ISBN 0-534-94728-X. (see section 1.2: Nondeterminism, pp. 47-63.)
• John E. Hopcroft and Jeffrey D. Ullman, Introduction to Automata Theory, Languages, and Computation, Addison-Wesley Publishing, Reading Massachusetts, 1979. ISBN 0-201-02988-X. (See chapter 2.)