Stte Minimiztion for DFAs Red K & S 2.7 Do Homework 10. Consider: Stte Minimiztion 4 5 Is this miniml mchine? Step (1): Get rid of unrechle sttes. Stte Minimiztion 6, Stte is unrechle. Step (2): Get rid of redundnt sttes. Sttes 2 nd re redundnt. Getting Rid of Unrechle Sttes We cn't esily find the unrechle sttes directly. But we cn find the rechle ones nd determine the unrechle ones from there. An lgorithm for finding the rechle sttes:, Lecture Notes 10 Stte Minimiztion 1
Getting Rid of Redundnt Sttes Intuitively, two sttes re equivlent to ech other (nd thus one is redundnt) if ll strings in Σ* hve the sme fte, regrdless of which of the two sttes the mchine is in. But how cn we tell this? The simple cse:,, Two sttes hve identicl sets of trnsitions out. The hrder cse: Getting Rid of Redundnt Sttes The outcomes re the sme, even though the sttes ren't. Finding n Algorithm for Minimiztion Cpture the notion of equivlence clsses of strings with respect to lnguge. Cpture the (weker) notion of equivlence clsses of strings with respect to lnguge nd prticulr FSA. Prove tht we cn lwys find deterministic FSA with numer of sttes equl to the numer of equivlence clsses of strings. Descrie n lgorithm for finding tht deterministic FSA. Defining Equivlence for Strings We wnt to cpture the notion tht two strings re equivlent with respect to lnguge L if, no mtter wht is tcked on to them on the right, either they will oth e in L or neither will. Why is this the right notion? Becuse it corresponds nturlly to wht the sttes of recognizing FSM hve to rememer. Exmple: (1) (2) Suppose L = {w {,}* : w is even}. Are (1) nd (2) equivlent? Suppose L = {w {,}* : every is immeditely followed y }. Are (1) nd (2) equivlent? Lecture Notes 10 Stte Minimiztion 2
Defining Equivlence for Strings If two strings re equivlent with respect to L, we write x L y. Formlly, x L y if, z Σ*, xz L iff yz L. Notice tht L is n equivlence reltion. Exmple: Σ = {, } L = {w Σ* : every is immeditely followed y } The equivlence clsses of L : L is the numer of equivlence clsses of L. Σ = {, } Another Exmple of L The equivlence clsses of L : Σ = {, } L = * The equivlence clsses of L : Yet Another Exmple of L An Exmple of L Where All Elements of L Are Not in the Sme Equivlence Clss Σ = {, } L = {w {, }* : no two djcent chrcters re the sme} The equivlence clsses of L : Lecture Notes 10 Stte Minimiztion
Σ = {, } L = n n The equivlence clsses of L : Is L Alwys Finite? L is n idel reltion. Bringing FSMs into the Picture Wht if we now consider wht hppens to strings when they re eing processed y rel FSM? Σ = {, },, Define ~ M to relte pirs of strings tht drive M from s to the sme stte. Formlly, if M is deterministic FSM, then x ~ M y if there is some stte q in M such tht (s, x) - * M (q, ) nd (s, y) - * M (q, ). Notice tht M is n equivlence reltion. An Exmple of ~M Σ = {, },, The equivlence clsses of ~ M : ~ M = Lecture Notes 10 Stte Minimiztion 4
Another Exmple of ~M Σ = {, },, The equivlence clsses of ~ M : ~ M = L: [,,,, ] w is even [,,,,,, ] w is odd ~ M, stte mchine: q1: [,,,, ] w is even q2: [,,, ] ( )* q: [,, ] ( )* ~ M, 2 stte mchine: q1: [,,,, ] w is even q2: [,,,,,, ] w is odd The Reltionship Between L nd ~ M ~ M is refinement of L. The Refinement L [even length] [odd length] (S) ~ M [even length] odd ending odd ending (R) ( stte) in in An equivlence reltion R is refinement of nother one S iff xry xsy In other words, R mkes ll the sme distinctions S does, plus possily more. R S Lecture Notes 10 Stte Minimiztion 5
~ M is Refinement of L. Theorem: For ny deterministic finite utomton M nd ny strings x, y Σ*, if x ~ M y, then x L y. Proof: If x ~ M y, then x nd y drive m to the sme stte q. From q, ny continution string w will drive M to some stte r. Thus xw nd yw oth drive M to r. Either r is finl stte, in which cse they oth ccept, or it is not, in which cse they oth reject. But this is exctly the definition of L. Corollry: ~ M L. When is this true? Going the Other Wy If x L(M) y then x ~ M y. Finding the Miniml FSM for L Wht's the smllest numer of sttes we cn get wy with in mchine to ccept L? Exmple: The equivlence clsses of L : Miniml numer of sttes for M(L) = This follows directly from the theorem tht sys tht, for ny mchine M tht ccepts L, ~ M must e t lest s lrge s L. Cn we lwys find mchine with this miniml numer of sttes? The Myhill-Nerode Theorem Theorem: Let L e regulr lnguge. Then there is deterministic FSA tht ccepts L nd tht hs precisely L sttes. Proof: (y construction) M = K sttes, corresponding to the equivlence clsses of L. s = [], the equivlence clss of under L. F = {[x] : x L} δ([x], ) = [x] For this construction to prove the theorem, we must show: 1. K is finite. 2. δ is well defined, i.e., δ([x], ) = [x] is independent of x.. L = L(M) Lecture Notes 10 Stte Minimiztion 6
The Proof (1) K is finite. Since L is regulr, there must exist mchine M, with ~ M finite. We know tht ~ M L Thus L is finite. (2) δ is well defined. This is ssured y the definition of L, which groups together precisely those strings tht hve the sme fte with respect to L. The Proof, Continued () L = L(M) Suppose we knew tht ([x], y) - M * ([xy], ). Now let [x] e [] nd let s e string in Σ*. Then ([], s) - M * ([s], ) M will ccept s if [s] F. By the definition of F, [s] F iff ll strings in [s] re in L. So M ccepts precisely the strings in L. The Proof, Continued Lemm: ([x], y) - M * ([xy], ) By induction on y : Trivil if y = 0. Suppose true for y = n. Show true for y = n+1 Let y = y', for some chrcter. Then, y' = n ([x], y') - M * ([xy'], ) (induction hypothesis) ([xy',] ) - M * ([xy'], ) (definition of δ) ([x], y') - M * ([xy'], ) (trns. of - M *) ([x], y) - M * ([xy], ) (definition of y) Theorem: A lnguge is regulr iff L is finite. Exmple: Consider: L = n n,,,, Equivlence clsses: Another Version of the Myhill-Nerode Theorem Proof: Regulr L is finite: If L is regulr, then there exists n ccepting mchine M with finite numer of sttes N. We know tht N L. Thus L is finite. L is finite regulr: If L is finite, then the stndrd DFSA M L ccepts L. Since L is ccepted y FSA, it is regulr. Lecture Notes 10 Stte Minimiztion 7
Σ = {, } L = {w {, }* : no two djcent chrcters re the sme} Constructing the Miniml DFA from L The equivlence clsses of L : 1: [] 2: [,,,,,...] ( )()* : [,,,,...] ( )()* 4: [,,,,...] the rest Equivlence clsses ecome sttes Strt stte is [] Finl sttes re ll equivlence clsses in L δ([x], ) = [x] 2 1 4, L = { n : n is prime} Using Myhill-Nerode to Prove tht L is not Regulr Consider: Equivlence clsses: So Where Do We Stnd? 1. We know tht for ny regulr lnguge L there exists miniml ccepting mchine M L. 2. We know tht K of M L equls L.. We know how to construct M L from L. But is this good enough? Consider: 4 5 6 Lecture Notes 10 Stte Minimiztion 8
Constructing Miniml FSA Without Knowing L We wnt to tke s input ny DFSA M' tht ccepts L, nd output miniml, equivlent DFSA M. Wht we need is definition for "equivlent", i.e., mergele sttes. Define q p iff for ll strings w Σ*, either w drives M to n ccepting stte from oth q nd p or it drives M to rejecting stte from oth q nd p. Exmple: Σ = {, },, Constructing s the Limit of Sequence of Approximting Equivlence Reltions n (Where n is the length of the input strings tht hve een considered so fr) We'll consider input strings, strting with, nd incresing in length y 1 t ech itertion. We'll strt y wy overgrouping sttes. Then we'll split them prt s it ecomes pprent (with longer nd longer strings) tht their ehvior is not identicl. Initilly, 0 hs only two equivlence clsses: [F] nd [K - F], since on input, there re only two possile outcomes, ccept or reject. Next consider strings of length 1, i.e., ech element of Σ. Split ny equivlence clsses of 0 tht don't ehve identiclly on ll inputs. Note tht in ll cses, n is refinement of n-1. Continue, until no splitting occurs, computing n from n-1. Constructing, Continued More precisely, for ny two sttes p nd q K nd ny n 1, q n p iff: 1. q n-1 p, AND 2. for ll Σ, δ(p, ) n-1 δ(q, ) Lecture Notes 10 Stte Minimiztion 9
The Construction Algorithm The equivlence clsses of 0 re F nd K-F. Repet for n = 1, 2, For ech equivlence clss C of n-1 do For ech pir of elements p nd q in C do For ech in Σ do See if δ(p, ) n-1 δ(q, ) If there re ny differences in the ehvior of p nd q, then split them nd crete new equivlence clss. Until n = n-1. is this nswer. Then use these equivlence clsses to colesce sttes. Σ = {, } An Exmple 1 2 4 5 6 0 =, 1 = 2 = (**)* 0 = 1 = Another Exmple Miniml mchine: Lecture Notes 10 Stte Minimiztion 10
Exmple: L={w {, }* : w is even} Another Exmple (() () () ())* S S T S T, T T T S T S S T #, Convert to deterministic: S = {s} δ = Minimize: Another Exmple, Continued S(1), T(2),, #S() 0 = 1 = Miniml mchine: Lecture Notes 10 Stte Minimiztion 11