meljun cortes automata theory 6
TRANSCRIPT
-
8/13/2019 MELJUN CORTES Automata Theory 6
1/36
CSC 3130: Automata theory and formal languages
DFA minimization algorithm
MELJUN P. CORTES MBA MPA BSCS ACS
MELJUN CORTES
-
8/13/2019 MELJUN CORTES Automata Theory 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
-
8/13/2019 MELJUN CORTES Automata Theory 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
-
8/13/2019 MELJUN CORTES Automata Theory 6
4/36
Even smaller DFA?
Suppose we had a 3 state DFAMfor L
We do not know what this M looks like
but lets imagine what happens when:
By the pigeonhole principle, on two of theseinputsMends in the same state
Minputs:
e, 1, 11, 111
-
8/13/2019 MELJUN CORTES Automata Theory 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.
-
8/13/2019 MELJUN CORTES Automata Theory 6
6/36
A smaller DFA?
Minputs:
e, 1, 11, 111
What goes wrong if
Mends up in same state on input 1and input 111?
Mends up in same state on input eand input 11?
-
8/13/2019 MELJUN CORTES Automata Theory 6
7/36
A smaller DFA
SupposeMends up in the same state afterreading inputs x= 1iandy= 1j, where 0 i
-
8/13/2019 MELJUN CORTES Automata Theory 6
8/36
No smaller DFA!
Conclusion
So, this DFA is minimal
In fact, it is the uniqueminimal DFA for L
There is no DFA with 3 states for L
1q0
q1
q2
q3
1 11
0
0
0
0
-
8/13/2019 MELJUN CORTES Automata Theory 6
9/36
DFA minimization
There is an algorithmto 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
-
8/13/2019 MELJUN CORTES Automata Theory 6
10/36
Examples of equivalent states
q0, q1equivalent
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, q1equivalent
q2, q3also equivalent
-
8/13/2019 MELJUN CORTES Automata Theory 6
11/36
Equivalent and distinguishable states
Two states q, qare equivalentif
Here, d(q, w)is the state that the machine is in if itstarts at qand reads the string w
q, qare distinguishableif they are not equivalent:
^
For every string w, the states d(q, w)and d(q,
w)are either both acceptingor both rejecting
^ ^
For some string w, one of the states d(q, w),
d(q, w)isacceptingand the other isrejecting
-
8/13/2019 MELJUN CORTES Automata Theory 6
12/36
Examples of distinguishable states
q0, q1equivalent
q3distinguishable from q0, q1, q2q3is 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
-
8/13/2019 MELJUN CORTES Automata Theory 6
13/36
DFA minimization algorithm
Find all pairs of distinguishablestates as follows:
For any pair of states q, q:
If qis accepting and qis 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 qinto a single state
-
8/13/2019 MELJUN CORTES Automata Theory 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
-
8/13/2019 MELJUN CORTES Automata Theory 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
q11is distinguishable from all other states
-
8/13/2019 MELJUN CORTES Automata Theory 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
q1is distinguishable from qe, q0, q00,q10On transition 1, they go to distinguishable states
-
8/13/2019 MELJUN CORTES Automata Theory 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
q01is distinguishable from qe, q0, q00,q10On transition 1, they go to distinguishable states
-
8/13/2019 MELJUN CORTES Automata Theory 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, q10are equivalent group A
q1, q01are equivalent group Bq11cannot be merged group C
A
B
C
-
8/13/2019 MELJUN CORTES Automata Theory 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
-
8/13/2019 MELJUN CORTES Automata Theory 6
20/36
Why does DFA minimization work?
We need to convince ourselves of threeproperties:
Consistency
The new DFA Mis well-defined
Correctness
The new DFA Mis equivalentto the original DFA
Minimality
The new DFA Mis the smallestDFA ossiblefor L
M M
-
8/13/2019 MELJUN CORTES Automata Theory 6
21/36
Main claim
Proof outline:
First, we assume q, qare marked distinguishable, and
show they must be distinguishable
Then, we assume q, qare not marked distinguishable,and show they must be equivalent
Any two states qand qin Maredistinguishable if and only ifthe algorithm
marks them as distinguishable
-
8/13/2019 MELJUN CORTES Automata Theory 6
22/36
Proof of part 1
First, suppose q, qare 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
-
8/13/2019 MELJUN CORTES Automata Theory 6
23/36
Proof idea for part 2
Now suppose q, qare 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
qas distinguishable at some point
^ ^
-
8/13/2019 MELJUN CORTES Automata Theory 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:If qis accepting and qis rejecting
Mark(q, q)as distinguishable
-
8/13/2019 MELJUN CORTES Automata Theory 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
-
8/13/2019 MELJUN CORTES Automata Theory 6
26/36
Why does DFA minimization work?
We need to convince ourselves of threeproperties:
Consistency
The new DFA Mis well-defined
Correctness
The new DFA Mis equivalentto the original DFA
Minimality
The new DFA Mis the smallestDFA ossiblefor L
M M
-
8/13/2019 MELJUN CORTES Automata Theory 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
-
8/13/2019 MELJUN CORTES Automata Theory 6
28/36
Proof of consistency
Suppose there were two inconsistent transitionsin Mlabeled aout of merged stateqA
Aq3
q7q
1
Bq5
q2
Cq6
a
a
-
8/13/2019 MELJUN CORTES Automata Theory 6
29/36
Proof of consistency
States q5and q6must be distinguishable because
they were not merged together
Aq3
q7q
1
Bq5
q2
q6
a
a
-
8/13/2019 MELJUN CORTES Automata Theory 6
30/36
Proof of consistency
Then, q3and q7must also be distinguishable, and
this is impossible
Aq3
q7q
1
Bq5
q2
Cq6
a
a
w
w
-
8/13/2019 MELJUN CORTES Automata Theory 6
31/36
Consistency of states
Why are the merged states either all acceptingor
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
-
8/13/2019 MELJUN CORTES Automata Theory 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
-
8/13/2019 MELJUN CORTES Automata Theory 6
33/36
Correctness
Each state of Mcorresponds to a class of
mutually equivalent statesin M
0
1qe
q0
q1
q00
q10
q01
q11
0
1
0
1
1
0
01
1
0
0
1
A
B
C
-
8/13/2019 MELJUN CORTES Automata Theory 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, Maccepts win state qiif and only if Mon input wlands in the state qAthat represents qi qAmust be accepting by consistency of states
Mis in state qAif and only ifMis in one of
the states represented byA
-
8/13/2019 MELJUN CORTES Automata Theory 6
35/36
Proof of minimality
Now suppose there is some smallerMfor L
By the pigeonhole principle, there is a state q ofMsuch that
All pairs of states in Mare distinguishable
M M
q
q
v
v
q
v,v
-
8/13/2019 MELJUN CORTES Automata Theory 6
36/36
Proof of minimality
Since qand qare 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 Mare distinguishable
M M
q
q
v
v
q
v,v
w
w
?w