ToA - Lecture 07 08 - Nondeterministic Finite Automata Transition Graphs
ToA - Lecture 07 08 - Nondeterministic Finite Automata Transition Graphs
qj
1
qi
1
qk
Nondeterministic Finite Automata (NFA)
• A Nondeterministic Finite Automaton (NFA) consists of:
• a finite set of states
• a finite set of input symbols (alphabet)
• a start state
• set of accepting states
• a transition function, which is a mapping between
• An NFA is also defined by the 5-tuple:
How to use an NFA?
• Input: a word w in
• Question: Is acceptable by the NFA?
• Steps:
• Start at the “start state”
• For every input symbol in the sequence do
• Determine all possible next states from all current states, given the current input symbol in w and the transition
function
• If after all symbols in are consumed, and if at least one of the current states is accepting state, then
accept ; Otherwise, reject .
NFA Example – Substring
• Start State =
• F=
0 1
• Regular Expression:
*
What is an “Error State”?
• A DFA for recognizing the key word “while”
w h i l e
q0 q1 q2 q3 q4 q5
• Induction
• Let
• for
• Then,
DFA vs. NFA
• DFA • NFA
1. All transitions are deterministic 1. Some transitions could be non-deterministic
a) Each transition leads to exactly one state a) A transition could lead to a subset of states
2. For each state, transition on all possible 2. Not all symbol transitions need to be defined
symbols (alphabet) should be defined explicitly (if undefined will go to an error state
3. Accepts input if the last state visited is in F – this is just a design convenience, not to be
confused with “non-determinism”)
4. Sometimes harder to construct because of
the number of states 3. Accepts input if one of the last states is in F
5. Practical implementation is feasible 4. Generally easier than a DFA to construct
5. Practical implementations limited but
emerging (e.g., Micron automata processor)
Equivalence of DFA & NFA
• Theorem:
• A language L is accepted by a DFA if and only if it is accepted by an NFA.
• Proof:
• If part:
• Every NFA can be converted to an equivalent DFA
• Only-if part is trivial:
• Every DFA is a special case of an NFA where each state has exactly one transition for every input symbol. Therefore,
if L is accepted by a DFA, it is accepted by a corresponding NFA.
Relaxing the Restrictions on Inputs
• Let’s consider a very specialized FA that accepts only the word “baa”
• Beginning at the start state, anything but the string baa will drop down into the garbage
collecting state and never be seen again.
• Even the string baabb will fail.
• Hence, the language accepted by this FA is
Relaxing the Restrictions on Inputs
• We saw that we could build an FA that accepts only the word baa.
• The example we gave required five states primarily because an FA can read only one letter from the
input string at a time.
• Suppose we designed a more powerful machine that could read either one or two letters of
the input string at a time and could change its state based on this input information.
• We might design a machine like the one below:
Relaxing the Restrictions on Inputs
• If we are interested in a machine that accepts only the word baa, why stop at if the machine
can read just two letters at a time?
• A machine that accepts this word and that can read up to three letters at a time from the input string
could be built with even fewer states:
Relaxing the Restrictions on Inputs
• If we interpret the picture on the right as an FA-like machine, we see that not only does baa
alone get to the final state, but all other input strings end up nowhere.
• If we start in the minus state and the first letter of the input is an a, we have no direction as to what to
do.
• The picture on the left at least tells us that when the input fails to be of the desired form, we
must go to the garbage collection state and read through the rest of the input string in the
full knowledge that we can never leave there.
• The picture on the right gives us another problem with the input baabb.
• The first three letters take us to the accept state, but then something undetermined (presumably bad)
happens when we read any more of the input letters.
• According to the rules of FAs, one cannot stop reading input letters until the input string completely
runs out.
Relaxing the Restrictions on Inputs
• Rather than an imaginary error-state, it is more standard to introduce a new term to
describe what happens when an input is running on a machine and gets into a state from
which it cannot escape though it has not yet been fully read.
• Definition
• “When an input string that has not been completely read reaches a state (final or otherwise) that it
cannot leave because there is no outgoing edge that it may follow, we say that the input (or the
machine) crashes at that state. Execution then terminates and the input must be rejected.”
Relaxing the Restrictions on Inputs
• If we hypothesize that a machine can read one or two letters at a time, then one can be built
using only two states that can recognize all words that contain a double letter:
• If we are going to bend the rules to allow for a machine like the last one
• We must realize that we have changed something more fundamental than just the way the edges are
labeled, or the number of letters read at a time.
• This last machine makes us exercise some choice in its running. We must decide how many letters to
read from the input string each time we go back for more.
Relaxing the Restrictions on Inputs
• Let us say, for example, that the input string is baa, It is easy to see how this string can be
accepted by this machine.
• We first read the letter b, which leaves us back at the start state by taking the loop on the left. Then we
decide to read both letters aa at once, which allows us to take the path to the final state where we end.
• However, if after reading the single character b, we then decided to read the single character a, we
would loop back and be stuck at the start state again. When the third letter is read, we would still be at
the starting post. We could not then accept this string.
• There are two different paths that the input baa can take through this machine.
• This is totally different from the situation we had before, especially because one path leads to
acceptance and one to rejection.
Relaxing the Restrictions on Inputs
• The result of these considerations is that if we are going to change the definition of our
abstract machine to allow for more than one letter to be read at a time, we must also change
the definition of acceptance.
• “We shall say that a string is accepted by a machine if there is some way it could be processed to arrive
at a final state.”
• We now have observed many of the difficulties inherent in expanding our definition of
"machine" to allow word-labeled edges (or, equivalently, to reading more than one letter of
input at a time).
• We shall leave the definition of the finite automaton alone and call these new machines transition
graphs.
Transition Graph
• A transition graph, abbreviated TG, is a collection of three things:
• A finite set of states, at least one of which is designated as the start state (-) and some (maybe none) of
which are designated as final states (+)
• An alphabet of possible input letters from which input strings are formed.
• A finite set of transitions (edge labels) that show how to go from some states to some others, based on
reading specified substrings of input letters (possibly even the null string ).
Transition Graph
• We should note the following from the definition of TG:
• Clause 3 in the definition means that every edge is labeled by some string or strings of letters, not
necessarily only one letter.
• We are also not requiring that there be any specific number of edges originating from any state. Some
states may have no edge coming out of them at all , and some may have thousands (e.g., edges labeled
a, aa, aaa, aaaa, …) .
• A successful path through a transition graph is a series of edges forming a path beginning at some start
state (there may be several) and ending at a final state.
• If we concatenate in order the string of letters that label each edge in the path, we produce a word that
is accepted by this machine.
Transition Graph – Example
• The path from state 1 to state 2 to state 3 back to state 1 then to state 4 corresponds to the
• string .
• This is one way of factoring the word , which, we now see, is accepted by this machine.
• Some other words accepted are and .
• When an edge is labeled with the string , it means that we can take the path (without
consuming any letters from the input string).
Transition Graph – Example
• If we are presented with a particular string of a's and b's to run on a given TG, we must
decide how to break the word into substrings that might correspond to the labels of edges in
a path.
• If we run the input string on the machine, we see that from state 1, where we must start, we
can proceed along the outgoing edge labeled .
• This word then moves along the edge from state 1 to state 2. The input letters abb are read and
consumed.
• What is left of the input string is , and we are now in state 2.
• From state 2, we must move to state 3 along the -edge. At state 3 , we cannot read aa, so we must read
only and go to state 4.
• Here, we have a left in the input string but no edge to follow, so despite our best efforts we
still must crash and reject the input string .
Transition Graph – Example
• Because we have allowed some edges to be traversed for free, it is logical to allow for the
possibility of more than one start state.
• Important Note: Every FA is also a TG. However, NOT every TG satisfies the definition of an
FA
Transition Graph – Examples
Transition Graph – Examples
Transition Graph – Examples
Generalized Transition Graph
• A generalized transition graph (GTG) is a collection of three things:
• A finite set of states, of which at least one is a start state, and some (maybe none) are final states.
• An alphabet of input letters.
• Directed edges connecting some pairs of states, each labeled with a regular expression.
Generalized Transition Graph - Example
Nondeterminism
Thank You
Any Questions?