Finite Automata
Finite Automata
Finite Automata
• Two types – both describe what are called regular
languages
– Deterministic (DFA) – There is a fixed number of states and we
can only be in one state at a time
– Nondeterministic (NFA) –There is a fixed number of states but we
can be in multiple states at one time
• While NFA’s are more expressive than DFA’s, we will see
that adding nondeterminism does not let us define any
language that cannot be defined by a DFA.
• One way to think of this is we might write a program using
a NFA, but then when it is “compiled” we turn the NFA
into an equivalent DFA.
Informal Example
• Customer shopping at a store with an electronic
transaction with the bank
– The customer may pay the e-money or cancel the e-
money at any time.
– The store may ship goods and redeem the electronic
money with the bank.
– The bank may transfer any redeemed money to a
different party, say the store.
• Can model this problem with three automata
Bank Automata
Actions in bold are initiated by the entity.
Otherwise, the actions are initiated by someone else and received by the specified automata
Cancel
2
Pay
Cancel
1 3 4
Redeem Transfer
Start Start
Customer Bank
Ignoring Actions
• The automata only describes actions of interest
– To be more precise, with a DFA (deterministic finite automaton) we
should specify arcs for all possible inputs.
– E.g., what should the customer automaton do if it receives a
“redeem”?
– What should the bank do if it is in state 2 and receives a “redeem”?
• The typical behavior if we receive an unspecified action is
for the automaton to die.
– The automaton enters no state at all, and further action by the
automaton would be ignored.
– The best method though is to specify a state for all behaviors, as
indicated as follows for the bank automaton.
Complete Bank Automaton
Redeem, Transfer,
Pay, Ship, Cancel
Start
Bank
R S
3
T
T S
4
Product Automaton
• How is this useful? It can help validate our protocol.
• It tells us that not all states are reachable from the start
state.
– For example, we should never be in state (g,1) where we have
shipped and transferred cash, but the bank is still waiting for a
redeem.
• It allows us to see if potential errors can occur.
– We can reach state (c, 2). This is problematic because it allows a
product to be shipped but the money has not been transferred to
the store.
– In contrast, we can see that if we reach state (d, 3) or (e, 3) then
the store should be okay – a transfer from the bank must occur
• assuming the bank automaton doesn’t “die” which is why it is useful
to add arcs for all possible inputs to complete the automaton
Simple Example – 1 way door
• As an example, consider a
one-way automatic door.
This door has two pads
that can sense when
someone is standing on
Front Rear
them, a front and rear pad.
Pad Pad
We want people to walk
through the front and
toward the rear, but not
allow someone to walk the
other direction:
One Way Door
• Let’s assign the following codes to our different input cases:
a - Nobody on either pad
b - Person on front pad
c - Person on rear pad
d - Person on front and rear pad
• We can design the following automaton so that the door doesn’t open
if someone is still on the rear pad and hit them:
a,c,d b b,c,d
Start C O
a
Formal Definition of a Finite
Automaton
1. Finite set of states, typically Q.
2. Alphabet of input symbols, typically
3. One state is the start/initial state, typically q0 // q0 Q
4. Zero or more final/accepting states; the set is typically F. // F Q
5. A transition function, typically δ. This function
• Takes a state and input symbol as arguments.
• Returns a state.
• One “rule” would be written δ(q, a) = p, where q and p are states, and a is
an input symbol.
• Intuitively: if the FA is in state q, and input a is received, then the FA
goes to state p (note: q = p OK).
6. A FA is represented as the five-tuple: A = (Q, , δ,q0, F). Here, F is
a set of accepting states.
One Way Door – Formal Notation
Using our formal notation, we have:
Q = {C, O} (usually we’ll use q0 and q1 instead)
F = {} There is no final state
q0 = C This is the start state
= {a,b,c,d}
The transition function, , can be specified by the table:
a b c c
C C O C C Write each δ(state,symbol)?
O C O O O
q2 q3
1
Aside: Type Errors
• A major source of confusion when dealing with
automata (or mathematics in general) is making
“type errors."
• Don't confuse A, a FA, i.e., a program, with L(A),
which is of type “set of strings."
• The start state q0 is of type “state," but the
accepting states F is of type “set of states."
• a could be a symbol or a could be a string of
length 1 depending on the context
DFA Exercise
• The following figure below is a marble-rolling
toy. A marble is dropped at A or B. Levers x1, x2,
and x3 cause the marble to fall either to the left or
to the right. Whenever a marble encounters a
lever, it causes the lever to reverse after the marble
passes, so the next marble will take the opposite
branch.
• Model this game by a finite automaton. Let
acceptance correspond to the marble exiting at D.
Non-acceptance represents a marble exiting at C.
Marble Rolling Game
A B
x1 x3
x2
C D
Marble Game Notation
• The inputs and outputs (A-D) become the alphabet of the automaton,
while the levers indicate the possible states.
• If we define the initial status of each lever to be a 0, then if the levers
change direction they are in state 1.
• Let’s use the format x1x2x3 to indicate a state. The initial state is 000.
If we drop a marble down B, then the state becomes to 011 and the
marble exits at C.
• Since we have three levers that can take on binary values, we have 8
possible states for levers, 000 to 111.
• Further identify the states by appending an “a” for acceptance, or “r”
for rejection.
• This leads to a total of 16 possible states. All we need to do is start
from the initial state and draw out the new states we are led to as we
get inputs from A or B.
Messy Marble DFA
A
B B
Start 000r 011r 010a
To 010r
A A A To 111r
A
B B B
100r 111r 110a
B 100a
B A B A
A
A B A
010r 001a 101r
B B
A B
More specifically, L0 is the set we can make selecting zero strings from L. L 0 is
always { ε }.
• Running in “parallel q0
1
1
q0
threads” for string 0
q0 q1 - stuck
1100101 0
q0 q1
1
q0 q2 - stuck
0
q0 q1
1
q0 q2 - accept
Formal Definition of an NFA
• Similar to a DFA
0 1
q0 {q0,q1} {q0}
q1 Ø {q2}
* q2 Ø Ø
NFA Example
• Practice with the following NFA to satisfy
yourself that it accepts ε, a, baba, baa, and
aa, but that it doesn’t accept b, bb, and
babba.
q1
b a
ε
a q2 q3
a,b
NFA Exercise
• Construct an NFA that will accept strings over
alphabet {1, 2, 3} such that the last symbol
appears at least twice, but without any intervening
higher symbol, in between:
– e.g., 11, 2112, 123113, 3212113, etc.
• Trick: use start state to mean “I guess I haven't
seen the symbol that matches the ending symbol
yet.” Use three other states to represent a guess
that the matching symbol has been seen, and
remembers what that symbol is.
NFA Exercise
1. QD = 2Qn ; i.e. QD is the set of all subsets of QN; that is, it is the power set of QN.
Often, not all of these states are accessible from the start state; these states may be
“thrown away.”
2. FD is the set of subsets S of QN such that S FN Ø. That is, FD is all sets of N’s
states that include at least one accepting state of N.
3. For each set S QN and for each input symbol a in :
D ( S , a) N ( p, a)
pS
0 1
The power set of these Ø Ø Ø
states is: { Ø, {q0}, {q1}, {q0} {q0, q1} {q0}
{q2}, {q0, q1}, {q0, q2},{q1, {q1} Ø {q2}
q2}, {q0, q1, q2} } *{q2} Ø Ø
{q0, q1} {q0, q1} {q0, q2}
0 1
Ø Ø Ø
{q0} {q0, q1} {q0}
{q0, q1} {q0, q1} {q0, q2}
*{q0, q2} {q0, q1} {q0}
1
0
a,b
• Convert the following Start q0 q1
NFA’s to DFA’s
b
15 possible states on
this second one (might
be easier to represent in
table format)
Corollary
• A language is regular if and only if some
nondeterministic finite automaton
recognizes it
• A language is regular if and only if some
deterministic finite automaton recognizes it
Epsilon Transitions
• Extension to NFA – a “feature” called epsilon
transitions, denoted by ε, the empty string
• The ε transition lets us spontaneously take a
transition, without receiving an input symbol
• Another mechanism that allows our NFA to be in
multiple states at once.
– Whenever we take an ε edge, we must fork off a new
“thread” for the NFA starting in the destination state.
• While sometimes convenient, has no more power
than a normal NFA
– Just as a NFA has no more power than a DFA
Formal Notation – Epsilon
Transition
• Transition function δ is now a function that
takes as arguments:
– A state in Q and
– A member of {ε}; that is, an input symbol
or the symbol ε. We require that ε not be a
symbol of the alphabet to avoid any
confusion.
Epsilon NFA Example
0
1 ε
Start q r s
0 ε
1
0 ε
1
• ε-closure(q) = { q }
• ε-closure(r) = { r, s}
Epsilon NFA Exercise
• Exercise: Design an ε-NFA for the
language consisting of zero or more a’s
followed by zero or more b’s followed by
zero or more c’s.
Eliminating Epsilon Transitions
To eliminate ε-transitions, use the following to convert to a DFA:
1. Compute ε-closure for the current state, resulting in a set of states S.
2. δD(S,a) is computed for all a in by
a. Let S = {p1, p2, … pk}
k
b. Compute ( p , a) and call this set {r1, r2, r3 … rm}
i 1
i This set is achieved
1 ε
Start q r s
0 ε
1
Converts to:
0,1
Start q sr
0,1
Epsilon Elimination Exercise
• Exercise: Here is the ε-NFA for the language
consisting of zero or more a’s followed by zero or
more b’s followed by zero or more c’s.
• Eliminate the epsilon transitions.
a b c
Start q r s