Presentation is loading. Please wait.

Presentation is loading. Please wait.

DFA Minimization 1 2 Equivalent States Consider the accept states c and g. They are both sinks meaning that any string which ever reaches them is guaranteed.

Similar presentations


Presentation on theme: "DFA Minimization 1 2 Equivalent States Consider the accept states c and g. They are both sinks meaning that any string which ever reaches them is guaranteed."— Presentation transcript:

1

2 DFA Minimization 1

3 2 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 1 c g f 0 0 0 0 1 1

4 3 Equivalent States 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 cg f 0 00 0 1 1

5 4 Equivalent States 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 cg f 0 00 0 1 1

6 5 Equivalent States 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 e 1 cg bf 0 0 1

7 6 DFA Minimization: Algorithm Idea Equate & collapse states having same behavior. p q z z or p q z z Build equivalence relation on states: p  q  (  z  *,  (p,z)  F   (q,z)  F) I.e., iff for every string z, one of the following is true:

8 7 DFA Minimization: Algorithm Build table to compare each unordered pair of distinct states p,q. Each table entry has a “mark” as to whether p & q are known to be not equivalent, and a list of entries, recording dependences: “If this entry is later marked, also mark these.”

9 8 DFA Minimization: Algorithm 1. Initialize all entries as unmarked & with no dependences. 2. Mark all pairs of a final & nonfinal state. 3. For each unmarked pair p,q & input symbol a: 1. Let r=  (p,a), s=  (q,a). 2. If (r,s) unmarked, add (p,q) to (r,s)’s dependences, 3. Otherwise mark (p,q), and recursively mark all dependences of newly-marked entries. 4. Coalesce unmarked pairs of states. 5. Delete inaccessible states.

10 9 DFA Minimization: Example fbcdeag h g f e d c b a e b f c g d h 0 0 0 0 0 0 0 0 1 1 11 1 1 1 1 1.Initialize table entries: Unmarked, empty list

11 10 DFA Minimization: Example fbcdeag h g f e d c b 2.Mark pairs of final & nonfinal states a e b f c g d h 0 0 0 0 0 0 0 0 1 1 11 1 1 1 1

12 11 DFA Minimization: Example a e b f c g d h 0 0 0 0 0 0 0 0 1 1 11 1 1 1 1 3.For each unmarked pair & symbol, …  (b,0)  ?  (a,0)  (b,1)  ?  (a,1) fbcdeag h g f e d c b

13 12 DFA Minimization: Example g  ? b c  ? f fbcdeag h g f e d c b Maybe. No! a e b f c g d h 0 0 0 0 0 0 0 0 1 1 11 1 1 1 1 3.For each unmarked pair & symbol, …

14 13 fbcdeag h g f e d c b DFA Minimization: Example a e b f c g d h 0 0 0 0 0 0 0 0 1 1 11 1 1 1 1 3.For each unmarked pair & symbol, …

15 14 fbcdeag h g f e d c b DFA Minimization: Example a e b f c g d h 0 0 0 0 0 0 0 0 1 1 11 1 1 1 1  (e,0)  ?  (a,0)  (e,1)  ?  (a,1) 3.For each unmarked pair & symbol, …

16 15 fbcdeag h g f e d c b DFA Minimization: Example a e b f c g d h 0 0 0 0 0 0 0 0 1 1 11 1 1 1 1 h  ? b f  ? f Maybe. Yes. (a,e) 3.For each unmarked pair & symbol, …

17 16 DFA Minimization: Example fbcdeag h g f e d c b a e b f c g d h 0 0 0 0 0 0 0 0 1 1 11 1 1 1 1 (a,e) (g,a) 3.For each unmarked pair & symbol, …

18 17 DFA Minimization: Example fbcdeag h g f e d c b a e b f c g d h 0 0 0 0 0 0 0 0 1 1 11 1 1 1 1 (a,e) (g,a) Need to mark. So, mark (g,a) also. 3.For each unmarked pair & symbol, …

19 18 DFA Minimization: Example fbcdeag h g f e d c b a e b f c g d h 0 0 0 0 0 0 0 0 1 1 11 1 1 1 1 (g,a) (a,e) 3.For each unmarked pair & symbol, …

20 19 DFA Minimization: Example fbcdeag h g f e d c b a e b f c g d h 0 0 0 0 0 0 0 0 1 1 11 1 1 1 1 4.Coalesce unmarked pairs of states. (a,e) a  e b  h d  f aebh df c g 0 0 0 0 0 1 1 1 1 1

21 20 DFA Minimization: Example fbcdeag h g f e d c b a e b f c g d h 0 0 0 0 0 0 0 0 1 1 11 1 1 1 1 5.Delete unreachable states. (a,e) aebh df c g 0 0 0 0 0 1 1 1 1 1 None.

22 21 DFA Minimization: Notes Order of selecting state pairs was arbitrary. All orders give same ultimate result. But, may record more or fewer dependences. Choosing states by working backwards from known non-equivalent states produces fewest dependences. Can delete unreachable states initially, instead. This algorithm: O(n 2 ) time; Huffman (1954), Moore (1956). Constant work per entry: initial mark test & possibly later chasing of its dependences. More efficient algorithms exist, e.g., Hopcroft (1971).

23 22 What About NFA Minimization? This algorithm doesn’t find a unique minimal NFA. Is there a (not necessarily unique) minimal NFA? ? ? Of course.

24 23 NFA Minimization In general, minimal NFA not unique! Example NFAs for 0 + : Both minimal, but not isomorphic. 0 0 0 0

25 24 Minimizing DFA’s By Partitioning

26 25 Minimizing DFA’s Lots of methods All involve finding equivalent states: States that go to equivalent states under all inputs (sounds recursive) We will learn the Partitioning Method

27 26 Minimizing DFA’s by Partitioning Consider the following dfa Accepting states are yellow Non-accepting states are blue Are any states really the same?

28 27 S 2 and S 7 are really the same: Both Final states Both go to S6 under input b Both go to S3 under an a S 0 and S 5 really the same. Why? We say each pair is equivalent Are there any other equivalent states? We can merge equivalent states into 1 state

29 28 Partitioning Algorithm First Divide the set of states into Final and Non-final states Partition I Partition II a b S0S0 S1S1 S4S4 S1S1 S5S5 S2S2 S3S3 S3S3 S3S3 S4S4 S1S1 S4S4 S5S5 S1S1 S4S4 S6S6 S3S3 S7S7 *S 2 S3S3 S6S6 *S 7 S3S3 S6S6

30 29 Partitioning Algorithm Now See if states in each partition each go to the same partition S 1 & S 6 are different from the rest of the states in Partition I (but like each other) We will move them to their own partition a b S0S0 S 1 I S 4 I S1S1 S 5 I S 2 II S3S3 S 3 I S4S4 S 1 I S 4 I S5S5 S 1 I S 4 I S6S6 S 3 I S 7 II *S 2 S 3 I S 6 I *S 7 S 3 I S 6 I

31 30 Partitioning Algorithm a b S0S0 S1S1 S4S4 S5S5 S1S1 S4S4 S3S3 S3S3 S3S3 S4S4 S1S1 S4S4 S1S1 S5S5 S2S2 S6S6 S3S3 S7S7 *S 2 S3S3 S6S6 *S 7 S3S3 S6S6

32 31 Partitioning Algorithm Now again See if states in each partition each go to the same partition In Partition I, S 3 goes to a different partition from S 0, S 5 and S 4 We’ll move S3 to its own partition a b S0S0 S 1 III S 4 I S5S5 S 1 III S 4 I S3S3 S 3 I S4S4 S 1 III S 4 I S1S1 S 5 I S 2 II S6S6 S 3 I S 7 II *S 2 S 3 I S 6 III *S 7 S 3 I S 6 III

33 32 Partitioning Algorithm Note changes in S 6, S 2 and S 7 a b S0S0 S 1 III S 4 I S5S5 S 1 III S 4 I S4S4 S 1 III S 4 I S3S3 S 3 IV S1S1 S 5 I S 2 II S6S6 S 3 IV S 7 II *S 2 S 3 IV S 6 III *S 7 S 3 IV S 6 III

34 33 Partitioning Algorithm Now S 6 goes to a different partition on an a from S 1 S 6 gets its own partition. We now have 5 partitions Note changes in S 2 and S 7 a b S0S0 S 1 III S 4 I S5S5 S 1 III S 4 I S4S4 S 1 III S 4 I S3S3 S 3 IV S1S1 S 5 I S 2 II S6S6 S 3 IV S 7 II *S 2 S 3 IV S 6 V *S 7 S 3 IV S 6 V

35 34 Partitioning Algorithm All states within each of the 5 partitions are identical. We might as well call the states I, II III, IV and V. a b S0S0 S 1 III S 4 I S5S5 S 1 III S 4 I S4S4 S 1 III S 4 I S3S3 S 3 IV S1S1 S 5 I S 2 II S6S6 S 3 IV S 7 II *S 2 S 3 IV S 6 V *S 7 S 3 IV S 6 V

36 35 Partitioning Algorithm a b I III I *II IVV III I II IV V II Here they are:

37 36 V a a a a a b b b b b b


Download ppt "DFA Minimization 1 2 Equivalent States Consider the accept states c and g. They are both sinks meaning that any string which ever reaches them is guaranteed."

Similar presentations


Ads by Google