CPS 140 - Mthemticl Foundtions of CS Dr. Susn Rodger Section: Finite Automt (Ch. 2) (lecture notes) Things to do in clss tody (Jn. 13, 2004): ffl questions on homework 1 ffl finish chpter 1 ffl Red Chpter 1-2 1
Section: Finite Automt Deterministic Finite Accepter (or Automt) - DFA ADFA=(Q,±,ffi,q 0,F) input tpe tpe hed hed moves current stte 0 1 where Q is finite set of sttes ± is tpe (input) lphet (finite set) q 0 is initil stte q 0 2 Q F Q is set of finl sttes. ffi:q ±!Q ADFAisvery simple mchine with not lot of power. It cn nswer yes or no (for exmple, if 6789 is vlid integer, or SUM is vlid vrile nme in C++). It cnnot tell whether ((9+5)+) is vlid rithmetic expression. Exmple: Crete DFA tht ccepts even inry numers. BUILD IT USING JFLAP Assign mening to the sttes: - odd numers, - even numers, SHOW Lels in JFLAP Trnsition Digrm: 1 0 0 1 M=(Q,±,ffi,q 0,F) = (f;g, f0;1g, ffi,, fg) Tulr Formt 2
0 1 Exmple of move: ffi(,1)= Algorithm for DFA: Strt in strt stte with input on tpe q = current stte s = current symol on tpe while (s!= lnk) do q=ffi(q,s) s = next symol to the right on tpe if q2f then ccept Exmple of trce: 11010-26 Pictoril Exmple of trce for 100: 1) 1 0 0 2) 1 0 0 3) 1 0 0 4) 1 0 0 Definition: ffi Λ (q; ) =q You didn't go nywhere, you re still in stte q ffi Λ (q; w) =ffi(ffi Λ (q; w);) Apply Delt to ll of w first ( string) nd then to Definition The lnguge ccepted y DFA M=(Q,±,ffi,q 0,F) is set of ll strings on ± ccepted y M. Formlly, L(M)=fw 2 ± Λ j ffi Λ (q 0 ;w)2fg Drw picture: rc... some finl stte, ny pth to finl stte is string tht is ccepted. -Sy this: This is the lnguge ccepted y DFA M. ll strings formed of the lphet such tht if you strt in nd process ll the symols in w, then you end up in finl (or ccepting) stte 3
Set of strings not ccepted L(M)=fw 2 ± Λ j ffi Λ (q 0 ;w)62 F g 4
Trp Stte NOTE: Assign mening to sttes! You could drw this in JFLAP with trp stte nd note you don't hve to do this. Exmple: L(M) = f n j n>0g q2 trp, You don't need to show trp sttes! Any rc not shown will y defult go to trp stte. Exmple: Crete DFA tht ccepts even inry numers tht hve neven numer of 1's. ssign lels: - strt, -even inry numer, even no. 1's, q2 - odd numer, odd numer of 1's, q3 - odd numer, even numerof1's 0 0 0 q3 1 0 1 1 q2 1 Tlk out determinism! - only 1 choice 5
Exmple: L=fw2± Λ jw hs n even numer of 's nd n even numer of 'sg NOTE: Other exmples to mention: Exmple: Cn crete DFA for rel numers, integers, vrile nmes, etc. Definition A lnguge is regulr iff there exists DFA M s.t. L=L(M). Chpter 2.2 Nondeterministic Finite Automt (or Accepter) (NFA) Definition An NFA=(Q,±,ffi,q 0,F) where Q is finite set of sttes ± is tpe (input) lphet (finite set) q 0 is initil stte q 0 2 Q F Q is set of finl sttes. ffi:q (± [f g)!2 Q Note: the result of delt is suset of q Allows choices - from stte on, might e le to go to more thn one stte (drw picture). 6
Exmple q3 q2 Note: In this exmple ffi(q 0 ;)=fq 1 ;q 2 g. Note: ffi is no longer function. L=fg [f n j n 0g Exmple L=f() n j n>0g[f n jn>0g l l q3 q5 l q2 q4 q6 Definition q j 2 ffi Λ (q i ;w) if nd only if there is wlk from q i to q j leled w. Exmple From previous exmple: ffi Λ (q 0 ;)= fq 5 ;q 6 ;q 1 g ffi Λ (q 0 ; )= fq 3 g Definition: For n NFA M, L(M)=fw 2 ± Λ j ffi Λ (q 0 ;w) F 6=;g The lnguge ccepted y nf M is ll strings w such tht there exists wlk leled w from the strt stte to finl stte. Why nondeterminism? It mkes it esier to descrie FA. The only prolem is tht it cn tke LONG time to try out ll possiilities. 2.3 NFA vs. DFA: Which is more powerful? Exmple: 7
q2,q2,q2 NOTE: DO THIS USING JFLAP explining generlly how to do it Theorem Given n NFA M N =(Q N ; ±;ffi N ;q 0 ;F N ), then there exists DFA M D =(Q D ; ±;ffi D ;q 0 ;F D ) such tht L(M N )=L(M D ). 8
Proof: We need to define M D sed on M N. Q D =2 QN F D =fq2q D j9q i 2 Q with q i 2 F N g ffi D : Q D ±! Q D Algorithm to construct M D 1. strt stte is fq 0 g[closure(q 0 ) 2. While cn dd n edge () Choose stte A=fq i ;q j ; :::q k g with missing edge for 2 ± () Compute B = ffi Λ (q i ;)[ffi Λ (q j ;)[:::[ffi Λ (q k ;) (c) Add stte B if it doesn't exist (d) dd edge from A to B with lel 3. Identify finl sttes 4. if 2 L(M N ) then mke the strt stte finl. Exmple:,,q2 q3,q4,q5,q6 q3 q5, q4 q6 λ λ λ q3 q5 q2 q4 q6 DO THIS USING JFLAP. 9
Minimizing Numer of sttes in DFA Why? If you hve nnfa with n sttes, wht is the mximum numer of sttes in the equivlent DFA creted? 2 n Algorithm ffl Identify sttes tht re indistinguishle These sttes form new stte Definition Two sttes p nd q re indistinquishle if for ll w 2 ± Λ ffi Λ (q; w) 2 F ) ffi Λ (p; w) 2 F ffi Λ (p; w) 62 F ) ffi Λ (q; w) 62 F Definition Two sttes p nd q re distinquishle if 9 w 2 ± Λ s.t. ffi Λ (q; w) 2 F ) ffi Λ (p; w) 62 F OR ffi Λ (q; w) 62 F ) ffi Λ (p; w) 2 F p nd q pper to e different. 10
Exmple: Look t A on, Look t F on, Look t D on, This is in file rodger/cl/cps140/jflpex/chp2mindf1.jff A B C D E F G ADF ABDEFG ADEF E BG C AD BG C E F AD F Exmple: This is in file rodger/cl/cps140/jflpex/chp2mindf2.jff 11
A B C D E F G H I ABCDEFGHI ABDFHI CEG ABFHI D AB FHI CE G CE G D FHI AB 12