Automata
Automata
Automata
Nondeterminism
Given a current state of the machine and input symbol to be read, the next state is not uniquely
determined.
Comparison to DFAs
-Transitions
Transitions without reading input symbols
Example 1. The British spelling of “color” is “colour”. In a web search application, you may want
to recognize both variants.
1
c o l o u r
q0 q1 q2 q3 q4 q5 q6
No transitions
q0 1 q1 ...
Example 2.
Figure 4: No 0-transition out of initial state
In the above automaton, if the string starts with a 0 then the string has no computation (i.e.,
rejected).
Multiple Transitions
0, 1 0, 1
q 0 q0 0 q00 1 qp
At each step, the machine “forks” a thread corresponding to one of the possible next states.
• If a state has an -transition, then you fork a new process for each of the possible -transitions,
without reading any input symbol
• If the state has multiple transitions on the current input symbol read, then fork a process for
each possibility
• If from current state of a thread, there is no transition on the current input symbol then the
thread dies
2
Parallel Computation View: An Example
0, 1 0, 1
0 0 1
q q0 q00 qp
q
0 0
q00 q0 q
1 1 1
qp X q
0 0 0
qp q q0 q00
0 0 0 0 0
qp q0 q q00 X
q00
Nondeterministic Acceptance
Parallel Computation View
Input is accepted if after reading all the symbols, one of the live threads of the automaton is in a
final/accepting state. If none of the live threads are in a final/accepting state, the input is rejected.
0, 1 0, 1
0
q 0 q0 q00 1 qp
0 1 0 0
0100 is accepted because one thread of computation is q → q0 → q00 → qp → qp → qp
3
The machine magically guesses the choices that lead to acceptance
c o l o u r
q0 q1 q2 q3 q4 q5 q6
After seeing “colo” the automaton guesses if it will see the british or the american spelling. If
it guesses american then it moves without reading the next input symbol.
• If there is a sequence of choices that will lead to the automaton (not “dying” and) ending up
in an accept state, then those choices will be magically guessed
• On the other hand, if the input will not be accepted then no guess will lead the to automaton
being in an accept state
– On the input “colobr”, whether automaton Mcolor guesses british or american, it will
not proceed when it reads ‘b’.
2 Formal Definitions
2.1 NFAs
Nondeterministic Finite Automata (NFA)
Formal Definition
• q0 ∈ Q initial state
• F ⊆ Q final/accepting states
Example of NFA
4
0, 1 0, 1
q 0 q0 0 q00 1 qp
Formally, the NFA is M001 = ({q , q0 , q00 , qp }, {0, 1}, δ, q , {qp }) where δ is given by
δ(q , 0) = {q , q0 } δ(q , 1) = {q } δ(q0 , 0) = {q00 }
δ(q00 , 1) = {qp } δ(qp , 0) = {qp } δ(qp , 1) = {qp }
δ is ∅ in all other cases.
w
Definition 4. For an NFA M = (Q, Σ, δ, q0 , F ), string w, and states q1 , q2 ∈ Q, we say q1 −→M q2
w
if there is one thread of computation on input w from state q1 that ends in q2 . Formally, q1 −→M q2
if there is a sequence of states r0 , r1 , . . . rk and a sequence x1 , x2 , . . . xk , where for each i, xi ∈ Σ∪{},
such that
• r0 = q1 ,
• for each i, ri+1 ∈ δ(ri , xi+1 ),
• rk = q2 , and
• w = x1 x2 x3 · · · xk
Example Computation
0, 1 0, 1
0
q 0 q0 q00 1 qp
5
0100
q −→M qp because taking r0 = q , r1 = q0 , r2 = q00 , r3 = qp , r4 = qp , r5 = qp , and x1 = 0,
x2 = , x3 = 1, x4 = 0, x5 = 0, we have
• x1 x2 · · · x5 = 0100 = 0100
• ri+1 ∈ δ(ri , xi+1 )
Acceptance/Recognition
w
Definition 5. For an NFA M = (Q, Σ, δ, q0 , F ) and string w ∈ Σ∗ , we say M accepts w iff q0 −→M q
for some q ∈ F .
Definition 6. The language accepted or recognized by NFA M over alphabet Σ is L(M ) = {w ∈
Σ∗ | M accepts w}. A language L is said to be accepted/recognized by M if L = L(M ).
Useful Notation
Definition 7. For an NFA M = (Q, Σ, δ, q0 , F ), string w, and state q ∈ Q, we say δ̂M (q, w) to
denote states of all the active threads of computation on input w from q. Formally,
w
δ̂M (q, w) = {q 0 ∈ Q | q −→M q 0 }
We could say M accepts w iff δ̂M (q0 , w) ∩ F 6= ∅.
Observation 1
For NFA M , string w and state q1 it could be that
• δ̂M (q1 , w) = ∅
• δ̂M (q1 , w) has more than one element
Observation 2
However, the following proposition about DFAs continues to hold for NFAs
uv
For NFA M , strings u and v, and states q1 , q2 , q1 −→M q2 iff there is a state q such
u v
that q1 −→M q and q −→M q2
Example
0, 1 0, 1
0 0 1
q q0 q00 qp
6
δ̂M (q , 0100) = {qp , q00 , q }
q
0 0
q00 q0 q
1 1 1
qp X q
0 0 0
qp q q0 q00
0 0 0 0 0
qp q0 q q00 X
q00
2.3 Examples
Example I
0, 1
1 0, 1 0, 1
q0 q1 q2 q3
Figure 12: Automaton accepts strings having a 1 three positions from end of input
The automaton “guesses” at some point that the 1 it is seeing is 3 positions from end of input.
Example II
7
0
B C
0
A
0
D E
0 0
F
Figure 13: NFA accepting strings where the length is either a multiple 2 or 3
The NFA “guesses” at the begining whether it will see a multiple of 2 or 3, and then confirms that
the guess was correct.
Example III
0, 1 0, 1
q 0 q0 0 q00 1 qp
At some point the NFA “guesses” that the pattern 001 is starting and then checks to confirm the
guess.
3 Power of Nondeterminism
3.1 Overview
Using Nondeterminism
• You follow the same methodology as for DFAs, like identifying what needs to be remembered
8
3.2 Examples
Back to the Future
Problem
For Σ = {0, 1, 2}, let
L = {w#c | w ∈ Σ∗ , c ∈ Σ, and c occurs in w}
So 1011#0 ∈ L but 1011#2 6∈ L. Design an NFA recognizing L.
Solution
• Read symbols of w, i.e., portion of input before # is seen
• Guess at some point that current symbol in w is going to be the same as ‘c’; store this symbol
in the state
• Read the rest of w
• On reading #, check that the symbol immediately after is the one stored, and that the input
ends immediately after that.
0, 1, 2
#
q0 q00
0, 1, 2 0, 1, 2
0 0
1 # 1
qs q1 q10 qf
0, 1, 2
2 2
#
q2 q20
Pattern Recognition
Problem
For alphabet Σ and u ∈ Σ∗ , let
Lu = {w ∈ Σ∗ | ∃v1 , v2 ∈ Σ∗ . w = v1 uv2 }
That is, Lu is all strings that have u as a substring.
Solution
9
• Read symbols of w
To do this, the automaton will remember in its state what prefix of u it has seen so far; the initial
state will assume that it has not seen any of u, and the final state is one where all the symbols of
u have been observed.
Formally, we can define this automaton as follows. Let u = a1 a2 · · · an . The NFA M =
(Q, Σ, δ, q0 , F ) where
• q0 = ,
• F = {u},
Problem
For alphabet Σ = {0, 1},
That is, Lk is all strings that have a 1 k positions from the end.
Solution
• Read symbols of w
• Guess at some point that there are only going to be k more symbols in the input
• Check that the first symbol after this point is a 1, and that we see k − 1 symbols after that
10
The states need to remember that how far we are from the end of the input; either very far (initial
state), or less that k symbols from end.
Formally, M = (Q, Σ, δ, q0 , F ) where
• Q = {qi | 0 ≤ i ≤ k}. The subscript of the state counts how far we are from the end of the
input; q0 means that there can be many symbols left before the end, and qi (i > 1) means
there are k − i symbols left to read.
• q0 = q0
• F = {qk },
Proposition 8. There is a family of languages Lk (for k ∈ N) such that the smallest DFA recog-
nizing Lk has at least 2k states, whereas there is an NFA with only O(k) recognizing Lk .
Halving a Language
1
L = {x | ∃y. |x| = |y| and xy ∈ L}
2
In other words, 12 L consists of the first halves of strings in L
Proposition 11. If L is recognized by a DFA M then there is a NFA N such that L(N ) = 21 L.
Proof Idea
On input x, need to check if x is the first half of some string w = xy that is accepted by M .
11
• “Run” M on input x; let M be in state qi after reading all of x
How do you guess a string y of equal length to x using finite memory? Seems to require remembering
the length of x!
• How do you guess a string y of equal length to x using finite memory? Guess one symbol of
y as you read one symbol of x!
• How do you “run” M on y from qi , if you cannot store all the symbols of y? Run M on y as
you guess each symbol, without waiting to finish the execution on x!
• If we don’t first execute M on x, how do we know the state qi from which we have to execute
y from? Guess it! And then check that running M on x does indeed end in qi , your guessed
state.
New Algorithm
On input x, NFA N
1. Guess state qi and place “left finger” on (initial state of M ) q0 and “right finger” on qi
2. As characters of x are read, N moves the left finger along transitions dictated by x and
simultaneously moves the right finger along nondeterministically chosen transitions labelled
by some symbol
3. Accept if after reading x, left finger is at qi (state initially guessed for right finger) and right
finger is at an accepting state
Things to remember: initial guess for right finger, and positions of left and right finger.
Algorithm on Example
12
0 0
1 1
q0 q1 q2
1
0 1 0
q3
• Q0 = Q × Q × Q ∪ {s}, where s 6∈ Q
• q00 = s
• Transitions
δ 0 (s, ) = {hq0 , qi , qi i | qi ∈ Q}
“Guess” the state qi that the input will lead to
δ 0 (hqi , qj , qk i, a) = {hql , qj , qm i | δ(qi , a) = ql ,
∃b ∈ Σ. δ(qk , b) = qm }
b is the guess for the next symbol of y and initial guess
does not change
• F 0 = {hqi , qi , qj i | qi ∈ Q, qj ∈ F }
13