Lecture 2

Nondeterministic Finite Automata
Ü
Nondeterminism means a choice of moves for an automaton.
Ü
We allow a set of possible moves (we achieve this by defining
the transition function so that its range is a set of possible
states).
Definition
A nondeterministic finite automaton (NFA) is defined by the
quintuple
A = (Q, Σ, δ, q0 , F ), where
Ü
Q, Σ, q0 , F are defined as for DFA’s but
Ü
δ : Q × (Σ ∪ {λ}) → 2Q .
Nondeterministic Finite Automata
There are three major differences to a DFA:
1. Range of δ is 2Q . This subset defines the set of all possible
states that can be reached.
I
Example: In δ(q1 , a) = {q0 , q2 }, either q0 or q2 could be the
next state of the NFA.
2. We allow λ as second argument of δ. NFA’s can make
transitions without consuming an input symbol.
3. The set δ(qi , a) may be empty. (There is no transition defined
for this specific situation).
Nondeterministic Finite Automata
Ü
Like DFA’s, NFA’s can be represented by transition graphs.
Ü
A string is accepted by an NFA if there is some sequence of
possible moves that will put the machine in a final state at
the end of the string.
Ü
A string is rejected if there is no possible sequence of
moves by which a final state can be reached.
Nondeterminism can be viewed as involving “intuitive” insight
by which the best move can be chosen at every state.
Ü
I
Example: NFA for L = {a3 } ∪ {a2n | n ≥ 1}
q1
a
q2
a
start
q0
a
a
q4
q5
a
a
q3
Nondeterministic Finite Automata
Ü
Example: NFA for L = {(10)n | n ≥ 0}
λ
1
start
q0
q1
0,1
q2
0
Ü
Note: Some transitions such as δ(q2 , 0) are unspecified.
Interpretation: δ(q2 , 0) = ∅.
Ü
Note: For the string 10 there are two alternative walks, one
leading to q0 , the other to q2 . Even though q2 is not a final
state, the string is accepted because one walk leads to a final
state.
Nondeterministic Finite Automata
Definition
For an NFA, the extended transition function is defined so
that δ ∗ (qi , w) contains qj if and only if there is a walk in the
transition graph from qi to qj labeled w.
This holds for all qi , qj ∈ Q and w ∈ Σ∗ .
Ü
Example:
start
q0
a
q1
λ
q2
λ
Suppose we want to find δ ∗ (q1 , a). There is a walk labeled a
involving two λ-transitions from q1 to itself. By using some the
λ-edges twice, we see that there is also walks involving
λ-transitions to q0 and q2 . Thus, δ ∗ (q1 , a) = {q0 , q1 , q2 }.
Nondeterministic Finite Automata
Definition
The language L accepted by an NFA A = (Q, Σ, δ, q0 , F ) is
defined as
L(A) = {w ∈ Σ∗ | δ ∗ (q0 , w) ∩ F 6= ∅}.
Algorithm for finding all labeled walks between two vertices
1. Starting from a given vertex, say vi , list all outgoing edges
(vi , vk ), (vi , vl ), . . .
At this point we have all walks of length one starting at vi .
2. For all vertices vk , vl , . . ., so reached, we list all outgoing
edges.
After we do this, we will have all walks of length two
originating at vi .
3. We continue this until all possibilities are accounted for.
→ If between two vertices vi and vj there is any walk labeled w,
then there must be some walk labeled w of length no more than
Λ + (1 + Λ)|w|, where Λ is the number of λ-edges in the graph.
⇒ Computing δ ∗ (vi , w): evaluate all walks of length at most
Λ + (1 + Λ)|w| originating at some vi .
Equivalence of Finite Automata
Definition
Let M1 and M2 be two finite automata. Then M1 and M2
are said to be equivalent if L(M1 ) = L(M2 ).
(That is, if they both accept the same language).
Ü
There are generally many automata for a given language, so
any DFA or NFA has many equivalent automata.
Ü
When we compare different classes of automata, the question
arises whether one class of automata is more powerful than
the other.
Ü
Can an automaton of one kind achieve something that cannot
be achieved by any automaton of another kind?
Ü
The classes of DFA’s and NFA’s are equally powerful:
For every language accepted by some NFA there exists a DFA
that accepts the same language.
Converting an NFA to a DFA
Theorem
Let L be the language accepted by an NFA
MN = (QN , Σ, δN , q0 , FN ).
Then there exists a DFA
MD = (QD , Σ, δD , {q0 }, FD )
such that
L = L(MD ).
⇒ We use the procedure NFA2DFA to construct the transition
graph GD for MD .
Converting an NFA to a DFA: procedure NFA2DFA
1. Create a graph GD with vertex {q0 }. Identify this vertex as
the initial vertex.
2. Repeat the following steps until no more edges are missing.
Take any vertex {qi , qj , . . . , qk } of GD that has no outgoing
edge for some a ∈ Σ.
∗ (q , a), δ ∗ (q , a), . . . , δ ∗ (q , a).
Compute δN
i
N j
N k
∗
∗ (q , a) ∪ . . . ∪ δ ∗ (q , a) = {q , q , . . . , q },
If δN (qi , a) ∪ δN
j
n
l m
N k
create a vertex for GD labeled {ql , qm , . . . , qn } if it does not
already exists.
Add to GD an edge from {qi , qj , . . . , qk } to {ql , qm , . . . , qn }
and label it with a.
3. Every state of GD whose label contains any qf ∈ FN is
identified as a final vertex.
4. If the NFA accepts λ, the vertex {q0 } in GD is also made a
final vertex.
Example NFA2DFA
Let Σ = {0, 1} and
L = {w ∈ Σ∗ | |w| ≥ 2 and 2nd letter from the right is a 1}.
Converting the following NFA MN
0,1
start
1
q0
0,1
q1
q2
into the following equivalent DFA MD
1
{q0 , q2 }
0
1
0
start
q0
1
0 0
{q0 , q1 }
L(MN ) = L(MD ) = L.
1
{q0 , q1 , q2 }
Example NFA2DFA
Converting the following NFA AN
a
start
a
q0
q1
λ
q2
b
into the following equivalent DFA AD
start
{q0 }
b
a
b
∅
a, b
{q1 , q2 }
a
References
LINZ, P. An introduction to Formal Languages and Automata.
Jones and Bartlett Learning, 2012.
HEGNER, S. Slides on finite automata.
Available at:
http://www8.cs.umu.se/kurser/5DV037/H10/Slides/index.html