Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

AT2: Automata Theory

Colin Campbell
Engineering Mathematics Department

C.Campbell@bris.ac.uk

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


A Non-deterministic Finite Automaton (NFA)

This automaton is best illustrated with an example:

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Non-deterministic Finite Automata

It is not a DFA since there are two edges labelled 0 which are
outgoing from node q1 : this makes it non-deterministic.
the other feature is that q2 is a sink node: once you enter
there is no escape!

We will call a non-deterministic automaton, a NFA.

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


NFA definition

The definition of a NFA is the same as for a DFA except that the
transition function returns a set of states, rather than a single
state. Formally, we write this as δ : Q × Σ → 2Q , basically the
power set of Q, the set of all subsets of Q.
The operation on an input string w is then:

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Non-deterministic Finite Automata

we start on q0
for each symbol in w , determine all possible states reachable
from the current set of states.
when all symbols in w have been used, w is accepted if and
only if at least one current state is final.

An NFA is not a “practical machine”, but it is important


theoretically.

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Epsilon transitions

For an epsilon transition, or e-transition, we move from node to


node without reading anything from the input.

Some links in a diagram can be labelled with a e.

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Example 1

Let’s start with a simple automaton which accepts the string 010
and nothing else:

Thus, if at q1 and we receive a 0, then we end up in a dead node


(trap node). Here we haven’t drawn the trap node - it is assumed.

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


. . . but to be strictly careful

However, to be strictly careful, we can draw it there with any


incoming links, and with this dead node depicted below (which can
also have the symbol φ inside):

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Example 2

The following new machine accepts 0 in addition to 010:

0 1 0
q0 q1 q2 q3

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Example: 3

The following machine accepts an expression we will denote as


01n 0, n ≥ 1:

0 1 0
q0 q1 q2 q3

We note again that if in q1 and we receive a 0 then we end up in


{}, the trap or dead state.

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Examples of e-transition diagrams
Example 1: the NFA must recognise two languages A and B such
that A = {w |m contains the string 11} and B = {w |w does not
contain 000}. We can construct two machines which recognise
these two languages independently and then connect them in
parallel using e-transitions:

The lower branch (with the B’s) excludes strings containing 000: if
we get to B3 and receive a 0 then we enter the dead state and do
not accept.

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Example 2

An NFA for recognising the following: there should be at least


one 1 in the string and the number of 0’s after the last 1 must be
3n where n ≥ 1. We solve this problem as follows. After each 1,
we assume it is the last and check 0’s to find if there is a multiple
of 3, so:

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Let’s consider a new example

Let’s consider the NFA:

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Transition table

δ 0 1
→ q0 {q0, q1 } {q0 }
q1 {} {q2 }
? q2 { q2 } {q2 }

This automaton accepts any binary string containing a 01.


We say that it accepts the regular expression (0|1)∗ 01(0|1)? .
A regular expression is a term often used in text searches.
It consists of symbols from the alphabet, inclusive of the
empty string, together with |, ∗ and (), where:

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Comments 2

| indicates alternatives (it is called an alternation or pipe)


∗ means repetition
() is used for grouping
Repetition: given a regular expression in A, its Kleene
closure is A∗ . A string is in A∗ if it is the concatenation of
zero or more strings, all of which are in A.
Thus ((a|b )a)∗ represents the infinite set
{“ 00 ,`,“aa00 , “ba00 , “aaaa00 , “baaa00 , “aaba00 , “baba00 , . . .}.
(0|1)∗ 01(0|1)? , the (0|1)∗ represents
{“ 00 , “000 , “100 , “0000 , “1000 , . . .}.
Same notation is used for text matching. Thus, with
repetition on the p, the regular expression (a|A)p ∗ (e |s |art )
matches: as, As, ape, Apps, appps, Apart, . . .

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Observations

Two observations about NFA’s are:


does this non-determinism increase the computing power of
the machine? The answer is no.
a related observation is then: given a NFA it is always possible
to construct an equivalent DFA. Equivalent means that the
machines accept exactly the same strings.

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Finding a DFA equivalent to a NFA

Example: consider the regular expression (0|1)∗ 01 which accepts


any binary string ending in 01. This is implemented using the NFA:

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Transition table

It’s transition table is:


δN 0 1
(start) → q0 {q0 , q1 } {q0 }
q1 {} ?{q2 }
(final) ?q2 {} {}

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


The Power Set
To find the equivalent DFA we list the power set of all states in the
NFA. Given there are 3 nodes there are 23 = 8 of these and the
relevant subsets are 0, q0 , q1 , q2 ,{q0 , q1 }, {q0, q2 }, {q1 , q2 } and
{q0 , q1 , q2 }˙ . For each such subset we find the set of linked states,
for each input symbol, giving:
Row δD 0 1
1 0 0 0
2 (start) {q0 } {q0, q1 } {q0 }
3 {q1 } {} {q2 }
4 (final) {q2 } {} {}
5 {q0 , q1 } {q0 , q1 } {q0, q2 }
6 (final) {q0 , q2 } {q0 , q1 } {q0 }
7 (final) {q1 , q2 } {} {q2 }
8 (final){q0 , q1 , q2 } {q0 , q1 } {q0 , q2 }
Thus, in row 5, looking at the transition table δN , across from q0
and q1 we have q0 and q1 in the column under 0 and q0 and q2 in
the column under 1.
Colin Campbell Engineering Mathematics Department AT2: Automata Theory
Tracking through the process

However, we realise that some of these states are not reachable


from the start {q0 }. Let’s track through the process:
Row 2: the start {q0 } takes us to {q0 , q1 } or {q0 }. For
{q0 , q1 } we move to row 5.
Row 5: {q0 , q1 } can take us to {q0 , q1 } or the new node
{q0, q2 }: for the latter we move to row 6.
Row 6: {q0 , q2 } just takes us back to nodes we have already
encountered.

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Ignoring non-reachable states

Hence, we ignore all states that are not reachable from {q0 }, i.e.
we delete rows 1,3,4,7 and 8, giving a much reduced transition
table:
δD 0 1
(start) {q0 } {q0 , q1 } { q0 }
{q0 , q1 } {q0 , q1 } { q0 , q2 }
(final) {q0 , q2 } {q0 , q1 } { q0 }

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


The transition diagram

The transition diagram for the equivalent DFA is then:

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


How to do it in practice?

In reality we don’t follow such a cumbersome deduction, instead


generating the power set of states by creating them as we need
them: we never consider the redundant rows. Let’s start again. We
have the NFA:

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Its associated transition table

δN 0 1
→ q0 { q0 , q1 } {q0 }
q1 {} ?{q2 }
? q2 {} {}

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


The transition diagram

From the start → q0 we see that we must create a new row and
node {q0 , q1 }. We read off the entries for {q0 , q1 } from entries
under 0 and 1 in the NFA δN transition table. We find a new node
{q0 , q2 } is created. We again read off its entries from δN but, at
that point, we have no new nodes, so we stop and draw the
transition diagram. As a procedure, and starting from the
transition diagram for the NFA:

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


So our starting point is:

Q = {q0 , q1 , q2 }
Σ = (0, 1)
F = {q2 }

δN (Transition function of the NFA)


State 0 1
→ q0 {q0 , q1 } {q0 }
q1 {} ?{q2 }
? q2 {} {}

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Transition function

We let Q 0 denote the running set of states under consideration and


we start with Q 0 = {q0 }.
δD (Transition function of the DFA)
State 0 1
q0 {q0 , q1 } q0

{q0 , q1 } will be considered as a single state. As its entry is not in


Q 0 , add it to Q 0 so: Q 0 = {q0 , {q0 , q1 }}.

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


The Transition Table

The moves from state {q0 , q1 } are not present in the transition
table of the DFA so we calculate it as so:
δ0 ({q0 , q1 }, 0) = δ(q0 , 0) ∪ δ(q1 , 1) = {q0 , q1 }
δ0 ({q0 , q1 }, 1) = δ(q0 , 1) ∪ δ(q1 , 1) = {q0 , q2 }

Now we update the transition table:

State 0 1
q0 {q0 , q1 } q0
{ q0 , q1 } {q0 , q1 } { q0 , q2 }

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Comments

Now {q0 , q2 } will be considered as a single state. As its entry


is not in Q 0 , add it to Q 0 , so:
Q 0 = {q0 , {q0 , q1 }, {q0 , q2 }}
Moves from state {q0 , q2 } to different nodes are not present
in the so we calculate them:
δ0 ({q0 , q2 }, 0) = δ(q0 , 1) ∪ δ(q2 , 1) = {q0, q1 }
δ0 ({q0 , q1 }, 1) = δ(q0 , 1) ∪ δ(q2 , b ) = {q0 }

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


The Transition Table

We now update the transition table:

State 0 1
→ q0 {q0 , q1 } q0
{ q0 , q1 } {q0 , q1 } ?{q0 , q2 }
?{q0 , q2 } {q0 , q1 } q0

No new state is generated so we are finished.

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


The DFA

The DFA is:


Q 0 = {q0 , {q0 , q1 }, {q0 , q2 }}
Σ = (0, 1)
F = {{q0 , q2 }}
and the automaton is as before:

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Example 2

Convert the following NFA to a DFA:

0,1

q2

0 0

0
q0 q1

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


The Transition Table

It’s transition table is:


State/node 0 1
→ {q0 } ?{q1 , q2 } {}
∗{q1 } {} {}
q2 ?{q1 , q2 } {q2 }

Let Q 0 be the set of states for the equivalent DFA and let T 0 be
the transition table for the new DFA we are constructing. We first
add the state q0 to T 0 :
State/node 0 1
→ {q0 } { q1 , q2 } φ (dead state)
where, as commented → marks our start node, and φ represents
the null set or dead node.

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


New state added

A new state {q1 , q2 } now needs to be added to Q 0 :


State/node 0 1
→ {q0 } { q1 , q2 } φ
{ q1 , q2 } { q1 , q2 } {q2 }

A new state {q2 } now needs to be added to Q 0 :


State/node 0 1
→ {q0 } ?{q1 , q2 } φ
{ q1 , q2 } ?{q1 , q2 } {q2 }
{ q2 } ?{q1 , q2 } {q2 }

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Transition diagram of the resultant DFA

Since the dead state is present in this table, for


completeness, we can add it as a row in the table and as a
node in the transition diagram for the resultant DFA, thus:
State/node 0 1
→ {q0 } ?{q1 , q2 } φ
{ q1 , q2 } ?{q1 , q2 } {q2 }
{ q2 } ?{q1 , q2 } {q2 }
φ φ φ

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


The equivalent DFA

and so the equivalent DFA is:

0 1

0 1
q0 q 1 ,q 2 q2

0,1
0
1

Dead node (sink),


representing {}

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Comments

Often we ‘drop’ the trap node from the diagram: if


there is no link out from a node to the accepting node
for given input then the DFA has not been accepted:
this is implied (it is OK to do this in an exam, provided it is
apparent that you know (state) that this is the outcome for
nodes/states with no outgoing link).
In the resulting DFA, all those states that contain the
final (i,e. accepting) state(s) of the original NFA are
treated as final (accepting) states in the equivalent DFA.

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Example 3

Consider the NFA below, find the equivalent DFA:

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Example 3

We do not give the working now: see if you find the transition
table and diagram for the equivalent DFA given below. Transition
table of the NFA:
Present state 0 1
→ q0 q0 {q0 , q1 }
q1 {} ?q2
? q2 {} {}

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Example 3

Transition table of the DFA:


Present state 0 1
→ {q0 } q0 {q0 , q1 }
{q0 , q1 } q0 ?{q0 , q1 , q2 }
?{q0 , q1 , q2 } q0 ?{q0 , q1 , q2 }

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Example 3

Giving:
0
1

1 1
q0 q 0 ,q 1 q 0 ,q 1 ,q 2

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Example 4

Consider the NFA below, find the equivalent DFA:

0
0

1
q0 q1

1 0,1

0 0,1

q 22

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Example 4

Transition table of the NFA:


Present state 0 1
→ q0 {q0 } ?{q1 , q2 }
q1 ?{q1 , q2 } ?{q2 }
? q2 {q0 , q1 } {q1 }
We do not give the working now: see if you find the transition
table and diagram for the equivalent DFA given below. Transition
table of the DFA:
Present state 0 1
{q0 } { q0 } {q1 , q2 }
{q1 , q2 } {q0 , q1 , q2 } {q1 , q2 }
{q0 , q1 , q2 } {q0 , q1 , q2 } {q1 , q2 }

Colin Campbell Engineering Mathematics Department AT2: Automata Theory


Example 4

Giving:

0 1 0

1 0
q0 q 1 ,q 2 q 0 ,q 1 ,q 2

Colin Campbell Engineering Mathematics Department AT2: Automata Theory

You might also like