7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
1/36
CSC 3130: Automata theory and formal languages
Andrej Bogdanov
http://www.cse.cuhk.edu.hk/~andrejb/csc3130
DFA minimization algorithm
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
2/36
Example
Construct a DFA over alphabet {0, 1} thataccepts those strings that end in 111
This is big, isnt there a smallerDFA for this?
0
1
qe
q0
q1
q00
q10
q01
q11
q000
q001
q101
q111
0
1
0
1
0
1
1
1
1
1
0
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
3/36
Smaller DFA
Yes, we can do it with 4 states:
The state remembers the number of consecutive 1s at
the end of the string (up to 3)
Can we do it with 3 states?
1q0
q1
q2
q3
1 11
0
0
0
0
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
4/36
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
5/36
Pigeonhole principle
Here, balls are inputs, bins are states:
Suppose you are tossing mballs into nbins,
and m> n. Then two balls end up in the
same bin.
If you have a DFA with nstates and you run
it on minputs, and m> n, then two inputs end
up in same state.
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
6/36
A smaller DFA?
Minputs:
e, 1, 11, 111
What goes wrong if
Mends up in same state on input 1 and input 111?
Mends up in same state on input e and input 11?
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
7/36
A smaller DFA
SupposeMends up in the same state afterreading inputs x= 1iandy= 1j, where 0 i
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
8/36
No smaller DFA!
Conclusion
So, this DFA is minimal
In fact, it is the unique minimal DFA forL
There is no DFA with 3 states forL
1q0
q1
q2
q3
1 11
0
0
0
0
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
9/36
DFA minimization
There is an algorithm to start with any DFA andreduce it to the smallest possible DFA
The algorithm attempts to identify classes ofequivalent states
These are states that can be merged togetherwithout affecting the answer of the computation
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
10/36
Examples of equivalent states
q0, q1 equivalent
q0
q1
q2
q3
0
1
0
1
0,1
0, 1
q0
q1
q2
q3
1
0, 1
0, 1
0, 1
0
q0, q1 equivalent
q2, q3 also equivalent
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
11/36
Equivalent and distinguishable states
Two states q, q are equivalent if
Here, d(q, w) is the state that the machine is in if itstarts at qand reads the string w
q, q are distinguishable if they are not equivalent:
^
For every string w, the states d(q, w) and d(q,
w) are eitherboth accepting orboth rejecting
^ ^
For some string w, one of the states d(q, w),
d(q, w) is accepting and the other is rejecting
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
12/36
Examples of distinguishable states
q0, q1 equivalent
q3 distinguishable from q0, q1, q2q3 is accepting, others are rejecting
q0
q1
q2
q3
0
1
0
1
0,1
0, 1
(q0, q2) and (q1, q2) distinguishablethey behave differently on input 0
q01
q2
q3
0
10,1
0, 1
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
13/36
DFA minimization algorithm
Find all pairs ofdistinguishable states as follows:
For any pair of states q, q:
Ifqis accepting and q is rejecting
Mark(q, q) as distinguishable
Repeat until nothing is marked:For any pair of states (q, q):
For every alphabet symbol a:
If(d(q, a), d(q, a)) are marked as distinguishable
Mark(q, q) as distinguishable
For any pair of states (q, q):
If(q, q) is not marked as distinguishable
Mergeqand q into a single state
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
14/36
Example of DFA minimization
0
1
qe
q0
q1
q00
q10
q01
q11
0
1
0
1
1
0
01
1
0
0
1
q0
q1
q00
q01
q11
q10
q10
qe
q0
q1
q01
q00
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
15/36
Example of DFA minimization
0
1
qe
q0
q1
q00
q10
q01
q11
0
1
0
1
1
0
01
1
0
0
1
x x x x x x
q0
q1
q00
q01
q11
q10
q10
qe
q0
q1
q01
q00
q11 is distinguishable from all other states
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
16/36
Example of DFA minimization
0
1
qe
q0
q1
q00
q10
q01
q11
0
1
0
1
1
0
01
1
0
0
1
x
x
x
x
x
xx x x x
q0
q1
q00
q01
q11
q10
q10
qe
q0
q1
q01
q00
q1 is distinguishable from qe, q0, q00, q10On transition 1, they go to distinguishable states
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
17/36
Example of DFA minimization
0
1
qe
q0
q1
q00
q10
q01
q11
0
1
0
1
1
0
01
1
0
0
1
x
x
x
x
x
x
x
xx
x
xxx x
q0
q1
q00
q01
q11
q10
q10
qe
q0
q1
q01
q00
q01 is distinguishable from qe, q0, q00, q10On transition 1, they go to distinguishable states
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
18/36
Example of DFA minimization
0
1
qe
q0
q1
q00
q10
q01
q11
0
1
0
1
1
0
01
1
0
0
1
A
x
A
x
Ax
x
A
x
Ax
x
B
xx
x
Ax
xx x
q0
q1
q00
q01
q11
q10
q10
qe
q0
q1
q01
q00
Merge states not marked distinguishable
qe, q0, q00, q10 are equivalent group A
q1, q01 are equivalent group Bq11 cannot be merged group C
A
B
C
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
19/36
Example of DFA minimization
0
1
qe
q0
q1
q00
q10
q01
q11
0
1
0
1
1
0
01
1
0
0
1
A
x
A
x
Ax
x
A
x
Ax
x
B
xx
x
Ax
xx x
q0
q1
q00
q01
q11
q10
q10
qe
q0
q1
q01
q00
1qA
qB
qC1
1
0
0
0
minimized DFA:
A
B
C
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
20/36
Why does DFA minimization work?
We need to convince ourselves of threeproperties:
Consistency
The new DFA M is well-defined
Correctness
The new DFA M is equivalent to the original DFA
Minimality
The new DFA M is the smallestDFA ossible forL
M M
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
21/36
Main claim
Proof outline:
First, we assume q, q are marked distinguishable, and
show they must be distinguishable
Then, we assume q, q are not marked distinguishable,and show they must be equivalent
Any two states qand q in M aredistinguishable if and only ifthe algorithm
marks them as distinguishable
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
22/36
Proof of part 1
First, suppose q, q are marked as distinguishable Could it be that they are equivalent?
No, because recall how the algorithm works:
q
q
w1
w1
w2
w2
wk
wk
wk-1
wk-1
xxxx
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
23/36
Proof idea for part 2
Now suppose q, q are not marked asdistinguishable
Could it be that they are distinguishable?
Suppose so
Then for some string w, d(q, w) accepts, but d(q, w)
rejects
Working backwards, the algorithm will mark qand
q as distinguishable at some point
^ ^
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
24/36
Proof of part 2
q
q
w1
w1
w1
w1
wk
wk
wk-1
wk-1
For any pair of states q, q:Ifqis accepting and q is rejecting
Mark(q, q) as distinguishable
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
25/36
Proof of part 2
q
q
w1
w1
w2
w2
wk
wk
wk-1
wk-1
x
Repeat until nothing is marked:For any pair of states (q, q):
For every alphabet symbol a:
If(d(q, a), d(q, a)) are marked as distinguishable
Mark(q, q) as distinguishable
xxx
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
26/36
Why does DFA minimization work?
We need to convince ourselves of threeproperties:
Consistency
The new DFA M is well-defined
Correctness
The new DFA M is equivalent to the original DFA
Minimality
The new DFA M is the smallestDFA ossible forL
M M
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
27/36
Consistency of transitions
Why are the transitions between the merged
states consistent?
0
1
qe
q0
q1
q00
q10
q01
q11
0
1
0
1
1
0
01
1
0
0
1
A
B
C
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
28/36
Proof of consistency
Suppose there were two inconsistent transitionsin M labeled aout ofmerged stateqA
Aq3
q7q
1
Bq5
q2
Cq6
a
a
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
29/36
Proof of consistency
States q5 and q6 must be distinguishable because
they were not merged together
Aq3
q7q
1
Bq5
q2
q6
a
a
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
30/36
Proof of consistency
Then, q3 and q7 must also be distinguishable, and
this is impossible
Aq3
q7q
1
Bq5
q2
Cq6
a
a
w
w
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
31/36
Consistency of states
Why are the merged states eitherall accepting or
all rejecting?
0
1
qe
q0
q1
q00
q10
q01
q11
0
1
0
1
1
0
01
1
0
0
1
A
B
C
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
32/36
Consistency of states
Because merged states are not distinguishable,
so they are mutually equivalent
0
1
qe
q0
q1
q00
q10
q01
q11
0
1
01
1
0
01
1
0
0
1
A
B
C
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
33/36
Correctness
Each state ofM corresponds to a class of
mutually equivalent states in M
0
1qe
q0
q1
q00
q10
q01
q11
0
1
0
1
1
0
01
1
0
0
1
A
B
C
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
34/36
Proof of correctness
Claim: After reading input w,
Proof: True in start state, and stays true after,
because transitions are consistent
At the end, M accepts win state qi if and only ifMon input wlands in the state qA that represents qi qA must be accepting by consistency of states
M is in state qAif and only ifM is in one of
the states represented byA
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
35/36
Proof of minimality
Now suppose there is some smallerM forL
By the pigeonhole principle, there is a state q ofM such that
All pairs of states in M are distinguishable
M M
q
q
v
v
q
v, v
7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 6
36/36
Proof of minimality
Since qand q are distinguishable, for some w
But in M,d(q, w) cannot both accept and reject!
So, Mcannot be smallerthan M
All pairs of states in M are distinguishable
M M
q
q
v
v
q
v, v
w
w
?w