✖

Non-deterministic finite automaton

The finite automaton from the previous chapters (we call it an FA or DFA) was always deterministic, which means that at every moment it was clear what the automaton would do. Now we will look at the more general concept of non-deterministic finite automata (NFAs), in which several transitions can leave the same state for the same symbol.

Example

Look at the following diagram of an automaton:

What is unusual about it? Two edges for the symbol 0 leave the node q0. If we try to accept the string 001, the automaton seems not to know whether it should stay in state q0 or follow the transition to q1. Such an automaton is non-deterministic.

Determinism in this sense means that in every situation it is clear what the automaton will do, with no room for ambiguity. A deterministic automaton has exactly one transition for each state and each symbol. Non-determinism means ambiguity: from one state there may be, say, three transitions for the same symbol.

So how does the automaton decide? A non-deterministic automaton always chooses (we sometimes say “guesses”) the transition that leads to accepting the string, if that is possible.

If we feed the string 001 into the automaton, it has these ways of processing it:

q0→q0→q0→q0q0→q0→q1→q2

plus the ways in which it does not read the whole string. In the first one it stays in state q0 all the time, which it certainly may, because the transition rules allow it. In the second branch it ventured into further states and finally reached state q2, which is an accept state. In this branch the automaton would accept the string. A non-deterministic automaton automatically chooses the branch in which it accepts the string, if such a branch exists.

How does it automatically choose the “favourable” branch? We can imagine that the non-deterministic automaton goes through all the possible branches, and if it finds a branch in which it accepts the string, then it accepts the string. How could it go through all the branches? Put simply, whenever it is in a state with n edges for the current symbol, the automaton copies itself n times and each copy goes a different way. This way we gradually go through all the possibilities.

Tree representation of a computation

Let’s modify the previous automaton slightly:

The non-determinism stays in the first state. The previous automaton accepted all strings that end with 01. This modified automaton accepts all strings that contain the substring 01. So it accepts, for example, the strings 101 and 0101. We can easily visualise all the branches of a computation using a tree. For the string 01010 the tree could look like this:

At the start we are in the start state q0. The input is the string 01010 and the first unread character is 0. Two transitions for the symbol 0 leave state q0, to states q0 and q1. So the tree has two children at this node: q0 and q1. Next we read the symbol 1. There is no ambiguity there, and we move to states q0 and q2. Then we read the character 0, and in the left branch there is ambiguity again, so we split the computation into two more branches. In one we stay in q0 again, and in the other we go to q1. And so on.

Notice that two branches in total end in state q2, an accept state. There are two branches of the computation by which the automaton can accept the string. So this automaton would accept the string 01010. We can try to build a similar tree for the string 1000.

The computation either stays in state q0 or moves to state q1, but from there it has nowhere to go, because the rest of the input is all zeros and state q1 has a transition only for the symbol 1. No branch of the computation ends in an accept state, so the automaton does not accept the string 1000.

Epsilon transitions

In non-deterministic automata we can also use epsilon transitions. These are transitions that we can take whatever symbol is on the input, without reading any symbol of the string. How could we write a non-deterministic automaton that accepts the empty string, all strings consisting only of 1s and strings of the form (10)n, that is 10, 1010, …?

We have split the automaton into two “sub-automata”. The upper one recognises the strings consisting of 1s, and the lower one handles the strings of the form (10)n. Let’s try to accept the string 111. At the start the automaton is in state q0 and the input is the string 111. Because epsilon transitions are available, it can decide to move to state q1 or q2, whichever suits it. We can see that moving to state q1 will suit it. The automaton moves there. Meanwhile the input is still the string 111! Only now does it start reading symbols of the string and moving from q1 to q1 again, until it has read the whole string and accepts it.

If we tried to accept 1010, the automaton would move to state q2 at the start, and only from there would it start processing symbols, and again it would accept the string.

For the string 1100 the automaton could do anything, because it has no way to accept it.

Epsilon transitions and non-determinism make our work much easier in many ways. For example, the proof that the union of regular languages is again a regular language can be written much more simply with epsilon transitions than in the case of deterministic finite automata. In that proof we had examples of these automata

and

and then we constructed a deterministic finite automaton that accepted the union of both languages. With epsilon transitions we can build such an automaton like this:

We simply add a new start state, and from it two epsilon transitions lead to the original start states of the original automata, and that is all. The automaton decides non-deterministically whether to try to accept the string with one “sub-automaton” or the other.

Definition of a non-deterministic automaton

We have already shown how a deterministic and a non-deterministic finite automaton differ, so it only remains to formalise it. We defined a deterministic finite automaton as a 5-tuple ⟨Q, Σ, δ, q0, F⟩, where Q is the set of states, Σ is the alphabet, δ is the transition function, q0 is the start state and F is the set of accept states. The only thing that changes in the definition of non-deterministic automata is the definition of the transition function.

In the deterministic version the function call δ(q, w) could return only one state, whereas in the non-deterministic version it can return several states, if several transitions are defined for one symbol. Because we also need to define epsilon transitions, we denote the set Σ ∪ {ε} by Σε, that is Σε = Σ ∪ {ε}. We define the function δ as

δ:Q×Σε→2Q

Here 2Q denotes the power set, the set of all subsets of Q. The formalisation of a computation is very similar to that for finite automata. A configuration of the automaton is again a pair ⟨q, w⟩ ∈ Q × Σ∗. A computation step ↦ is a relation on the set of configurations such that

⟨qi,w0w1…wn⟩↦⟨qj,w1…wn⟩,

if and only if qj ∈ δ(qi, w0). This sentence contains the biggest difference. In deterministic automata we wrote this condition as qj = δ(qi, w0), because the function δ always returned one state. The non-deterministic version of the transition function returns a set of states, which is why the symbol ∈ appears. We will again denote the reflexive and transitive closure of the relation by ↦ ∗. We say that the automaton accepts a string w if and only if there exists qf ∈ F such that

⟨q0,w⟩↦∗⟨qf,ε⟩.

Sources

Chapters: Finite-state machine, Complete automaton, Non-deterministic finite automaton, Simulating a non-deterministic automaton, Converting an NFA to a DFA