420 likes | 432 Views
Learn how to minimize DFAs by merging indistinguishable states to enhance efficiency. Explore the DFA minimization algorithm through two detailed examples and understand the concept of Context-Free Grammar (CFG).
E N D
CSCI 3130: Formal Languages and Automata TheoryTutorial 4 Hung Chun Ho Office: SHB 1026 Department of Computer Science & Engineering
Agenda • DFA Minimization • Algorithm • 2 Examples • Context Free Grammar (CFG) • Design • Parse Tree
DFA Minimization 0 1 1 1 1 q0 q1 q2 q3 0 0 0 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 will show how to turn any DFAfor L into the minimal DFAfor L
DFA Minimization • After we design a DFA, there may be some redundant states • They have the same “function” • They are indistinguishable • No matter what happens in the future (inputs), they will have the same fate (accept/reject)! • We can merge indistinguishable states into groups
0 0 q00 q0 1 0 1 q01 0 qe 1 0 q10 0 1 1 q1 0 1 q11 1 Indistinguishable States • DFA example from lecture note • Indistinguishable states example: {q0, q00} • Explain: next few slides
0 0 q00 q0 1 0 1 q01 0 qe 1 0 q10 0 1 1 q1 0 1 q11 1 Indistinguishable States • On continuation string (further input) “0” • q0 Reject • q00 Reject
0 0 q00 q0 1 0 1 q01 0 qe 1 0 q10 0 1 1 q1 0 1 q11 1 Indistinguishable States • On continuation string “11” • q0 Accept • q00 Accept
0 0 q00 q0 1 0 1 q01 0 qe 1 0 q10 0 1 1 q1 0 1 q11 1 Indistinguishable States • On continuation string “01011” • q0 Accept • q00 Accept
0 0 q00 q0 1 0 1 q01 0 qe 1 0 q10 0 1 1 q1 0 1 q11 1 Indistinguishable States • On all continuation strings, {q0 , q00} have the same outcome • q0 , q00 are indistinguishable Can be merged
How to find? • Infinitely many continuation states • A bit difficult to find indistinguishable states directly • We do the reverse: • To recognize distinguishable states • Pair of states, s.t. there is a continuation string, causing them have different “fates” • (De Morgan’s Law, still remember?)
0 0 q00 q0 1 0 1 q01 0 qe 1 0 q10 0 1 1 q1 0 1 q11 1 Distinguishable States • For continuation string = “1” • q01 Accept • q10 Reject • Distinguishable
0 0 q00 q0 1 0 1 q01 0 qe 1 0 q10 0 1 1 q1 0 1 q11 1 Distinguishable States • For continuation string = “e” • q10 Reject • q11 Accept • Distinguishable
Distinguishable States • 2 states are distinguishable, if there is a continuation string, that bring them different fate!! (Accept / Reject) • Try all continuation strings? How? • Idea: • For any pair of {Accept, Reject} states, they are distinguishable • For these pairs, walk k transitions backward, mark the obtained pairs as distinguishable
Finding (in)distinguishable states Repeat !! as long as you mark some pairs 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 1 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 1 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 1 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 1 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 1 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 1 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 Unmarked Cells Pairs of Indistinguishable states Blue line links indistinguishable states
Example 1 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 1 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 2 0 q0 0 q00 q1 q0 1 0 1 q00 q01 0 qe 1 0 q01 q10 0 1 1 x x x x x q10 q1 0 1 q11 q11 qe q0 q1 q00 q01 q10 1 q10 is distinguishable from all other states
Example 2 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 x x x q10 q1 0 1 q11 q11 x x x x qe q0 q1 q00 q01 q10 1 After going thru the whole table several times (See white board for simulation)
Example 2 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 x x x q10 q1 0 1 q11 q11 x x x x qe q0 q1 q00 q01 q10 1 Merge unmarked pairs into groups
Why it works? • For a pair of states, there is a continuation string that bring them different fates • Distinguishable • Idea: For any pair of {Accept, Reject} states, they are distinguishable, mark them • These pairs have a length-0 continuation string that distinguishes them • 1 step backward, mark them as distinguishable • These pairs have a length-1 continuation string that distinguishes them
Why it works? • 1 further step backward, mark • These pairs have a length-2 continuation string that distinguishes them • 1 further step backward, mark • These pairs have a length-3 continuation string that distinguishes them • So-on …
Another Terminal 4) Start Variable 3) Production Rule Another Production Rule 1) Variables 2) Terminal Context-Free Grammar (Recap) • A context free grammar is consisted of S AB | ba A aA | a B b
= Apply Production Rule CFG Example S AB | ba A aA | a B b S • AB • aAB • aaB • aab Context-Free Grammar (Recap) • A string is said to belong to the language (of the CFG) if it can be derived from the start variable Derivation Therefore, aab belongs to the language
S 0S1 S 01 Why CFG? • L = {w = 0n1n : n is an positive integer} • L is not a regular language • Proved by “Pumping Lemma” • A Context-Free Grammar can describe it • Thus, CFG is more general than regular expression • Recall: NFA Regular Expression DFA
CFG Design • Given a context-free language, design the CFG • L = { ab-string, w : Number of a’s < Number of b’s } • Some time for you to get into think… 1 min S ? …
CFG Design (Con’t) • Idea: Bottom-up • Shortest string in L : “b” • Given a string in L, we can expand it, s.t. it is still in L • i.e., Add terminals, while not violating the constraints
After adding 1 “b”, number of “b” is still greater than that of “a” Adding 1 “a” and 1 “b”, the difference between the numbers of “a” and “b” keep constant CFG Design (Con’t) Good Try: S b S bS | Sb S abS | baS | bSa | aSb However, cannot parse strings like “aabbbaa” Explaination makes sense.But there is a counter example.
Base Case #b still > #a 1st S 2nd S ‘a’ : #b ≥ #a + 1 : #b ≥ #a + 1 : #a = 1 SUM #b ≥ #a + 2 - 1 CFG Design (Con’t) Consider the answer: S b S SS S SaS | aSS | SSa Explaination makes sense.But, is the grammar is correct?
A set of strings CFG Design (Con’t) • After designing the grammar, G, you may have to prove (if required) that the language of this grammar is equivalent to the given language • i.e., Prove that L(G) = L • Proof: Part 1) L(G) ⊂ L Part 2) L ⊂ L(G) • Due to time limit, I will not do this part
Derivation S • AB • aAB • aaB • aab Parse Tree • How to parse “aab” in this grammar? (Previous example) CFG Example S AB | ba A aA | a B b
S A B b a A a Parse Tree (Con’t) • Idea: Production Rule = Node + Children • Should be very intuitive to understand Derivation S • AB • aAB • aaB • aab
S S S - S S - S - 2 3 S - A S S 3 1 1 2 3 – (1 – 2) S S S - S 1 | 2 | 3 Different parse trees!The grammar is ambiguous! Parse Tree (Con’t) • Ambiguity: String: 3 - 1 - 2 CFG: 3 – 1 – 2
Parse Tree (Con’t) • Useful in programming language • CSC3180 • Useful in compiler • CSC3120
End • Thanks for coming! • Any questions?