CS54 Non-Regular Languages, Minimizing FAs
CS54 Homework is due! Homework 2 will appear this afternoon 2
The Pumping Lemma: Structure in Regular Languages Let L be a regular language Then there is a positive integer P s.t. for all strings w L with w P there is a way to write w = xyz, where:. y > (that is, y ε) 2. xy P 3. For all i, xy i z L Why is it called the pumping lemma? The word w gets pumped into longer and longer strings 3
Proof: Let M be a FA that recognizes L Let P be the number of states in M Let w be a string where w L and w P We show: w = xyz y. y > 2. xy P 3. xy i z L for all i x z q q j q k q w Claim: There must exist j and k such that j < k P, and q j = q k 4
Applying the Pumping Lemma Let s prove that EQ = { w w has equal number of s and s} is not regular. By contradiction. Assume EQ is regular. Let P be as in pumping lemma. Let w = P P ; note w EQ. If EQ is regular, then there is a way to write w as w = xyz, y >, xy P, and for all i, xy i z is also in EQ Claim: The string y must be all zeroes. Why? Because xy P and w = xyz = P P But then xyyz has more s than s Contradiction!
Applying the Pumping Lemma Let s prove that SQ = { n2 n } is not regular Assume SQ is regular. Let w = P2 If SQ is regular, then we can write w = xyz, y >, xy P, and for any i, xy i z is also in SQ So xyyz SQ. Note that xyyz = P2 + y Note that < y P So xyyz = P 2 + y P 2 + P < P 2 + 2P + = (P+) 2 and P 2 < xyyz < (P+) 2 Therefore xyyz is not a perfect square! Hence P2 + y = xyyz SQ, so our assumption must be false. That is, SQ is not regular!
oes this FA have a minimal number of states? NO 8
9
Is this minimal? How can we tell in general?
Theorem: For every regular language L, there is a unique (up to re-labeling of the states) minimal-state FA M* such that L = L(M*). Furthermore, there is an efficient algorithm which, given any FA M, will output this unique M*. If this were true for more general models of computation, that would be an engineering breakthrough!!
Note: There isn t a uniquely minimal NFA 2
Extending transition function δ to strings Given FA M = (Q, Σ, δ, q, F), we extend δ to a function : Q Σ* Q as follows: (q, ε) = (q, σ) = q δ(q, σ) (q, σ σ k+ ) = δ( (q, σ σ k ), σ k+ ) (q, w) = the state of M reached after reading in w, starting from state q Note: (q, w) F M accepts w ef. w Σ* distinguishes states q and q 2 iff (q, w) F (q 2, w) F 3
Extending transition function δ to strings Given FA M = (Q, Σ, δ, q, F), we extend δ to a function : Q Σ* Q as follows: (q, ε) = (q, σ) = q δ(q, σ) (q, σ σ k+ ) = δ( (q, σ σ k ), σ k+ ) (q, w) = the state of M reached after reading in w, starting from state q Note: (q, w) F M accepts w ef. w Σ* Σ* distinguishes states q and q 22 iff iff exactly oneof of (q,, w), (q 22,, w) is is a final state 4
istinguishing two states ef. w Σ* distinguishes states q and q 2 iff exactly one of (q, w), (q 2, w) is a final state I m in q or q 2, but which? How can I tell? 5
istinguishing two states ef. w Σ* distinguishes states q and q 2 iff exactly one of (q, w), (q 2, w) is a final state Here read this I m in q or q 2, but which? How can Ok, I I m tell? accepting! Must have been q W 6
Fix M = (Q, Σ, δ, q, F) and let p, q Q efinition: State p is distinguishable from state q iff there is w Σ* that distinguishes p and q iff there is w Σ* so that exactly one of (p, w), (q, w) is a final state State p is indistinguishable from state q iff p is not distinguishable from q iff for all w Σ*, (p, w) F (q, w) F Pairs of indistinguishable states are redundant 7
q, q Which pairs of states are distinguishable here? q 3 q 2 ε distinguishes all final states from non-final states 8
q, q q 2 Which pairs of states are distinguishable here? q 3 The string distinguishes q and q 3 9
q, q q 2 Which pairs of states are distinguishable here? q 3 The string distinguishes q and q 2 2
Fix M = (Q, Σ, δ, q, F) and let p, q, r Q efine 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? 2
Fix M = (Q, Σ, δ, q, F) and let p, q, r Q Proposition: is an equivalence relation Therefore, the relation partitions Q into disjoint equivalence classes Q q [q] := { p p q } q 22
23
Algorithm: MINIMIZE-FA Input: FA M Output: FA M MIN such that: L(M) = L(M MIN ) M MIN has no inaccessible states M MIN is irreducible For all states p q of M MIN, p and q are distinguishable Theorem: M MIN is the unique minimal FA that is equivalent to M 24
Intuition: The states of M MIN will be the equivalence classes of states of M We ll uncover these equivalent states with a dynamic programming algorithm 25
The Table-Filling Algorithm Input: FA M = (Q, Σ, δ, q, F) Output: () M = { (p, q) p, q Q and p q} (2) EQUIV M = { [q] q Q } High-Level Idea: 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 26
The Table-Filling Algorithm Input: FA M = (Q, Σ, δ, q, F) Output: () M = { (p, q) p, q Q and p q} (2) EQUIV M = { [q] q Q } q q Base Case: For all (p, q) such that p accepts and q rejects p q q i q n q q q i q n 27
q q q i q n q q q i q n The Table-Filling Algorithm Input: FA M = (Q, Σ, δ, q, F) Output: () M = { (p, q) p, q Q and p q} (2) EQUIV M = { [q] q Q } Base Case: For all (p, q) such that p accepts and q rejects p q Iterate: If there are states p, q and symbol σ Σ satisfying: δ (p, σ) = p mark p q δ (q, σ) = q Repeat until no more s can be added 28
q q q 2 q 3 q q q 2 q 3, q q q 2 q 3 29
q q q q q 3 q 2 q 2 q 3 q q q 2 q 3 3
Claim: If (p, q) is marked by the Table-Filling algorithm, then p q Proof: By induction on the number of steps in the algorithm before (p,q) is marked If (p, q) is marked at the start, then one state s in F and the other isn t, so ε distinguishes p and q Suppose (p, q) is marked at a later point. Then there are states p, q such that:. (p, q ) are marked p q (by induction) So there s a string w s.t. (p, w) F (q, w) F 2. p = δ(p,σ) and q = δ(q,σ), where σ Σ The string σw distinguishes p and q! 3
Claim: If (p, q) is not marked by the Table-Filling algorithm, then p q Proof (by contradiction): Suppose the pair (p, q) is not marked by the algorithm, yet p q (call this a bad pair ) Then there is a string w such that w > and: (p, w) F and (q, w) F (Why is w >?) Of all such bad pairs, let p, q be a pair with the shortest distinguishing string w 32
Claim: If (p, q) is not marked by the Table-Filling algorithm, then p q Proof (by contradiction): Suppose the pair (p, q) is not marked by the algorithm, yet p q (call this a bad pair ) Of all such bad pairs, let p, q be a pair with the shortest distinguishing string w (p, w) F and (q, w) F We have w = σw, for some string w and some σ Σ Let p = δ(p,σ) and q = δ(q,σ) (Why is w >?) Then (p, q ) is also a bad pair, but with a SHORTER distinguishing string, w! 33
Algorithm MINIMIZE Input: FA M Output: Equivalent minimal-state FA M MIN. Remove all inaccessible states from M 2. Run Table-Filling algorithm on M to get: EQUIV M = { [q] q is an accessible state of M } 3. efine: M MIN = (Q MIN, Σ, δ MIN, q MIN, F MIN ) Q MIN = EQUIV M, q MIN = [q ], F MIN = { [q] q F } δ MIN ( [q], σ ) = [ δ( q, σ ) ] Claim: L(M MIN ) = L(M) 34
q q q 4, q, q 5 q q 2 q 3 q 3 q 4 q 5 q q q 3 q 4 q 5 36
q q q 4, q q 5 q q 3 q 3 q 4 q 5 q q q 3 q 4 q 5 37
Thm: M MIN is the unique minimal FA equivalent to M Claim: Suppose for a FA M, L(M )=L(M MIN ) and M has no inaccessible states and M is irreducible. Then there is an isomorphism between M and M MIN If M is a minimal FA, then M has no inaccessible states and is irreducible. So the Claim implies: If M is a minimal FA for M, then there is an isomorphism between M and M MIN. So the Thmholds! Corollary: If M has no inaccessible states and is irreducible, then M is minimal. Proof: Let M min be minimal for M. Then L(M) = L(M min ), no inaccessible states in M, and M is irreducible. By Claim, both M min and M are isomorphic to M MIN! 38