Title

ECS 120 Theory of Computation
Nondeterministic finite automata (NFA)
David Doty (adapted from slides authored by Julian Panetta)
University of California, Davis

Nondeterministic finite automata

  • Next model of computation is the Nondeterministic Finite Automaton (NFA).

    04e9016a-ab15-49f5-b294-8fa0b73122ce a a 04e9016a-ab15-49f5-b294-8fa0b73122ce->a a->a 0,1 b b a->b 1 c c b->c 0,ε d d c->d 1 d->d 0,1
    Example NFA

What’s different here vs. a DFA?

  • NFAs are a generalization of DFAs: every DFA is a valid NFA.
  • NFAs offer additional features that make them “easier to program” than DFAs:
    • They can have multiple transitions for the same state and input symbol.
    • They can have \(\emptystring\)-transitions (that do not consume any input symbols).
    • They allow states to have no transitions for a given input symbols.
  • They are less straightforward to simulate, hence “declarative”.
  • We’ll later prove that it’s possible to convert any NFA into a (usually larger) DFA.

Operation of an NFA

  • Intuitively, upon reading each symbol, an NFA follows all possible paths.
    • We can think of this as the machine “forking” into multiple copies to take each available transition.
    • For each copy, if there is no transition for the consumed symbol, that copy “dies”.
  • The NFA accepts the input string if any of its “copies” is in an accept state after reading the entire input string. Otherwise, it rejects.

04e9016a-ab15-49f5-b294-8fa0b73122ce a a 04e9016a-ab15-49f5-b294-8fa0b73122ce->a a->a 0,1 b b a->b 1 c c b->c 0,ε d d c->d 1 d->d 0,1

Example processing the string \(010110\):

Symbol Read Start a 0 a 1 a b c 0 a c 1 a b c d 1 a b c d d 0 a c d d

← Accept!

Operation of an NFA

  • A simpler way to simulate the NFA: keep track of the set of states reachable after reading each symbol.
data/images/nfa/n1/diagram.svg

Example processing the string 010110:

010110

data/images/nfa/n1/computation_tree_010110.svg
  • Begin in the state(s) \(\{a\}\).

    data/images/nfa/n1/state_a.svg
    data/images/nfa/n1/computation_tree_010110_level0.svg
  • Read 0 and transition to state(s) \(\{a\}\).

    010110

    data/images/nfa/n1/state_a.svg
    data/images/nfa/n1/computation_tree_010110_level1.svg
  • Read 1 and transition to state(s) \(\{a, b, c\}\).

    010110

    data/images/nfa/n1/state_a_b_c.svg
    data/images/nfa/n1/computation_tree_010110_level2.svg
  • Read 0 and transition to state(s) \(\{a, c\}\).

    010110

    data/images/nfa/n1/state_a_c.svg
    data/images/nfa/n1/computation_tree_010110_level3.svg
  • Read 1 and transition to state(s) \(\{a, b, c, d\}\).

    010110

    data/images/nfa/n1/state_a_b_c_d.svg
    data/images/nfa/n1/computation_tree_010110_level4.svg
  • Read 1 and transition to state(s) \(\{a, b, c, d\}\).

    010110

    data/images/nfa/n1/computation_tree_010110_level5.svg
  • Read 0 and transition to state(s) \(\{a, c, d\}\).

    010110

    data/images/nfa/n1/state_a_c_d.svg
    data/images/nfa/n1/computation_tree_010110_level6.svg
  • Since an accept state is highlighted, the NFA accepts the string.

Operation of an NFA

  • A simpler way to simulate the NFA: keep track of the set of states that are “active” after reading each symbol.
data/images/nfa/n1/diagram.svg

Which of the following strings are accepted by this NFA?

This NFA decides the language: \[\setbuild{w \in \{0, 1\}^*}{w \text{ has 101 or 11 as a substring }}\]

Example NFAs

Let’s design an NFA that accepts binary strings with a \(1\) in the third-to-last position: \(\setbuild{w \in \{0, 1\}^*}{w[|w| - 2] = 1}\).

data/images/nfa/third_to_last/nfa.svg

DFA for same language?

data/images/nfa/third_to_last/dfa.svg

Example NFAs

What language over the alphabet \(\{0, 1\}\) does this NFA decide?

d0c42ec3-913f-4388-9fb6-533f6d3576da a a d0c42ec3-913f-4388-9fb6-533f6d3576da->a

Formal definition of an NFA (syntax)

Key differences from DFAs that we must encode:

  1. Any number of transitions (including 0) for the same state and input symbol.
    Make the transition function output a set of states!
  2. The possibility of \(\emptystring\)-transitions, which do not consume any input symbols.
    Include \(\emptystring\) in the set of possible second arguments to transition function: \(\Sigma_\emptystring \defeq \Sigma \cup \{\emptystring\}\)

A nondeterministic finite automaton (NFA) is a 5-tuple \((Q, \Sigma, \Delta, s, F)\) where:

  1. \(Q\) is a finite set of states.
  2. \(\Sigma\) is a finite set of symbols called the input alphabet.
  3. \(\Delta: \fragment{Q \times} \fragment{\Sigma_\emptystring \to} \fragment{\powerset(Q)}\) is the transition function.
  4. \(s \in Q\) is the start state.
  5. \(F \subseteq Q\) is the set of accept states.

For convenience, we can assume that if \(\Delta(q, a)\) is not explicitly defined for \(q \in Q\) and \(a \in \Sigma_\emptystring\), then \(\Delta(q, a) = \emptyset\).

Translating NFA state diagrams to formal definitions

04e9016a-ab15-49f5-b294-8fa0b73122ce a a 04e9016a-ab15-49f5-b294-8fa0b73122ce->a a->a 0,1 b b a->b 1 c c b->c 0,ε d d c->d 1 d->d 0,1
  1. \(Q = \{a, b, c, d\}\)

  2. \(\Sigma = \{0, 1\}\)

  3. \(\Delta\) is defined by the following lookup table:

    \(0\) \(1\) \(\emptystring\)
    \(a\) \(\fragment{\{a\}}\) \(\fragment{\{a, b\}}\) \(\fragment{\emptyset}\)
    \(b\) \(\fragment{\{c\}}\) \(\fragment{\emptyset}\) \(\fragment{\{c\}}\)
    \(c\) \(\fragment{\emptyset}\) \(\fragment{\{d\}}\) \(\fragment{\emptyset}\)
    \(d\) \(\fragment{\{d\}}\) \(\fragment{\{d\}}\) \(\fragment{\emptyset}\)
  4. \(a\) is the start state.

  5. \(F = \{d\}\)

Alternative specification of an NFA: Sets of transitions

  • We can equivalently formalize the arrows of the NFA state diagram as a set of transitions.

  • This is similar to how we defined the productions of a CFG as a set of rules.

    04e9016a-ab15-49f5-b294-8fa0b73122ce a a 04e9016a-ab15-49f5-b294-8fa0b73122ce->a a->a 0,1 b b a->b 1 c c b->c 0,ε d d c->d 1 d->d 0,1
  • This NFA has the transitions:
    \[ \{ a \stackrel{0}{\to} a, \quad a \stackrel{1}{\to} a, \quad a \stackrel{1}{\to} b, \quad b \stackrel{0}{\to} c, \quad b \stackrel{\emptystring}{\to} c,\quad c \stackrel{1}{\to} d, \quad d \stackrel{0}{\to} d, \quad d \stackrel{1}{\to} d \} \]

  • Each of these transitions is formally an ordered triple \((q_i, \sigma, q_j) \in Q \times \Sigma_\emptystring \times Q\) where \(q_i\in Q\) is the source state, \(\sigma \in \Sigma_\emptystring\) is the symbol read, and \(q_j \in Q\) is the destination state.

  • We can therefore specify the NFA’s transitions as some subset of \(Q \times \Sigma_\emptystring \times Q\).

  • This formulation makes it easy to talk about adding or removing transitions in the next chapter when we dealing with “constructions”: algorithmic specification of automata.

Formal definition of NFA computation (semantics)

The finite automaton \(N = (Q, \Sigma, \Delta, q_0, F)\) accepts a string \(w\)
if we can write \(w= y_1 y_2 \ldots y_n \in \Sigma_\emptystring^n\); note possibly \(n>|w|\) since some \(y_i\) may be \(\varepsilon\) and there is a sequence of states \(r_0, r_1, \ldots, r_n \in Q\) such that:

  1. \(r_0 = q_0\)
  2. \(r_n \in F\)
  3. \(r_i\ {\color{red} \in}\ \Delta(r_{i-1}, {\color{red} y_i})\) for all \(1 \leq i \leq n\)

We refer to the sequence \(r_0, r_1, \ldots, r_n\),
or the sequence of transitions followed, as a computation sequence of \(N\) on input \(w\).

\(N\) decides a language \(A\) (denoted \(L(N) = A\)) if \(A = \setbuild{w \in \Sigma^*}{N \text{ accepts } w}\).

A language/decision problem \(A\) is NFA-decidable if there is an NFA \(N\) such that \(L(N) = A\).