会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization...

62
Automata and Languages Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Transcript of 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization...

Page 1: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

Automata and Languages

Prof. Mohamed Hamada

Software Engineering Lab. The University of Aizu

Japan

Page 2: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

•  DFA Minimization •  Examples •  Minimization Algorithms

2

Today’s Topics

Page 3: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

3

DFA Minimization

Page 4: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

4

Strings over {a,b} with even number of a’s Σ*

Ea Oa Eb Ob Ob Eb

b

b

b

b

a a a a

[Oa,Ob]

[Ea,Eb] [Ea,Ob]

[Oa,Eb]

DFA Minimization Example

Page 5: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

5

Σ*

Ea Oa

a

a [Ea] [Oa]

b b

Strings over {a,b} with even number of a’s

DFA Minimization Example

Page 6: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

6

•  The states among the state sets {[Ea,Eb], [Ea,Ob]} and {[Oa,Eb], [Oa,Ob]} differ on aspect immaterial for the problem at hand.

•  Why not collapse these state sets into one state each, to get a smaller DFA?

DFA Minimization

Observation

Page 7: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

7

Equivalent or Indistinguishable States •  Recall that a DFA state summarizes the substring consumed so far

(that is, the past history). •  Two states qi and qj are equivalent or (indistinguishable), if, when

started in these states, every string causes the machine to either end up in a final state for both or end up in a non-accepting state for both.

DFA Minimization

Two states qi and qk are equivalent (or indistinguishable), if for all strings w ∈ Σ*

δ(qi, w) ∈ F ⇔ δ(qk, w) ∈ F

Two states qi and qk are distinguishable, if for some string w ∈ Σ*

δ(qi, w) ∈ F ⇔ δ(qk, w) ∉ F

Definition

Page 8: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

8

qi

qn qj

qm

If qm and qn are distinguishable, then so are qi and qj

a

a

DFA Minimization

Main Idea

Page 9: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

9

Example DFA

q0

q4

q1 q2 q3

q7 q5

q6

a

b

a a,b

a a,b

b

b

b

a

b (a u b)(a u b*)

a,b a

DFA Minimization Example

Page 10: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

10

Refinement of State Partitions •  { {q0,q7} , {q1,q2,q3,q4,q5,q6} } •  { {q0},{q7}, {q1,q2,q3,q4,q5,q6} }

•  On any transition

•  { {q0},{q7}, {q1,q2,q3,q4,q5,q6} } •  { {q0},{q7}, {q1,q4}, {q2,q3,q5,q6} }

•  On “a” transition

•  { {q0},{q7}, {q1,q4}, {q2,q5},{q3,q6} } •  On “b” transition

DFA Minimization Example

Page 11: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

11

Showing equivalent states in DFA

q0

q4

q1 q2 q3

q7 q5

q6

a

b

a a,b

a a,b

b

b

b

a

b (a u b)(a u b*)

a,b a

DFA Minimization Example

Page 12: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

12

Minimum DFA

q0

q1,q4 q7 q2,q5

q3,q6

a a,b

a a,b

b

a,b

b (a u b)(a u b*)

DFA Minimization Example

Page 13: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

13

Equivalent States Consider the accept states c and g. They are both

sinks meaning that any string which ever reaches them is guaranteed to be accepted later.

Q: Do we need both states?

a

b

1

d

0,1

e

0,1

1

c

0,1

g f

0

0

0

0 1

1

DFA Minimization Example

Page 14: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

14

A: No, they can be unified as illustrated below. Q: Can any other states be unified because any

subsequent string suffixes produce identical results?

a

b

1

d 0,1 e 1

0,1

cg

f

0

0 0

0 1

1

Joining Equivalent States

DFA Minimization Example

Page 15: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

15

A: Yes, b and f. Notice that if you’re in b or f then: 1.  if string ends, reject in both cases 2.  if next character is 0, forever accept in both cases 3.  if next character is 1, forever reject in both cases So unify b with f.

a

b

1

d 0,1 e 1

0,1

cg

f

0

0 0

0 1

1

Equivalent States

DFA Minimization Example

Page 16: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

16

Intuitively two states are equivalent if all subsequent behavior from those states is the same.

Q: Come up with a formal characterization of state equivalence.

a

0,1

d 0,1 e 1

0,1

cg

bf

0

0 1

Joining Equivalent States

DFA Minimization Example

Page 17: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

17

Finishing the Example Q: Any other ways to simplify the

automaton?

a

0,1

d 0,1 e 1

0,1

cg

bf

0

0 1

DFA Minimization Example

Page 18: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

18

A: Get rid of d. Getting rid of unreachable useless states

doesn’t affect the accepted language.

a

0,1

0,1 e

0,1

cg

bf 0

1

DFA Minimization

Useless States

Page 19: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

19

An automaton is irreducible if –  it contains no useless states, and –  no two distinct states are equivalent.

DFA Minimization

Definition

• The goal of minimization algorithm is to create irreducible automata from arbitrary ones. • The algorithm actually produces smallest possible DFA for the given language, hence the name “minimization”.

Goals of the Minimization Algorithm

Page 20: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

20

DFA minimize(DFA (Q, Σ, δ, q0, F ) ) remove any state q unreachable from q0 Partition P = {F, Q - F } boolean Consistent = false while( Consistent == false ) Consistent = true for(every Set S ∈ P, symbol a ∈ Σ, Set T ∈ P ) Set temp = {q ∈T | δ(q,a) ∈S } if (temp != Ø && temp != T ) Consistent = false P = (P -T )∪{temp,T-temp} return defineMinimizor( (Q, Σ, δ, q0, F ), P )

DFA Minimization

Minimization Algorithm First Part: Partition

Page 21: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

21

DFA defineMinimizor (DFA (Q, Σ, δ, q0, F ), Partition P ) Set Q’ =P State q’0 = the set in P which contains q0 F’ = { S ∈ P | S ⊆ F } for (each S ∈ P, a ∈ Σ)

define δ’ (S,a) = the set T ∈ P which contains the states δ’(S,a)

return (Q’, Σ, δ’, q’0, F’ )

DFA Minimization

Minimization Algorithm

Second Part: Minimization

Page 22: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

22

Minimization Example

Start with a DFA

DFA Minimization

Example

Page 23: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

23

Miniature version à

Minimization Example

DFA Minimization

Example

Page 24: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

24

Split into two parts. ACCEPT vs. REJECT

Minimization Example

DFA Minimization

Example

Page 25: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

25

0-label doesn’t split

Minimization Example

DFA Minimization

Example

Page 26: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

26

1-label splits up REJECT's

Minimization Example

DFA Minimization

Example

Page 27: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

27

No further splits. HALT!

Minimization Example

DFA Minimization

Example

Page 28: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

28

End Result

States of the minimal automata are remaining.

DFA Minimization

Example

Page 29: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

29

Minimization Example. Compare

100100101

DFA Minimization

Example

Page 30: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

30

100100101

Minimization Example. Compare DFA Minimization

Example

Page 31: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

31

100100101

Minimization Example. Compare DFA Minimization

Example

Page 32: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

32

100100101

Minimization Example. Compare DFA Minimization

Example

Page 33: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

33

100100101

Minimization Example. Compare DFA Minimization

Example

Page 34: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

34

100100101

Minimization Example. Compare DFA Minimization

Example

Page 35: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

35

100100101

Minimization Example. Compare DFA Minimization

Example

Page 36: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

36

100100101

Minimization Example. Compare DFA Minimization

Example

Page 37: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

37

100100101

Minimization Example. Compare DFA Minimization

Example

Page 38: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

38

100100101 ACCEPTED.

Minimization Example. Compare DFA Minimization

Example

Page 39: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

39

10000

Minimization Example. Compare DFA Minimization

Example

Page 40: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

40

10000

Minimization Example. Compare DFA Minimization

Example

Page 41: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

41

10000

Minimization Example. Compare DFA Minimization

Example

Page 42: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

42

10000

Minimization Example. Compare DFA Minimization

Example

Page 43: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

43

10000

Minimization Example. Compare DFA Minimization

Example

Page 44: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

44

10000 REJECT.

Minimization Example. Compare DFA Minimization

Example

Page 45: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

45

Previous algorithm guaranteed to produce an irreducible FA. Why should that FA be the smallest possible FA for its accepted language?

DFA Minimization

Minimal Automaton

Page 46: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

46

Minimal Automaton (Myhill-Nerode): The minimization

algorithm produces the smallest possible automaton for its accepted language.

Show that any irreducible automaton is

the smallest for its accepted language L: We say that two strings u,v ∈ Σ* are

indistinguishable if for all suffixes x, ux is in L exactly when vx is.

Notice that if u and v are distinguishable, the path from their paths from the start state must have different endpoints.

DFA Minimization

Theorem

Proof

Page 47: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

47

Consequently, the number of states in any DFA for L must be as great as the number of mutually distinguishable strings for L.

But an irreducible DFA has the property that every state gives rise to another mutually distinguishable string!

Therefore, any other DFA must have at least as many states as the irreducible DFA

DFA Minimization

Proof (cont.)

Page 48: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

48

IS THIS DFA MINIMAL?

1 1 1

1

0

0

0 0

NO

DFA Minimization

Quiz 1

Page 49: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

49

IS

0

1

0 1

DFA Minimization

Quiz 1 ITS MINIMAL DFA

Page 50: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

50

0 0,1

0 0

1

1

1

q0

q1

q2

q3

IS THIS DFA MINIMAL?

DFA Minimization

Quiz 2

Page 51: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

51

For a DFA M = (Q, Σ, δ, q0, F), let p, q, r ∈ Q

Definition: p ~ q iff p is indistinguishable (equivalent) 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)

DFA Minimization Definition

Page 52: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

52

Definition: 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)

DFA Minimization

For a DFA M = (Q, Σ, δ, q0, F), let p, q, r ∈ Q

Definition

Page 53: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

53

Because ~ is an equivalence relation

so ~ partitions the set of states of M into disjoint equivalence classes

q0

Q q

[q] = { p | p ~ q }

DFA Minimization Definition

Page 54: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

54

1 1 1

1

0

0

0 0

DFA Minimization Example

Page 55: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

55

Algorithm MINIMIZE Input: DFA M Output: DFA MMIN such that:

M ≡ MMIN

MMIN has no inaccessible states

MMIN is irreducible

states of MMIN are pairwise distinguishable ||

Theorem: MMIN is the unique minimum

DFA Minimization

Idea: States of MMIN will be blocks of equivalent states of M

Algorithm

Page 56: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

56

TABLE-FILLING ALGORITHM Input: DFA M = (Q, Σ, δ, q0, F) Output:

(2) EM = { [q] | q ∈ Q } (1) DM = { (p,q) | p,q ∈ Q and p ~ q } /

q0 q1

qi

qn q0 q1 qi qn

Base Case: p accepts and q rejects ⇒ p ~ q /

Recursion:

d

d d d

d

d d p pʹ

q qʹ ~ /

a

a ⇒ p ~ q /

DFA Minimization Algorithm

Page 57: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

57

1 0

1

0 1

0,1

0

q0

q0 q1

q1

q2

q2 q3

q3

q0 q1 q2 q3

d d d

d

d

d

DFA Minimization Example

Page 58: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

58

1 1 1

1

0

0

0 0

q0

q0 q1

q1

q2

q2 q3

q3 d d

d

d

q0 q1

q2 q3

DFA Minimization

Example

Page 59: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

59

Algorithm MINIMIZE Input: DFA M Output: DFA MMIN (1) Remove all inaccessible states from M

(2) Apply Table-Filling algorithm to get EM = { [q] | q is an accessible state of M }

MMIN = (QMIN, Σ, δMIN, q0 MIN, FMIN)

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

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

DFA Minimization Algorithm

Page 60: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

60

1

0

1 0 1 0 1

0

1

0 q0 q1

q2

MINIMIZE DFA Minimization Example

Page 61: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

61

1 0

1

0

1

0,1

0

q0 q1

q3

q5

q4

0

1

q2

0,1

Exercise

DFA Minimization

Minimize the following DFA?

Page 62: 会津大学 - Automata and Languageshamada/AF/L07-FA.pdf · 2016. 4. 22. · DFA Minimization Definition •The goal of minimization algorithm is to create irreducible automata from

62

1 0

1

0

1

0,1

0

q0 q1

q3

q5

q4

0

1

q2

0,1 q0

q0 q1

q1

q3

q3 q4

q4

d

d d

d

d d q5

q5

d

d

d

Example