0.265. Non-Deterministic Finite Automata¶
0.265.1. NFA: Non-Deterministic Finite Automata¶
Definition:
Define a NFA as (Q,Σ,δ,q0,F) where
Q is a finite set of states
Σ is the input alphabet (a finite set)
q0 is the initial state (q0∈Q)
F⊆Q is a set of final states
δ:Q×(Σ∪{λ})→2Q (2Q here means the power set of Q)
The specific difference from a DFA is that, while the result of δ for the DFA is some state q∈Q, the result of δ for the NFA is any subset of Q.
Non-deterministic means that it allows choices. From a state on input b, δ might include transitions to more than one state.
Example:
Figure 0.266.1: Example of NFA
In this example, δ(q0,a)={q1,q2}. (So, δ is no longer meets the mathematical definition of a function!)
Hopefully this one is easy to understand: We two disjoint paths, effectively giving us the union of two languages: L={aa}∪{abnb∣n≥0}.
Example:
L={(ab)n∣n>0}∪{anb∣n>0}.
Figure 0.266.2: Second Example of NFA: A simple “go this way or go the other way” choice.
Definition: qj∈δ∗(qi,w) if and only if there exists some walk from qi to qj labeled w.
From previous example:
δ∗(q0,ab)={q5,q6,q1}.
δ∗(q0,aba)={q3}.
Definition: For an NFA M, L(M)={w∈Σ∗∣δ∗(q0,w)∩F≠∅}
What does this mean? It means that the machine accepts all strings w from the set of all possible strings (generated from the alphabet Σ) such that the states reachable on w from the start state (q0) is in the final state set.
Note that it does not matter that there are paths where w can go wrong. What matters is that there is at least one way for w to be right.
Why nondeterminism? It makes it easier to describe a FA. What does “easier” mean? It could mean easier to comprehend when looking at it. Or maybe easier for the developer to write it. Or maybe smaller (in terms of the number of states). Or maybe it is more efficient (but probably not because non-determinism can be expensive to simulate). From a performance point of view, to determine if a string is accepted can take a LONG time to try out all possibilities. But, all that we care about right now is existance, not performance.
0.265.2. NFA vs. DFA: Which is more powerful?¶
Consider the following NFA.
Figure 0.266.3: An NFA.
Note
Q: What language is this?
A: Alternating a’s and b’s, starting with a.
Can this NFA be converted to a DFA?
Yes, because here is one. Note that the names of the states are chosen to help see their relationships to the original NFA.
Figure 0.266.4: A DFA that accepts the same language.
Note
Q: Is this a proof?
A: Yes. It is a proof by construction. The theorem is of the form “There exists X”. (In our case, it was written as “Does there exist a DFA that corresponds to this NFA?”) The proof is of the form “Here is an example of X”. (In our case, “Here is an acceptable DFA that answers the question”.)
Note
Try this conversion out using JFLAP. JFLAP can convert a NFA to a DFA.
Theorem 0.266.1 and Proof
Theorem: Given an NFA MN=(QN,Σ,δN,q0,FN), there exists a DFA MD=(QD,Σ,δD,q0,FD) such that L(MN)=L(MD).
Proof: We can use an algorithm to convert MN to MD.
QD=2QN
FD={Q∈QD∣∃qi∈Qwithqi∈FN}
Interpretation: A state qD in MD is final if any of the states from MN in the subset that qD corresponds to is final.
δD:QD×Σ→QD
Algorithm to construct MD
Start state is {q0}∪closure(q0) (Note that “closure” of q0 is a set of states defined as q0 plus all states reachable from q0 by λ transitions.
While can add an edge (that is, while missing a transition from δD)
Choose a state A={qi,qj,...,qk} with missing edge for a∈Σ
Compute B=δ∗(qi,a)∪δ∗(qj,a)∪…∪δ∗(qk,a)
Add state B if it doesn’t exist
Add edge from A to B with label a
Identify final states.
For a state in QD, if any of its base QN states are final, then it is final.
If λ∈L(MN), then make the start state final.
Intuition: Given a state in MN and a character, you can get to some subset of the states in MN. Consider that to be a state in MD. There are only so many subsets of the set of MN states: That would be members of the powerset of MD states.
Example:
Figure 0.266.5: Another NFA to convert
Let’s begin with the start state. Closure(q0) in MN is {q0,q1,q2}. So this is the start state.
For example: From MD state q0,q1,q2, determine the subset of states that can be reached from any of those states on letter a. This would be the subset q3,q4.
Note
Do this conversion using JFLAP. You should get the following result.
Answer:
Figure 0.266.6: Converted DFA
Conclusion: NFA adds no new capability. So why bother with the idea?
First, it wasn’t obvious that they are the same. NFA is a useful concept.
An NFA tends to be “smaller” and “simpler” than the equivalent DFA. (At least in terms of the number of states and transition. But perhaps the language of a NFA is harder for a person to grasp.)
Throughout the semester, we will do a lot of converting from one machine type to another. The conversion process might be easier to understand when the target is an NFA, and we know that this can always be converted in turn to a DFA.