2 Finite Automata 2019
2 Finite Automata 2019
FINITE AUTOMATA
Introduction
Cohen (2001) notes that several children's games fit the following description: Pieces are set
up on a playing board; dice are thrown (or a wheel is spun) and a number is generated at
random. Based on the generated number, the pieces on the board are rearranged by the rules
of the game. Then, another child throws or spins and rearranges the pieces again. There is no
skill or choice involved - the entire game is based on the values of the random numbers.
Consider all possible positions of the pieces on the board and call them states. We begin with
the initial state of the starting positions of the pieces on the board. The game then changes
from one state to another based on the value of the random number. For each possible
number, there is one and only one resulting state given the input of the number, and the prior
state. This continues until one player wins and the game is over. This is called a final state.
Now consider a very simple computer with an input device, a processor, some memory and an
output device. We want to calculate 3 + 4, so we write a simple list of instructions and feed
them into the machine one at a time (e.g., STORE 3 TO X; STORE 4 TO Y; LOAD X; ADD
Y; WRITE TO OUTPUT). Each instruction is executed as it is read. If all goes well, the
machine outputs '7' and terminates execution. This process is similar to the board game. The
state of the machine changes after each instruction is executed, and each state is completely
determined by the prior state and the input instruction (thus this machine is defined as
deterministic). No choice or skill is involved; no knowledge of the state of the machine for
instructions ago is needed. The machine simply starts at an initial state, changes from state to
state based on the instruction and the prior state, and reaches the final state of writing '7'.
Definition of an FA
A Finite Automaton is yet another method for defining languages. This model is said to be
finite because the number of possible states and number of letters in the alphabet are both
finite. It is called an automaton because the change of states is totally governed by the input.
The determination of the next state is automatic (involuntary and mechanical) and not wilful,
just as the motion of the hands of a clock is automatic while the motion of a human is
presumably the result of desire and thought (Cohen 2001).
Finite Automata are good models for computers with an extremely limited amount of
memory, like an automatic door, switches, elevator or digital watches.
Finite automata and their probabilistic counterpart “Markov chains” are useful tools when we
are attempting to recognize patterns in data. These devices are used in speech processing and
in optical character recognition.
The foregoing definitions do not describe how an FA works but instead what it is. The FA
works by being presented with an input string of letters that it reads letter by letter starting at
the leftmost letter. Beginning at the start state, the letters determine the sequence of states.
The sequence ends when the last input letter has been read.
• A deterministic finite automaton accepts its input if starting in its initial state with the
tape head over the first input symbol it moves to an accept state after reading the last
input symbol.
• If the machine reaches the end of its input before reading any symbols then its input is
called the empty string and is denoted Λ
Note that since Λ contains no symbols, a DFA accepts Λ if and only if its initial state is also
an accept state.
Note that the requirement that the next state of a DFA be uniquely determined by the current
state and the current input means that in the corresponding transition diagram no two arcs
leaving a state can be labelled with the same symbol (otherwise a choice would be necessary
and the machine would be nondeterministic).
• The transition function δ for a DFA is interpreted in such a way that δ:(s i, xj) → sk if and
only if the machine can move from state si to state sk while reading the symbol xj.
• A DFA (S, Σ, δ, i, F) accepts a nonempty string x 1x2 · · · xn iff there is a sequence of states
s0s1 · · · sn such that
1. s0 = i
2. sn F
3. for each integer j from 1 to n, δ(sj−1, xj) = sj
Transition Diagrams
1) A transition diagram consists of a finite set of labelled circles called states connected by
arrows called arcs.
2) Exactly one state, called the initial state, is indicated with a pointer or contains - symbol
3) One or more states, called accept states or final states are indicated with double circles.
State
Start State
Final State
b Transition
Door movement:
– When the door is closed and there is somebody on the rear pad or on both pads or there is
no one on the pads, the door remains closed
– When the door is closed and somebody steps on the front pad the door opens
– When the door is open and somebody is on the front pad, rear pad, or on both pads the
door stays open
– When the door is open and nobody is on the pads the door closes
Controller movement:
– When controller is in the state closed and the input is rear, both, or neither the controller
remains in the state closed
– When controller is in the state closed and the input is front the controller moves to the
state open
– When the controller is in the state open and the input is one of front, rear, both, the
controller remains in the state open
– When the controller is in the state open and the input is neither the controller moves to the
state closed
Transition Tables
Is used to summarise transition rules in instances where the list of rules is very long. Each
row of the TT is the name of one of the states in the FA and each column of the TT is a letter
of the input alphabet. The entries inside the TT are the new states that the FA moves into-the
transition states (Cohen, 2001). A transition table is a two-dimensional array which
summarises a transition diagram. If there is no arc labelled leaving a state then the destination
state is labelled error.
Input signals
When the controller receives input signals it moves from one state to another. For example
when the controller receives input signals NEITHER or REAR or BOTH and is in closed
state it remains in that state.
Note that the door controller has only two states and such it requires less memory than say an
elevator controller if the floor on which the elevator is on is regarded as a state. In this
example the input may be the signals from the buttons.
Note that this controller is a computer that has just a single bit of memory that is used to
record the controller’s state
Other examples of computers with limited memory are controllers for dishwashers, electronic
thermostats, parts of digital watches and calculators.
The above devices are designed by keeping the methodology and terminology of finite
automata in mind.
Example 1 of an FA
1. Suppose Σ = {a, b}
2. There three states namely 1, 2 and 3 and state 1 is the initial state and state 2 is the
only finial state.
3. Let the following be the rules of the transition
a. Rule 1: from state 1 and input a go to state 3
b. Rule 2: from state 1 and input b go to state 2
c. Rule 3: from state 3 and input a go to state 1
d. Rule 4: from state 3 and input b go to state 2
e. Rule 5: from state 2 and any input stay at state 2
When aaaaa string is presented to this FA, it does not lead us to the final state 2, so the string
is not accepted or is rejected by this FA. The set of all strings that leave us in a final state is
called the language defined by the FA.
The above FA accepts all strings that have a letter b in them and no other strings. Therefore
the language associated with or accepted by this FA is the one defined by the RE:
a b
start 3 2
1
1 2
3
Final 2 2
2
Transition Diagram
An FA is considered as a machine that has dynamic capabilities. It moves. It processes input.
Something goes from state to state as the input is read. One way to represent an FA that feels
more like a machine is a transition diagram. This representation makes it much easier to see
that this FA accepts only strings with at least one 'b' in them.
Example 2 of an FA
Question: draw a state transition diagram and a state table for a machine M which is defined
as (S, Σ, δ, i, F)
Where S = {1, 2} Σ = {a, b} δ: (S x Σ) →S: δ(1,a) →2,
δ(1,b) →1, δ(2,a) →2, δ(2,b) →1 i = 1
F = {2}
1 2
a b #
Start state 1 2 1 R
End state 2 2 1 A
Note that Λ is a special case because it does not appear in the transition function and is not in
the alphabet Σ.
For all s an element of S, δ(s, Λ) = s
Practice Questions
Give the transition graph of a finite state automaton to accept the language;
11. L(a*b*)- all strings over a and b in which all a’s (if any) precede all
b’s (also if any)
12. L(a*b*)* - all strings over a and b
13. L(a(a+b)*a +b(a+b)*b + a + b – all strings over a and b that start and
end with the same symbol
In each part below, draw an FA accepting the indicated language over {a, b}. Give the RE
defining each language
a. The language of all strings containing exactly two a’s.
b. The language of all strings containing at least two a’s.
c. The language of all strings that do not end with ab.
d. The language of all strings that begin or end with aa or bb.
e. The language of all strings not containing the substring aa.
f. The language of all strings in which the number of a’s is even.
g. The language of all strings in which both the number of a’s and the number of b’s are
even.
Prepared By Zanamwe N ©2/25/2019
Page 9 of 30
h. The language of all strings containing no more than one occurrence of the string aa.
(The string aaa contains two occurrences of aa.)
i. The language of all strings in which every a (if there are any) is followed immediately
by bb.
j. The language of all strings containing both bb and aba as substrings.
k. The language of all strings containing both aba and bab as substrings.
l. The language that accepts the string abaa and no other strings
For each of the FAs pictured below, give a simple verbal description of the language it
accepts and the corresponding RE
a) Give the transition graph of a finite state automaton to accept the language; [15 marks]
i. (a+b)*(aa+bb)(a+b)*
ii. a*(aba*b*ba+abb+bbb*)(a+b)*
iii.
iv. the set of strings in {0, 1}∗ that are binary representations of integers divisible by 5.
b) Let us consider the operation of a soft drink vending machine which charges 8 bond coins
for a can. Initially the machine is waiting for a customer to come and put some coins, that
is, it is in the waiting-for-customer state. Let us assume that only 1, 2 and 5 bond coins
are used for simplicity. When a customer comes and puts in the first coin, say a 5 bond
coin, the machine is no longer in the waiting-for-customer state. The machine has
received 5 bond coin and is waiting for more coins to come. So we might say the machine
is in the 5-bond coin state. If the customer puts in a 2 bond coins, then the machine has
now received 7 bond coins. If the customer puts in a 1 bond coin, then the machine has
now received 8 and it waits for the customer to select a soft drink. So the machine is in
another state, say 8 bond coins. When the customer selects a soft drink, the machine must
give the customer a can of soft drink. After that it stays in that state until another coin is
put in to start the process anew or the operation may be terminated and start all over from
the initial state. It is assumed that the machine terminates its operation when it receives 8
bond coins.
Required:
i. Give the formal definition of a DFA modelling the solution to the above problem [5]
ii. Draw the Transition Table [5]
iii. Draw a DFA with the least number of states [5]
iv. Come up with a regular expression defining the language accepted by the machine
[5]
v. Give five strings which are accepted by the machine [5]
vi. Give five strings which are rejected by the machine. [5]
vii. Implement your solution using Python [10]
Examples:
1. Machine that accepts strings over a and b except λ. The regular expression defining the
language is L(a+b)(a+b)*.
D
E
DE D
ED
E
2. Machine that accepts all strings over a and b and the RE for this language is L(a+b)*.
1
R D
E
H8
)LQ
D
O
6
W Q
U
H
D
F
K
E
O
H D
E
)LQ
D
6
W
H1D
RE
,Q
LW
O
6
D
H
3. Machine that accepts no language
a. FAs with no final states
b. FAs with unreachable final states
c. FAs with no initial states ????
If given a language, one must be able to construct a machine for it and also if given a machine
one must be able to deduce its language.
Example:
Machine that accepts the language of all words over the alphabet {a,b} with an even number
of letters.
Algorithm
Input: input string
Output: Accept, Reject string
Set EVEN = True
ED
E
DDED
E
EDDDE E
ED
E
Note that it is possible to have at least one FA that accepts each possible language for
example, below are three machines which accept words in the language L(a(a+b)*).
Example:
D
ED
EE
FA that accepts all words containing a triple letter either aaa or bbb and only those words
Question
ED EE
Determine the language accepted by the machine given below:
DED D
E
EED
E
The Machine accepts all string over {a,b} that have a double letter in them and the language
is defined by the RE (a+b)*(aa+bb)(a+b)*
Example:
Accepts all strings over {a,b} with b as the third letter and rejects all other words
E$D
EED
E
D
E DED
FH
S
W
V
D
Q
G
D
ED
E
Transition Graph
A Transition Graph (TG) is a collection of three things:
i A finite set of states, at least one of which is designed as the start state
and some (may be none) of which are designed as final states.
ii An alphabet Σ of possible input letters from which input string are
formed.
iii A finite set of transitions that show how to go from one state to another
based on reading specified substrings of input letters (possibly even the
null string Λ).
The last clause in the definition means that every edge is labelled by some
strings of letters not necessarily only one letter. It is not a requirement that
there be any specific number of edges emanating from any state. Some states
may have no edge coming out of them at all and some may have thousands.
Examples:
i. TG that recognises all words that contain a double letter
ii.
iii.
NJ
NJ
NJ NJ
A TG that accepts nothing
iv.
NJDD E
NJ
DDE
Note that if an edge is labelled with λ , it means we can take the ride it offers
without consuming any letters from the input string.
D
EE
ED
vi. TG that accepts words that end in a
D
E
vii. TG that accepts the language of all words that begin and end with different letters
Generalised TG
Examples
D
EEDD
E
ii The following machines are equivalent
Kleen’s Theorem
States that any language that can be defined by:
i Regular expression or
ii Finite automata or
iii Transition graph
Can be defined by all three methods.
Proofs
The three sections of our proof will be:
Part1: every language that can be defined by a FA can also be defined by
a TG.
Part2: every language that can be defined by a TG can also be defined by
a RE.
Part3: every language that can be defined by a RE can also be defined by
a FA.
The proof of this part will be by constructive algorithm. This means that we
present a procedure that starts out with a TG and ends up with a RE that defines
D
E
the same language.
Algorithm:
i Create a unique unenterable initial state and a unique final unleavable
D
E
state
D
E
ii One by one in any order, by-pass and eliminate all the non-final or non
D
E
start states in the TG. A state is by-passed by connecting each incoming
edge with each outgoing edge. The label of each resultant edge is the
concatenation of the label on the incoming edge with the label on the loop
edge if there is any and label on the outgoing edge
iii When two states are joined by more than one edge going in the same
direction unify them by adding their labels
D
E
iv Finally, when all that is left is one edge from start to final, the label on
NJ
that edge is a regular expression that generates the same language as was
recognised by the original machine
D
E
Example:
ENJ
D
After Step 1
DE
After eliminating state 4 it becomes
D
ED
NJE
D
ED
ENJ
D
E
D
Question
EDED
E
Using the constructive algorithm generate the RE for the language defined by the
DDDENJ
Transition Graph given below.
ED
EE
After step 1:
1-3-5 a(ba)*a
1-3-4 a(ba)*b
4-3-5 a(ba)*a
4-3-4 a(ba)*b
D
O
ƶH
1-4-5 (b+a(ba)*b) (a(ba)*b)* (b+a(ba)*a)
[FO
S
W
DD
ƶD
Proof of part 3
The proof will be by recursive definition and constructive algorithm. It is known that
every RE can be built from letters of the alphabet Σ∧ Λ by repeated application of
D
O
ƶ
certain rules: addition, concatenation and closure. We should show that as we are
building up a RE, we could at the same time be building up an FA that accepts the
same language. Below is the presentation of the algorithm with four rules.
O
ƶ
Rule 1
There is an FA that accepts any particular letter of the alphabet. There is an FA that
accepts only the word Λ
Proof of Rule 1
If a is in Σ, then the FA accepts only the word a.
Rule 2
If there is an FA called M1 that accepts the language defined by the RE r1 and there is
an FA called M2 that accepts the language defined by the RE r 2, then there is an FA
that we shall call M3 that accepts the language defined by the RE (r1 + r2)
Proof of rule 2
This rule shall be proved by showing how to construct the new machine in the most
reasonable way from the two old machines. We shall prove that M 3 exists by showing
0
how to construct it.
0[[[[\\ \\
Algorithm
i. Starting with the two machines M1 with states x1,x2,x3…xn and M2 with states
y1,y2,y3… yn build a new machine M3 with states z1, z2, z3 … zn where each z is of
the form xsomething or ysomething.
ii. The combination state xstart or ystart is the start state of the new FA
iii. If either the x part or the y part is a final state, then the corresponding z is a final
state
iv. To go from one z to another by reading a letter from the input string, we look at
what happens to the x part and the y part and go to the new z state accordingly.
This can be written as a formula:
Znew after p = {xnew after p on M1} or { ynew after p on M2}
v. Useless states may or may not be removed
Example:
M1 accepts all words with a double 0 in them and M 2 accepts all words that have both an even
number of total 0’s and an even number of total 1’s
0 0 1 1
Start/final z1 x1 or y1 z2 x2 or y3 z3 x1 or y2
z2 x2 or y3 z4 x3 or y1 z5 x1 or y4
z3 x1 or y2 z6 x2 or y4 z1 x1 or y1
Final z4 x3 or y1 z7 x3 or y3 z8 x3 or y2
z5 x1 or y4 z9 . z10 .
z6 x2 or y4 z8 . z10 .
Final z7 x3 or y3 z4 . z11 .
Final z8 x3 or y2 z1 z4
0
z9 . z1 z1
z10 . z12 z5
\ \
Final z11 . z8 z7
Final z12 z7 z3
[\
0[\[[\\[\
Example
M1 accepts all words with a double 0 in them and M2 accepts all words ending in 1.
Rule 3
If there is an FA that accepts the language defined by a RE r and there is an FA that accepts
the language defined by the RE s there is an FA that accepts the language defined by the RE
rs.
Algorithm
i. Make a z-state for every non-final x-state in M 1 reached before ever hitting a final
state on M1
ii. For each final state in M1, we establish a z-state that expresses the options that we are
continuing on M1 or beginning on M2
Are in xsomething, which is a final state but still continuing on M1
Or
Have finished the M1 part of the string and have jumped to y1 to continue tracing
the remainder of the input string on M2
iii. After reaching a jump to M2 state, any other states we reach have an x and a y possibility like
the z states in union machines, with the additional possibility that every time we hit yet
another final state on M1 machine, we may again exercise the option of jumping to y 1. This
implies that every time we pass through a final state while processing the M 1 part of the input
string we jettison an alter-ego jumping to y1 that runs on the M2 machine
Every z state can have the nature of one and only one x-state, but a whole set of possible y-
states. So the full nature of a z-state is:
Or
Example
Procedure
i. Begin in x1 which we shall call z1, if we read a 0 we go to x 2 which we call z2. If we read a 1
we go to x3 which being a final state means that we have the option of jumping to y1. Z 3 = x3
or y1
ii. From z2, like x2 both a 0 and 1 takes us to z2
iii. In z3 if we are in x3 condition and we read a 0, we stay in x 3 or we choose to jump to y 1. If we
were in z3 in the y1 condition already and we read a 0 we would loop back to y1 on the M 2
machine. So if we are in z3 (x3 or y1) and we read a 0 we end up in z3
iv. If we are in z3 (x3 or y1) and we read a 1, we stay there or use the occasion to jump to y 1. If we
were in z3 already in the y1 condition, then the 1 would take us to y 2. Therefore z4 a new state
must be equal to x3 or y1 or y2. If the input processing ends in this state, then it will be
accepted because it may have gotten to the final state on the M2 machine. So z 4 is a final state
for m3
v. If in z4 and we read a 0, x 3 goes to x3 staying on M1 or x3 goes to x3 then jumps to y1 on M2 or
y1 stays in y1 or y2 goes to y1. Therefore from z4 a 0 takes us to x3 or y1 which is z3.
If in z4 and we read the input letter 1, x 3 goes to x3 staying on M1 or x3 goes to x3 which jumps
to y1 or y1 goes to y2 or y2 loops back to y2. So from z4 a 1 will loop us back to z4
M3 looks like:
0 1
z1 z2 z3
z2 z2 z2
z3 z3 z4
z4 z3 z4
0]] [
\
Note that M1M2 ≠ M2M1
i. Connect all final states of the first TG to all states pointed to by the initial state of the
second TG and label them with the same symbols
ii. The final states of the first TG cease to be final states but the final states of the second
TG remain final states
iii. The start state of the first machine remains the start state but that of the second cease
to be a start state.
For example
Example
i.
ii.
D
M1- words that do not contain double a
M2- words with an odd number of letters
0E D
\]\\
\
E D
D
\E
0 D
E
[\
[
D
E
0E\EDD
\
E]D
] \]E
Note that if a word w has an odd number of letters, we can factor it as ( Λ )(w)
Where Λ is in L1 and w is in L2 while if it has an even number of letters, we factor it as w =
(firstletter)(the rest)
Rule 4
If r is a RE and M 1 is an FA that accepts exactly the language defined by r, then there is an FA called
M2 that will accept exactly the language defined by r*
[E
D D
Proof of rule 4
The rule shall be proved by a machine that accepts r* given a machine that accepts r
Algorithm
[
Given an FA whose states are x 1, x2, x3 … xn, an FA that accepts the Kleen closure of the language of
the original machine can be built as follows:
i. Create a state for every subset of x’s (Power set of the set of states). cancel any subset that
E
contains a final state x but does not contain the start state
ii. For all the remaining non-empty states, draw an a edge and a b edge to the collection of x-
[D
states reachable in the original FA from the component x’s by a and b edges respectively. If a
state is final consider the possibility of going back to the initial state
D
iii. Call the null subset an initial and final state and connect it to whatever states the original start
\
[
state is connected to by a and b edges, even possibly the start state itself.
iv. Put final state signs to every state containing an x-component that is a final component of the
EE
original FA
Example
Find the Kleen closure of the machine that accepts all words over a and b where all a’s (at least 1)
come before all the b’s (at least 1)
{{}, x1, x2, x3, x4, x1x2, x1x3,x1x4,x2x3,x2x4, x3x4, x1x2x3, x1x3x4, x2x3x4, x1x2x4,x1x2x3x4}
E
DE EE D
[\
[
ED
Note that if the initial state has a loop two states are required to represent it. When the original start
state is converted to a new start state it can either be the original start state and a final state or original
start state and a non-final state. Z1 = x1 and final state and z2 = x1 and nonfinal state
The first state is 4 which is like state 1 except that it is a final state if in state 4 and read an a you
remain in state 1 but this time in its capacity as a nonfinal state. We have to give a different name to
this state lets call it 5. 4 =x1 and a final state, 5 = x1 and a non-final state.
If in state 4 and we read a b we go to state 2 but since state 2 is a final state we include the possibility
of going back to state 1. So state 6 is 1 or 2 and is final state because of 2
If in state 5 and we read an a we stay in state 5 and if in state 5 and we read a b we go to state 6.
If we are in state 6 and we read an a we remain in that state because if we are in state 1 we would stay
there and the same is true of state 2
If we are in state 6 and we read a b we remain in that state