CS 275 Automata and Formal Language TheoryCourse Notes
Part II: The Recognition Problem (II)Chapter II.3.: Finite State Automata (13)
Anton Setzer(Based on a book draft by J. V. Tucker and K. Stephenson)
Dept. of Computer Science, Swansea University
http://www.cs.swan.ac.uk/∼csetzer/lectures/automataFormalLanguage/12/index.html
April 14, 2013
CS 275 Chapter II.3 1/ 56
II.3.1. String Recognition (13.1)
II.3.2. Nondeterministic Finite State Automata (13.2)
II.3.3. Examples of Automata (13.3)
II.3.4. Automata with Empty Move Transitions (13.4)
II.3.5. Deterministic Finite State Automata (13.6)
CS 275 Chapter II.3 2/ 56
II.3.1. String Recognition (13.1)
Example Recognising Strings, Step 1
We want to define a program which recognises strings “start” and “stop”.We start by defining a program which recognises the letter “s”.This can be given by a system given by the following diagram, which willbe our first automaton (automata will be introduced soon).
s
q0 q1
CS 275 Sect. II.3.1. 3/ 56
II.3.1. String Recognition (13.1)
Example Recognising Strings, Step 1
s
q0 q1
This automaton has the following ingredients.
I States q0, q1.
I State q0 is the starting state, indicated by the arrow into it comingfrom nowwhere.
I State q1, the state indicating that we have recognised letter “s”.
I A transition, which when recognising letter “s”, goes from state q0 toq1.
CS 275 Sect. II.3.1. 4/ 56
II.3.1. String Recognition (13.1)
Example Recognising Strings, Step 2
In order to recognise the letter “t”, we extend this automaton as follows:
s
q0 q1 q2
t
CS 275 Sect. II.3.1. 5/ 56
II.3.1. String Recognition (13.1)
Example Recognising Strings, Step 2
s
q0 q1 q2
t
I q0 is the start state.
I q1 indicates we have read string “s”.
I q2 indicates we have read string “st”.
CS 275 Sect. II.3.1. 6/ 56
II.3.1. String Recognition (13.1)
Example Recognising Strings, Step 3
For the 3rd letter, we have two choices: “a” as part of the word “start”,and “o” as part of the word “stop”.
s
q0 q1 q2
tq3
q6
a
o
CS 275 Sect. II.3.1. 7/ 56
II.3.1. String Recognition (13.1)
Example Recognising Strings, Step 3
s
q0 q1 q2
tq3
q6
a
o
I q0 is the start state.
I q1 indicates we have read string “s”.
I q2 indicates we have read string “st”.
I q3 indicates we have read string “sta”.
I q6 indicates we have read string “sto”.
CS 275 Sect. II.3.1. 8/ 56
II.3.1. String Recognition (13.1)
Example Recognising Strings, Step 3
We can complete our automaton and obtain the following
s
q0 q1 q2
tq3
q6
a
o
q4 q5
q7
r t
p
CS 275 Sect. II.3.1. 9/ 56
II.3.1. String Recognition (13.1)
Example Recognising Strings, Step 3
s
q0 q1 q2
tq3
q6
a
o
q4 q5
q7
r t
p
This diagram contains a new ingredient:States q5 and q7 are
:::::::::::accepting
::::::::states. If we have processed a word and
reached such a state then the word is accepted as a string of the languageof the automaton.(In our example L = {start, stop}.)
CS 275 Sect. II.3.1. 10/ 56
II.3.1. String Recognition (13.1)
Automata with Loops
In order to recognise infinite languages, we need automata with loops. Thefollowing automaton recognises L =???
q0 q1
a
b
b
CS 275 Sect. II.3.1. 11/ 56
II.3.1. String Recognition (13.1)
Automata with Loops
The following automaton recognises L =???
q0 q1
a
b
b
CS 275 Sect. II.3.1. 12/ 56
II.3.1. String Recognition (13.1)
Automata with Loops
The following automaton recognises L =???
q0 q1
a
b
CS 275 Sect. II.3.1. 13/ 56
II.3.1. String Recognition (13.1)
Nondeterministic Automata
The language {start, stop } can be as well recognised by the following
:::::::::::::::::::nondeterministic
:::::::::::::automaton:
q0
q3
o
q4 q5
r t
p
s q1
t
q2
a
t
sq6 q7 q8 q9
CS 275 Sect. II.3.1. 14/ 56
II.3.1. String Recognition (13.1)
Nondeterministic Automata
q0
q3
o
q4 q5
r t
p
s q1
t
q2
a
t
sq6 q7 q8 q9
I The automaton chooses in state q0, non-deterministically, when instate q0 and recognising a letter s, whether to go to q1 or q6.
I The accepted language is the set of strings such that for each of themwe obtain an accepting state for at least one non-deterministicchoice.
CS 275 Sect. II.3.1. 15/ 56
II.3.1. String Recognition (13.1)
Nondeterministic Automata
q0
q3
o
q4 q5
r t
p
s q1
t
q2
a
t
sq6 q7 q8 q9
I If we try to accept the word “stop” by moving q0s−→ q1
t−→ q2 weget stuck at q2.
I That we fail to accept a word for one specific non-deterministicchoice doesn’t imply that this word is not in the language.
I A word is not accepted only if for all non-deterministic choices thecorresponding run of the automaton doesn’t accept the string.
CS 275 Sect. II.3.1. 16/ 56
II.3.1. String Recognition (13.1)
Why Nondeterministic Automata?
I When translating regular grammars into automata, we will obtainnon-deterministic automata.
I We will show later that from a non-deterministic automaton we canobtain an equivalent deterministic automaton.
I Non-deterministic machine models play an important role in thetheory of algorithms and complexity.
I In some cases (as for automata), deterministic and non-deterministicare equivalent.
I Sometimes they are not.I In other cases it is an open problem whether they are equivalent.I One example is the famous open “P = NP?” problem.
Whether the problems recognised by deterministic (P) andnon-deterministic (NP) Turing machines in polynomial time coincide ornot.
CS 275 Sect. II.3.1. 17/ 56
II.3.2. Nondeterministic Finite State Automata (13.2)
II.3.1. String Recognition (13.1)
II.3.2. Nondeterministic Finite State Automata (13.2)
II.3.3. Examples of Automata (13.3)
II.3.4. Automata with Empty Move Transitions (13.4)
II.3.5. Deterministic Finite State Automata (13.6)
CS 275 Sect. II.3.2. 18/ 56
II.3.2. Nondeterministic Finite State Automata (13.2)
Definition NFA
Definition
A::::::::::::::::::::non-deterministic
::::::finite
:::::::state
:::::::::::::automaton, in short
:::::NFA
(Q, q0,F ,T ,−→) is given by
I A finite set Q of:::::::states.
I A single::::::initial
::::::state q0.
I A set F ⊆ Q of:::::::::::accepting
::::::::states.
I A finite set of:::::::::terminal
:::::::::::symbols T .
I −→ is a relation between states q ∈ Q, a ∈ T and states q′ ∈ Q. Wewrite q
a−→ q′ if q, a, q′ are in this relation.
More formally −→ is a subset of Q × T × Q and qa−→ q′ is an
abbreviation for (q, a, q′) ∈−→.
CS 275 Sect. II.3.2. 19/ 56
II.3.2. Nondeterministic Finite State Automata (13.2)
Presentation of NFA by Picture
An NFA can be presented by a picture, like the following diagram:
q0
q3
o
q4 q5
r t
p
s q1
t
q2
a
t
sq6 q7 q8 q9
Alphabet = {a, . . . , z}I The arrow from nowhere into state q0 denotes that q0 is the initial
state.I Circles denote states.I Arrows from a state q to q′ labelled by a mean that q
a−→ q′.I Double circle like for q5 and q9 denote the accepting states.I The alphabet needs to be stated, unless all elements of the alphabet
occur as labels of arrows, or it is clear from the context.
CS 275 Sect. II.3.2. 20/ 56
II.3.2. Nondeterministic Finite State Automata (13.2)
Presentation of NFA by Table
automaton A
states q0, . . . , qn
terminals a0, . . . , am
start qi
final qi0 , qi1 ,. . ., qim .
transitions qk1al1−→ qm1
· · ·
CS 275 Sect. II.3.2. 21/ 56
II.3.2. Nondeterministic Finite State Automata (13.2)
Example
The automaton for alphabet {a, . . . , z} given by the diagram
q0
q3
o
q4 q5
r t
p
s q1
t
q2
a
t
sq6 q7 q8 q9
is represented by the table on the next slide.
CS 275 Sect. II.3.2. 22/ 56
II.3.2. Nondeterministic Finite State Automata (13.2)
Example
automaton Astart,stop
states q0, q1, q2, q3, q4, q5, q6, q7terminals a, . . . , zstart q0final q5, q9.
transitions q0s−→ q1
q0s−→ q6
q1t−→ q2
q2a−→ q3,
q3r−→ q4,
q4t−→ q5,
q6t−→ q7,
q7o−→ q8,
q8p−→ q9.
CS 275 Sect. II.3.2. 23/ 56
II.3.2. Nondeterministic Finite State Automata (13.2)
The Extended Transition Relation
We define an extended transition relation which determines, whether theNFA from a state q when reading word w ∈ T ∗ can reach state q′:
Definition
Let A = (Q, q0,F ,T ,−→) be an NFA. Then we define −→∗ betweenstates q ∈ Q, words w ∈ T ∗ and q′ ∈ Q.
We write q w ∗−→ q′, if q,w , q′ are in this relation.
I If w = a0 · · · an, then
q w ∗−→ q′ iff qa0−→ q0
a1−→ q1a2−→ · · · an−→ qn = q′
for some qi ∈ Q
Especially qε−→ q′ iff q′ = q.
CS 275 Sect. II.3.2. 24/ 56
II.3.2. Nondeterministic Finite State Automata (13.2)
The Extended Transition Relation
Again, more formally −→∗ is a subset of Q × T ∗ × Q and q w ∗−→ q′
means that (q,w , q′) ∈−→∗
CS 275 Sect. II.3.2. 25/ 56
II.3.2. Nondeterministic Finite State Automata (13.2)
Example
For the automaton
q0
q3
o
q4 q5
r t
p
s q1
t
q2
a
t
sq6 q7 q8 q9
we give some definitions of −→∗:
CS 275 Sect. II.3.2. 26/ 56
II.3.2. Nondeterministic Finite State Automata (13.2)
Example
For each of the words given in the following all possible transitions from q0will be given:
I q0ε ∗−→ q0,
I q0s ∗−→ q1, q0
s ∗−→ q6.
I q0st ∗−→ q2, q0
st ∗−→ q7.
I q0sta ∗−→ q3.
I q0star ∗−→ q4.
I q0start∗−→ q5.
I q0sto ∗−→ q8.
I q0stop ∗−→ q9.CS 275 Sect. II.3.2. 27/ 56
II.3.2. Nondeterministic Finite State Automata (13.2)
The Language accepted by an NFA
Definition
Let A = (Q, q0,F ,T ,−→) be an NFA. The::::::::::language
::::::::::::accepted
:::by
::A is
defined as
L(A) := {w ∈ T ∗ | q0 w ∗−→ q for some q ∈ F}
CS 275 Sect. II.3.2. 28/ 56
II.3.2. Nondeterministic Finite State Automata (13.2)
Operational Understanding of NFAs
To an NFA A = (Q, q0,F ,T ,−→) corresponds a non-deterministicprogram for checking whether an input string is in L(A).The program will have
I a variable q ∈ Q,
I and a pointer to a position in the intput string,
I a Boolean variable stopped , which will we true if the automaton hasstopped without having reached the end of the input string.
A run of the NFA on an input string s is as given by the pseudo-code onthe next slide:
CS 275 Sect. II.3.2. 29/ 56
II.3.2. Nondeterministic Finite State Automata (13.2)
Operational Understanding of NFAs
{− Initialisation −}q := q0;p := beginning of word s;stopped := false;while (p 6= end of word s) ∧ ¬stopped do{a := next symbol in s at position p;
if (¬∃q′.(q a−→ q′))then {stopped := true; }else {choose q′ s.t. q
a−→ q′;q := q′;p := position of next symbol in s from position p; }
}
CS 275 Sect. II.3.2. 30/ 56
II.3.2. Nondeterministic Finite State Automata (13.2)
Operational Understanding of NFAs
I If at the end of the execution stopped = true (therefore we have notreached the end of the string), the string is not accepted in this run.
I Otherwise the string is accepted if q ∈ F .
A string is accepted if there exist a sequence of non-deterministic choices,such that the string is accepted in the corresponding run.
CS 275 Sect. II.3.2. 31/ 56
II.3.2. Nondeterministic Finite State Automata (13.2)
Operational Understanding of NFAs
One can now see that a string s is accepted by some run of the programfor automaton A iff s ∈ L(A).
CS 275 Sect. II.3.2. 32/ 56
II.3.3. Examples of Automata (13.3)
II.3.1. String Recognition (13.1)
II.3.2. Nondeterministic Finite State Automata (13.2)
II.3.3. Examples of Automata (13.3)
II.3.4. Automata with Empty Move Transitions (13.4)
II.3.5. Deterministic Finite State Automata (13.6)
CS 275 Sect. II.3.3. 33/ 56
II.3.3. Examples of Automata (13.3)
Example 1: An Automaton accepting {1, 2, 3}
q0
q1
q2
q3
1
2
3
CS 275 Sect. II.3.3. 34/ 56
II.3.3. Examples of Automata (13.3)
Example 1: An Automaton accepting {1, 2, 3}
Display style:
automaton A1,2,3
states q0, q1, q2, q3
terminals 1, 2, 3
start q0
final q1, q2, q3
transitions q01−→ q1
q02−→ q2
q03−→ q3
CS 275 Sect. II.3.3. 35/ 56
II.3.3. Examples of Automata (13.3)
Simplified Version
2
3
1
q1q0
CS 275 Sect. II.3.3. 36/ 56
II.3.3. Examples of Automata (13.3)
Simplified Version
Shorter pictorial presentation of the same automaton:
q1q0
1,2,3
CS 275 Sect. II.3.3. 37/ 56
II.3.3. Examples of Automata (13.3)
Simplified Version
Display style:
automaton A1,2,3′
states q0, q1
terminals 1, 2, 3
start q0
final q1
transitions q00−→ q1
q01−→ q1
q02−→ q1
CS 275 Sect. II.3.3. 38/ 56
II.3.3. Examples of Automata (13.3)
Example 2: Automaton accepting ???
q0
0, 1 , . . ., 9
CS 275 Sect. II.3.3. 39/ 56
II.3.3. Examples of Automata (13.3)
Simplified Version
Display style:
automaton A
states q0
terminals 0, . . . , 9
start q0
final q0
transitions q0a−→ q0 (for a ∈ {0, 1, . . . , 9})
CS 275 Sect. II.3.3. 40/ 56
II.3.3. Examples of Automata (13.3)
Example 3: Automaton accepting ???
0, 1 , . . ., 9
1, 2, . . ., 9
q0 q1
q2
0
CS 275 Sect. II.3.3. 41/ 56
II.3.3. Examples of Automata (13.3)
Display Style
Display style:
automaton A
states q0, q1, q2
terminals 0, . . . , 9
start q0
final q1, q2
transitions q00−→ q2
q0a−→ q1, (a ∈ {1, . . . , 9})
q1a−→ q1, (a ∈ {0, . . . , 9})
CS 275 Sect. II.3.3. 42/ 56
II.3.5. Deterministic Finite State Automata (13.6)
II.3.1. String Recognition (13.1)
II.3.2. Nondeterministic Finite State Automata (13.2)
II.3.3. Examples of Automata (13.3)
II.3.4. Automata with Empty Move Transitions (13.4)
II.3.5. Deterministic Finite State Automata (13.6)
CS 275 Sect. II.3.5. 43/ 56
II.3.5. Deterministic Finite State Automata (13.6)
Definition DFA
Definition
Let A = (Q, q0,F ,T ,−→) be an NFA. A is a:::::::::::::::deterministic
::::::finite
:::::::state
::::::::::::automaton, in short
:::::DFA, if for all q ∈ Q, there exist at most one q′ s.t.
qa−→ q′
So deterministic finite state automata are those automata correspondingto real programs: we have never to make a choice.
CS 275 Sect. II.3.5. 44/ 56
II.3.5. Deterministic Finite State Automata (13.6)
Example (DFA)
The following NFA is a DFA:
s
q0 q1 q2
tq3
q6
a
o
q4 q5
q7
r t
p
Note that there is no transition from q0 labelled by letter 6= t.For a DFA from each state and element of the alphabet there need to beat most one transition – it is possible to have no transition.
CS 275 Sect. II.3.5. 45/ 56
II.3.5. Deterministic Finite State Automata (13.6)
Example (not a DFA)
The following NFA is not a DFA:
q0
q3
o
q4 q5
r t
p
s q1
t
q2
a
t
sq6 q7 q8 q9
CS 275 Sect. II.3.5. 46/ 56
II.3.5. Deterministic Finite State Automata (13.6)
Theorem II.3.5.1.
Theorem (II.3.5.1.)
Let A be an NFA.Then there exists a DFA A′ s.t. L(A) = L(A′).A′ can be computed from A.
CS 275 Sect. II.3.5. 47/ 56
II.3.5. Deterministic Finite State Automata (13.6)
Example
Consider the NFA above which was not an DFA:
q0
q3
o
q4 q5
r t
p
s q1
t
q2
a
t
sq6 q7 q8 q9
CS 275 Sect. II.3.5. 48/ 56
II.3.5. Deterministic Finite State Automata (13.6)
Definition of the DFA
I We define a DFA as follows:I The states are all possible sets of states.
I Note that since the set of states is finite, the set of sets of states isfinite, too.
I The initial state is the set of initial states, here {q0}.
CS 275 Sect. II.3.5. 49/ 56
II.3.5. Deterministic Finite State Automata (13.6)
Definition of the DFA
I From a set of states B we can make an a-transition to the set ofstates q, which can be reached from any state q ∈ B by ana-transition.
I For instance
I {q0}a
−→′ {q1, q6}since the set of states we can reach by an a-transition from q0 arestates q1, q6.
I {q1, q6}t
−→′ {q2, q7}since the set of states we can reach by a t-transition from any of thestates q1 or q6 are the states q2, q7.
I A set of states is an accepting state, if at least of the states in it is inF .
I For instance {q9} or {q5} or {q4, q9} are accepting.
CS 275 Sect. II.3.5. 50/ 56
II.3.5. Deterministic Finite State Automata (13.6)
Definition of the DFA
I ∅ is not an accepting state, and ∅ a−→ ∅.I So ∅ is a
::::sink of the DFA.
It’s non-accepting and all transitions from it go back to it (why?). Sowe can’t escape from it (why?).
I We can omit it and all transitions into it.
I We can as well omit all states which are unreachable from the intialstate.
I So we get a (usually much smaller) automaton, by starting from theinitial state {q0}, and successively determine all transitions of theDFA which don’t end up in ∅.
CS 275 Sect. II.3.5. 51/ 56
II.3.5. Deterministic Finite State Automata (13.6)
Example above Repeated
q0
q3
o
q4 q5
r t
p
s q1
t
q2
a
t
sq6 q7 q8 q9
CS 275 Sect. II.3.5. 52/ 56
II.3.5. Deterministic Finite State Automata (13.6)
Corresponding DFA
Using the technique above we obtain the following automaton:
st
a
o
r t
p
{q0}
{q4}
{q7}
{q5}
{q1, q6} {q2, q7}
{q3}
{q6}
Note that this was the original DFA above (up to renaming of states).
CS 275 Sect. II.3.5. 53/ 56
II.3.5. Deterministic Finite State Automata (13.6)
Example 2
Consider the following NFA accepting L = . . .:
a
a,b
b
q1q0
CS 275 Sect. II.3.5. 54/ 56
II.3.5. Deterministic Finite State Automata (13.6)
Corresponding DFA
a
b
b
a
{q0}
{q1}
{q0, q1}
b
CS 275 Sect. II.3.5. 55/ 56
II.3.5. Deterministic Finite State Automata (13.6)
Proof Idea
I We denote the transitions of the NFA by −→ and and those of theDFA by −→′
I One can easily see that in the DFA we have that
{q0} w ∗−→′ {q1, . . . , qn} =: B
iff
B = {q | q0 w ∗−→ q}
so B is the set of states which we can reach from q0 by a w -transition.I Therefore in the DFA we reach an accepting state (one containing an
element of F ) by a w -transition,iff the NFA can reach an accepting state by a w -transition.
I Therefore the language accepted by this DFA is the same as thelanguage accepted by the original NFA.
I A formal proof can be found in the Additional Material.
CS 275 Sect. II.3.5. 56/ 56