You are on page 1of 20

DFA Minimization

Note these slides are not mine. http://faculty.kutztown.edu/spiegel Minimal?


Minimal number of states.

Unique?
A minimal DFA for a given language is unique up to renaming of states.

DFA Minimization: Algorithm Idea


2

Equate & collapse states having same behavior.

Build equivalence relation on states:


pq

(zS*, (p,z)F (q,z)F)

I.e., iff for every string z, one of the following is true:

p
q

z or z

p
q

z z

DFA Minimization: Algorithm


3

Build table to compare each unordered pair of distinct states p,q. Each table entry has
a mark as to whether p & q are known to be not equivalent, and a list of entries, recording dependences: If this entry is later marked, also mark these.

DFA Minimization: Algorithm


4

1.

Initialize all entries as unmarked & with no dependences. Mark all pairs of a final & nonfinal state. For each unmarked pair p,q & input symbol a:
1. 2. 3.

2.

3.

Let r=(p,a), s=(q,a). If (r,s) unmarked, add (p,q) to (r,s)s dependences, Otherwise mark (p,q), and recursively mark all dependences of newly-marked entries.

4.

Merge unmarked pairs of states.


Delete inaccessible states.

5.

DFA Minimization: Example


0 1 1 0 0 1 0 0

a
e

0 1 1

b
f
1

c
g

0 1 0 1

d
h

b
c d e f g h a b c d e
5

1. Initialize table entries: Unmarked, empty list

DFA Minimization: Example


0 1 1 0 0 1 0 0

a
e

0 1 1

b
f
1

c
g

0 1 0 1

d
h

b
c d e f g h a b c d e
6

2. Mark pairs of final and non-final states

DFA Minimization: Example


0 1 1 0 0 1 0 0

a
e

0 1 1

b
f
1

c
g

0 1 0 1

d
h

b
c d e f g h a b

(b,0) ? (a,0) (b,1) ? (a,1)

3. For each unmarked pair and symbol,

DFA Minimization: Example


0 1 1 0 0 1 0 0

a
e

0 1 1

b
f
1

c
g

0 1 0 1

d
h

b
c d e f g h a b

g ? b c ? f

Maybe. No!

3. For each unmarked pair and symbol,

DFA Minimization: Example


0 1 1 0 0 1 0 0

a
e

0 1 1

b
f
1

c
g

0 1 0 1

d
h

b
c d e f g h a b c d e
9

3. For each unmarked pair and symbol,

DFA Minimization: Example


0 1 1 0 0 1 0 0

a
e

0 1 1

b
f
1

c
g

0 1 0 1

d
h

b
c d e f g h a b

(e,0) ? (a,0) (e,1) ? (a,1)

3. For each unmarked pair and symbol,

10

DFA Minimization: Example


0 1 1 0 0 1 0 0

a
e

0 1 1

b
f
1

c
g

0 1 0 1

d
h

b
c d e f g h a
(a,e)

h ? b f ? f

Maybe. Yes.

3. For each unmarked pair and symbol,

11

DFA Minimization: Example


0 1 1 0 0 1 0 0

a
e

0 1 1

b
f
1

c
g

0 1 0 1

d
h

b
c d e f g h a
(g,a) (a,e) (g,a)

3. For each unmarked pair and symbol,

12

DFA Minimization: Example


0 1 1 0 0 1 0 0

a
e

0 1 1

b
f
1

c
g

0 1 0 1

d
h

b
c d e f g h a
(g,a) (a,e)

Need to mark. So, mark (g,a) also.

3. For each unmarked pair and symbol,

(g,a)

13

DFA Minimization: Example


0 1 1 0 0 1 0 0

a
e

0 1 1

b
f
1

c
g

0 1 0 1

d
h

b
c d e f g h a
(a,e) (g,a)

3. For each unmarked pair and symbol,

14

DFA Minimization: Example


0 1 1 0 0 1 0 0

a
e

0 1 1

b
f
1

c
g

0 1 0 1

d
h

b
c d e

4. Merge unmarked pairs of states.


0 1 1 0 1 0

f g h a
(a,e)

ae bh df

ae

0 1 1

bh
0

c g

df

15

DFA Minimization: Example


0 1 1 0 0 1 0 0

a
e

0 1 1

b
f
1

c
g

0 1 0 1

d
h

b
c d e f g h a
(a,e)

5. Delete unreachable states.


0 1 1 0 1 0

None.

ae

0 1 1

bh
0

c g

df

16

DFA Minimization: Notes


17

Order of selecting state pairs was arbitrary, but all orders give the same result. Can delete unreachable states initially, instead. This algorithm: O(n2) time; Huffman (1954), Moore (1956).
Constant work per entry: initial mark test & possibly later chasing of its dependences. More efficient algorithms exist, e.g., Hopcroft (1971).

DFA Minimization: Correctness


18

Why is new DFA no larger than old DFA?


Only removes states, never introduces new states.

Why is new DFA equivalent to old DFA?


Only identify states that have same behavior.

What About NFA Minimization?


19

This algorithm does not find a unique minimal NFA.

Is there a (not necessarily unique) minimal NFA for a given language?

Of course.

NFA Minimization
20

In general, minimal NFAs not unique!

Example NFAs for 0+:


0 0

Both minimal, but not isomorphic.

You might also like