DFA Minimization

25
DFA minimization

description

Algorithm for minimizing DFA

Transcript of DFA Minimization

Page 1: DFA Minimization

DFA minimization

Page 2: DFA Minimization

Example• Construct a DFA over alphabet {0, 1} that

accepts those strings that end in 111

• This is big, isn’t there a smaller DFA for this?

0

1

……

……

q

q

q

q

q

q

q

q

q

q

q

0

1

0

1

0

1

1

1

1

1

0

Page 3: DFA Minimization

Smaller DFA

10

00

0

1q q q q1 1

Can we do it with 3 states?

Page 4: DFA Minimization

Even smaller DFA?• Suppose we had a 3 state DFA M for L

… let’s imagine what happens when:

• By the pigeonhole principle, on two of these inputs M ends in the same state

Minputs:

, 1, 11, 111

Page 5: DFA Minimization

Pigeonhole principle

• Here, balls are inputs, bins are states:

Suppose you are tossing m balls into n bins, and m > n. Then two balls end up in the same bin.

If you have a DFA with n states and you run it on m inputs, and m > n, then two inputs end up in same state.

Page 6: DFA Minimization

A smaller DFA

• Suppose M ends up in the same state after reading inputs x = 1 and y = 11

• Then after reading one more 1– The state of x1 = 11 should be rejecting– The state of y1 = 111 should be accepting

… but they are both the same state!

Minputs:

, 1, 11, 111

1, 11

11, 111

“ends in 111”

Page 7: DFA Minimization

A smaller DFA

• Suppose M ends up in the same state after reading inputs x = and y = 1

• Then after reading 11– The state of x1 = 11 should be rejecting– The state of y1 = 111 should be accepting

… but they are both the same state!

Minputs:

, 1, 11, 111

, 1

11, 111

“ends in 111”

Page 8: DFA Minimization

No smaller DFA!• After looking at all possible pairs for x, y, x ≠ y

we conclude that

• So, this DFA is minimal

There is no DFA with 3 states for L

1q q q q1 1 1

0

00

0

(, 1) (, 11) (, 111) (1, 11) (1, 111) (11, 111)

Page 9: DFA Minimization

DFA minimization

We will show how to turn any DFA for L into the minimal DFA for L

0

1

q

q

q

q

q

q

q

q

q

q

q

01

01

0

1

1

1

1

1

0

1

q

q

q

q

1

1

1

00 0

0

Page 10: DFA Minimization

Minimal DFAs and distinguishable states

• First, we have to understand minimal DFAs:

10

00

0

1q q q q1 1

reject accept

minimal DFA every pair of states is distinguishable

Page 11: DFA Minimization

Distinguishable states• Two states q and q’ are distinguishable if

q

q’

w1

w1

w2

w2

wk

wk

wk-1

wk-1

on the same continuation string w1w2...wk, one accepts, but the other rejects

reject

accept

Page 12: DFA Minimization

Examples of distinguishable states

distinguishable by 01(q0, q2)(q0, q3)

(q0, q1)

(q1, q3)(q2, q3)

(q1, q2)

distinguishable by 1distinguishable by

distinguishable by 1distinguishable by

distinguishable by

0 0

1q q q q0 1

0 1 1

DFA is minimal

Page 13: DFA Minimization

Examples of distinguishable states

distinguishable by

q

q

q

q

0

1

0

1

0, 1

0, 1

q0

q

q

0

1

0, 1

0, 1

(q1, q3)(q2, q3)

(q0, q3)

(q0, q2)(q0, q1)

(q1, q2)

distinguishable by

distinguishable by

distinguishable by 0distinguishable by 0indistinguishable

indistinguishable pairs can be merged

Page 14: DFA Minimization

Examples of distinguishable states

distinguishable by

(q1, q2)(q0, q3)

(q0, q2)

(q0, q1)(q2, q3)

(q1, q3)

distinguishable by

distinguishable by

distinguishable by

indistinguishable

indistinguishable

q

q

q

q

1

0, 1

0, 1

0, 1

0

qq0, 1

0, 1

Page 15: DFA Minimization

Finding (in)distinguishable states

Rule 1:

Rule 2:

q q’If q is accepting and q’ is rejectingMark (q, q’) as distinguishable (x)x

q1 q1’x

xq2 q2’

a a If (q1, q1’) are marked,Mark (q2, q2’) as distinguishable (x)

Rule 3: Unmarked pairs are indistinguishableMerge them together

Page 16: DFA Minimization

Example of DFA minimization

0

1q

q

q

q

q

q

q

01

01

1

0

01

1

0

0

1

q

q

q

q

q

q

qq q q qq

Page 17: DFA Minimization

Example of DFA minimization

0

1q

q

q

q

q

q

q

01

01

1

0

01

1

0

0

1

x x x x x x

q

q

q

q

q

q

qq q q qq

q11 is distinguishable from all other states

Page 18: DFA Minimization

Example of DFA minimization

0

1q

q

q

q

q

q

q

01

01

1

0

01

1

0

0

1

x

x

x

x

x

x

x x x x

q

q

q

q

q

q

qq q q qq

q1 is distinguishable from q, q0, q00, q10

On transition 1, they go to distinguishable states

Page 19: DFA Minimization

Example of DFA minimization

0

1q

q

q

q

q

q

q

01

01

1

0

01

1

0

0

1

x

x

x

x

x

x

x

x

x

x

x

x

x x

q

q

q

q

q

q

qq q q qq

q01 is distinguishable from q, q0, q00, q10

On transition 1, they go to distinguishable states

Page 20: DFA Minimization

Example of DFA minimization

0

1q

q

q

q

q

q

q

01

01

1

0

01

1

0

0

1

A

x

A

x

A

x

x

A

x

A

x

x

B

x

x

x

A

x

x

x x

q

q

q

q

q

q

qq q q qq

Merge states not marked distinguishable q, q0, q00, q10 are equivalent → group A q1, q01 are equivalent → group B q11 cannot be merged → group C

A

B

C

Page 21: DFA Minimization

Example of DFA minimization

0

1q

q

q

q

q

q

q

01

01

1

0

01

1

0

0

1

A

x

A

x

A

x

x

A

x

A

x

x

B

x

x

x

A

x

x

x x

q

q

q

q

q

q

qq q q qq

1q q qC1 1

0

00

minimized DFA:

A

B

C

Page 22: DFA Minimization

Food for thought• Why does method find all distinguishable pairs?

q

q’

w1

w1

w2

w2

wk

wk

wk-1

wk-1

xxxx

Because we work backwards

Page 23: DFA Minimization

Food for thought• Why are there no inconsistencies when we merge?

Aq3

q7

q1

B

q5

q2

C

q6

a

a

Because we only merge indistinguishable states

w

w

Page 24: DFA Minimization

Food for thought• Why is there no smaller DFA?

Mq

q’

v

v’

smaller DFA M’

q’’v, v’

Suppose there is

By the pigeonhole principle this must happen:

Page 25: DFA Minimization

Food for thought• Why is there no smaller DFA?

But then

M smaller DFA M’q

q’

v

v’

q’’v, v’

w

w?

w

Every pair of statesis distinguishable

q’’ cannot exist!