25
CSC 3130: Automata theory and formal languages Andrej Bogdanov http://www.cse.cuhk.edu.hk/ ~andrejb/csc3130 The Chinese University of Hong Kong DFA minimization Fall 2009

CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong DFA minimization

Embed Size (px)

DESCRIPTION

Smaller DFA qq qq qq qq 11 Can we do it with 3 states?

Citation preview

Page 1: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong DFA minimization

CSC 3130: Automata theory and formal languages

Andrej Bogdanovhttp://www.cse.cuhk.edu.hk/~andrejb/

csc3130

The Chinese University of Hong Kong

DFA minimization

Fall 2009

Page 2: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong 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: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong DFA minimization

Smaller DFA

1

0

00

0

1q q q q1 1

Can we do it with 3 states?

Page 4: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong 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: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong 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: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong 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: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong 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: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong 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: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong 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: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong DFA minimization

Minimal DFAs and distinguishable states• First, we have to understand minimal DFAs:

1

0

00

0

1q q q q1 1

reject accept

minimal DFA

every pair of states is

distinguishable

Page 11: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong 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: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong 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: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong 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: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong 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 indistinguishableindistinguishable

q

q

q

q

1

0, 1

0, 1

0, 1

0

qq0, 1

0, 1

Page 15: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong 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: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong DFA minimization

Example of DFA minimization

0

1q

q

q

q

q

q

q

0

1

0

1

1

0

01

1

0

0

1

q

q

q

q

q

q

qq q q qq

Page 17: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong DFA minimization

Example of DFA minimization

0

1q

q

q

q

q

q

q

0

1

0

1

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: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong DFA minimization

Example of DFA minimization

0

1q

q

q

q

q

q

q

0

1

0

1

1

0

01

1

0

0

1

x

x

x

x

x

xx 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: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong DFA minimization

Example of DFA minimization

0

1q

q

q

q

q

q

q

0

1

0

1

1

0

01

1

0

0

1

x

x

x

x

x

x

x

xx

x

xxx 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: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong DFA minimization

Example of DFA minimization

0

1q

q

q

q

q

q

q

0

1

0

1

1

0

01

1

0

0

1

AxAxAx

xAxAx

xBxx

xAx

xx 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

BC

Page 21: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong DFA minimization

Example of DFA minimization

0

1q

q

q

q

q

q

q

0

1

0

1

1

0

01

1

0

0

1

AxAxAx

xAxAx

xBxx

xAx

xx x

q

q

q

q

q

q

qq q q qq

1q q qC1 1

0

00

minimized DFA:

A

BC

Page 22: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong 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: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong DFA minimization

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

A q3

q7

q1

Bq5

q2

Cq6

a

a

Because we only merge indistinguishable states

w

w

Page 24: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong 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: CSC 3130: Automata theory and formal languages Andrej Bogdanov  The Chinese University of Hong Kong 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!