Skip to main content

Non-Deterministic Finite Automaton

Non-Deterministic Finite automaton (NFA) is an extension to the DFA that is simpler to design. It can be converted into a DFA and thus, it also accepts a regular language.

Components of NFA

Components

A NFA has the following components,

  • State set Q={qs,q1,q2,,qn}\mathcal{Q} = \{qs, q_1, q_2, \cdots, q_n\}
  • A vocabulary set V\mathcal{V}
  • A transition table T(q,v)\mathcal{T}(q, v)
  • A start state {qs}\{ qs \}
  • An accept state set F={qf,qf1,qf2,,qfn}\mathcal{F} = \{q_f, q_{f1}, q_{f2}, \cdots, q_{fn}\}

Unlike DFA, the transition table for NFA yields a set instead of a single state. In addition, the current state of NFA is also a set instead of a single state.

For the transition function, we extend it to take a set of states, that is,

T(iIqi,v)=i=IT(qi,v)\mathcal{T}(\bigcup_{i\in \mathbb{I}} q_i, v) = \bigcup_{i=\mathbb{I}} \mathcal{T}(q_i, v)

Where I\mathbf{I} is any set of indices.

This means that, unlike DFA, NFA has the capacity at staying at multiple states at the same time.

In the end, the accept condition is,

T({qs},v)F\mathcal{T}(\{q_s\}, v) \cap \mathcal{F} \neq \emptyset

Example

We consider the following example,

Let's test it on 001001, first do it graphically,

After taking in 00, both q0q0 and q1q1 are in the current state set,

After taking another 00, the current state set remains unchanged,

After taking 11, only q1q1 is in the current state set,

Because there exists a accept state in the final state set, we accept the string.

Let's consider what would happen with 00100010. Obviously, we can continue from before.

Because q1q1 transits to an empty set after taking in 00, so, now the current state set is \emptyset.

As we reached the end of the string, there exists no accept state in the current state set, so we reject the string.

Please note that, when the current state set is empty, no matter how many more words are going in, it will remain empty. This is what we call the dead state. If the prefix of a test string will lead the automaton into the dead state, we can early reject it.

Converting NFA to DFA

Based on the previous example, we can tell that, if we use the subset of Q\mathcal{Q} as the state of a new automaton, we will have the corresponding DFA automatontically. However, if we just list all subset one by one, there will be too many states. Although you can minimize that DFA, it's still hard to convert considering the exponential complexity.

So we have a way to iterate through all possible subset of Q\mathcal{Q}, and we call it the Kleene's algorithm.

We do it in the following steps. Still based on the previous example,

We will list the transition table in our new DFA as follows,

01
{qs}--

Again, the state of DFA is a subset of the state of NFA. The starting state is obviously, {qs}\{qs\}.

Now we must find all possible subset of Q\mathcal{Q}. We consider the now unfilled cell.

Because, T({qs},0)={q0,q1}\mathcal{T}(\{qs\}, 0) = \{q0, q1\} and T({qs},1)={q1}\mathcal{T}(\{qs\}, 1) = \{q1\}, we can fill in the table as follows,

01
{qs}{qs, q1}{q1}
{qs, q1}--
{q1}--

Because {qs}\{qs\} can transit to {qs,q1}\{qs, q1\} or {q1}\{q1\}, our new DFA must have theses stats as well. So we append {qs,q1}\{qs, q1\} and {q1}\{q1\} to the state set of DFA.

Then, we repeat this process until there is no more appending.

For {qs,q1}\{qs, q1\}, if it receives 00, it transit to itself. If it receives 11, it transits to {q1}\{q1\}.

For {q1}\{q1\}, if it receives 00, it transit to \emptyset. If it receives 11, it transits to itself.

Now we have,

01
{qs}{qs, q1}{q1}
{qs, q1}{qs, q1}{q1}
{q1}\emptyset{q1}
\emptyset--

Obviously, \emptyset transits to itself no matter what it receives. So,

01
{qs}{qs, q1}{q1}
{qs, q1}{qs, q1}{q1}
{q1}\emptyset{q1}
\emptyset\emptyset\emptyset

Now, there are no unfilled cells nor new rows. Thus, this is the transition table for DFA.

Then we mark the accept state of the DFA- it's simple. If a new state set in DFA contains an accept state in NFA, it's an accept state in DFA.

Thus,

01
{qs}{qs, q1}{q1}
{qs, q1}*{qs, q1}{q1}
{q1}*\emptyset{q1}
\emptyset\emptyset\emptyset

The state of the new DFA is, {{qs},{qs,q1},{q1},}\{ \{qs\}, \{qs, q1\}, \{q1\}, \emptyset \}, and the accept state is, {{qs,q1},{q1}}\{ \{qs, q1\}, \{q1\} \}.

We can draw the automatonton as follows,

The Grammar of NFA

NFA Grammar is Regular Grammar

With the same method as the DFA, we can have the following rules.

AaBAaA \rightarrow aB \\ A \rightarrow a

This is exactly the regular grammar. The grammar of NFA is the regular grammar, the regular grammar is the grammar of NFA. And unlike DFA that poses extra constraints, NFA does not pose any constraints. The grammar of NFA is the regular grammar.

From Regular Grammar to NFA

For a regular grammer, it takes either of the forms,

AaBA \rightarrow aB

Or,

AaA \rightarrow a

If it is the first case, then we create two states AA and BB if there doesn't exist one. Then adding an edge with label aa from AA to BB.

If it's the second case, we create a state AA and an accept state qaqa if there doesn't exist one. Then we add an edge from AA to qaqa with label aa.

By constructing the automaton, we can test all strings in the given regular language.

This is because, for any sentence vv generated by the regular grammar.

SvS \Rightarrow^* v

Let's break it into,

Sv1v2vS \Rightarrow v_1 \Rightarrow v_2 \ldots \Rightarrow v

Then for each intermediate sentence viv_i,

T(qs,0@v)=q1\mathcal{T}(qs, 0@v) = q_1

Then,

T(qi,i@v)=qi+1\mathcal{T}(q_i, i@v) = q_{i + 1}

So there exists a one to one map from,

T(qi,i@v)=qi+1\mathcal{T}(q_i, i@v) = q_{i + 1}

To,

vivi+1v_i \Rightarrow v_{i + 1}

Thus, derivation is state transition. And thus, we can prove that all regular languages can be represented by NFA.

And because NFA cna be converted to a DFA, we can prove that all regular languages can be represented by DFA.