CD5560
FABER
Formal Languages, Automata
and Models of Computation
Lecture 2
Mälardalen University
2005
1
Content
Finite Automata, FA
Deterministic Finite Automata, DFA
Nondeterministic Automata NFA
NFA DFA Equivalence
2
Finite Automata
FA
3
There is no formal general definition for
"automaton". Instead, there are various kinds of
automata, each with it's own formal definition.
Generally, an automaton
• has some form of input
• has some form of output
• has internal states,
• may or may not have some form of storage
• is hard-wired rather than programmable
4
Finite Automaton
Input
String
Output
Finite
Automaton
String
5
Finite Accepter
Input
String
Finite
Automaton
Output
“Accept”
or
“Reject”
6
FA as Directed Graph
Nodes = States
q0
a
q1
Edges = Transitions
An edge with several symbols is a short-hand
for several edges:
a
q0 a, b
q1
q0
q1
b
7
Deterministic Finite Automata
DFA
8
DFA
• Deterministic
there is no element of choice
• Finite
only a finite number of states and arcs
• Acceptors
produce only a yes/no answer
9
Transition Graph
abba -Finite Acceptor
a, b
Alphabet = {a, b}
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
initial
state
transition
state
a, b
q4
final
state
“accept”
10
Initial Configuration
a b b a
Input String
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
11
Reading the Input
a b b a
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
12
a b b a
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
13
a b b a
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
14
a b b a
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
15
a b b a
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
Output: “accept”
16
Rejection
a b a
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
17
a b a
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
18
a b a
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
19
a b a
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
20
a b a
a, b
b
q0 a
Output:
q5 “reject”
a, b
a
a
b
q1 b q2 b q3 a
q4
21
Another Example
a a b
a, b
a
q0
b
q1
a, b
q2
22
a a b
a, b
a
q0
b
q1
a, b
q2
23
a a b
a, b
a
q0
b
q1
a, b
q2
24
a a b
a, b
a
q0
b
q1
a, b
q2
25
a a b
a
q0
Output: “accept”
b
q1
a, b
a, b
q2
26
Rejection
b a b
a, b
a
q0
b
q1
a, b
q2
27
b a b
a, b
a
q0
b
q1
a, b
q2
28
b a b
a, b
a
q0
b
q1
a, b
q2
29
b a b
a, b
a
q0
b
q1
a, b
q2
30
b a b
a, b
a
q0
b
q1
a, b
q2
Output: “reject”
31
Formal definitions
Deterministic Finite Accepter (DFA)
M Q, , , q0 , F
Q : set of states
: input alphabet
: transition function
q0 : initial state
F : set of final states
32
Input Aplhabet
a, b
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
33
Set of States Q
Q q0 , q1, q2 , q3 , q4 , q5
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
34
Initial State q0
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
35
Set of Final States F
F q4
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
36
Transition Function
:Q Q
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
37
q0 , a q1
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
38
q0 , b q5
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
39
q2 , b q3
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
40
Transition Function
q0
q1
q2
q3
q4
q5
a
q1
q5
q5
b
q5
q4
q5
q5
q5 b
q5
q5
q2
q3
a, b
q0 a
q5
a
a
b
q1 b q2 b q3 a
a, b
q4
41
Extended Transition Function *
* : Q * Q
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
42
* q0 , ab q2
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
43
* q0 , abba q4
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
44
* q0 , abbbaa q5
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
45
Observation: There is a walk from
with label abbbaa
* q0 , abbbaa q5
q0 to q5
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
46
Recursive Definition
* q , q
* q, wa ( * (q, w), a )
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
47
* q0 , ab
* (q0 , a ), b
* q0 , , a , b
q0 , a , b
q1 , b q2
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
48
Languages Accepted by DFAs
Take DFA
M
Definition:
The language LM contains
all input strings accepted by M
LM = { strings that drive M
to a final state}
49
Example
LM abba
Alphabet = {a, b}
M
a, b
q5
b
q0 a
a
a
b
q1 b q2 b q3 a
a, b
q4
accept
50
Another Example
LM , ab, abba
Alphabet = {a, b}
M
a, b
q5
b
q0 a
accept
a
a
b
q1 b q2 b q3 a
accept
a, b
q4
accept
51
Formally
For a DFA
M Q, , , q0 , F
Language accepted by
M:
LM w * : * q0 , w F
alphabet
transition
function
initial
state
final
states
52
Observation
Language accepted by
M
LM w * : * q0 , w F
Language rejected by
M
LM w * : * q0 , w F
53
More Examples
LM {a b : n 0}
n
a, b
a
q0
b
q1
accept
a, b
q2
trap state
Alphabet = {a, b}
54
LM = { all strings with prefix ab }
a, b
q0
a
q1
b
a
q3
Alphabet = {a, b}
b
q2
accept
a, b
55
LM = { all strings without substring 001 }
0
1
0,1
1
0
0
00
1
001
0
Alphabet = {0,1}
56
Regular Languages
L is regular if there is
M such that L LM
A language
a DFA
All regular languages form a language family
57
Example
The language
L awa : w a, b*
a
b
b
is regular
q0
a
q2
q3
a
b
q4
Alphabet = {a, b}
a, b
58
Nondeterministic Automata
NFA
59
NFA
• Nondeterministic
there is an element of choice: in a given state
NFA can act on a given string in different
ways. Several start/final states are allowed.
-transitions are allowed.
• Finite
only a finite number of states and arcs
• Acceptors
produce only a yes/no answer
60
Nondeterministic Finite Accepter (NFA)
Alphabet =
{a}
Two choices
a
q0
q1
a
q2
a
q3
61
First Choice
a a
a
q0
q1
a
q2
a
q3
62
First Choice
a a
a
q0
q1
a
q2
a
q3
63
First Choice
a a
a
q0
q1
a
q2
a
q3
64
First Choice
a a
a
q0
q1
a
q2
“accept”
a
q3
65
Second Choice
a a
a
q0
q1
a
q2
a
q3
66
Second Choice
a a
a
q0
q1
a
q2
a
q3
67
Second Choice
a a
a
q0
q1
a
q3
a
q2
No transition:
the automaton hangs
68
Second Choice
a a
a
q0
q1
a
q2
a
q3
“reject”
69
Observation
An NFA accepts a string if
there is a computation of the NFA
that accepts the string
70
Example
aa
is accepted by the NFA:
a
q0
q1
a
q2
a
q3
71
Lambda Transitions
q0
a
q1
q1
a
q3
72
a a
q0
a
q1
q2
a
q3
73
a a
q0
a
q1
q2
a
q3
74
(read head doesn’t move)
a a
q0
a
q1
q2
a
q3
75
a a
q0
a
q1
q2
a
q3
76
a a
“accept”
q0
String
aa
a
q1
is accepted
q2
a
q3
77
Language accepted:
q0
a
q1
L {aa}
q2
a
q3
78
Another NFA Example
q0
a
b
q1
q2
q3
Alphabet = {a, b}
79
a b
q0
a
b
q1
q2
q3
80
a b
q0
a
b
q1
q2
q3
81
a b
q0
a
b
q1
q2
q3
82
a b
“accept”
q0
a
b
q1
q2
q3
83
Another String
a b a b
q0
a
b
q1
q2
q3
Alphabet = {a, b}
84
a b a b
q0
a
b
q1
q2
q3
85
a b a b
q0
a
b
q1
q2
q3
86
a b a b
q0
a
b
q1
q2
q3
87
a b a b
q0
a
b
q1
q2
q3
88
a b a b
q0
a
b
q1
q2
q3
89
a b a b
q0
a
b
q1
q2
q3
90
a b a b
“accept”
q0
a
b
q1
q2
q3
91
Language accepted
L ab, abab, ababab, ...
ab
q0
a
b
q1
q2
q3
Alphabet = {a, b}
92
Another NFA Example
0
q0
1
q1
0, 1 q2
Alphabet = {0,1}
93
Language accepted
L , 10, 1010, 101010, ...
10*
0
q0
1
q1
0, 1 q2
94
Formal Definition of NFA
M Q, , , q0 , F
Q:
Set of states, i.e.
q0 , q1, q2
:
Input alphabet, i.e.
a, b
:
Transition function
q0 : Initial state
F:
Final states
95
Transition Function
q0 , 1 q1
0
q0
1
q1
0, 1 q
2
96
(q1,0) {q0 , q2 }
0
q0
1
q1
0, 1 q
2
97
(q0 , ) {q0 , q2}
0
q0
1
q1
0, 1 q
2
98
(q2 ,1)
0
q0
1
q1
0, 1 q
2
99
Extended Transition Function *
(Utvidgad övergångsfunktion)
* q0 , a q1
q5
q4
a
q0
a
a
b
q1
q2
q3
100
* q0 , aa q4 , q5
q5
q4
a
q0
a
a
b
q1
q2
q3
101
* q0 , ab q2 , q3 , q0
q5
q4
a
q0
a
a
b
q1
q2
q3
102
Formally
q j * qi , w
if and only if
there is a walk from
with label w
qi to q j
103
The Language of an NFA M
F q0 ,q5
q5
q4
a
q0
a
a
b
q1
q2
q3
* q0 , aa q4 , q5
Alphabet = {a, b}
aa L(M )
104
F q0 ,q5
q5
q4
a
q0
a
a
b
q1
q2
q3
* q0 , ab q2 , q3 , q0
ab LM
105
F q0 ,q5
q0
q5
q4
a
a
a
b
q1
q2
q3
* q0 , abaa q4 , q5
abaa L(M )
106
F q0 ,q5
q5
q4
a
q0
a
a
b
q1
q2
q3
* q0 , aba q1
aba LM
107
q5
q4
a
q0
a
a
b
q1
q2
q3
LM aa ab* ab aa
108
Formally
M
The language accepted by NFA
is:
LM w1, w2 , w3 ,...
where
* (q0 , wm ) {qi , q j ,...}
and there is some (at least one)
qk F
(final state)
109
w LM
* (q0 , w)
qi
w
q0
qk
w
w
qk F
qj
110
NFA DFA Equivalence
111
Equivalence of NFAs and DFAs
NFAs
DFAs ?
Same power?
Accept the same languages?
YES!
112
We will prove:
Languages
accepted
by NFAs
Languages
accepted
by DFAs
NFAs and DFAs have the same
computation power!
113
Step 1
Languages
accepted
by NFAs
Proof
Languages
accepted
by DFAs
Every DFA is also an NFA
A language accepted by a DFA
is also accepted by an NFA
114
Step 2
Languages
accepted
by NFAs
Languages
accepted
by DFAs
Proof Any NFA can be converted to an
equivalent DFA
A language accepted by an NFA
is also accepted by a DFA
115
Procedure NFA to DFA
1. Initial state of NFA: q0
Initial state of DFA:
q0
116
Example
NFA
a
q0
a
q1
q2
b
Step 1
DFA
q0
117
Procedure NFA to DFA
2. For every DFA’s state
{qi , q j ,..., qm }
Compute in the NFA
* qi , a ,
* q j , a ,
}
{qi , qj ,..., qm
...
Add transition
}
{qi , q j ,..., qm }, a {qi , qj ,..., qm
118
Example
a
NFA
q0
a
q1
q2
b
* (q0 , a) {q1, q2}
Step 2
DFA
q0
a
q1,q2
q0 , a q1, q2
119
Procedure NFA to DFA
Repeat Step 2 for all letters in alphabet,
until
no more transitions can be added.
120
Example
a
NFA
q0
a
q1
q2
b
a
b
Step 3
DFA
q0
a
q1,q2
b
a, b
121
Procedure NFA to DFA
3. For any DFA state {qi , q j ,..., qm }
If some
q j is a final state in the NFA
Then {qi , q j ,..., qm }
is a final state in the DFA
122
Example
a
NFA
q0
a
q1
q1 F
q2
b
a
b
Step 4
DFA
q0
a
q1,q2
b
q1, q2 F
a, b
123
Theorem
Take NFA
M
Apply procedure to obtain DFA
Then
M
M and M are equivalent :
LM LM
124
We have proven (proof by construction):
Languages
accepted
by NFAs
Languages
accepted
by DFAs
Regular Languages
END OF PROOF
125
Nondeterministic
vs.
Deterministic Automata
126
Formal Definition of NFA
NFA is a mathematical model defined as a quintuple:
M Q, , , q0 , F
Q:
:
:
q0 :
F:
Set of states, i.e.
Input alphabet, i.e.
Transition function
q0 , q1, q2
a, b
Initial state
Final (accepting) states
127
Deterministic Finite Automata
A deterministic finite automaton (DFA) is a
special case of a nondeterministic finite
automaton in which
1. no state has an -transition, i.e. a transition
on input , and
2. for each state q and input symbol a, there
is at most one edge labeled a leaving q.
128
Example
a
start
a
0
b
1
b
2
3
b
A nondeterministic finite automaton
STATE
0
1
2
INPUT SYMBOL
a
b
{0, 1}
-
{0}
{2}
{3}
Transition table for the finite automaton above
129
Example
a
1
a
2
start
0
b
3
b
4
NFA accepting aa* + bb*
130
Example
b
b
start
0
a
1
b
b
2
3
a
a
a
a
NFA accepting (a+b)*abb
131
Example
start
a
1
start
2
a
3
b
4
a
start
6
b
b
7
b
5
8
(a) NFA for a, abb, and a*b+.
a
1
2
start
0
a
3
a
7
b
4
5
b
6
b
b
8
(b) Combined NFA.
NFA recognizing three different patterns.
132
Ways to think of nondeterminism
• always make the correct guess
• “backtracking” (systematically try all possibilities)
For a particular string, imagine a tree of possible
state transitions:
q4
a
a
q1
a
q2
a
q0
b
a
q0
q3
133
Advantages of nondeterminism
• an NFA can be smaller, easier to construct and
easier to understand than a DFA that accepts
the same language
• useful for proving some theorems
• good introduction to nondeterminism in more
powerful computational models, where
nondeterminism plays an important role
134
Determinism vs. nondeterminism
AUTOMATON
SPACE
TIME
NFA
DFA
O(|r|)
O(2|r|)
O(|r||x|)
O(|x|)
Space and time taken to recognize regular expressions:
- NFA more compact but take time to backtrack all choices
- DFA take place, but save time
(Where r is regular expression, and x is input string)
135
Equivalent automata
Two finite automata M1 and M2 are equivalent if
L(M1) = L(M2)
that is, if they both accept the same language.
136
Equivalence of NFAs and DFAs
To show that NFAs and DFAs accept the same class of
languages, we show two things:
– Any language accepted by a DFA can also be accepted
by some NFA (As DFA is a special case of NFA)
– Any language accepted by a NFA can also be accepted
by some (corresponding, specially constructed) DFA
137
Proof strategy
To show that any language accepted by a NFA is
also accepted by some DFA, we describe an
algorithm that takes any NFA and converts it into
a DFA that accepts the same language.
The algorithm is called the “subset construction
algorithm”.
We can use mathematical induction (on the length
of a string accepted by the automaton) to prove
the DFA that is constructed accepts the same
language as the NFA.
138
Converting NFA to DFA
Subset Construction
139
Subset construction
Given a NFA constructs a DFA that accepts the
same language
The equivalent DFA simulates the NFA by
keeping track of the possible states it could
be in. Each state of the DFA is a subset of
the set of states of the NFA -hence, the name
of the algorithm.
If the NFA has n states, the DFA can have as
many as 2n states, although it usually has
many less.
140
Steps of subset construction
The initial state of the DFA is the set of all states the
NFA can be in without reading any input.
For any state {qi,qj,…,qk} of the DFA and any input a,
the next state of the DFA is the set of all states of the
NFA that can result as next states if the NFA is in any
of the states qi,qj,…,qk when it reads a. This includes
states that can be reached by reading a, followed by
any number of -moves. Use this rule to keep adding
new states and transitions until it is no longer
possible to do so.
The accepting states of the DFA are those states that
contain an accepting state of the NFA.
141
Example
a
a
b b
b
a
b
Here is a NFA that we want to convert to an
equivalent DFA.
142
a
a
NFA
0
DFA
{0,1}
1
b b
a
b
2
b
The start state of the DFA is the set of states the NFA can be
in before reading any input. This includes the start state of
the NFA and any states that can be reached by a -transition.
143
DFA
a
a
a
NFA
0
{0,1}
b b
a
b
1
b
2
b
{2}
For start state {0,1}, make transitions for each possible
input, here a and b. Reading b from start {0,1}, we reach
state {2}. Means from either {0}, or {1} we reach {2}.
144
NFA
DFA
a
a
a
{0,1}
a
{1,2}
b
0
b b
a
b
1
b
2
b
{2}
For state {2}, we create a transition for each possible input,
a and b. From {2}, with b we are either back to {2} (loop) or
we reach {1}- see the little framed original NFA. So from
{2}, with b we end in state {1, 2}. Reading a leads us from
{2} to {0} in the original NFA, which means state {0, 1} in
the new DFA.
145
DFA
a
b
NFA
a
a
{0,1}
a
{1,2}
b
a
0
b b
a
b
1
b
2
b
{2}
For state {1, 2}, we make again transition for each
possible input, a and b. From {2} a leads us to {0}. From {1}
with a we are back to {1}. So, we reach {0, 1} with a from {1,2}.
With b we are back to {1,2}.
At this point, a transition is defined for every state-input pair.
146
NFA
a
{0,1}
DFA
a
b
0
{2}
a
b
1
b b
{1,2}
b
a
a
a
b
2
b
The last step is to mark the final states of the DFA.
As {1} was the accepting state in NFA, all states containing
{1} in DFA will be accepting states: ({0, 1} and {1, 2}).
147
Subset Construction
Algorithm
148
Subset Construction
States of nondeterministic M´ will correspond to
sets of states of deterministic M
Where q0 is start state of M, use {q0} as start
state of M´.
Accepting states of M´ will be those state-sets
containing at least one accepting state of M.
149
Subset Construction (cont.)
For each state-set S and for each s in alphabet
of M, we draw an arc labeled s from state S to
that state-set consisting of all and only the ssuccessors of members of S.
Eliminate any state-set, as well as all arcs
incident upon it, such that there is no path
leading to it from {q0}.
150
The power set of a finite set, Q,
consists of 2|Q| elements
The DFA corresponding to a given NFA with Q states
have a finite number of states, 2|Q|.
If |Q| = 0 then Q is the empty set, | P(Q)| = 1 = 20.
If |Q| = N and N 1, we construct subset of a given
set so that for each element of the initial set there are
two alternatives, either is the element member of a
subset or not. So we have
2 · 2 · 2 · 2 · 2 · 2 · 2…. ·2 = 2N
N times
151
From an NFA to a DFA
Subset Construction
Operation
- closure(s)
- closure(T)
Move(T,a)
Description
Set of NFA states reachable from
an NFA state s on -transitions
along
Set of NFA states reachable from
some NFA state s in T on transitions along
Set of NFA states reachable from
some NFA state set with a transition
on input symbol a
152
From an NFA to a DFA
Subset Construction
Initially, -closure (s0) is the only states in D and it is
unmarked
while there is an unmarked state T in D do
mark T;
for each input symbol a do
U:= e-closure(move(T,a));
if U is not in D then
add U as an unmarked state to D
Dtran[T,a]:=U;
end(for)
end(while)
153
Ett exempel på en dugga
1. Antag att M är följande NFA
q0
,a
q1
a
b
q2
b
b
b
q3
a
- Ge övergångsfunktionen för M
- Använd algoritmen för delmängdskonstruktion
för att skapa motsvarande DFA
- Ge ett reguljärt uttryck för L (M)
- Minimera automaten
154
2. Givet är följande NFA M
a
q0
a
b
q1
a
q2
b
- Skapa en reguljär grammatik för M som genererar L(M)
- Ge ett reguljärt uttryck för L (M)
155
3. Reguljärt eller inte?
-
Mängden av alla strängar på formen xwxR
där x och w är strängar över alfabetet = {0,1}.
-
Språket L = {an : n inte är ett primtal}.
DUGGA 1: 21 april (F6) [13:15-15:00 Lambda]
156
© Copyright 2026 Paperzz