14 DFA Operations Revised 2
14 DFA Operations Revised 2
Some DFAs
Here are some simple DFAs we will use as examples in today’s lecture.
What languages do they accept?
DFA5
a
K L
b b b
DFA1 a DFA3 b a,b
b a
a M
A B b a
a E F G a,b
1
Complement of DFAs
If DFA accepts language L, then L is accepted by DFA,
a version of DFA in which the accepting and
non-accepting states have been swapped. DFA5
a
K L
b b b
DFA1 a DFA3 b a,b
b a
a M
A B b a
a E F G a,b
DFA5
a
K L
b b b
DFA1 a DFA3 b a,b
b a
a M
A B b a
a E F G a,b
- DFA.complement;
val it = fn : dfa * sym
y set -> dfa
- DFA.output ("dfa_begin_and_end_with_a_comp.dfa",
dfa_comp);
val it = () : unit
2
Product of DFAs
We can run two DFAs in parallel on the same input via the
product construction, as long as they share the same alphabet.
Suppose DFA1 = (Q1, , 1, s1, F1) and DFA2 = (Q2, , 2, s2, F2)
W d
We define
fin DFA1 x DFA2 ass follows:
f ll s:
States: Q1x2 = Q1 x Q2
Alphabet: ( q1 ,q ) 2
Transitions:
1x2 Q1x2 x Q1x2
1x2 ( ((q1,q
q2),
) ) ) (r ,r ) 1 2
= ( 1( (q1,) ), 2( (q2,) )
Start State: s1x2 = (s1, s2)
Final States: Definition depends on how we use product
Sample Products
b DFA1 b a DFA3 b a,b
a b a
A B E F G
a
a a b DFA4 a a,b
DFA2
b a b
C D H I J
b
3
Practice
b DFA1 b a DFA3 b a,b
a b a
A B E F G
a
DFA1 x DFA3
Intersection of DFAs
We can intersect DFA1 and DFA2 (written DFA1 DFA2) by
defining the accepting states of DFA1 x DFA2 as those state
pairs in which both states are final states of their DFAs.
4
Union of DFAs
We can union DFA1 and DFA2 (written DFA1 DFA2) by
defining the accepting states of DFA1 x DFA2 as those state
pairs in which either state is a final state of its DFA.
Difference of DFAs
The difference of two DFAs (written DFA1 − DFA2) can be defined
in terms of complement and intersection:
DFA1 − DFA2 = DFA1 DFA2
So we can take the difference of DFA1 and by defining the final
states of DFA1 − DFA2 as those state pairs in which the first state
is final in DFA1 and the is second state is not final in DFA2.
5
What is a Closure Property?
Examples:
6
Are Any of the Following DFAs Equivalent?
DFA5 DFA6
a a
K L N O
b b
b a b a
M P
a,b a,b
7
DFA5 and DFA7 Are Equivalent
Look at their product!
DFA5 DFA7 b
a a
K L Q R S
b a
a b
b a b
M T U
a,b a,b a,b
DFA5 x DFA7
a b
(K,Q) (L,R) (K,S)
a
a
b a
(M,T) (M,U)
a,b
a,b
DFA Operations 14-15
8
Products in Forlan
val inter : dfa * dfa -> dfa
val minus : dfa * dfa -> dfa
datatype
yp relationship
p
= Equal | Incomp of str * str | ProperSub of str | ProperSup of str
val relation : dfa * dfa -> relationship
val relationship : dfa * dfa -> unit
val subset : dfa * dfa -> bool
val equivalent
q : dfa * dfa -> bool
- DFA.output("baewa.dfa", baewa);
val it = () : unit
9
Forlan Products: Example (Continued)
- val dfa1 = DFA.input "begin_and_end_with_a.dfa";
val dfa1 = - : dfa
- DFA.relationship(baewa, dfa1);
languages are equal
val it = () : unit
- DFA.relation(baewa, dfa1);
val it = Equal : DFA.relationship
- DFA.relation(bwa, baewa);
val it = ProperSup [-,-] : DFA.relationship
- DFA.subset(baewa, bwa);
val it = true : bool
Minimal DFAs
DFA5 DFA7 b
a a
K L Q R S
b a
a b
b a b
M T U
a,b a,b a,b
10
State Merging
DFA5 DFA7 b
a a
K L Q R S
b a
a b
b a b
M T U
a,b a,b a,b
Merge Q with S
b
a
{ Q ,S } R
a
b {T,U}
a,b
DFA Operations 14-22
11
Problem: States Can’t Always be Merged Iteratively
Simultaneously merge
DFA8 V with X and W with Y
b
V W
b a
a a
X Y a {V,X} {W,Y}
b
b a
b
b a
Z
Z
a,b
a,b
U T S R
Us Unmergeable by string s
Q Us Us ? Us
R ? ? Us ? MightBeMergeable
S Us Us
T ? Table1
12
DFA Minimization Algorithm: Step 2
Change from MightBeMergeable to Unmergeable
DFA7 b
any pair (A,B) such that there is a transition to a
(C,D) in Unmergeable: a
Q R S
a
MightBeMergeable ( A , B ) a b
b
a a a T U
a,b a,b
Unmergeable ( C , D )
Repeat this step until no more state pairs can be changed.
U T S R U T S R In Table2,
In Table1, pairs
no pairs can
Q U U ? U (R,T) and (R,U) Q U U ? U be changed
be changed:
R ? ? U R Ub Ub U
U U b U U
S (R,T) (S,T) S
T ? Table1 b T ? Table2
(R,U) (S,T)
DFA Operations 14-25
U T S R U T S R
Us Unmergeable by s
Q U U ? U Q U U M U
R Ub Ub U R Ub Ub U ? MightBeMergeable
S U U S U U
M Mergeable
T ? Table2 T M Table2
b
DFA7 b Merge
g Q with S
a
a and T with U { Q ,S } R
Q R S
a a
a b
b b {T,U}
T U
a,b a,b a,b
DFA Operations 14-26
13
DFA Minimization: More Practice
DFA8
b b
V W
b a
Merge V with X { V ,W } {W,Y}
a
and W with Y
X a
Y a
b
b a b a
Z Z
a,b a,b
Z Y X W Z Y X W Z Y X W
V U U ? U V U U ? U V U U M U
W ? ? U W Ub ? U W Ub M U
X U U X U U X U U
Y ? Table1 Y Ub Table2 Y Ub Table3
Both examples happen to converge after 1 iteration of step 2,
but in general can take 0 to (|Q|-1) iterations. DFA Operations 14-27
Minimization in Forlan
val minimize : dfa -> dfa
14