Fact-checked by Grok 2 weeks ago

Nondeterministic finite automaton

A nondeterministic finite automaton (NFA) is a theoretical model in and that recognizes languages by allowing, for each combination of current state and input symbol, zero, one, or multiple possible transitions to next states, thereby introducing nondeterminism into the . Formally, an NFA is defined as a five-tuple (Q, \Sigma, \delta, q_0, F), where Q is a of states, \Sigma is a finite input alphabet, \delta: Q \times \Sigma \to 2^Q is the transition function that maps each state and input symbol to a (possibly empty or with multiple elements) of Q, q_0 \in Q is the unique start state, and F \subseteq Q is the set of accepting (or final) states. The NFA accepts an input string if there exists at least one sequence of transitions—chosen nondeterministically—that consumes the entire string and reaches a state in F at the end. This model was introduced by and in their seminal 1959 paper "Finite Automata and Their Decision Problems," which established that NFAs are equivalent in expressive power to deterministic finite automata (DFAs), meaning they recognize precisely the same class of languages, called regular languages, though NFAs can often be constructed with fewer states for the same language. Many NFAs incorporate epsilon transitions (\varepsilon-transitions), which permit the automaton to move between states without consuming any input symbol, further simplifying the design of recognizers for complex patterns while preserving equivalence to standard NFAs and DFAs. NFAs play a foundational role in , enabling efficient algorithms for converting regular expressions to automata and vice versa, and they underpin practical applications such as in compilers and in text processing tools.

Introduction and Motivation

Informal Description

A nondeterministic finite automaton (NFA) is a that processes input strings over a finite alphabet by allowing multiple possible transitions from a given upon reading a specific input . This nondeterminism enables the automaton to branch into several potential paths simultaneously, exploring various sequences of s as it consumes the input, rather than following a single predetermined path. In contrast to a (DFA), which commits to exactly one next for each input symbol from any current , an NFA's ability to fork into multiple s reflects a form of parallel exploration in . This feature makes NFAs particularly useful for modeling choices or ambiguities in , where the effectively "guesses" the correct path among possibilities. One intuitive way to visualize an NFA is through the of a "choose-your-own-adventure" , in which the reader encounters branching narratives and selects different options to proceed; similarly, the NFA considers all branches and succeeds if any one leads to a favorable outcome. A string is accepted by the NFA if there exists at least one path through its state transitions that ends in an accepting state after processing the entire input, even if other paths fail or loop indefinitely. For a basic illustration, consider a simple NFA over the {0, 1} that recognizes strings ending in "01". It has three states: a start state q₀ (non-accepting), an intermediate state q₁ (non-accepting), and an accepting state q₂. From q₀, reading a 0 loops back to q₀, while reading a 1 moves to q₁. From q₁, reading a 0 moves to q₂, and reading a 1 stays in q₁. From q₂, both 0 and 1 loop back to q₂. This allows multiple paths for inputs like "101", where one branch (via q₀ → q₁ → q₂) reaches acceptance, while others may not.

Historical Context and Motivation

The nondeterministic finite automaton (NFA) was formally introduced by and in their 1959 paper "Finite Automata and Their Decision Problems," published in the Journal of Research and Development. In this work, they defined NFAs as a generalization of deterministic finite automata, allowing multiple possible transitions for a given state and input symbol, and proved their equivalence in expressive power to deterministic variants, establishing that both recognize the same class of regular languages. The primary motivation for introducing NFAs arose within the emerging field of theory, where they provided a more intuitive and concise means to model and prove properties of regular languages under operations like and . Unlike deterministic automata, which require explicit state expansions for such constructions, NFAs enable straightforward designs by permitting nondeterministic choices, deferring the complexity of determinization until necessary, thus simplifying theoretical proofs and language descriptions. NFAs contributed significantly to bridging abstract mathematical models of with practical algorithmic concerns, forming a foundational element of that influenced subsequent developments in . Their introduction predated the widespread adoption of technologies in the early 1960s, yet the underlying framework they formalized later became essential for and in programming language processors. Rabin and Scott's paper marked an early exploration of nondeterministic computation within finite automata, extending prior deterministic models and paving the way for broader applications of nondeterminism in .

Formal Definition

Core Components

A nondeterministic finite automaton (NFA) is formally defined as a five-tuple (Q, \Sigma, \delta, q_0, F), where Q is a of states, \Sigma is the finite input , \delta is the transition function, q_0 \in Q is the initial state, and F \subseteq Q is the set of accepting states. The set of states Q represents the possible internal configurations of the automaton, with the initial state q_0 serving as the starting point for processing any input string, and the accepting states F designating the successful halting positions upon completion of the input. The input alphabet \Sigma is a finite collection of symbols that constitute the strings to be recognized by the NFA. In contrast to a , the transition function \delta of an NFA permits nondeterminism by allowing multiple possible next states for a given current and input symbol, though its detailed structure is addressed separately. A of the NFA at any point in its computation is given by a pair (q, w), consisting of the current q \in Q and the remaining input w \in \Sigma^*.

Transition Function and States

The transition function of a nondeterministic finite automaton (NFA) specifies the possible state changes upon reading symbols from the input Σ, building on the of Q. Formally, it is defined as δ: Q × Σ → 2^Q, where 2^Q denotes the power set of Q, meaning δ(q, a) yields a of Q for any q ∈ Q and symbol a ∈ Σ. This may be empty (indicating no ), contain exactly one , or include multiple , thereby encoding the automaton's potential paths through the state space. The nondeterministic aspect arises precisely from this set-valued output: upon encountering symbol a in state q, the NFA can proceed to any state in δ(q, a), simulating parallel computation paths or "guesses" about the correct continuation. If δ(q, a) is empty, no path is possible from that point for that input; if it contains multiple elements, the NFA explores all options nondeterministically. This , introduced by and Scott, enables concise representations of computations that would require exponentially more states in deterministic models. In graphical depictions, the transition function is illustrated via state diagrams, with nodes representing states in Q and directed arrows labeled by symbols from Σ connecting a source state q to all target states in δ(q, a). Multiple arrows sharing the same label may depart from q, directly conveying the branching possibilities inherent to nondeterminism and aiding in the intuitive and of NFAs.

Language Acceptance

A string w \in \Sigma^* is accepted by a nondeterministic finite automaton (NFA) N = (Q, \Sigma, \delta, q_0, F) if there exists at least one sequence of transitions beginning at the initial state q_0, consuming the symbols of w in order, and terminating in some accepting state f \in F. This condition captures the nondeterministic nature of the automaton, where multiple transition paths may arise due to the transition function \delta: Q \times \Sigma \to \mathcal{P}(Q) mapping to subsets of states. The language recognized by N, denoted L(N), is formally defined as the set L(N) = \{ w \in \Sigma^* \mid \hat{\delta}(q_0, w) \cap F \neq \emptyset \}, where \hat{\delta}: Q \times \Sigma^* \to \mathcal{P}(Q) is the extended transition function. The function \hat{\delta} recursively computes the set of all states reachable from a given state after processing a string: \hat{\delta}(q, \epsilon) = \{q\} for the empty string \epsilon, and \hat{\delta}(q, wa) = \bigcup_{p \in \hat{\delta}(q, w)} \delta(p, a) for string w and symbol a \in \Sigma. Thus, acceptance holds whenever the set of reachable states after reading w includes at least one accepting state. Acceptance can also be viewed through the lens of in the NFA's computation , where each represents a after consuming a of w, and branches reflect nondeterministic choices. A string w is accepted if at least one path in this —from the root (initial ) to a leaf (after consuming w)—ends in an accepting . In edge cases, if no state in F is reachable from q_0 via any sequence of transitions, then L(N) = \emptyset, the empty language. Conversely, the NFA recognizes the universal language L(N) = \Sigma^* if for every w \in \Sigma^*, there exists at least one computation path ending in a state in F.

Basic Examples

Illustrative NFA for a Simple Language

To illustrate the core concepts of an NFA, consider a simple example that recognizes the language of all binary strings ending with the substring "01", denoted as L = (0 \cup 1)^* 01. This NFA uses three states: Q = \{q_0, q_1, q_2\}, with q_0 as the initial state and F = \{q_2\} as the set of accepting states. The transition function \delta: Q \times \Sigma \to 2^Q (where \Sigma = \{0, 1\}) is defined partially as follows, with undefined transitions leading to the empty set (causing those computation paths to terminate). The transition table is:
State01
q_0\{q_0, q_1\}\{q_0\}
q_1\emptyset\{q_2\}
q_2\emptyset\emptyset
In the transition diagram, q_0 has a self-loop labeled 1, and two outgoing arrows labeled 0 (one looping back to q_0 and one to q_1); q_1 has an arrow labeled 1 to q_2; q_2 is the accepting state with no outgoing transitions. Nondeterminism is evident in the transition from q_0 on input 0, which branches to both q_0 (continuing to process the string as part of the prefix) and q_1 (guessing that this 0 is immediately followed by the final 1). If the guess is premature and additional input follows without a matching transition, that branch dies; acceptance occurs if at least one complete path ends in q_2 after consuming the entire string. To demonstrate acceptance, trace the string "001". Begin with the current set of states S_0 = \{q_0\}.
  • After the first 0: S_1 = \delta(S_0, 0) = \{q_0, q_1\}.
  • After the second 0: S_2 = \delta(S_1, 0) = \delta(\{q_0\}, 0) \cup \delta(\{q_1\}, 0) = \{q_0, q_1\} \cup \emptyset = \{q_0, q_1\} (the branch to q_1 after the first 0 terminates here, but a new branch reaches q_1 from q_0).
  • After the third 1: S_3 = \delta(S_2, 1) = \delta(\{q_0\}, 1) \cup \delta(\{q_1\}, 1) = \{q_0\} \cup \{q_2\} = \{q_0, q_2\}.
Since q_2 \in S_3, the string is accepted via the path that guessed correctly on the second 0. For rejection, trace "00":
  • After the first 0: S_1 = \{q_0, q_1\}.
  • After the second 0: S_2 = \{q_0, q_1\}.
Since q_2 \notin S_2, the string is rejected.

NFA Recognizing of Languages

A nondeterministic finite automaton (NFA) can recognize the of two languages by combining the components of two separate NFAs that accept each language individually, leveraging nondeterminism in the transition function to explore paths corresponding to either language. This approach exploits the ability of NFAs to branch into multiple computational paths upon reading the same input symbol, allowing the machine to simulate the disjunction without requiring a full of states as in deterministic constructions. Consider the L = (0+1)^*0 \cup (0+1)^*1, which consists of all non-empty strings ending in either 0 or 1. To construct an NFA M = (Q, \Sigma, \delta, q_0, F) for L, where \Sigma = \{0, 1\}, begin with NFAs for each component language. The NFA for (0+1)^*0 has states \{q_0', q_1\}, start state q_0' (non-accepting), accepting state \{q_1\}, and transitions: \delta(q_0', 0) = \{q_1\}, \delta(q_0', 1) = \{q_0'\}, \delta(q_1, 0) = \{q_1\}, \delta(q_1, 1) = \{q_0'\}. The NFA for (0+1)^*1 has states \{p_0', p_1\}, start state p_0' (non-accepting), accepting state \{p_1\}, and transitions: \delta(p_0', 0) = \{p_0'\}, \delta(p_0', 1) = \{p_1\}, \delta(p_1, 0) = \{p_0'\}, \delta(p_1, 1) = \{p_1\}. The combined NFA uses states Q = \{q_0, q_0', q_1, p_0', p_1\}, start state q_0 (non-accepting), and accepting states F = \{q_1, p_1\}. Nondeterminism branches the computation at the initial step: upon reading the first symbol, the machine splits into the two sub-automata. The transition function is defined as follows:
From \ StateOn 0On 1
q_0\{q_1, p_0'\}\{q_0', p_1\}
q_0'\{q_1\}\{q_0'\}
q_1\{q_1\}\{q_0'\}
p_0'\{p_0'\}\{p_1\}
p_1\{p_0'\}\{p_1\}
Subsequent transitions follow the original NFAs within their respective branches, ensuring that paths ending in q_1 accept strings from the first language and paths ending in p_1 accept from the second. For the string "010", the computation begins at q_0 and, on the first 0, nondeterministically reaches \{q_1, p_0'\}. On the second symbol 1: from q_1 to q_0', from p_0' to p_1, yielding \{q_0', p_1\}. On the final 0: from q_0' to q_1, from p_1 to p_0', yielding \{q_1, p_0'\}. Since q_1 \in F, the string is accepted via the path through the 0-ending branch. Similarly, for "011", the sets evolve to \{q_1, p_0'\} after the first 0, \{q_0', p_1\} after the second 1, and \{q_0', p_1\} after the final 1; acceptance occurs via the path ending in p_1 \in F, corresponding to the 1-ending branch. This construction highlights the advantage of NFAs for modeling unions: it requires only the sum of the states from the component NFAs plus one additional start state (total 5 states here), enabling direct branching via nondeterminism without constructing an exponential-sized product as needed for the equivalent DFA, which would have up to 4 states but demands systematic enumeration of all state combinations for acceptance.

with Deterministic Finite Automata

Expressive Power

Nondeterministic finite automata (NFAs) and deterministic finite automata (DFAs) possess equivalent expressive power, as both recognize precisely the class of regular languages. Formally, for every NFA M, there exists a DFA M' such that L(M) = L(M'), and conversely, every DFA is a trivial special case of an NFA where the transition function is single-valued. This fundamental result establishes that nondeterminism does not extend the computational capability of finite automata beyond . The equivalence was proven by and in their 1959 paper, where they introduced NFAs and demonstrated that any language accepted by an NFA can be accepted by a DFA through a systematic construction process. At a high level, the proof shows that NFAs define regular languages and that determinization via state subset simulation preserves this regularity, ensuring the constructed DFA accepts exactly the same set of strings. The subset construction method serves as the key tool for this conversion. While equivalent in power, NFAs often provide a more concise representation for certain regular languages, potentially leading to exponential increases in state complexity when converted to DFAs. A classic example is the language L_n = \{ w \in \{0,1\}^* \mid the nth symbol of w from the end is 1 \}, which admits an NFA with n+1 states but requires a DFA with at least $2^n states. This illustrates the practical trade-off: NFAs enable succinct descriptions at the cost of nondeterminism, whereas DFAs offer unambiguous computation paths but may demand vastly more resources. This parity aligns with the Myhill-Nerode theorem, which characterizes s via the finiteness of classes under the relation distinguishing strings based on their suffixes' acceptance behavior. Both NFAs and DFAs realize these languages, with the theorem implying that the minimal DFA corresponds directly to the number of such classes, underscoring the shared theoretical foundation for minimality in recognition.

Subset Construction Method

The subset construction method provides a of the between nondeterministic finite automata (NFAs) and deterministic finite automata (DFAs) by transforming any NFA into an equivalent DFA that recognizes the same . In the subset construction, the state set Q' of the DFA is the power set $2^Q of the NFA's state set Q. The initial state of the DFA is the singleton set \{q_0\}, where q_0 is the NFA's initial state. The accepting states of the DFA are those s S \subseteq Q such that S \cap F \neq \emptyset, where F is the NFA's set of accepting states. The function \delta' of the DFA is defined for any S \subseteq Q and input symbol a \in \Sigma by \delta'(S, a) = \bigcup_{q \in S} \delta(q, a), where \delta is the NFA's function. This definition resolves nondeterminism by collecting all possible next states into a single DFA state. To implement the construction efficiently, begin with the initial DFA state \{q_0\} and use a (or ) to explore only the reachable subsets: enqueue the initial state, then repeatedly dequeue a subset S, compute \delta'(S, a) for each a \in \Sigma, and enqueue any new unvisited subsets. This prunes unreachable subsets from the power set, as the full $2^{|Q|} states are not always generated. In the worst case, the resulting DFA has $2^{|Q|} states, illustrating the potential for state explosion. For example, consider a simple NFA over \{a, b\} with states Q = \{q_0, q_1, q_2\}, initial state q_0, accepting state q_2, and transitions \delta(q_0, a) = \{q_1, q_2\}, \delta(q_1, b) = \{q_2\}, \delta(q_2, a) = \{q_0\}, \delta(q_2, b) = \emptyset (other transitions empty). The DFA starts at S_0 = \{q_0\}. On a, \delta'(S_0, a) = \{q_1, q_2\} = S_1 (accepting, since it contains q_2). From S_1 on b, \delta'(S_1, b) = \delta(q_1, b) \cup \delta(q_2, b) = \{q_2\} = S_2 (accepting). From S_2 on a, \delta'(S_2, a) = \{q_0\} = S_0. Other transitions lead to the \emptyset (a trap state). The reachable DFA states are \{q_0\}, \{q_1, q_2\}, \{q_2\}, \emptyset, showing how nondeterminism from q_0 on a creates combined states.

Epsilon-Nondeterministic Finite Automata

Extended Definition with Epsilon Transitions

An ε-nondeterministic finite automaton, or ε-NFA, extends the nondeterministic finite automaton by allowing transitions that do not consume input symbols, known as ε-transitions. These spontaneous moves enable the automaton to change states freely at any point, facilitating more modular and intuitive designs for recognizing regular languages. Formally, an ε-NFA is defined as the 5-tuple (Q, \Sigma, \delta, q_0, F), where Q is a finite set of states, \Sigma is a finite input alphabet, \delta: Q \times (\Sigma \cup \{\varepsilon\}) \to 2^Q is the extended transition function mapping a state and input (or ε) to a set of possible next states, q_0 \in Q is the initial state, and F \subseteq Q is the set of accepting states. ε-Transitions, labeled with the empty string ε, permit the automaton to proceed from one state to another without advancing the input pointer, effectively allowing optional or parallel paths in the computation. A string w \in \Sigma^* is accepted if there exists a sequence of states r_0, r_1, \dots, r_m with r_0 = q_0, r_m \in F, and labels a_1, a_2, \dots, a_m where each a_i \in \Sigma \cup \{\varepsilon\}, such that r_{i} \in \delta(r_{i-1}, a_i) for $1 \leq i \leq m, and the concatenation of the non-ε labels equals w. This accommodates any number of ε-moves before, after, or between symbol transitions. The use of ε-transitions is particularly motivated by their role in simplifying the conversion of regular expressions to NFAs, as in , which composes subpatterns via ε-links to produce an equivalent ε-NFA efficiently. A standard NFA arises as a special case when \delta is defined only over Q \times \Sigma, excluding ε from the domain.

Epsilon Closure Computation

The closure of a q in an \epsilon-NFA, denoted \epsilon-closure(q), is defined as the set of all states p \in Q such that there exists a path from q to p consisting solely of \epsilon-transitions, including q itself. This set captures all states implicitly reachable from q without consuming any input symbols, forming the basis for simulating \epsilon-NFAs by accounting for nondeterministic "free" moves. To compute \epsilon-closure(q), treat the \epsilon-transitions as edges in a over the state set Q, and perform a starting from q. Standard algorithms include (DFS) or (BFS): initialize a set with \{q\}, then iteratively explore all outgoing \epsilon-transitions from current states, adding newly reached states until no further states are discovered. This process handles cycles in the \epsilon-graph—such as self-loops or mutual \epsilon-transitions—via , where a visited set prevents redundant exploration, ensuring termination. For instance, consider an \epsilon-NFA with states q_0, q_1, q_2 where q_0 \xrightarrow{\epsilon} q_1, q_1 \xrightarrow{\epsilon} q_1 (a loop), and q_1 \xrightarrow{\epsilon} q_2; starting from q_0, BFS would enqueue q_1, then from q_1 add q_2 while skipping the loop revisit, yielding \epsilon-closure(q_0) = \{q_0, q_1, q_2\}. For a set of states S \subseteq Q, the epsilon closure is the union \epsilon-closure(S) = \bigcup_{q \in S} \epsilon-closure(q). Computation proceeds by applying the single-state algorithm to each element of S and merging the results, or more efficiently by initiating a single traversal from all states in S simultaneously. This operation is finite because Q is finite, guaranteeing that the closure sets are well-defined and computable in time polynomial in |Q|, typically O(|Q| + |E_\epsilon|) where E_\epsilon is the number of \epsilon-transitions.

Extended Transition Function

In the context of ε-nondeterministic finite automata (ε-NFAs), the extended transition function, often denoted as \hat{\delta}, provides a formal to determine the set of reachable from a given after processing an entire input , accounting for both symbol-driven transitions and spontaneous ε-moves. This extends the basic transition \delta: Q \times (\Sigma \cup \{\varepsilon\}) \to 2^Q to operate on over the input alphabet \Sigma, effectively interleaving ε-transitions with those triggered by input symbols. By incorporating the ε-closure—a set of reachable from a via zero or more ε-transitions without consuming input—it ensures that all possible nondeterministic paths, augmented by free ε-jumps, are considered during computation. The extended transition function is defined recursively as follows. For the base case, where the input is the \varepsilon, \hat{\delta}(q, \varepsilon) = \varepsilon-closure(q) for any state q \in Q, which initializes the reachable states including any immediate ε-reachable ones. Inductively, for a nonempty string wa where w \in \Sigma^* and a \in \Sigma, first compute the set of states T = \hat{\delta}(q, w); then, \hat{\delta}(q, wa) = \varepsilon-closure\left( \bigcup_{r \in T} \delta(r, a) \right). This inductive extension builds upon the string length, iteratively applying symbol transitions followed by ε-closures to capture the full nondeterminism over the input. A string w \in \Sigma^* is accepted by an ε-NFA M = (Q, \Sigma, \delta, q_0, F) if the set \hat{\delta}(q_0, w) contains at least one accepting from F, meaning there exists a from the start q_0 to some f \in F labeled with w possibly interspersed with ε-transitions. Computationally, this function interprets the ε-NFA's behavior as exploring all feasible ε-augmented paths in parallel, simulating the nondeterministic choice at each step without explicitly enumerating branches, which aligns with the automaton's ability to recognize regular languages equivalently to deterministic finite automata.

Conversion to Equivalent NFA

The conversion of an ε-NFA to an equivalent NFA without ε-transitions modifies the transition function to incorporate the effects of all possible ε-moves, preserving the recognized while eliminating ε-transitions. The set of states Q and the set of accepting states F remain identical to those of the original ε-NFA, as do the start state and alphabet \Sigma. The new transition function \delta' is defined for every state q \in Q and input symbol a \in \Sigma as \delta'(q, a) = \varepsilon\text{-closure}\left( \bigcup_{r \in \varepsilon\text{-closure}(q)} \delta(r, a) \right), where \varepsilon-closure(p) denotes the set of all states reachable from p via zero or more ε-transitions, including p itself. This formulation includes all states reachable by first traversing ε-transitions from q, then consuming a, and finally traversing additional ε-transitions; ε-loops are handled inherently by the closure operation, ensuring comprehensive reachability without explicit ε-handling during simulation. The resulting NFA accepts the same language as the original ε-NFA because its transitions simulate all valid paths in the ε-NFA: ε-moves are "free" and get folded into the input-consuming transitions via closures, allowing any accepting computation sequence in the ε-NFA to map directly to an equivalent path in the NFA, and vice versa. For illustration, consider a simple ε-NFA recognizing the (0 \cup 1)^* with states \{q_0, q_1\}, where q_0 is the start and accepting state, q_1 is accepting, there is an ε-transition from q_0 to q_1, and transitions from q_1 to q_1 on both 0 and 1. The ε-closures are \varepsilon-closure(q_0) = \{q_0, q_1\} and \varepsilon-closure(q_1) = \{q_1\}. The new transitions are \delta'(q_0, 0) = \{q_1\}, \delta'(q_0, 1) = \{q_1\}, \delta'(q_1, 0) = \{q_1\}, and \delta'(q_1, 1) = \{q_1\}, adding direct transitions from q_0 on 0 and 1 to q_1 to bypass the ε-move. This NFA can then be converted to an equivalent DFA using the subset construction method.

Advanced Properties

Closure Under Regular Operations

The class of languages recognized by nondeterministic finite automata (NFAs) is closed under the regular operations of , , and , meaning that applying these operations to NFA-recognizable languages yields another NFA-recognizable language. These properties follow from explicit constructions that combine NFAs while preserving language equivalence, often using transitions to enable nondeterministic branching. Such closures demonstrate that NFAs directly model the structure of languages defined by regular expressions. Union. Given two NFAs M_1 = (Q_1, \Sigma, \delta_1, q_{01}, F_1) and M_2 = (Q_2, \Sigma, \delta_2, q_{02}, F_2) with disjoint state sets Q_1 \cap Q_2 = \emptyset, construct a new NFA M = (Q, \Sigma, \delta, q_0, F) where Q = Q_1 \cup Q_2 \cup \{q_0\}, q_0 is a new start state, F = F_1 \cup F_2, and the transition function \delta extends \delta_1 and \delta_2 by adding \epsilon-transitions from q_0 to both q_{01} and q_{02}. This NFA accepts a string w \in \Sigma^* if there exists an accepting path starting from q_0 that nondeterministically enters either M_1 or M_2 and reaches a state in F_1 or F_2, thus L(M) = L(M_1) \cup L(M_2). Concatenation. For the same NFAs M_1 and M_2, construct M = (Q, \Sigma, \delta, q_{01}, F_2) with Q = Q_1 \cup Q_2 (disjoint), where \delta extends \delta_1 and \delta_2 by adding \epsilon-transitions from every state in F_1 to q_{02}. A computation on w = uv accepts if it processes u to reach some state in F_1 in the M_1 component, then nondeterministically transitions via \epsilon to q_{02} to process v and reach F_2, ensuring L(M) = L(M_1) \cdot L(M_2). Kleene star. Given an NFA M = (Q, \Sigma, \delta, q_0, F), construct M^* = (Q', \Sigma, \delta', q_0', F') where Q' = Q \cup \{q_0'\} with q_0' a new start state, F' = F \cup \{q_0'\} (making the start also accepting for the ), and \delta' extends \delta by adding \epsilon-transitions from q_0' to q_0 and from every state in F to both q_0 (for ) and q_0' (to exit). This allows computations to accept the directly or to concatenate zero or more accepting runs of M, so L(M^*) = L(M)^*. These constructions ensure state disjointness and equivalence to the original languages, implying that NFAs recognize exactly the regular languages, as regular expressions are built from these operations starting from singletons.

Decision Problems and Emptiness

Nondeterministic finite automata (NFAs) recognize regular languages, for which all standard decision problems are decidable, in contrast to context-free languages where problems like equivalence are undecidable. Key decidable properties include emptiness, membership, and universality, each solvable via graph-based algorithms that exploit the finite state space. The emptiness problem for an NFA N = (Q, \Sigma, \delta, q_0, F) determines whether its language L(N) = \emptyset, which holds if and only if no accepting state in F is reachable from the initial state q_0. To decide this, model the NFA as a directed graph with states Q as vertices and transitions \delta(q, a) as edges labeled by input symbols a \in \Sigma, then perform a graph search such as (DFS) or (BFS) starting from q_0 to check for paths to any state in F. This algorithm runs in time linear in the size of the graph, specifically O(|Q| + |E|), where |E| is the number of edges bounded by |\Sigma| \cdot |Q|^2. The membership problem decides, for a given string w \in \Sigma^* and NFA N, whether w \in L(N). This is solved by simulating all possible computation paths of N on w using BFS or DFS on the configuration , where nodes are subsets of Q representing possible states after reading prefixes of w, and edges correspond to transitions under the next symbol. The configuration graph has at most $2^{|Q|} nodes and |\Sigma| \cdot 2^{|Q|} edges per layer, leading to time in |Q| but in |w|, ensuring decidability. The universality problem checks whether L(N) = \Sigma^*, i.e., the NFA accepts every possible string over its alphabet. This is decidable by converting N to an equivalent (DFA) via subset construction, then testing if the complement DFA accepts the empty by checking to its accepting states. The overall process is decidable but potentially exponential in time due to the DFA construction, which can have up to $2^{|Q|} states.

Implementation Aspects

Simulation Algorithms

Simulation of a nondeterministic finite automaton (NFA) on an input string involves tracking all possible states the automaton could occupy after processing each symbol, thereby handling the inherent nondeterminism without constructing an equivalent . The standard on-the-fly simulation algorithm maintains a set of current s, starting from the initial , and iteratively updates this set based on the transition function for each input . This approach explores all nondeterministic paths in parallel, accepting the input if any path reaches an accepting at the end. The basic simulation for an NFA without epsilon transitions proceeds as follows: Initialize the current set S to contain the start state q_0. For each symbol a in the input string w, compute the next set as S \leftarrow \bigcup_{q \in S} \delta(q, a), where \delta is the transition function. After processing all symbols, check if S intersects the set of accepting states F; if so, accept w. This method ensures completeness by considering all branches simultaneously. Here is pseudocode for the basic NFA simulator:
function simulateNFA(NFA, w):
    S = {q0}  // current set of [state](/page/State)s
    for each [symbol](/page/Symbol) a in w:
        nextS = [empty set](/page/Empty_set)
        for each q in S:
            nextS = nextS ∪ δ(q, a)
        S = nextS
    return S ∩ F ≠ ∅
This algorithm runs in time proportional to |w| \times |Q| \times d, where d is the maximum out-degree per , though the set S can grow up to |Q| in the worst case. For epsilon-nondeterministic finite automata (), the simulation incorporates epsilon closures to account for transitions without consuming input. The initial set is the epsilon closure of \{q_0\}, the set of all states reachable from q_0 via zero or more epsilon transitions. For each input symbol a, first compute the move on a from the current set S as \bigcup_{q \in S} \delta(q, a), then take the epsilon closure of that result to obtain the next S. Epsilon closure is typically computed using (BFS) from the states in question, employing a to explore reachable states via epsilon edges without revisiting nodes. This extended process ensures all spontaneous moves are followed. Efficient data structures enhance practicality, especially when the state set |Q| is small (e.g., up to 64 or 256 for modern word sizes). Bit vectors or bitsets represent subsets of states compactly, allowing operations via bitwise OR, which accelerates step. For instance, precomputing bitsets for each state and symbol enables constant-time updates per step. BFS for can also use bitsets to mark visited states, avoiding exploration. These techniques are common in engines where state counts are manageable. An alternative to set-based simulation is , which performs a (DFS) over the nondeterministic choices, recursively exploring one path and retreating () upon dead ends. Pruning occurs by halting branches that cannot reach accepting states, but this risks for deep or long inputs with many branches, making it less suitable for large NFAs compared to the breadth-first set method.

Practical Complexity Considerations

The simulation of an NFA on an input of length |w| exhibits worst-case of O(|w| \cdot |Q|^2), where |Q| is the number of (assuming maximum transition set size O(|Q|)), as the current set S has size at most |Q| and each update scans O(|Q|^2) transitions. This leads to a of O(|Q|) during , as only the current set of reachable is maintained. In contrast, converting an NFA to an equivalent DFA via subset construction demands exponential space in the worst case, up to O(2^{|Q|}) , which can render full determinization impractical for large |Q|. The problem for an NFA—determining whether its contains any strings—can be resolved in linear time, specifically O(|Q| + |\delta|), where |\delta| denotes the number of transitions, by modeling the NFA as a and checking from the start state to any accepting state using or similar algorithms. This efficiency stems from the graph's structure, allowing a single pass to identify accepting paths without exploring nondeterministic branches exhaustively. To mitigate the exponential costs of full DFA conversion, especially in scenarios with repeated queries such as in compilers, lazy DFA builds only the reachable subsets of states during processing, avoiding upfront computation of the entire . This approach trades initial time for amortized efficiency over multiple inputs, as subsets are cached and reused. Direct NFA remains polynomial-time efficient for single inputs. NFAs demonstrate superior space efficiency over equivalent DFAs when transitions are sparse, as nondeterminism allows compact of languages that would require exponentially many states in a DFA; for instance, NFAs for certain remain succinct while encoding behaviors that explode the DFA state space.

Applications

Role in Regular Expression Matching

Nondeterministic finite automata (NFAs) play a central role in the implementation of (regex) engines by providing an efficient mechanism for against text strings. A key method for integrating NFAs into regex processing is , which systematically converts a regex into an equivalent ε-NFA. This algorithm parses the regex into a tree structure and builds the NFA by composing sub-automata for basic operations: links the accepting state of the first sub-NFA to the start state of the second via an ε-transition; union creates a new start state with ε-transitions to the starts of both sub-NFAs and connects their accepting states to a new accepting state via ε-transitions; and introduces a new start and accepting state with ε-loops to handle repetition. The resulting ε-NFA has a number of states and transitions linear in the size of the regex, enabling preprocessing in O(m) time, where m is the regex length. To match a regex against input text, the ε-NFA is simulated directly on the string, maintaining a set of possible states (a power set) as the simulation advances character by character. For each input symbol, the simulator computes the ε-closure of the current state set and follows all applicable transitions, propagating nondeterminism by exploring multiple paths simultaneously without backtracking in pure NFA implementations. This approach tracks active positions in the NFA, allowing acceptance if any path reaches an accepting state after processing the entire text. Regex engines like those in Perl and PCRE often employ a variant of this simulation combined with backtracking to handle captures and advanced features, though it can lead to exponential time in worst cases due to pathological patterns. In contrast, pure NFA simulators, as used in engines like RE2, prioritize linear-time guarantees by limiting features and using lazy DFA construction where needed. The advantages of using NFAs for regex matching stem from their ability to handle nondeterminism compactly, avoiding the state explosion that can occur when converting to deterministic finite automata (DFAs) for patterns with many alternations or repetitions. Preprocessing remains efficient at O(m), and simulation runs in O(m n) time in the worst case, where n is the text length, making it suitable for practical applications without full DFA materialization. For a simple example, the regex a|b is converted via to an ε-NFA with a start state S emitting ε-transitions to two intermediate states I1 and I2; I1 transitions to an accepting state A1 on 'a', and I2 to A2 on 'b', with ε-transitions from A1 and A2 back to a shared accepting state. This structure allows parallel exploration of the 'a' and 'b' branches during matching. In modern regex implementations, alternatives to Thompson's ε-NFAs, such as Glushkov automata (also known as position automata), offer optimizations by constructing ε-free NFAs directly from the regex. Glushkov's method identifies positions in the regex as states and defines transitions based on follow sets, resulting in an NFA with exactly as many states as symbols in the regex plus one, without ε-transitions, which simplifies and reduces overhead in hardware or high-performance software engines. These position NFAs maintain the O(m n) matching complexity while enabling faster execution in scenarios with dense transition tables, as explored in variants like dual Glushkov NFAs for .

Use in Compiler Design

In compiler design, nondeterministic finite automata (NFAs) play a central role in lexical analysis, the initial phase of the compiler front-end where source code is scanned to identify tokens such as identifiers, keywords, literals, and operators. These tokens are defined using regular expressions, which are converted into NFAs to recognize patterns in the input stream. For instance, an identifier might be matched by the regular expression [a-zA-Z][a-zA-Z0-9]*, while keywords like "if" or "while" use exact string matches. The NFA simulates the scanning process by allowing multiple possible transitions on the same input symbol, enabling flexible pattern recognition without immediate resolution of ambiguities. Tools such as Lex and its open-source successor Flex automate the construction of lexical analyzers by generating NFAs from user-specified rules. Lex, developed at , compiles these rules into an NFA and then converts it to a (DFA) for efficient simulation during scanning. Flex extends this approach with optimizations, producing C code that implements the DFA to process input character by character. To prioritize the longest match—ensuring that the most specific token, like a multi-character operator over a single one, is selected—the tools backtrack or use DFA states that track the extent of matches. Rule priority is enforced by the order of definitions in the input file, with earlier rules taking precedence in case of ties. Nondeterminism in NFAs allows compact representation of complex regular expressions, avoiding the exponential state explosion upfront, but it is typically resolved by subset construction to yield a DFA for linear-time scanning at runtime. This conversion ensures that the lexical analyzer runs in O(n) time for input length n, crucial for processing large source files. While the NFA form is retained internally for rule specification and initial construction, the DFA enables deterministic, high-speed tokenization without simulating multiple paths. A practical example involves recognizing keywords like "if" and "while" alongside identifiers. An NFA for keywords might have paths for exact matches, but conflicts arise if "if" prefixes an identifier like "ifvar". Resolution occurs via rule ordering: keyword patterns are listed before the general identifier rule, so the DFA accepts "if" as a keyword token first. Upon recognizing a token, the lexical analyzer returns it—along with attributes like its lexeme and type—to the subsequent syntactic parser for further processing. For invalid sequences, such as unrecognized characters, the analyzer reports a lexical error, often skipping to the next valid token or halting compilation.

References

  1. [1]
    [PDF] Finite Automata and Their Decision Proble'ms#
    Abstract: Finite automata are considered in this paper as instruments for classifying finite tapes. Each one- tape automaton defines a set of tapes, ...
  2. [2]
    [PDF] Nondeterministic Finite Automata
    A nondeterministic finite automaton (NFA) can have zero, one, or multiple transitions corre- sponding to a particular symbol. It is defined to accept the ...
  3. [3]
    [PDF] NFA with epsilon transitions
    We extend the class of NFAs by allowing instantaneous (ε) transitions: 1. The automaton may be allowed to change its state without reading the input symbol.
  4. [4]
    (PDF) Finite Automata and Their Decision Problems - ResearchGate
    Nov 12, 2016 · Finite automata are considered in this paper as instruments for classifying finite tapes. Each one-tape automaton defines a set of tapes, ...
  5. [5]
    [PDF] 1 Introducing Nondeterminism
    Nondeterministic Finite Automata (NFA). NFAs have 3 features when compared with DFAs. 1. Ability to take a step without reading any input symbol. 2. A state may ...
  6. [6]
    [PDF] q1 q2 q3 a b b a a, b - NJIT - New Jersey Institute of Technology |
    ... simplify the proof that the class of regular languages is closed under union. ... union, intersection, concatenation, Kleene-star, complementation. • NFA can ...
  7. [7]
    [PDF] CMPSCI 250: Introduction to Computation
    Nov 20, 2013 · • Nondeterministic Finite Automata. • The Language of an NFA. • The ... • The main reason to use NFA's is that they are easier to design ...
  8. [8]
    [PDF] Automata, Computability and Complexity: Theory and Applications.
    To introduce students to the elegant theory that underlies modern computing. 2. To motivate students by showing them that the theory is alive.
  9. [9]
    [PDF] History of Compilers - cs.wisc.edu
    The term 'compiler' was coined in the early 1950s by Grace Murray Hopper. The FORTRAN compiler of the late 1950s was one of the first real compilers.
  10. [10]
    [PDF] CSE 105 Homework 3
    See Sipser for the formal definition of an NFA. The following is the definition of a Multi-Start-State-NFA and its definition of computation. A Multi-Start ...
  11. [11]
    [PDF] Lecture Notes: Finite State Automata 1 DFAs
    In our case, a configuration (q, u) represents the current state of the automaton q ∈ Q, and the portion of the input u ∈ Σ∗ that still needs to be consumed.
  12. [12]
    [PDF] Introduction to Theory of Computation
    • the transition function of a DFA maps a state and a symbol to a state, whereas. • the transition function of an NFA maps a state and a symbol to a set of ...
  13. [13]
    Language Accepted by NFA
    Here we are going to formally define what is meant by an NFA (nondeterministic finite automaton) accepting a string or a language. We start with the concept of ...
  14. [14]
    [PDF] Nondeterministic Finite Automaton NFA Definition -transitions Equiv
    Jan 28, 2019 · NFA Definition. A nondeterministic finite automaton is a 5-tuple (Q, Σ, δ, q0,F) where. ▷ Q is a finite set of states. ▷ Σ is the finite ...
  15. [15]
    [PDF] Chapter 3 DFA's, NFA's, Regular Languages - UPenn CIS
    We give six definitions of the regular languages. 1. Using deterministic finite automata (DFAs). 2. Using nondeterministic finite automata (NFAs).
  16. [16]
    [PDF] Lecture 12 12.1 Nondeterminism 12.2 Nondeterministic Finite ...
    The idea of “nondeterministic” computations is to allow our algorithms to make “guesses”, and only require that they accept when the guesses are “correct”.
  17. [17]
    Design NFA with Σ = {0, 1} and accept all string of length at least 2.
    Jun 11, 2021 · Construct an NFA with Σ = {0, 1} which accepts all string ending with 01. In a given problem, the language accepts all strings ending with 01.
  18. [18]
    Use of NFA's for Closure Properties of Regular Languages
    NFAs are useful to show regular languages are closed under the last three operations (union, concatenation, star). ... The construction of the regular ...
  19. [19]
    [PDF] DFA can be exponentially larger than equivalent NFA
    A DFA can be exponentially larger than an equivalent NFA because the transformation can cause an exponential increase in states, as shown for language Ln.
  20. [20]
    [PDF] Lecture 5: Nondeterministic Automata
    Feb 3, 2009 · 1.1 NFA feature #1: Epsilon transitions. An NFA can do a state transition without reading input. This makes it easy to represent optional ...
  21. [21]
    Programming Techniques: Regular expression search algorithm
    Programming Techniques: Regular expression search algorithm. Author: Ken Thompson ... Published: 01 June 1968 Publication History. 727citation14,084Downloads.
  22. [22]
    None
    Below is a merged summary of Epsilon-NFAs and Epsilon-Closure based on all provided segments from "Introduction to Automata Theory, Languages, and Computation" (3rd Ed.) and related documents. To retain all information in a dense and organized manner, I will use a combination of narrative text and a table in CSV format for detailed comparisons across sources. The narrative will provide an overview, while the table will capture specific details such as definitions, algorithms, examples, page numbers, and URLs.
  23. [23]
    [PDF] Customer 1 3 2 4 a b d f c e g Store
    When coupled with the basis that δ^(q, ε) = ε-closure(q) lets us inductively define an extended transition function for a ε-NFA. Eliminating ε-Transitions ε- ...
  24. [24]
    Conversion of Epsilon-NFA to NFA - GeeksforGeeks
    May 10, 2025 · An Epsilon-NFA (ε-NFA) is a special type of NFA where transitions can happen without reading any input symbol, using epsilon (ε) transitions.
  25. [25]
    [PDF] Regular Languages: Closure Properties and Decidability
    Mar 17, 2025 · The regular languages are closed under all usual operations. (union, intersection, complement, concatenation, star). All usual decision problems ...
  26. [26]
    Decidability Properties of Regular and Contest Free Languages
    ANFA: N; AREX: P; ACFG: S; APDA: S. All CF languages are decidable; EQCFG is NOT decidable (proved in Chapter 5). Summary: New Notation for Specifying Languages.
  27. [27]
    [PDF] Properties of regular languages
    ▷ Emptiness of L(M): Easy, check whether some final state is reachable from the start state (cf. graph reachability, takes n2 steps). ▷ Universality of L ...
  28. [28]
    [PDF] 1 Decision Problems 2 Decidable Problems for Regular Languages
    We will use this idea in the algorithm to decide the emptiness problem for deterministic finite automata. Since every finite automaton has a finite set of ...
  29. [29]
    [PDF] CS/ECE-374: Lecture 22 - Reductions
    Apr 15, 2021 · How do we solve NFA Universality? Reduce it to DFA Universality? Given an NFA N, convert it to an equivalent DFA M, and use the. DFA ...Missing: decidable | Show results with:decidable
  30. [30]
    [PDF] Introduction to Theory of Computation Equivalence of DFA and NFA
    Feb 3, 2015 · Very useful in simulating/running. NFA on inputs. Page 17. Algorithm for Simulating an NFA. Algorithm. Keep track of the current state of each ...<|control11|><|separator|>
  31. [31]
    [PDF] CS 241 Lecture 8 - Non-Deterministic Finite Automata with
    Define E(S) to be the epsilon closure of a set of states S, that is, the set of all states reachable from S in 0 or more -transitions. Note this implies that S ...
  32. [32]
    [PDF] Harvard CS 121 and CSCI E-121 Lecture 4: Nondeterministic Finite ...
    Sep 12, 2013 · The subset construction shows that any n-state NFA can be implemented as a 2 n. -state DFA. NFA States DFA States. 4. 16. 10. 1024. 100. 2. 100.Missing: algorithm original
  33. [33]
    [PDF] Semantics, analysis and security of backtracking regular expression ...
    A more naive approach to NFA simulation is to perform a depth-first search of the NFA. ... The backtracking machine performs a depth-first search of a search tree ...
  34. [34]
    [PDF] Introduction to the Theory of Computation, 3rd ed.
    This is an electronic version of the print textbook. Due to electronic rights restrictions, some third party content may be suppressed.
  35. [35]
    [PDF] Lecture Notes on Emptiness Checking, LTL B ¨uchi Automata
    The old and next sets of the successor are initialized to be empty. In the following graph, we have taken this step for both nodes. old: {PUQ, Q} now: ∅.Missing: reachability | Show results with:reachability
  36. [36]
    Automating lexical analysis - CS [45]12[01] Spring 2023
    The intuition is that we make a DFA that simulates all possible executions of the NFA. At any given point in the input stream, the NFA could be in some set of ...
  37. [37]
    [PDF] Improving NFA Signature Matching w/ OBBDs?
    While non-determinism leads to frontiers of size O(|Q|) in NFAs, it also makes them space-efficient in two ways. First, NFAs for certain regular expressions ...<|separator|>
  38. [38]
    Regular Expression Matching Can Be Simple And Fast
    This article reviews the good theory: regular expressions, finite automata, and a regular expression search algorithm invented by Ken Thompson in the mid-1960s.
  39. [39]
    [PDF] A Unified Construction of the Glushkov, Follow, and Antimirov ...
    Many alternative techniques have been introduced in the last few decades to create -free automata representing regular expressions, in particular, Glushkov.
  40. [40]
    [PDF] Fast regular expression matching using dual glushkov NFA
    May 26, 2014 · 1. Another popular method of constructing an NFA from a regular expression is Glushkov's algorithm [3]. We call the automaton constructed by ...
  41. [41]
    [PDF] Lexical Analysis - SUIF Compiler
    Jun 25, 2008 · A nondeterministic finite automaton. (NFA) has: 1) A finite set of states with one start state and some (maybe none) final state. 2) An ...
  42. [42]
    Lex - A Lexical Analyzer Generator by M. E. Lesk and E. Schmidt
    Lex is a program generator designed for lexical processing ofharacter input streams. It accepts a high-level, problem oriented specification for character ...
  43. [43]
    Lexical Analysis With Flex, for Flex 2.6.2: Top
    ### Summary of Flex and Lexical Analysis (Flex 2.6.2 Manual)
  44. [44]
    [PDF] Topic 2: Lexing and Flexing - cs.Princeton
    Lexical Analysis Example. 5. Implementing a Lexer. 6. Regular Expressions ... NFA to DFA Conversion. 17. NFA to DFA Example. 18. DFA Representation. Page 7. 19.