49
Lecture 5: Minimizing DFAs 6.045 1

PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

Lecture 5:Minimizing DFAs

6.045

1

Page 2: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

Announcements:- Pset 2 is up (as of last night)

- Dylan says: “It’s fire.”

- How was Pset 1?

6.045

2

Page 3: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

DFAs NFAs

RegularLanguages

RegularExpressions

DEFINITION

3

Page 4: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

Some Languages Are Not Regular:

Limitations on DFAs/NFAsa.k.a.

“Lower Bounds” on DFAs/NFAs

4

How to Make a DFA Lose Its Mind

Page 5: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

Minimizing DFAs

5

Page 6: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

Does this DFA have a minimal number of states?

11

1

1

0

0

00

6

Page 7: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

0

1

01

11

1

1

0

0

00

7

Recognizes{w | w ends in 0}

Page 8: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

Is this minimal?

0

1

01

8

How can we tell in general?

Page 9: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

DFA Minimization Theorem:

For every regular language A, there is a unique (up to re-labeling of the states)

minimal-state DFA M* such that A = L(M*).

Furthermore, there is an efficient algorithm which, given any DFA M, will

output this unique M*.

9

If such algorithms existed for more general models of computation, that

would be an engineering breakthrough!!

Page 10: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

In general, there isn’t a uniquely minimal NFA

0

0

0

0

10

Page 11: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

Distinguishing states with strings

For a DFA M = (Q, Σ, , q0, F), and q ∈ Q,let Mq be the DFA equal to (Q, Σ, , q, F)

11

Def. w Σ* distinguishes states p and q if:Mp accepts wMq rejects w

Mw

w

p

q

accept

reject

Mw

w

p

qaccept

rejectOR

Page 12: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

Distinguishing states with strings

For a DFA M = (Q, Σ, , q0, F), and q ∈ Q,let Mq be the DFA equal to (Q, Σ, , q, F)

12

Def. w Σ* distinguishes states p and q if:Mp and Mq have different outputs on input w

Mw

w

p

q

accept

reject

Mw

w

p

qaccept

rejectOR

Page 13: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

Distinguishing two states

Def. w Σ* distinguishes states p and q iff

Mp and Mq have different outputs on w

I’m in p or q, but which?How can I tell?

W

Here… read thisOk, I’m accepting!Must have been p

13

Ok, I’m rejecting!Must have been q

Page 14: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

Fix M = (Q, Σ, , q0, F) and let p, q Q

Definition(s):

State p is distinguishable from state q iff there is a w Σ* that distinguishes p and qiff there is a w Σ* so that

Mp accepts w Mq rejects w

State p is indistinguishable from state q iff p is not distinguishable from qiff for all w Σ*, Mp accepts wMq accepts w

14

Big Idea: Pairs of indistinguishable states are redundant!From p or q, M has exactly the same output behavior

Let Mp = (Q, Σ, , p, F) and Mq = (Q, Σ, , q, F)

Page 15: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

00,1

00

1

1

1

q0

q1

q2

q3

15

Which pairs of states are distinguishable?

Are q0 and q1

distinguishable?

Page 16: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

00,1

00

1

1

1

q0

q1

q2

q3

16

Are q0 and q3

distinguishable?

Page 17: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

00,1

00

1

1

1

q0

q1

q2

q3

17

Are q1 and q2

distinguishable?

Page 18: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

Fix M = (Q, Σ, , q0, F) and let p, q, r Q

Define a binary relation ∼ on the states of M:

p ∼ q iff p is indistinguishable from q

p ≁ q iff p is distinguishable from q

Proposition: ∼ is an equivalence relation

p ∼ p (reflexive)

p ∼ q q ∼ p (symmetric)

p ∼ q and q ∼ r p ∼ r (transitive)

Proof?

18

Just look at the definition! p ∼ q means for all w, Mp accepts w Mq accepts w

Page 19: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

Fix M = (Q, Σ, , q0, F) and let p, q, r Q

Proposition: ∼ is an equivalence relation

Therefore, the relation ∼ partitions Q into disjoint equivalence classes

q0

Q

q

[q] := { p | p ∼ q }

19

Page 20: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

11

1

1

0

0

00

20

Page 21: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

Algorithm: MINIMIZE-DFA

Input: DFA M

Output: DFA MMIN such that:

1. L(M) = L(MMIN)

2. MMIN has no inaccessible states

3. MMIN is irreducible

for all states p q of MMIN, p and q are distinguishable

||

Theorem: Every MMIN satisfying 1,2,3 is the unique minimal DFA equivalent to M

21

unreachable from start state

Page 22: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

Intuition:States of MMIN = Equivalence classes

of states of M

We’ll discover the equivalent states witha dynamic programming algorithm

22

Page 23: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

The Table-Filling Algorithm

Input: DFA M = (Q, Σ, , q0, F)

(2) EQUIVM = { [q] | q Q }

(1) DM = { (p, q) | p, q Q and p ≁ q }

• We know how to find those pairs of states that the string ε distinguishes…

• Use this and iteration to find those pairs distinguishable with longer strings

• The pairs of states left over will be indistinguishable

Idea:

Output:

23

Page 24: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

The Table-Filling Algorithm

Input: DFA M = (Q, Σ, , q0, F)

(2) EQUIVM = { [q] | q Q }

(1) DM = { (p, q) | p, q Q and p ≁ q }Output:

24

q0

q1

qi

qn

q0 q1 qi qn

Suppose |Q|=n+1.Start by making a table of cells, with ½ of all possible state pairs.We want to fill in which pairs aredistinguishable.

Page 25: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

The Table-Filling Algorithm

Input: DFA M = (Q, Σ, , q0, F)

(2) EQUIVM = { [q] | q Q }

(1) DM = { (p, q) | p, q Q and p ≁ q }Output:

25

q0

q1

qi

qn

q0 q1 qi qn

Base Case: For all (p, q) such that p accepts and q rejects mark p ≁ q

Page 26: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

q0

q1

qi

qn

q0 q1 qi qn

Iterate rule: If there are states p, qand a symbol σ Σ satisfying: D D

D

(p, ) = p

(q, ) = q≁

mark p ≁ q

Repeat until the rule doesn’t apply

The Table-Filling Algorithm

Base Case: For all (p, q) such that p accepts and q rejects mark p ≁ q

26

Input: DFA M = (Q, Σ, , q0, F)

(2) EQUIVM = { [q] | q Q }

(1) DM = { (p, q) | p, q Q and p ≁ q }Output:

Page 27: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

1 0

1

0 1

0,1

0

q0

q0 q1

q1

q2

q2 q3

q3

q0 q1 q2 q3

27

Are q1 and q2

distinguishable?

Are q0 and q1

distinguishable?

Are q0 and q2

distinguishable?

Page 28: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

11

1

1

0

0

00

q0

q0 q1

q1

q2

q2 q3

q3 D D

D

D

q0 q1

q2q3

28

Recognizes{w | w ends in 0}

Page 29: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

Proof: Induction on the number of iterations 𝒏 in the algorithm when (p, q) is marked D

𝒏 = 𝟎: If (p, q) is marked D in the base case, then exactly one of them is final, so ε distinguishes p and q

To be marked, there must be states p, q such that:

1. p = (p,) and q = (q,), for some Σ

Then, the string w distinguishes p and q!

Suppose (p, q) is marked D in the (𝒏 + 𝟏)th iteration.

2. (p, q) is marked D p ≁ q (by induction)

So there’s a w s.t. w distinguishes p’ and q’

29

I.H. For all (p’, q’) marked D in the first 𝒏 iterations, p ≁ q

Claim: If (p, q) is marked D by the algorithm, then p ≁ q

Page 30: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

Claim: If (p, q) is not marked D by the algorithm, then p ∼ q

Proof (by contradiction):

Suppose there is a pair (p, q) not marked D by the algorithm, yet p ≁ q (call this a “bad pair”)

Then there is a string w such that |w| > 0 and:

Mp and Mq have different outputs on w

Of all such bad pairs, let (p, q) be a pair with a minimum-length distinguishing string w

(Why is |w| > 0?)

30

Page 31: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

Proof (by contradiction):

Suppose there is a pair (p, q) not marked D by the algorithm, yet p ≁ q (call this a “bad pair”)

We have w = w, for some string w’ and some Σ

Let p = (p,) and q = (q,).

Then (p, q) is also a bad pair! (It must be not marked D)

Of all such bad pairs, let (p, q) be a pair with a minimum-length distinguishing string w

31

But then (p, q) has a SHORTER distinguishing string, wContradiction!

(p,q’) distinguished by w’

Claim: If (p, q) is not marked D by the algorithm, then p ∼ q

Mp and Mq have different outputs on w (Why is |w| > 0?)

Page 32: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

Algorithm MINIMIZE

Input: DFA M

Output: Equivalent minimal-state DFA MMIN

1. Remove all inaccessible states from M

2. Run Table-Filling algorithm on M to get:EQUIVM = { [q] | q is an accessible state of M }

QMIN = EQUIVM, q0 MIN = [q0], FMIN = { [q] | q F }

MIN( [q], ) = [ ( q, ) ]

3. Define: MMIN = (QMIN, Σ, MIN, q0 MIN, FMIN)

Claim: L(MMIN) = L(M)32

(well-defined??)

Page 33: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

M

The MINIMIZE Algorithm in Pictures

1. Remove all inaccessible states

33

q0

q

Page 34: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

M

The MINIMIZE Algorithm in Pictures

2. Run Table-Filling to get equiv classes

34

q0

q

[q] := { p | p ∼ q }

Page 35: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

M

The MINIMIZE Algorithm in Pictures

3. Define MMIN with states = equiv classes

35

q0

q

States of MMIN = Equivalence classesof states of M

Page 36: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

101

0

1

0q0 q1

q2

MINIMIZE

36

q0 q1

q0

q1

q2 q3

q2 DD

Page 37: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

1

0

10

101

0

1

0q0 q1

q2

MINIMIZE

37

Page 38: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

1 0

1

0

1

0,1

0

q0 q1

q3

q5

q4

0

1

q2

0,1q0

q0 q1

q1

q3

q3 q4

q4

q5

q5

38

Distinguish q1

from q3 and q4

Distinguish q0

from q3 and q4

Page 39: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

Suppose we have proved the Claim is true.Assuming the Claim we can prove the Thm:

Proof of Thm: Let M be any minimal DFA for M.Since M is minimal, M has no inaccessible states and is

irreducible (otherwise, we could make M’ smaller!)By the Claim, there is an isomorphism between M’ and

the DFA MMIN that is output by MINIMIZE(M). That is, MMIN is isomorphic to every minimal M’.

Claim: Let M be any DFA where L(M)=L(MMIN) andM has no inaccessible states and M is irreducible. Then there is an isomorphism between M and MMIN

Thm: MMIN is the unique minimal DFA equivalent to M

40

Page 40: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

Proof: We recursively construct a map from the states of MMIN to the states of M

Base Case: q0 MIN q0

Recursive Step: If p p

q

q

Then q q

41

Claim: Let M be any DFA where L(M)=L(MMIN) andM has no inaccessible states and M is irreducible. Then there is an isomorphism between M and MMIN

Thm: MMIN is the unique minimal DFA equivalent to M

Page 41: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

q q

Base Case: q0 MIN q0

Recursive Step: If p p

Then q q

42

Page 42: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

q q

Base Case: q0 MIN q0

Recursive Step: If p p

Then q q

The map is defined everywhere

Claim: Map is an isomorphism. Need to prove:

The map is well defined

The map is a bijection (one-to-one and onto)

The map preserves all transitions:If p p then MIN(p, ) ’(p’, )(this follows from the definition of the map!)

43

Page 43: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

The map is defined everywhere

That is, for all states q of MMIN

there is a state q of M such that q q

If q MMIN, there is a string w such that MMIN is in state q after reading in w

Let q be the state of M’ after reading in w. Claim: q q (proof by induction on |w|)

q q

Base Case: q0 MIN q0

Recursive Step: If p p

Then q q

44

Page 44: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

The map is onto: ∀q’ ∃ q such that q q

Want to show: For all states q of M there is a state q of MMIN such that q q

For every q in M’ there is a string w such thatM reaches state q after reading in w

Let q be the state of MMIN after reading in w.Claim: q q (proof by induction on |w|)

q q

Base Case: q0 MIN q0

Recursive Step: If p p

Then q q

45

Page 45: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

The map is well defined: ∀q ∃! q’ such that q q

Suppose there are states q and q such that q q and q q

We show that q and q are indistinguishable, so it must be that q = q (why?)

q q

Base Case: q0 MIN q0

Recursive Step: If p p

Then q q

46

Page 46: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

MMIN M

Suppose there are states q and q such that q q and q q

q

u

q0

q

v

q0

q

u

q0 MIN

q

v

q0 MIN

Assume for contradiction q and q are distinguishable

w

Acce

pt

w

Reje

ct

wR

eje

ct

w

Ac

ce

pt

Contradiction!

47

Page 47: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

MMIN M

Proof by contradiction. Suppose there are states p ¹ q such that p q and q q

q

u

q0

q

v

q0

p

u

q0 MIN

q

v

q0 MIN

If p q, then p and q are distinguishable

w

Acce

pt

w

Reje

ct

wR

eje

ct

w

Ac

ce

pt

Map is 1-to-1: ∀p ≠ q, p q and q q’ ⇒ q’ ≠ q’’

Contradiction!

48

Page 48: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

How can we prove that two regular expressions are equivalent?

49

Page 49: PowerPoint Presentationpeople.csail.mit.edu/rrw/6.045-2020/lec5-before-class.pdf · Fix M = (Q, Σ, , q 0, F) and let p, q Q Definition(s): State p is distinguishable from state q

50