Chapter 2 - Finite Automata
Chapter 2 - Finite Automata
Deterministic
Finite Automata
(DFA)
Finite Automata
NonDeterministic
Finite Automata
(NFA)
An Informal Picture of Finite Automata
• We investigate protocols that support “electronic money”
• The Ground Rules
• There are three participants: the customer, the store and the bank
• For simplicity, we assume that there is only one "money" file in existence.
• Interaction among the three participants is thus limited to five events:
1. The customer may decide to pay. That is, the customer sends the money to the store.
2. The customer may decide to cancel. The money is sent to the bank with a message that the
value of the money is to be added to the customer's bank account
3. The store may ship goods to the customer
4. The store may redeem the money. That is the money is sent to the bank with a request that its
value be given to the store.
5. The bank may transfer the money by creating a new, suitably encrypted money file and
sending it to the store
An Informal Picture of Finite Automata
An Informal Picture of Finite Automata
• The start state is state 1; it represents the situation
where the bank has issued the money file in
question but has not been requested either to
redeem it or to cancel it.
• If a cancel request is sent to the bank by the
customer, then the bank restores the money to the
customer’s account and enters state 2.
• When in state 1, the bank may receive a redeem
request from the store -> it goes to state 3 and
sends the store a transfer message, with a new
money file that now belongs to the store. After
sending the transfer message, the bank goes to
state 4.
• In that state, it will neither accept cancel or redeem
requests nor will it perform any other actions
regarding this particular money file
An Informal Picture of Finite Automata
• The store starts out in state a. • From state f we expect that the store will
eventually ship, putting the store in state g,
• When the customer orders the goods by where the transaction is complete and nothing
more will happen.
performing the pay action, the store enters • In state e, the store is waiting for the transfer
state b. from the bank. Unfortunately, the goods have
already been shipped, and if the transfer never
• In this state, the store begins both the shipping occurs the store is out of luck.
and redemption processes.
• If the goods are shipped first, then the store
enters state c, where it must still redeem the
money from the bank and receive the transfer
of an equivalent money file from the bank.
• Alternatively, the store may send the redeem
message first, entering state d.
• From state d, the store might next ship, entering
state e, or it might next receive the transfer of
money from the bank, entering state f.
An Informal Picture of Finite Automata
• This automaton has only one state, reffecting the
fact that the customer “can do anything”.
• Another equivalent description using parameters x and y to the left of the vertical bar is:
{x01y | x and y are any strings of 0’s and 1’s}
=> What do we know about an automaton that can accept this language L?
Example 1
• Specify a DFA that accepts all and only the strings of 0’s and 1’s that have the sequence
01 somewhere in the string.
• First its input alphabet is {0, 1}.
• It has some set of states, Q, of which one, say q0 is the start state.
• To decide whether 01 is a substring of the input, A needs to remember:
1. Has it already seen 01? If so, then it accepts every sequence of further inputs; i.e., it will only
be in accepting states from now on.
2. Has it never seen 01, but its most recent input was 0, so if it now sees a 1, it will have seen 01
and can accept everything it sees from here on?
3. Has it never seen 01, but its last input was either nonexistent (it just started) or it last saw a 1?
In this case, A cannot accept until it first sees a 0 and then sees a 1 immediately after.
Example 1
• Specify a DFA that accepts all and only the strings of 0’s and 1’s that have the sequence 01 somewhere
in the string.
• Condition (3) represented by the start state, q0.
• When just starting, we need to see a 0 and then a 1.
• If we are in state q0
• If we next see a 1, then we are no closer to seeing 01, and so we must stay in state q0. That is (q0, 1) = q0.
• If we next see a 0, we are in condition (2). That is, we have never seen 01, but we have our 0. Thus, let us use q2
to represent condition (2). Our transition from q0 on input 0 is (q0, 0) = q2.
• Finally, we must design the transitions for state q1. In this state, we have already seen a 01 sequence,
so regardless of what happens, we shall still be in a situation where we’ve seen 01.
That is, (q1, 0) = (q1, 1) = q1.
Example 1
• Specify a DFA that accepts all and only the strings of 0’s and 1’s that have the sequence
01 somewhere in the string.
• Thus Q = {q0, q1, q2}.
• Start state: q0
• Accepting state: q1; that is, F = {q1}.
The complete specification of the automaton A that accepts the language L of strings that have a 01
substring, is
A = (Q, A, , q0, F),
Q = {q0, q1, q2}
A = {0, 1}
: (q0, 1) = q0, (q0, 0) = q2, (q2, 0) = q2, (q2, 1) = q1, (q1, 0) = q1, (q1, 1) = q1
q0: q0
F = {q1}
Simpler Notations for DFA’s
• Specifying a DFA as a five-tuple with a detailed description of the
transition function is both tedious and hard to read.
The transition diagram for the DFA accepting all strings with a substring 01
Transition Tables
• A transition table is a conventional tabular representation of a function like that takes
two arguments and returns a value.
• The rows of the table correspond to the states
• The columns correspond to the inputs
• The entry for the row corresponding to state q and the column corresponding to input a is the state
(q, a).
There are thus four states, which can be given the following interpretations:
• q0: Both the number of 0’s seen so far and the number of 1’s seen so far are even
• q1: The number of 0’s seen so far is even but the number of 1’sseen so far is odd
• q2: The number of 1’s seen so far is even but the number of 0’s seen so far is odd
• q3: Both the number of 0’s seen so far and the number of 1’s seen so far are odd
• State q0is both the start state and the lone accepting state.
• It is the start state because before reading any inputs the numbers of 0’s and 1’s seen
so far are both zero and zero is even.
Example 2
• Represent the DFA of Example 2 by
• a five-tuple
• a transition table
• 𝛿 𝑞, 𝑤 = ???
• Suppose w is of the form w = xa, where
• a is the final symbol of w and
• x is the rest of w
• Also suppose that 𝛿 𝑞, 𝑥 = {p1, p2, …., pk}
• Let 𝑘𝑖=1 𝛿 𝑝𝑖, 𝑎 = {𝑟1, 𝑟2, … . , 𝑟𝑚}
• Then 𝛿 𝑞, 𝑤 = {𝑟1, 𝑟2, … . , 𝑟𝑚}
Example 4
• Let us use 𝛿 to describe the processing of input 00101 by the NFA of Example 3
𝛿 𝑞0, 00101 = ????
𝛿 𝑞0, = {𝑞0}
𝛿 𝑞0, 0 = (𝛿 𝑞0, , 0) = (𝑞0, 0) = {q0, q1}
𝛿 𝑞0, 00 = (𝛿 𝑞0, 0 , 0) = (𝑞0, 0) (𝑞1, 0) = {q0, q1} = {q0, q1}
𝛿 𝑞0, 001 = (𝛿 𝑞0, 00 , 1) = (q0, 1) ) (𝑞1, 1) = {q0, q2}
𝛿 𝑞0, 0010 = (𝛿 𝑞0, 001 , 0) = (q0, 0) ) (𝑞1, 0) = {q0, q1} = {q0, q1}
𝛿 𝑞0, 00101 = (𝛿 𝑞0, 0010 , 1) = (q0, 1) ) (𝑞1, 1) = {q0, q2}
The Language of an NFA
• An NFA accepts a string w if
• it is possible to make any sequence of choices of next state, while reading the
characters of w and
• go from the start state to any accepting state
*
EpsilonClosures (closure)
• Informally, we close a state q by following all transitions out of q that
are labeled .
• However, when we get to other states by following , we follow the -
transitions out of those states, and so on,
• eventually finding every state that can be reached from q along any path whose arcs
are all labeled .
• Formally, we define the -closure ECLOSE(q) recursively, as follows:
• State q is in ECLOSE(q)
• If state p is in ECLOSE(q), and there is a transition from state p to state r labeled ,
then r is in ECLOSE(q).
• More precisely, if is the transition function of the -NFA involved, and p is in ECLOSE(q)
then ECLOSE(q) also contains all the states in (p, )
Example 7
• ECLOSE(1) =
• ECLOSE(2) =
• ECLOSE(3) =
• ECLOSE(4) =
• ECLOSE(5) =
• ECLOSE(6) =
• ECLOSE(7) =
Exercises
• Exercise 1: Consider the following -NFA