Post on 13-Mar-2021
transcript
FiniteStateMachines1
15-121IntroductiontoDataStructures
115-121IntroductiontoDataStructures
SomeResultsFirstComputingModel
FiniteAutomata
PushdownAutomata
LinearBoundedAutomata
TuringMachines
LanguageClass RegularLanguages
Context-FreeLanguages
Context-SensitiveLanguages
RecursivelyEnumerableLanguages
Non-determinism
Makesnodifference
Makesadifference
Nooneknows Makesnodifference
15-121IntroductiontoDataStructures 2
Alphabets
• S (sigma):afinite(non-empty)setofsymbolscalledthealphabet.
• EachsymbolinS isaletter.
• Lettersinthealphabetareusuallydenotedbylowercaseletters:a,b,c,…
315-121IntroductiontoDataStructures
Strings• Aword wisastringoflettersfromS inalinearsequence.
• Weareinterestedonlyinfinitewords(boundedlength).
• |w| denotesthelength ofwordw.
• Theemptystring containsnolettersandis
writtenase.415-121IntroductiontoDataStructures
Languages
• Alanguage Lisaset(finiteorinfinite)ofwordsfromagivenS.
• ThesetofallstringsoversomefixedalphabetS isdenotedbyS*.
• Forexample,ifS ={a},thenS*={e,a,aa,aaa,…}.
515-121IntroductiontoDataStructures
Languages
• Thesetofallstringsoflengthi oversomefixedalphabetS isdenotedbySi.
• Forexample,letS ={a,b}.• ThenL=S2 ={aa,ab,ba,bb}isthesetofwordswsuchthat|w| =2.
615-121IntroductiontoDataStructures
OperationsonWordsandLanguages
• Concatenation: puttingtwostringstogetherx=aa;y=bb;x.y=xy=aabb
• Power: concatenatingmultiplecopiesofaletterorwordan =a.an-1;a1 =a;a2 =a.a;etc.x=ab;x3 =ababab
• KleeneStar: zeroormorecopiesofaletterorworda*={e,a,aa,aaa,…}x=ab;x*={e,ab,abab,ababab,…}
715-121IntroductiontoDataStructures
Deterministic,FiniteStateAutomata
• Afinite-stateautomaton comprisesthefollowingelements:– Asequenceofinputsymbols (theinput“tape”).– Thecurrentlocationintheinput,whichindicatesthecurrentinputsymbol(theread“head”).
– Thecurrentstate ofthemachine(denotedq0,q1,…,qn).
– Atransitionfunction whichinputsthecurrentstateandthecurrentinput,andoutputsanew(next)state.
815-121IntroductiontoDataStructures
Duringcomputation,v TheFSAbeginsinthestartstate(usually,q0).v Ateachstep,thetransitionfunctioniscalledonthecurrentinputsymbolandthecurrentstate,thestateisupdatedtothenewstate,andthereadheadmovesonesymboltotheright.
v TheendofcomputationisreachedwhentheFSAreachestheendoftheinput.
Oneormorestatesmaybemarkedasfinalstates,suchthatthecomputationisconsideredsuccessfulifandonlyifcomputationendsinafinalstate.
915-121IntroductiontoDataStructures
AnExample
• AnFSAcanberepresentedgraphicallyasadirectedgraph,wherethenodesinthegraphdenotestates andtheedgesinthegraphdenotetransitions.Finalstatesaredenotedbyadoublecircle.
• Forexample,hereisagraphicalrepresentationofaDFSAthatacceptsthelanguageL={a2n :n³ 1}:
1015-121IntroductiontoDataStructures
• Input:aaaStates:q0,q1,q2,q1 (notaccepted)
• Input:aaaaStates:q0,q1,q2,q1,q2 (accepted)
1115-121IntroductiontoDataStructures
DFSADefinition
ADFSAcanbeformallydefinedasA=(Q,S,¶,q0,F):
– Q,afinitesetofstates– S,afinitealphabetofinputsymbols– q0 Î Q,aninitialstartstate– FÍ Q,asetoffinalstates
– ¶ (delta):QxS® Q,atransitionfunction
1215-121IntroductiontoDataStructures
Transitionfunction- ¶
• Wecanexpandthenotionof¶ onlettersto¶onwords,¶w,byusingarecursivedefinition:
• ¶w :QxS*® Q- (afunctionof(state,word)toastate)• ¶w(q,e)=q - (instateq,outputstateqifwordise)• ¶w(q,xa)=¶(¶w(q,x),a)- (otherwise,use¶ foronestep
andrecurse)
1315-121IntroductiontoDataStructures
LanguageRecognition• ForanautomatonA,wecandefinethelanguageofA:- L(A)={wÎ S*:¶w(q0,w)Î F}- L(A)isasubsetofallwordswoffinitelengthoverS,suchthatthetransitionfunction¶w(q0,w)producesastateinthesetoffinalstates(F).- Intuitively,ifwethinkoftheautomatonasagraphstructure,thenthewordsinL(A)representthe“paths”whichendinafinalstate.Ifweconcatenatethelabelsfromtheedgesineachsuchpath,wederiveastringwÎ L(A).
1415-121IntroductiontoDataStructures
ImplementingDFSAinJava(afirstattempt)
• ImplementingaDFSAinJavahassomesimilaritiestoimplementingagraphstructure.Asmentionedearlier,thestatesinaDFSAcorrespondtonodesinagraph,andthetransitionscorrespondtoedgesinagraph.
• First,let’sconsiderhowtoimplementthetransitionfunction,¶.Recallthat¶(<state>,<letter>)=<state>.So,foranygivenstateq,weneedtoknowifthereisatransitiontotheotherstates,andifso,whatletterofthealphabetmustbereadforthetransitiontooccur.
1515-121IntroductiontoDataStructures
• Ifeachvertexinadirectedgraphhasatmostoneedgeleadingtoanothervertex(possiblythevertexitself)thenwecanmodelthegraphusingatwo-dimensionalarray.
• SupposewewanttomodelaDFSAinthismanner.ForeachstateintheDFSA,thereisarowinthearray;eachcolumninthatrowcorrespondstoa(possible)transitiontoanotherstate.Eachcellisempty(null)ifthereisnosuchtransition;otherwise,acellcontainstheletterofthealphabetwhichmustbereadforthetransitiontobevalid.
• Assumingwenamethearraydelta,thendelta(m,n)==nullifthereisnotransitionfromQm toQn,anddelta(m,n)==<letter>if¶(Qm,<letter>)=Qn.
1615-121IntroductiontoDataStructures
Whatiswrongwiththisapproach?
TheDFSAonthepreviouspagehas3states,soitcanbemodeledbya3x3array:
Q0 Q1 Q2 array transitionfunction
Q0 null a null delta(0,1)=a ¶(Q0,a)=Q1
Q1 null null a delta(1,2)=a ¶(Q1,a)=Q2
Q2 null a null delta(2,1)=a ¶(Q2,a)=Q1
1715-121IntroductiontoDataStructures
• Notethatthisworksonlywhenthereisasingletransitionfromonestatetoanother.IfS wereexpandedtocontaintwocharactersratherthanone,andifthereweretwotransitionsleadingfromQ0tosay,Q1,thenwecouldnotusethisimplementation.
• BeforewewriteaJavaclassforthisDFSA,wehaveonemorethingtoconsider.– Howcanwerepresentthesetoffinalstates?
• Astraightforwardsolutionistouseaone-dimensionalbooleanarray,final,oflengthn(assumingwehavenstates).Thenfinal(i)=trueifqi isafinalstate,andfinal(i)=falseotherwise.
1815-121IntroductiontoDataStructures
publicclassDFSA{
privateboolean[]finalStates;privatechar[][]delta;privateintstartState;privateintcurrentState;privateinttotalStates;
publicDFSA(intn,intstart){totalStates=n;finalStates=newboolean[totalStates];delta=newchar[totalStates][totalStates];startState=start;
}
privatebooleanisFinal(){returnfinalStates[currentState];
}
publicvoidaddTransition(intfromState,inttoState,charletter){delta[fromState][toState]=letter;
}publicvoidaddFinalState(intq){finalStates[q]=true;
}
1915-121IntroductiontoDataStructures
publicbooleanisAccepted(Strings){currentState=startState;readingSymbols:for(inti=0;i<s.length();i++){System.out.println("currentstate:"+currentState);System.out.println("nextsymbol:"+s.charAt(i));for(intj=0;j<totalStates;j++){
if(delta[currentState][j]==s.charAt(i)){currentState=j;continuereadingSymbols;
}}System.out.println("d("+currentState+","+s.charAt(i)+")isundefined");returnfalse;
}System.out.println("finalstate:"+currentState);returnisFinal();
}
2015-121IntroductiontoDataStructures
publicstaticvoidmain(Stringargs[]){
//Definethethree-stateDFSAfromthehandout.
DFSAa=newDFSA(3,0);a.addTransition(0,1,'a');a.addTransition(1,2,'a');a.addTransition(2,1,'a');a.addFinalState(2);
//Foreachinputstring,checkvalueofisAccepted()
for(inti=0;i<args.length;i++){System.out.println("\nInputString:"+args[i]);if(a.isAccepted(args[i])){System.out.println("Accepted");
}else{System.out.println("Rejected");
}}
}
}
2115-121IntroductiontoDataStructures
>javaDFSAaaaaaaaaabaa
InputString:aaacurrentstate:0nextsymbol:acurrentstate:1nextsymbol:acurrentstate:2nextsymbol:afinalstate:1Rejected
InputString:aaaacurrentstate:0nextsymbol:acurrentstate:1nextsymbol:acurrentstate:2nextsymbol:acurrentstate:1nextsymbol:afinalstate:2Accepted
InputString:aabaacurrentstate:0nextsymbol:acurrentstate:1nextsymbol:acurrentstate:2nextsymbol:bd(2,b)isundefinedRejected
2215-121IntroductiontoDataStructures
Let’sconsidermakingcertainmodificationstotheprogramabovesothatitallowsformultipletransitionsfromonestatetoanotherbasedonaS withmorethanonecharacter.
Thereareseveralwaysthiscouldbedone.- Onemightusealargetwo-dimensionalarraywhoserowsareindexedwithstatenumbersandwhosecolumnsareindexedwithallthecharactersfoundinS.ThiswouldgiveO(1)transitionlookupsbutwouldbeinefficientwithrespecttospace.- Anotherapproachwouldbetouseanadjacenysetrepresentation.Asingledimensionalarray,saystates,couldbeindexedbasedonthestatenumber.Theelementatstates[i]wouldcontainasetof<character><nextstate>pairs.Eachsetwouldallowfast<nextstate>lookupsgiventhecharactersymbolfromS.Thesetcouldbeimplementedwithalinkedlistor,perhaps,withabalancedtree.
2315-121IntroductiontoDataStructures
• Considermodifyingtheprogramabovesothatitusesanadjacenysetrepresentationtocompute¶(Qm,<letter>).Thesetcouldbeimplementedasadigitalsearchtree.TheDSTclasswouldallowfortheinsertionandsearchof(<letter>,<next-state>)pairs.The<letter>partofthepairwouldbethesearchkey.Wheninsertingorsearching,thedecisiontomoveleftorrightinthesearchtreewouldbebasedonthebitsofthekey.Theimplementationeffortforthistypeofdatastructureisaboutthesameasbinarysearchtreesbutusuallyhasbetterperformance.
2415-121IntroductiontoDataStructures
AnotherExampleConsiderrunningsuchaprogramthatwouldmodeltheDFSAbelow.Suchaprogrammightreadaseriesofstringsfromthecommandline(justliketheprogramabove)andtelltheuserwhetherthemachinebelowacceptsorrejectseachstring.
Q={q0,q1,q2 }S ={R,0,1,2}q0:thestartstateF={q0 }¶ (delta):QxS® Q
2515-121IntroductiontoDataStructures
Thisautomatonkeepsarunningcountofthesumofthenumericalinputsymbolsitreads,modulo3.EverytimeitreceivestheR(reset)symbolitresetsthecountto0.Itacceptstheifthesumis0,modulo3,orinotherwords,ifthesumisamultipleof3.Thisautomatonisfrom“IntroductiontotheTheoryofComputation”byMichaelSipser
2615-121IntroductiontoDataStructures
HomeworkQuestions(nottobeturnedinbuttoprepareforexam)
GivestatediagramsforDFA’srecognizingthefollowinglanguages.S ={0,1}.
1.{w|wbeginswitha1andendswitha0}2.{w|wcontainsatleastthree1’s}3.{w|thelengthofwisatmost5}4.{w|wcontainsatleasttwo0’sandatmostone1.}5.{w|wcontainsanevennumberof0’s,orexactlytwo1’s}
6.{w|wisnote }
2715-121IntroductiontoDataStructures