DFA Minimisation Using The Myhill-Nerode Theorem
DFA Minimisation Using The Myhill-Nerode Theorem
Johanna Hogberg
Lars Larsson
Abstract
The Myhill-Nerode theorem is an important characterisation of
regular languages, and it also has many practical implications.
In this chapter, we introduce the theorem an present its proof.
One consequence of the theorem is an algorithm for minimising
DFAs that is outlined in the latter part of this paper. To clarify
how the algorithm works, we conclude with an example of its
application.
Introduction
This chapter gives an introduction to DFA minimisation using the MyhillNerode theorem. DFA minimisation is an important topic because it can be
applied both theoretically and practically, in for instance compilers. Minimising a DFA increases its efficiency by reducing its amount of states and it also
enables us to determine if two DFAs are equivalent.
The chapter is structured as follows. Following this introduction in Section 2,
some relevant background information and definitions are stated, which will be
used in the subsequent sections. In Section 3, the Myhill-Nerode theorem is
stated and its proof presented. Section 4 contains the algorithm for DFA minimisation that uses the Myhill-Nerode theorem. An example showing how such
a minimisation might be conducted in a specific case is found in Example 4.2.
a
q1
q2
q3
q4
a
a
a
a
a
a
q00
q10
q20
b
a
q11
q21
b
a
a, b
qbin
a, b
q22
a, b
Figure 2: DFA accepting the language an bn , n 2
Definition 2.2 (regular language) The class of languages that can be recognised by a deterministic finite automaton.
The Myhill-Nerode theorem that will provide the theoretical background that
allows us to minimise a DFA requires the following definitions that are related
to equivalence relations.
Definition 2.3 (equivalence relation) An equivalence relation, , is a binary relation on a set if it is
Reflexive (a a)
Symmetric (if a b then b a)
Transitive (if a b and b c then a c)
Definition 2.4 (equivalence classes, index) An equivalence relation on
a set S imposes a partition S/ on S, such that elements e, e0 S are in the
same part of S/ if e e0 . The parts of S/ are called equivalence classes, and
we write [e] to denote the unique equivalence class of S/ to which e belongs.
The index of equivalence relation is |S/|.
Definition 2.5 (right invariant) An equivalence relation on strings of symbols from some alphabet is said to be right invariant if x, y with x y
and all w we have that xw yw.
This last definition states that an equivalence relation has the right invariant
property if two equivalent strings (x and y) that are in the language still are
equivalent if a third string (w) is appended to the right of both of them.
3
In the rest of the chapter, the following two equivalence relations will be of
particular importance.
Definition 2.6 (equivalence relation for DFA:s) Let A be a DFA. For strings
x, y , let x A y iff the same state in the DFA is reached from the initial
state by reading the strings x and y.
Definition 2.7 (equivalence relation for languages) Let L be any language
over . For strings x, y , let x L y iff w we have that xw L
yw L.
Definition 2.8 (distinguishing string) Let A = (Q, , , q0 , F ) be a DFA.
The string w is a distinguishing string for states p, q Q if exactly one of
(p, w) and (q, w) is in F .
Intuitively, a distinguishing string s for states p and q in an automaton A is
string which is mapped to an accepting state from exactly one of p and q. In
other words, the existence of a distinguishing string for a pair of states p, q Q
proves that p and q are not equivalent.
Myhill-Nerode theorem
In this section, the Myhill-Nerode theorem [Ner58] and its proof are presented.
Theorem 3.1 Let L . Then the following statements are equivalent:
1. There is a DFA that accepts L (L is regular).
2. There is a right-invariant equivalence relation of finite index such that
L is the union of some of the equivalence classes of .
3. L is of finite index.
Proof
The proof that we give here is an adaption of that in [Vis05], which
uses the implication order (1) (2) (3) (1).
(1) (2). Since L is a regular language, there exits a DFA A = (Q, , , q0 , F )
that recognises L. Recall that A is a right-invariant relation of finite index.
It remains to observe that
[
L=
[x] ,
x:(q0 ,x)F
where x .
(2) (3). Assume that L is the union of some equivalence classes of a rightinvariant relation of finite index. Now, if (the partition imposed by) is a
refinement of (the partition imposed by) L , then L must be of finite index
since it has fewer equivalence classes than , which by assumption is of finite
index. As we shall see, is indeed a refinement of L .
Since is right-invariant, we have that for every pair x, y such that x y,
it must hold that (z) xz yz. Moreover, we have that L is the union of some
4
DFA minimisation
Not only does the Myhill-Nerode Theorem provide an informative characterisation of the recognisable languages, but it also suggests an algorithm for minimising DFAs. Before we attempt to describe this algorithm, we wish to make
a few definitions that will shorten and simplify the later discussion.
Definition 4.1 (partition, block) Let S be a set. A (finite) partition of S
is a set P of mutually disjoint non-empty sets {S1 , . . . , Sk }, such that
Si S, where 1 i k, and
the union of the sets in the partition equals the whole of S. Formally,
k
[
Si = S .
i=1
q2
a
q3
b
b
a
b
q1
q6
q4
b
q5
I:
II:
II:
III:
In this iteration, the only states that belong to the same block and disagrees
are q2 and q6 of block II. After II is divided into {q2 } and {q6 }, partition
P2 = {I, II, III, IV},
where I = {q1 , q5 }, II = {q2 }, III = {q3 , q4 } and IV = {q6 }
remains. When deciding whether P2 is stable, the algorithm could in theory
take a shortcut and only examine block I, as it contains all states that were
mapped to block IIIP1 in the previous iteration, and is hence the only block
that could have been affected by the refinement of IIIP1 . Indeed, it turns out
that I has indeed become inconsistent.
I:
The algorithm would need another iteration to notice that the partition does
not change and that it is done, but we can establish this by a simple observation.
7
The only blocks that could have been affected by the last refinement are I, II
and IV, but these are all singletons and cannot be refined further in any case.
It appears that the original DFA was close to minimal, as the minimised version contains only one state less. If we analyse the automaton, we see that it
recognises the language L, given by
L = L1 L2 , where
L1 = {w | |w|a is even}, and
L2 = {w | w = uav, where |u|b is even, and |u|a is odd.} ,
where |w|a denotes the number of occurences of a in w. In the light of this,
states q1 , q2 , q5 and q6 all remember a unique aspect of the the input string w
read so far. State q1 remembers that w has an even number of as and bs, q2
that it has and odd number of as and bs, while q5 and q6 covers the remaining
two combinations. As these four states are all both useful and distinct, no two
of the can be merged. States q3 and q4 are both accepting and remember that
w can be divided in the manner required by the definition of L2 . Since q3 and
q4 are equivalent in all aspects that matter, they can be merged.
4.1
Distinguishing strings
One could easily modify Algorithm 1 to generate, for each (reachable) state q
of the input automaton A, a string which distinguishes it from every state q 0 6
[q]L(A) : With every state q, we associate a string sq . When the algorithm
first enters RefinePartition, all associated strings are empty. Every time
a block B is found to be unstable with respect to some input symbol a and
is consequently partitioned into smaller blocks, the symbol which forced the
split is appended to sq for all q that belonged to B. Eventually, the algorithm
terminates by outputting the DFA A0 = (Q0 , , 0 , q00 , F 0 ), the states of which
are the equivalence classes of L(A) . It then holds for every pair of states
[q1 ], [q2 ] Q0 , that sq , where q [q1 ][q2 ], is a distinguishing string for [q1 ], [q2 ].
As a final observation, we note that the set {qs |q Q} is prefixed-closed and
thus forms a tree over .
Further reading
References
[Hop71] J. E. Hopcroft. An n log n algorithm for minimizing states in a finite
automaton. Theory of Machines and Computations, 1971.
[Ner58] A. Nerode. Linear automaton transformations. In In Proc. of the
American Mathematical Society 9, pages 541544, 1958.
[Vis05] Professor Mahesh Viswanathan.
Lecture notes.
CS475:
Automata, Formal Languages, and Computational Complexity
http://www.cs.uiuc.edu/class/fa05/cs475/Lectures/, 2005.
10