DFA Operations
Complement, Product, Union, Intersection, Difference, Equivalence
CS235 Languages and Automata
Wednesday, October 6 and Friday, October 8, 2010 Reading: Sipser pp. 45-46, Stoughton 3.11 – 3.12
and Minimization of DFAs
CS235 Languages and Automata
Department of Computer Science Wellesley College
Some DFAs
Here are some simple DFAs we will use as examples in today’s lecture.
What languages do they accept?
a L K DFA5
a B a
b b
A DFA1
a DFA2 a
b F
a b
E DFA3
a G a,b
b DFA4 a a,b
b L K
b M a
a,b
DFA6 b D
C b a I
H b J a O
N b
b P a
a,b
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.
a L K DFA5
a B a
b b
A DFA1
b F
a b
E DFA3
a G a,b
b L K
b M a
a,b
DFA5
DFA Operations 14-3
a B a
b b
A DFA1
b F
a b
E DFA3
a G a,b
a L K b
b M a
a,b
DFA Complement in Forlan
- val dfa1 = DFA.input “begin_and_end_with_a.dfa";
val dfa1 = - : dfa - DFA.complement;
val it = fn : dfa * sym set -> dfay
- val dfa1_comp = DFA.complement (dfa1, SymSet.fromString "a,b");
val dfa1_comp = - : dfa
- SymSet.toString (DFA.states dfa1_comp);
val it = "W, X, Y, <dead>" : string
- SymSet.toString (DFA.acceptingStates dfa1_comp);
val it = "W, Y, <dead>" : string
- DFA.output ("dfa_begin_and_end_with_a_comp.dfa", dfa_comp);
val it = () : unit
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 fin DFA x DFA s f ll s:
Product of DFAs
( )
We define DFA1x DFA2as follows:
States: Q1x2= Q1 x Q2 Alphabet:
Transitions:
1x2Q1x2 x Q1x2
( ((q q ) ) ) r r
q2 q1
( , )
DFA Operations 14-5
( , )
1x2( ((q1,q2), ) )
= ( 1( (q1,) ), 2( (q2,) ) Start State: s1x2= (s1, s2)
Final States: Definition depends on how we use product r2 r1
Sample Products
a B a
b b
A DFA1
b F
a b
E DFA3
a G a,b
b D b
a a
C DFA2
a I
b a
H DFA4
b J a,b
DFA1 x DFA2
a ( E H ) a ( E I ) b ( F J )
a b
DFA3 x DFA4
( A , D )
( A , C ) ( B , C )
( B , D ) a
a a
b b b b
( F , H )
( E , H ) ( E , I )
( G , I ) a
b
b
( F , J )
( G , J ) b
a
a
Practice
a B a
b b
A DFA1
b F
a b
E DFA3
a G a,b
DFA1 x DFA3
DFA Operations 14-7
Intersection of DFAs
We can intersect DFA1and DFA2(written DFA1DFA2) by defining the accepting states of DFA1x DFA2as those state pairs in which both states are final states of their DFAs.
( A D )
DFA1 DFA2
( A , C ) ( B , C )
( B D ) a
a
b b a b b
( F , H )
( E , H ) ( E , I )
( G , I ) a
a b
( F , J )
( G , J ) b
a
b b
a DFA3 DFA4
( A , D ) a ( B , D )
b a
a,b
Union of DFAs
We can union DFA1and DFA2(written DFA1DFA2) by defining the accepting states of DFA1x DFA2as those state pairs in which either state is a final state of its DFA.
( A D )
DFA1 DFA2
( A , C ) ( B , C )
( B D ) a
a
b b a b b
( F , H )
( E , H ) ( E , I )
( G , I ) a
a b
( F , J )
( G , J ) b
a
b b
a DFA3 DFA4
DFA Operations 14-9
( A , D ) a ( B , D )
b a
a,b
Difference of DFAs
The difference of two DFAs (written DFA1− DFA2) can be defined in terms of complement and intersection:
DFA1 − DFA2 = DFA1 DFA2
DFA1 − DFA2
( A C ) ( B C ) a
a ( E , H ) a ( E , I ) b ( F , J )
a b
DFA3 − DFA4
So we can take the difference of DFA1and by defining the final states of DFA1− DFA2as those state pairs in which the first state is final in DFA1and the is second state is not final in DFA2.
( A , D )
( A , C ) ( B , C )
( B , D ) a
a a
b b b b
( F , H ) a ( G , I ) b
b
( G , J ) b
a
a
a,b
What is a Closure Property?
A set S is closed under an n-ary operation f iff x
1,…, x
n S implies f(x
1,…, x
n) S
Examples:
• Bool is closed under negation, conjunction, disjunction.
• Nat is closed under + and * but not – and /.
CFL Properties 14-11
• Int is closed under +, *, and -, but not /.
• Rat is closed under +, *, -, and / (except division by 0).
Some Closure Properties of Regular Languages Recall that a language is regular iff there is a
DFA that accepts it.
Based on the previous DFA constructions, we know the p , following closure properties of regular languages.
Suppose L
1and L
2are regular languages. Then:
• L
1and L
2are regular;
• L
1 L
2is regular;
l
• L
1 L
2is regular;
• L
1− L
2and L
2− L
1are regular.
Are Any of the Following DFAs Equivalent?
a L K b DFA5
a O N b DFA6
a S b R DFA7 b Q a
b M a
a,b
b
b P a
a,b
a
b T b
a,b a
a,b U
DFA Operations 14-13
DFA
5and DFA
6are Not Equivalent
a L K DFA5
a O N DFA6 Look at their product!
b L K
b M a
a,b
b O N
b P a
a,b
DFA5 x DFA6 DFA5 accepts
but DFA6doesn’t
DFA6 accepts a but DFA5doesn’t
( K , N ) a ( L , O )
( M , P ) b
b a
but DFA6doesn t
DFA
5and DFA
7Are Equivalent
a L K b DFA5
S b R DFA7
Q a Look at their product!
b L K
b M a
a,b
DFA5 x DFA7
a S R
b T b
a,b Q
a
a b
a,b U
DFA Operations 14-15
( K , Q ) ( K , S )
( M , T )
b
b a
a,b
( L , R ) a ( M , U ) a,b
a
DFA Equivalence Algorithm
To determine if DFA1and DFA2are equivalent, construct DFA1 x DFA2 and examine all state pairs containing at least one accepting state from DFA1or DFA2:
• If in all such pairs, both components are accepting, DFAIf in all such pairs, both components are accepting, DFA11and and DFA2are equivalent --- i.e., they accept the same language.
• If in all such pairs, the first component is accepting but in some the second is not, the language of DFA1is a superset of the language of DFA2and it is easy to find a string accepted by DFA1and not by DFA2
• If in all such pairs, the second component is accepting but in th fi t i t th l f DFA i b t f th some the first is not, the language of DFA1is a subset of the language of DFA2, and it is easy to find a string accepted by DFA2and not by DFA1
• If none of the above cases holds, the languages of DFA1and DFA2are unrelated, and it is easy to find a string accepted by one and not the other.
Products in Forlan
val inter : dfa * dfa -> dfa val minus : dfa * dfa -> dfa
datatype relationshipyp 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 : dfa * dfa -> bool
DFA Operations 14-17
q
Note that a union operator is missing. It really should be there!
We’ll see later how it can be defined.
Forlan Products: Example
- val bwa = DFA.input "begin_with_a.dfa";
val bwa = - : dfa
- val ewa = DFA.input "end_with_a.dfa";
val ewa = - : dfa
- val baewa = DFA.inter(bwa,ewa);
val baewa = - : dfa
- DFA.output("baewa.dfa", baewa);
val it = () : unit
Forlan Products: Example (Continued)
- val dfa1 = DFA.input "begin_and_end_with_a.dfa";
val dfa1 = - : dfa
- DFA.relationship(baewa, dfa1);
languages are equal languages are equal val it = () : unit
- DFA.relation(baewa, dfa1);
val it = Equal : DFA.relationship
- DFA.relation(bwa, baewa);
val it = ProperSup [-,-] : DFA.relationship
- let val DFA.ProperSup s = it in Str.toString s end;
DFA Operations 14-19 stdIn:19.9-19.29 Warning: binding not exhaustive
ProperSup s = ...
val it = "ab" : string
- DFA.subset(baewa, bwa);
val it = true : bool
Minimal DFAs
a L K b DFA5
b
a S b R DFA7
b b
Q a a
b M a
a,b
• A DFA is minimal if it has the smallest number of states of any DFA accepting its language.
• Is DFA5minimal?
b T
a,b U
a,b
Is DFA5minimal?
• Is DFA7minimal?
State Merging
a L K b DFA5
b
a S b R DFA7
b b
Q a a
b M a
a,b
• A DFA is not minimal iff two states can be merged to form a single state without changing the meaning of the DFA.
• Final states and non-final states can never be merged.
b T
a,b U
a,b
DFA Operations 14-21
• Can merge two states iff for each symbol they transition to mergeable states.
• Which states in DFA7can be merged?
State Merging in DFA
7Merge T with U
a S b R DFA7
b Q a
a
a S b R
b Q a
a
Merge Q with S
b T b
a,b a
a,b U
b
a,b
{ T,U }
a b
a R
b a,b
a {T,U}
{ Q ,S }
Problem: States Can’t Always be Merged Iteratively
DFA8
a a
V ab W b
Simultaneously merge V with X and W with Y
Y
Z b
a,b X
a
a b
a
{W,Y}
{ V , X } b a
a,b Z
b a
DFA Operations 14-23
Key to solution: rather than iterating to find mergeablestate pairs, iterate to find all state pairs that are provably unmergeable. Then any remaining state pair is mergeable.
This is an example of a greatest fixed point iteration, in which items are assumed related unless proven otherwise.
DFA Minimization Algorithm: Step 1
a S b R DFA7
b b
Q a a List all pairs of states than must
not be merged = pairs of one final and one non-final state.
Other pairs might be mergeable;
th id d bl
Us Q
b T
a,b U
a,b they are considered mergeable
until proven otherwise.
It’s a good idea to keep track of state pairs in half of a table*:
U T
?
S RUs Unmergeable by string s
Us Us R ? S T
* Lyn adapted this table representation from Katie Sullivan (Olin)
MightBeMergeable
? Table1 Us Us Us
? ?
DFA Minimization Algorithm: Step 2
a S b R DFA7
b b
Q a a Change from MightBeMergeable to Unmergeable
any pair (A,B) such that there is a transition to a (C,D) in Unmergeable:
( , )
A BMightBeMergeable
b T
a,b U
( , )
C D a,ba a a
Unmergeable
Repeat this step until no more state pairs can be changed.
In Table2, In Table1 pairs
U T S R U T S R
DFA Operations 14-25
(R,T) b (S,T) (R,U) b (S,T)
In Table2, no pairs can be changed In Table1, pairs
(R,T) and (R,U) be changed:
Q U
R S T
?
? U
U U
?
? U U
Table1
Q U
R S T
? Ub U
U U
Ub
? U U
Table2
DFA Minimization Algorithm: Step 3
When no more pairs can be changed from MightBeMergeable to Unmergeable, merge the pairs remaining in MightBeMergeable.
Q U U
T M S R
U U
Q U U
T
? S R
U U Us Unmergeable by s
b a
DFA7 Merge Q with S
b M Mergeable
? MightBeMergeable
Q U
R S T
M Ub U U U
Ub M U U
Table2 Q U
R S T
? Ub U U U
Ub
? U U
Table2
a a S
R
7
b T b
a,b Q a
a
a,b U
and T with Ug Q R
b a,b
a {T,U}
{ Q ,S }
DFA Minimization: More Practice
DFA8
X a
W V ab
b Merge V with X
and W with Y { V ,W } {W,Y}
a b
Y
Z b
a,b X
a a
b
V Z U
Y
? X W
U U
Z U
Y
? X W
U U
Z U
Y X W a,b Z
b a
a
DFA Operations 14-27
V U W X
Y
?
? U
U U
?
? U U
Table1
V U
W X
Y
?
? U U U
Ub Ub U U
Table2
V U W X
Y
M M U
U U Ub
Ub U U
Table3
Both examples happen to converge after 1 iteration of step 2, but in general can take 0 to (|Q|-1) iterations.
Minimization in Forlan
val minimize : dfa -> dfa