310 likes | 317 Views
Fall 2011. The Chinese University of Hong Kong. CSCI 3130: Automata theory and formal languages. DFA minimization. Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130. Example. L = { w : w ends in 111 }. Isn ’ t there a smaller one?. Smaller DFA.
E N D
Fall 2011 The Chinese University of Hong Kong CSCI 3130: Automata theory and formal languages DFA minimization Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130
Example L = {w: w ends in 111} Isn’t there a smaller one?
Smaller DFA L = {w: w ends in 111} 0 1 1 1 1 q0 q1 q2 q3 0 0 0 Can we do it with 3 states?
Even smaller DFA? • Suppose we had a 3 state DFA for L • There are 4 inputs but 3 statesOn two of these inputs M ends in the same state L = {w: w ends in 111} M inputs: e, 1, 11, 111
Pigeonhole principle Suppose you are tossing 4 balls into 3 bins. Then two balls end up in the same bin. Take 4 inputs and feed them into a 3 state DFA. Then two inputs end up in the same state.
A smaller DFA • Suppose inputs x = 1, y = 11 lead to same state • Then after reading one more1 • The state of x1 = 11 should be rejecting • The state of y1 = 111 should be accepting L = {w: w ends in 111} 1 1, 11 M 11, 111 inputs: e, 1, 11, 111 “ends in 111” ✘
A smaller DFA • Suppose inputs x = e, y = 1 lead to same state • Then after reading 11 • The state of x1 = 11 should be rejecting • The state of y1 = 111 should be accepting L = {w: w ends in 111} 1 e, 1 M 11, 111 inputs: e, 1, 11, 111 ✘
0 1 1 1 1 q0 q1 q2 q3 0 0 0 No smaller DFA! • After looking at all possible pairs (x, y)we conclude that • So, this DFA is minimal (e, 1) (e, 11) (e, 111) (1, 11) (1, 111) (11, 111) There is no DFA with 3 states for L
0 1 1 1 1 q0 q1 q2 q3 0 0 0 DFA minimization 0 0 q000 1 0 q00 1 q001 q0 1 … 0 q01 … qe 1 q101 q10 0 … 1 q1 … 1 q11 1 q111 1 We now show how to turn any DFA for L into the minimal DFA for L
Minimal DFAs and distinguishable states • First, we have to understandminimal DFAs: reject accept 0 1 1 1 1 q0 q1 q2 q3 0 0 0 every pair of states is distinguishable minimal DFA
Distinguishable states • Two states q and q’ are distinguishable if accept w1 w2 wk-1 wk … q reject w1 w2 wk-1 wk … q’ on the same continuation string w1w2...wk, one accepts, but the other rejects
Examples of distinguishable states 1 0 1 1 0 1 q0 q1 q2 q3 0 0 (q0, q1) distinguishable by 01 (q0, q2) distinguishable by 1 (q0, q3) distinguishable by e DFA is minimal (q1, q2) distinguishable by 1 (q1, q3) distinguishable by e (q2, q3) distinguishable by e
q2 q2 0 0 q0 0 0, 1 0, 1 q01 1 1 q1 1 q3 q3 0, 1 0, 1 Examples of distinguishable states (q0, q3) distinguishable by e (q1, q3) distinguishable by e indistinguishable pairs can be merged (q2, q3) distinguishable by e (q1, q2) distinguishable by 0 (q0, q2) distinguishable by 0 (q0, q1) indistinguishable
Examples of distinguishable states 0 q2 q0 0, 1 0, 1 q23 q01 1 0, 1 0, 1 q3 q1 0, 1 (q0, q2) distinguishable by e (q1, q2) distinguishable by e (q0, q3) distinguishable by e (q1, q3) distinguishable by e (q0, q1) indistinguishable (q2, q3) indistinguishable
Finding (in)distinguishable states If q is accepting and q’ is rejectingMark(q, q’) as distinguishable (x) Rule 1: x q’ q x q1 q1’ If (q1, q1’) are marked,Mark(q2, q2’) as distinguishable (x) a a Rule 2: x q2 q2’ Unmarked pairs are indistinguishable Merge them into groups Rule 3:
Example of DFA minimization 0 q0 0 q00 q1 q0 1 0 1 q00 q01 0 qe 1 0 q01 q10 0 1 1 q10 q1 0 1 q11 q11 qe q0 q1 q00 q01 q10 1
Example of DFA minimization 0 q0 0 q00 q1 q0 1 0 1 q00 q01 0 qe 1 0 q01 q10 0 1 1 q10 q1 0 1 q11 q11 x x x x x x qe q0 q1 q00 q01 q10 1 q11 is distinguishable from all other states
Example of DFA minimization 0 q0 0 q00 0 q1 1 q0 1 0 1 q00 q01 0 qe 1 0 q01 q10 0 1 1 q10 q1 0 1 q11 q11 x x x x x x qe q0 q1 q00 q01 q10 1 Look at pair qe, q0 Neither (q0, q00) nor (q1, q01) are distinguishable
Example of DFA minimization 0 q0 0 q00 x q1 q0 1 0 1 q00 q01 0 qe 1 0 1 0 q01 q10 0 1 1 q10 q1 0 1 q11 q11 x x x x x x qe q0 q1 q00 q01 q10 1 Look at pair qe, q1 (q1, q11) is distinguishable
Example of DFA minimization 0 q0 0 q00 q1 x x q0 1 0 1 q00 x q01 0 qe 1 0 x x x q01 q10 0 1 1 x x q10 q1 0 1 q11 q11 x x x x x x qe q0 q1 q00 q01 q10 1 After going thru the whole table once Now we make another pass
Example of DFA minimization 0 q0 0 q00 0 q1 x x 1 q0 1 0 1 q00 x q01 0 qe 1 0 x x x q01 q10 0 1 1 x x q10 q1 0 1 q11 q11 x x x x x x qe q0 q1 q00 q01 q10 1 Look at pair qe, q0 Neither (q1, q00) nor (q1, q01) are distinguishable
Example of DFA minimization 0 q0 0 q00 q1 x x q0 1 0 0 1 q00 x q01 0 1 qe 1 0 x x x q01 q10 0 1 1 x x q10 q1 0 1 q11 q11 x x x x x x qe q0 q1 q00 q01 q10 1 Look at pair qe, q00 Neither (q0, q00) nor (q1, q01) are distinguishable
Example of DFA minimization 0 q0 0 q00 q1 x x q0 1 0 1 q00 x q01 0 qe 1 0 x x x q01 q10 0 1 1 x x q10 q1 0 1 q11 q11 x x x x x x qe q0 q1 q00 q01 q10 1 In the second pass, nothing changes So we are ready to apply Rule 3
Example of DFA minimization q0 q00 q1 x x q0 q00 x q01 qe x x x q01 q10 x x q10 q1 q11 q11 x x x x x x qe q0 q1 q00 q01 q10 Merge unmarked pairs into groups
Example of DFA minimization q0 A q00 q1 x x q0 A q00 A A x q01 qe x x B x q01 q10 A A x A x q10 q1 B q11 q11 x x x x x x C qe q0 q1 q00 q01 q10 Merge unmarked pairs into groups
Example of DFA minimization 0 q0 A 0 q00 q1 x x q0 1 A 0 1 q00 A A x q01 0 qe 1 0 x x B x q01 q10 0 1 1 A A x A x q10 q1 0 1 B q11 q11 x x x x x x C qe q0 q1 q00 q01 q10 1 0 1 1 minimized DFA: qA qB qC 1 0 0
Example of DFA minimization How do we know this DFA is minimal? qB 1 0 Answer: All pairs are distinguishable qC e e 1 1 qA qB qC 1 qA qB 0 0
Why it works • Why do we end up finding all distinguishable pairs? w1 w2 wk-1 wk … q x x x x w1 w2 wk-1 wk … q’ Because we work backwards
Why it works • Why are there no inconsistencies when we merge? B q5 w a A q2 q3 q1 q7 a C w q6 Because we only merge indistinguishable states
Why it works • Why is there no smaller DFA? Suppose there is By the pigeonhole principle this must happen: M smaller DFA M’ v q q” v, v’ v’ q’
Why it works • Why is there no smaller DFA? But then M smaller DFA M’ v w q q” w ? v, v’ v’ q’ w Every pair of states is distinguishable q” cannot exist!