0% found this document useful (0 votes)
31 views14 pages

14 DFA Operations Revised 2

This document provides an overview of operations that can be performed on deterministic finite automata (DFAs) that will be covered in lectures on October 6th and 8th, 2010. These operations include complement, product, union, intersection, difference, and minimization of DFAs. Examples of simple DFAs are given and how they relate to these operations. The document also gives examples of how to implement some of these operations, such as complement, product, and intersection, in Forlan code.

Uploaded by

Tahamid Hasan.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views14 pages

14 DFA Operations Revised 2

This document provides an overview of operations that can be performed on deterministic finite automata (DFAs) that will be covered in lectures on October 6th and 8th, 2010. These operations include complement, product, union, intersection, difference, and minimization of DFAs. Examples of simple DFAs are given and how they relate to these operations. The document also gives examples of how to implement some of these operations, such as complement, product, and intersection, in Forlan code.

Uploaded by

Tahamid Hasan.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

DFA Operations

Complement, Product, Union,


Intersection, Difference, Equivalence
and Minimization of DFAs
Wednesday, October 6 and Friday, October 8, 2010
Reading: Sipser pp. 45-46, Stoughton 3.11 – 3.12

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?
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

a DFA2 a b DFA4 a a,b DFA6


b a b a
C D H I J N O
b b
b a
P
a,b

DFA Operations 14-2

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 Operations 14-3

DFA Complement in Forlan


- val dfa1 = DFA.input “begin_and_end_with_a.dfa";
val dfa1 = - : dfa

- DFA.complement;
val it = fn : dfa * sym
y set -> dfa

- 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

DFA Operations 14-4

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

DFA Operations 14-5

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

DFA1 x DFA2 DFA3 x DFA4 a


b
a a b
(E,H) (E,I) (F,J)
(A,C) a (B,C)
b a
b b b b
a a b
(F,H) (G,I) (G,J)
(A,D) a (B,D)
b a
a,b
DFA Operations 14-6

3
Practice
b DFA1 b a DFA3 b a,b
a b a
A B E F G
a

DFA1 x DFA3

DFA Operations 14-7

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.

DFA1  DFA2 DFA3  DFA4 a


b
a a b
(E,H) (E,I) (F,J)
(A,C) a (B,C)
b a
b b b b
a a b
(F,H) (G,I) (G,J)
(A,D) a (B,D)
a
b a,b

DFA Operations 14-8

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.

DFA1  DFA2 DFA3  DFA4 a


b
a a b
(E,H) (E,I) (F,J)
(A,C) a (B,C)
b a
b b b b
a a b
(F,H) (G,I) (G,J)
(A,D) a (B,D)
a
b a,b

DFA Operations 14-9

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.

DFA1 − DFA2 DFA3 − DFA4 a


b
a a b
(E,H) (E,I) (F,J)
(A,C) a (B,C)
b a
b b b b
a a b
(F,H) (G,I) (G,J)
(A,D) a (B,D)
a
b a,b

DFA Operations 14-10

5
What is a Closure Property?

A set S is closed under an n-ary operation f


iff x1,…, xn  S implies f(x1,…, xn)  S

Examples:

• Bool is closed under negation, conjunction, disjunction.

• Nat is closed under + and * but not – and /.

• Int is closed under +, *, and -, but not /.

• Rat is closed under +, *, -, and / (except division by 0).

CFL Properties 14-11

Some Closure Properties of Regular Languages


Recall that a language is regular iff there is a
DFA that accepts it.
Based on the pprevious DFA constructions,, we know the
following closure properties of regular languages.
Suppose L1 and L2 are regular languages. Then:
• L1 and L2 are regular;
• L1  L2 is regular;
• L1  L2 is regular;
l
• L1 − L2 and L2 − L1 are regular.

DFA Operations 14-12

6
Are Any of the Following DFAs Equivalent?

DFA5 DFA6 DFA7 b


a a a
K L N O Q R S
b b a
a b
b a b a b
M P T U
a,b a,b a,b a,b

DFA Operations 14-13

DFA5 and DFA6 are Not Equivalent


Look at their product!

DFA5 DFA6
a a
K L N O
b b
b a b a
M P
a,b a,b

DFA5 x DFA6 DFA6 accepts a


DFA5 accepts 
but DFA5 doesn’t
but DFA6 doesn
doesn’tt
a
(K,N) (L,O)
b
b a
(M,P)
a,b
DFA Operations 14-14

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

DFA Equivalence Algorithm


To determine if DFA1 and DFA2 are equivalent, construct DFA1 x
DFA2 and examine all state pairs containing at least one accepting
state from DFA1 or DFA2:
• If in all such pairs, both components are accepting, DFA1 and
DFA2 are 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 DFA1 is a superset of the
language of DFA2 and it is easy to find a string accepted by
DFA1 and not by DFA2
• If in all such pairs, the second component is accepting but in
some the
th fifirstt iis not,
t th
the llanguage of
f DFA1 is
i a subset
b t of
f the
th
language of DFA2, and it is easy to find a string accepted by
DFA2 and not by DFA1
• If none of the above cases holds, the languages of DFA1 and
DFA2 are unrelated, and it is easy to find a string accepted by
one and not the other.
DFA Operations 14-16

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

Note that a union operator is missing. It really should be there!


We’ll see later how it can be defined.

DFA Operations 14-17

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

DFA Operations 14-18

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

- let val DFA.ProperSup s = it in Str.toString s end;


stdIn:19.9-19.29 Warning: binding not exhaustive
ProperSup s = ...
val it = "ab" : string

- DFA.subset(baewa, bwa);
val it = true : bool

DFA Operations 14-19

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

• A DFA is minimal if it has the smallest number


of states of any DFA accepting its language.
• Is DFA5 minimal?
• Is DFA7 minimal?

DFA Operations 14-20

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

• 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.
• Can merge two states iff for each symbol they transition to
mergeable states.
• Which states in DFA7 can be merged?

DFA Operations 14-21

State Merging in DFA7


b
DFA7 b
a
a Q R S
Q R S Merge T with U a
a
a b a b
b b
T U { 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

Key to solution: rather than iterating to find mergeable state 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 Operations 14-23

DFA Minimization Algorithm: Step 1


List all pairs of states than must DFA7 b
not be merged = pairs of one final
and one non-final state.
a
Q R S
a
Other pairs might be mergeable; a b
th are considered
they id d mergeable
bl b
until proven otherwise. T U
a,b a,b
It’s a good idea to keep track of
state pairs in half of a table*:

U T S R
Us Unmergeable by string s
Q Us Us ? Us
R ? ? Us ? MightBeMergeable
S Us Us
T ? Table1

* Lyn adapted this table representation from Katie Sullivan (Olin)


and the subscripted Unmergeability from Anna Loparev (Wellesley) DFA Operations 14-24

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

DFA Minimization Algorithm: Step 3


When no more pairs can be changed from MightBeMergeable to
Unmergeable, merge the pairs remaining in MightBeMergeable.

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

DFA Operations 14-28

14

You might also like