FALL 2010
Lexical Analysis:
DFA Minimization
Comp 412
Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.
Students enrolled in Comp 412 at Rice University have explicit permission to make copies
of these materials for their personal use.
Faculty from other educational institutions may use these materials for nonprofit
educational purposes, provided this copyright notice is preserved.
RE
NFA
DFA
minimal
DFA
Brzozowskis Algorithm
Hopcrofts algorithm
(today)
DFA Minimization
The Big Picture
DFA Minimization
The Big Picture
DFA Minimization
The Big Picture
DFA Minimization
D =(S,,,s0,F)
Q
Q has transitions
on to R, S, & T
T
R
Think of I as the
image of S under
the inverse of the
transition function
I 1( s, )
Comp 412, Fall 2010
Hopcroft's Algorithm
W {F, S-F}; P {F, S-F}; // W is the worklist, P the current partition
while ( W is not empty ) do begin
select and remove s from W ; // s is a set of states
for each in do begin
let I 1( s ); // I is set of all states that can reach s on
for each p P such that p Iis not empty
and p is not contained in Ido begin
partition p into p1 and p2 such that p1 p I; p2 p p1;
P (P p) p1 p2 ;
if p W
then W (W p) p1 p2 ;
else if |p1| |p2 |
then W W p1;
else W W p2;
end
end
end
pj
Q
pk
pk is everything
in pi - pj
T
R
10
A Detailed Example
(from last lecture)
Remember ( a | b )* abb ?
q0
a|
b
q1
q2
q3
q4
Our first
NFA
-closure(move(si,)
DFA
NFA
s0
q0,q1
q1,q2
q1
s1
q1,q2
q1,q2
q1,q3
s2
q1
q1,q2
q1
s3
q1,q3
q1,q2
q1,q4
s4
q1,q4
q1,q2
q1
contains q4
(final state)
11
A Detailed Example
The DFA for ( a | b )* abb
a
a
s0
a
b
s1
a
s3
a
s4
b
s2
b
Character
State
s0
s1
s2
s1
s1
s3
s2
s1
s2
s3
s1
s4
s4
s1
s2
(we feared exponential blowup)
12
A Detailed Example
P0
Current Partition
Worklist
{s4} {s0,s1,s2,s3}
{s4} {s0,s1,s2,s3}
a
b
s1
a
Split on a
Split on b
a
s0
(DFA Minimization)
s3
a
s4
b
s2
b
Comp 412, Fall 2010
13
A Detailed Example
P0
Current Partition
Worklist
Split on a
{s4} {s0,s1,s2,s3}
{s4} {s0,s1,s2,s3}
{s4}
none
a
b
s1
a
Split on b
a
s0
(DFA Minimization)
s3
a
s4
b
s2
b
Comp 412, Fall 2010
14
A Detailed Example
P0
Current Partition
Worklist
Split on a
Split on b
{s4} {s0,s1,s2,s3}
{s4} {s0,s1,s2,s3}
{s4}
none
{s3} {s0,s1,s2}
a
s0
(DFA Minimization)
a
b
s1
a
s3
a
s4
b
s2
b
Comp 412, Fall 2010
15
A Detailed Example
(DFA Minimization)
Current Partition
Worklist
Split on a
Split on b
P0
{s4} {s0,s1,s2,s3}
{s4} {s0,s1,s2,s3}
{s4}
none
{s3} {s0,s1,s2}
P1
{s3} {s0,s1,s2}
a
s0
a
b
s1
a
s3
a
s4
b
s2
b
Comp 412, Fall 2010
16
A Detailed Example
(DFA Minimization)
Current Partition
Worklist
Split on a
Split on b
P0
{s4} {s0,s1,s2,s3}
{s4} {s0,s1,s2,s3}
{s4}
none
{s3} {s0,s1,s2}
P1
{s3} {s0,s1,s2}
{s3}
none
a
s0
a
b
s1
a
s3
a
s4
b
s2
b
Comp 412, Fall 2010
17
A Detailed Example
(DFA Minimization)
Current Partition
Worklist
Split on a
Split on b
P0
{s4} {s0,s1,s2,s3}
{s4} {s0,s1,s2,s3}
{s4}
none
{s3} {s0,s1,s2}
P1
{s3} {s0,s1,s2}
{s3}
none
{s1} {s0,s2}
a
s0
a
b
s1
a
s3
a
s4
b
s2
b
Comp 412, Fall 2010
18
A Detailed Example
(DFA Minimization)
Current Partition
Worklist
Split on a
Split on b
P0
{s4} {s0,s1,s2,s3}
{s4} {s0,s1,s2,s3}
{s4}
none
{s3} {s0,s1,s2}
P1
{s3} {s0,s1,s2}
{s3}
none
{s1} {s0,s2}
P2
{s1} {s0,s2}
a
s0
a
b
s1
a
s3
a
s4
b
s2
b
Comp 412, Fall 2010
19
A Detailed Example
(DFA Minimization)
Current Partition
Worklist
Split on a
Split on b
P0
{s4} {s0,s1,s2,s3}
{s4} {s0,s1,s2,s3}
{s4}
none
{s3} {s0,s1,s2}
P1
{s3} {s0,s1,s2}
{s3}
none
{s1} {s0,s2}
P2
{s1} {s0,s2}
{s1}
none
none
a
s0
a
b
s1
a
s3
a
s4
b
s2
b
Comp 412, Fall 2010
20
A Detailed Example
(DFA Minimization)
Current Partition
Worklist
Split on a
Split on b
P0
{s4} {s0,s1,s2,s3}
{s4} {s0,s1,s2,s3}
{s4}
none
{s3} {s0,s1,s2}
P1
{s3} {s0,s1,s2}
{s3}
none
{s1} {s0,s2}
P2
{s1} {s0,s2}
{s1}
none
none
P2
{s1} {s0,s2}
{s0,s2}
none
none
a
s0
a
b
s1
a
s3
a
s4
b
s2
b
Comp 412, Fall 2010
21
A Detailed Example
(DFA Minimization)
Current Partition
Worklist
Split on a
Split on b
P0
{s4} {s0,s1,s2,s3}
{s4} {s0,s1,s2,s3}
{s4}
none
{s3} {s0,s1,s2}
P1
{s3} {s0,s1,s2}
{s3}
none
{s1} {s0,s2}
P2
{s1} {s0,s2}
{s1}
none
none
P2
{s1} {s0,s2}
{s0,s2}
none
none
a
s0
a
b
s1
a
s3
a
s4
b
b
s0 , s 2
a
a
s1
s3
a
s4
b
s2
b
Comp 412, Fall 2010
22
DFA Minimization
What about a ( b | c )* ?
q0
q1
q2
q4
q5
q3
q8
q6
q7
q9
-closure(Move(s,*))
DFA
NFA
s0
q0
s1
none
none
s1
q1 , q 2 , q 3 ,
q4 , q 6 , q 9
none
s2
s3
q5 , q 8 , q 9 ,
q3 , q 4 , q 6
none
s2
s3
q7 , q 8 , q 9 ,
q3, q2010
4 , q6
Comp 412, Fall
none
s2
s3
s2
s3
b
s2
b
s0
s1
b
c
c
s3
c
23
DFA Minimization
Then, apply the minimization algorithm
Split on
P0
Current Partition
{s1,s2,s3} {s0}
none
none
none
s2
b
s0
s1
b
c
c
s3
c
24
DFA Minimization
Then, apply the minimization algorithm
Split on
P0
Current Partition
{s1,s2,s3} {s0}
none
none
none
s2
b
s0
s1
b
c
c
s3
c
s1
25
RE
NFA
DFA
minimal
DFA
26
s0
sf
RE
NFA
DFA
minimal
DFA
27
sf0
2
8
sf9
sf1
sf2
sf8
RE
NFA
DFA
minimal
DFA
28
s0
0,1,2,3,4,
5,6,7,8,9
sf
RE
NFA
DFA
minimal
DFA
29
RE
NFA
DFA
minimal
DFA
Brzozowskis Algorithm
Hopcrofts algorithm
30
RE Back to DFA
Kleenes Construction
for i 0 to |D| - 1;
// label each immediate path
for j 0 to |D| - 1;
Rkij is the set of paths
R0ij { a | (di,a) = dj};
from i to j that include
if (i = j) then
no state higher than k
R0ii = R0ii | {};
for k 0 to |D| - 1;
// label nontrivial paths
for i 0 to |D| - 1;
for j 0 to |D| - 1;
Rkij Rk-1ik (Rk-1kk)* Rk-1kj | Rk-1ij
L {}
For each final state si
L L | R|D|-10i
RE
NFA
DFA
minimal
DFA
31
L = { wcwr | w *}
Neither of these is a regular language
(nor an RE)
32
33
34
DFA Minimization
The algorithm
T { F, {S-F}}
P{}
while ( P T)
PT
T{}
for each set pi P
T T Split(pi)
Split(S)
for each c
if c splits S into s1 & s2
then return {s1, s2}
return S
35
DFA Minimization
Refining the algorithm
36
s0
s1
s5
s8
d
a
s0
s1
s4
b
c
s2
s6
s9
s3
s7
s4
abc | bc | ad
s10
s3
s6
s2
s5
37
Then,
reachable(subset(reverse[reachable(subset(reverse(N))]))
[Brzozowski, 1962]
38
s0
s1
s5
s2
s6
s8
s9
s1
s2
s8
s9
b
c
d
s3
s4
s7
s11
Reversed NFA
s10
s3
d
s11
subset(reverse(NFA))
39
s0
s1
s2
s3
s8
s11
s9
s2
s0
s3
Minimal DFA
Comp 412, Fall 2010
RE
NFA
DFA
Brzozowski
minimal
DFA
40