Nondeterministic finite automaton explained

In automata theory, a finite-state machine is called a deterministic finite automaton (DFA), if

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 automata with ε-moves, finite-state transducers, pushdown automata, alternating automata, ω-automata, and probabilistic automata.Besides the DFAs, other known special cases of NFAsare unambiguous finite automata (UFA)and self-verifying finite automata (SVFA).

Informal introduction

There are two ways to describe the behavior of an NFA, and both of them are equivalent. 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[3] and lead to an accepting state, the input is rejected.[4] [5]

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] [6] [7]

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,\Sigma,\delta,q0,F)

, consisting of

Q

,

\Sigma

,

\delta

:

Q x \Sigmal{P}(Q)

,

q0\inQ

, and

F

distinguished as accepting (or final) states

F\subseteqQ

.

Here,

l{P}(Q)

denotes the power set of

Q

.

Recognized language

Given an NFA

M=(Q,\Sigma,\delta,q0,F)

, its recognized language is denoted by

L(M)

, and is defined as the set of all strings over the alphabet

\Sigma

that are accepted by

M

.

Loosely corresponding to the above informal explanations, there are several equivalent formal definitions of a string

w=a1a2...an

being accepted by

M

:

w

is accepted if a sequence of states,

r0,r1,...,rn

, exists in

Q

such that:

r0=q0

ri+1\in\delta(ri,ai+1)

, for

i=0,\ldots,n-1

rn\inF

.

In words, the first condition says that the machine starts in the start state

q0

. The second condition says that given each character of string

w

, the machine will transition from state to state according to the transition function

\delta

. The last condition says that the machine accepts

w

if the last input of

w

causes the machine to halt in one of the accepting states. In order for

w

to be accepted by

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

q0

to a state from

F

by following

w

, it is said that the automaton rejects the string. The set of strings

M

accepts is the language recognized by

M

and this language is denoted by

L(M)

.[5] [6]

w

is accepted if
*(q
\delta
0,

w)\capF\not=\emptyset

, where

\delta*:Q x \Sigma*l{P}(Q)

is defined recursively by:

\delta*(r,\epsilon)=\{r\}

where

\epsilon

is the empty string, and

\delta*(r,xa)=

cup
r'\in\delta*(r,x)

\delta(r',a)

for all

x\in\Sigma*,a\in\Sigma

.

In words,

\delta*(r,x)

is the set of all states reachable from state

r

by consuming the string

x

. The string

w

is accepted if some accepting state in

F

can be reached from the start state

q0

by consuming

w

.[4] [7]

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\},\delta,p,\{q\})

wherethe transition function

\delta

can be defined by this state transition table (cf. upper left picture):
01

p

\{p\}

\{p,q\}

q

\emptyset

\emptyset

Since the set

\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:

M

.

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.

M

may traverse the state sequence

\langler0,r1,r2,r3,r4\rangle=\langlep,p,p,p,q\rangle

, which satisfies conditions 1 to 3.

\delta*(p,\epsilon)=\{p\}

, hence

\delta*(p,1)=\delta(p,1)=\{p,q\}

, hence

\delta*(p,10)=\delta(p,0)\cup\delta(q,0)=\{p\}\cup\{\}

, hence

\delta*(p,101)=\delta(p,1)=\{p,q\}

, and hence

\delta*(p,1011)=\delta(p,1)\cup\delta(q,1)=\{p,q\}\cup\{\}

; since that set is not disjoint from

\{q\}

, the string "1011" is accepted.

In contrast, the string "10" is rejected by

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,

q

, by reading the final 0 symbol. While

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 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. 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,

(Q,\Sigma,\delta,q0,F)

, consisting of

Q

\Sigma

\delta:Q x (\Sigma\cup\{\epsilon\})l{P}(Q)

q0\inQ

F

distinguished as accepting (or final) states

F\subseteqQ

.

Here,

l{P}(Q)

denotes the power set of

Q

and

\epsilon

denotes empty string.

ε-closure of a state or set of states

For a state

q\inQ

, let

E(q)

denote the set of states that are reachable from

q

by following ε-transitions in the transition function

\delta

, i.e.,

p\inE(q)

if there is a sequence of states

q1,...,qk

such that

q1=q

,

qi+1\in\delta(qi,\varepsilon)

for each

1\lei<k

, and

qk=p

.

E(q)

is known as the epsilon closure, (also ε-closure) of

q

.

The ε-closure of a set

P

of states of an NFA is defined as the set of states reachable from any state in

P

following ε-transitions. Formally, for

P\subseteqQ

, define

E(P)=cup\limitsq\inE(q)

.

Extended transition function

Similar to NFA without ε-moves, the transition function

\delta

of an NFA-ε can be extended to strings.Informally,

\delta*(q,w)

denotes the set of all states the automaton may have reached when starting in state

q\inQ

and reading the string

w\in\Sigma*.

The function

\delta*:Q x \Sigma*l{P}(Q)

can be defined recursively as follows.

\delta*(q,\varepsilon)=E(q)

, for each state

q\inQ,

and where

E

denotes the epsilon closure;

Informally: Reading the empty string may drive the automaton from state

q

to any state of the epsilon closure of

q.

q\inQ,

each string

w\in\Sigma*

and each symbol

a\in\Sigma.

Informally: Reading the string

w

may drive the automaton from state

q

to any state

r

in the recursively computed set

\delta*(q,w)

; after that, reading the symbol

a

may drive it from

r

to any state in the epsilon closure of

\delta(r,a).

The automaton is said to accept a string

w

if
*(q
\delta
0,w)

\capF\emptyset,

that is, if reading

w

may drive the automaton from its start state

q0

to some accepting state in

F.

[4]

Example

Let

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 M = (\, \, \delta, S_0, \) wherethe transition relation

\delta

can be defined by this state transition table:

M

can be viewed as the union of two DFAs: one with states

\{S1,S2\}

and the other with states

\{S3,S4\}

. The language of

M

can be described by the regular language given by this regular expression

(1*01*0)*\cup(0*10*1)*

.We define

M

using ε-moves but

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.

Given an NFA with epsilon moves

M=(Q,\Sigma,\delta,q0,F),

define an NFA

M'=(Q,\Sigma,\delta',q0,F'),

where

F'=\begin{cases}F\cup\{q0\}&ifE(q0)\capF\{\}\F&otherwise\\end{cases}

and

\delta'(q,a)=\delta*(q,a)

for each state

q\inQ

and each symbol

a\in\Sigma,

using the extended transition function

\delta*

defined above.

One has to distinguish the transition functions of

M

and

M',

viz.

\delta

and

\delta',

and their extensions to strings,

\delta

and

\delta'*,

respectively.By construction,

M'

has no ε-transitions.

One can prove that

*(q
\delta'
0,w)

=

*(q
\delta
0,w)
for each string

w\varepsilon

, by induction on the length of

w.

Based on this, one can show that

*(q
\delta'
0,w)

\capF'\{\}

if, and only if,
*(q
\delta
0,w)

\capF\{\},

for each string

w\in\Sigma*:

w=\varepsilon,

this follows from the definition of

F'.

w=va

with

v\in\Sigma*

and

a\in\Sigma.

From

*(q
\delta'
0,w)

=

*(q
\delta
0,w)
and

F\subseteqF',

we have \delta'^*(q_0,w) \cap F' \neq \ \;\Leftarrow\; \delta^*(q_0,w) \cap F \neq \ ; we still have to show the "

" direction.
*(q
\delta'
0,w)
contains a state in

F'\setminus\{q0\},

then
*(q
\delta
0,w)
contains the same state, which lies in

F

.
*(q
\delta'
0,w)
contains

q0,

and

q0\inF,

then
*(q
\delta
0,w)
also contains a state in

F,

viz.

q0.

*(q
\delta'
0,w)
contains

q0,

and

q0\not\inF,

but

q0\inF',

then there exists a state in

E(q0)\capF

, and the same state must be in \delta^*(q_0,w) = \bigcup_ E(\delta(r,a)) .[4]

Since NFA is equivalent to DFA, NFA-ε is also equivalent to DFA.

Closure properties

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.

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. 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:

Complexity

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.

See also

References

Notes and References

  1. Book: Introduction to Languages and the Theory of Computation . Martin . John . 2010 . 108 . McGraw Hill . 978-0071289429.
  2. 10.1147/rd.32.0114 . Rabin . M. O. . Scott . D. . Finite Automata and Their Decision Problems . IBM Journal of Research and Development . 3 . 2 . 114–125 . April 1959 .
  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. Book: 0-201-02988-X . John E. Hopcroft and Jeffrey D. Ullman . Introduction to Automata Theory, Languages, and Computation . Reading/MA . Addison-Wesley . 1979 .
  5. Book: 0-201-00029-6 . Alfred V. Aho and John E. Hopcroft and Jeffrey D. Ullman . The Design and Analysis of Computer Algorithms . registration . Reading/MA . Addison-Wesley . 1974 .
  6. Book: Michael Sipser . Introduction to the Theory of Computation . Boston/MA . PWS Publishing Co. . 1997 . 0-534-94728-X .
  7. Book: Introduction to Automata Theory, Languages, and Computation . 0-201-44124-1 . John E. Hopcroft and Rajeev Motwani and Jeffrey D. Ullman . Upper Saddle River/NJ . Addison Wesley . 2003 .
  8. FOLDOC Free Online Dictionary of Computing, Finite-State Machine
  9. Web site: NFA to DFA blowup. cseweb.ucsd.edu. February 27, 2005. 6 March 2023. Chris Calabro.
  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.
  11. Historically shown in: Meyer . A. R. . Stockmeyer . L. J. . 1972-10-25 . The equivalence problem for regular expressions with squaring requires exponential space . Proceedings of the 13th Annual Symposium on Switching and Automata Theory (SWAT) . USA . IEEE Computer Society . 125–129 . 10.1109/SWAT.1972.29. For a modern presentation, see http://www.lsv.fr/~jacomme/1718/ca/td4_sol.pdf
  12. Álvarez . Carme . Jenner . Birgit . 1993-01-04 . A very hard log-space counting class . Theoretical Computer Science . 107 . 1 . 3–30 . 10.1016/0304-3975(93)90252-O . 0304-3975.