finite state machines 1 - carnegie mellon universitymm6/15-121/powerpoint/fsm1.pdf · 2016. 11....
TRANSCRIPT
![Page 1: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/1.jpg)
FiniteStateMachines1
15-121IntroductiontoDataStructures
115-121IntroductiontoDataStructures
![Page 2: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/2.jpg)
SomeResultsFirstComputingModel
FiniteAutomata
PushdownAutomata
LinearBoundedAutomata
TuringMachines
LanguageClass RegularLanguages
Context-FreeLanguages
Context-SensitiveLanguages
RecursivelyEnumerableLanguages
Non-determinism
Makesnodifference
Makesadifference
Nooneknows Makesnodifference
15-121IntroductiontoDataStructures 2
![Page 3: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/3.jpg)
Alphabets
• S (sigma):afinite(non-empty)setofsymbolscalledthealphabet.
• EachsymbolinS isaletter.
• Lettersinthealphabetareusuallydenotedbylowercaseletters:a,b,c,…
315-121IntroductiontoDataStructures
![Page 4: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/4.jpg)
Strings• Aword wisastringoflettersfromS inalinearsequence.
• Weareinterestedonlyinfinitewords(boundedlength).
• |w| denotesthelength ofwordw.
• Theemptystring containsnolettersandis
writtenase.415-121IntroductiontoDataStructures
![Page 5: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/5.jpg)
Languages
• Alanguage Lisaset(finiteorinfinite)ofwordsfromagivenS.
• ThesetofallstringsoversomefixedalphabetS isdenotedbyS*.
• Forexample,ifS ={a},thenS*={e,a,aa,aaa,…}.
515-121IntroductiontoDataStructures
![Page 6: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/6.jpg)
Languages
• Thesetofallstringsoflengthi oversomefixedalphabetS isdenotedbySi.
• Forexample,letS ={a,b}.• ThenL=S2 ={aa,ab,ba,bb}isthesetofwordswsuchthat|w| =2.
615-121IntroductiontoDataStructures
![Page 7: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/7.jpg)
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
![Page 8: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/8.jpg)
Deterministic,FiniteStateAutomata
• Afinite-stateautomaton comprisesthefollowingelements:– Asequenceofinputsymbols (theinput“tape”).– Thecurrentlocationintheinput,whichindicatesthecurrentinputsymbol(theread“head”).
– Thecurrentstate ofthemachine(denotedq0,q1,…,qn).
– Atransitionfunction whichinputsthecurrentstateandthecurrentinput,andoutputsanew(next)state.
815-121IntroductiontoDataStructures
![Page 9: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/9.jpg)
Duringcomputation,v TheFSAbeginsinthestartstate(usually,q0).v Ateachstep,thetransitionfunctioniscalledonthecurrentinputsymbolandthecurrentstate,thestateisupdatedtothenewstate,andthereadheadmovesonesymboltotheright.
v TheendofcomputationisreachedwhentheFSAreachestheendoftheinput.
Oneormorestatesmaybemarkedasfinalstates,suchthatthecomputationisconsideredsuccessfulifandonlyifcomputationendsinafinalstate.
915-121IntroductiontoDataStructures
![Page 10: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/10.jpg)
AnExample
• AnFSAcanberepresentedgraphicallyasadirectedgraph,wherethenodesinthegraphdenotestates andtheedgesinthegraphdenotetransitions.Finalstatesaredenotedbyadoublecircle.
• Forexample,hereisagraphicalrepresentationofaDFSAthatacceptsthelanguageL={a2n :n³ 1}:
1015-121IntroductiontoDataStructures
![Page 11: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/11.jpg)
• Input:aaaStates:q0,q1,q2,q1 (notaccepted)
• Input:aaaaStates:q0,q1,q2,q1,q2 (accepted)
1115-121IntroductiontoDataStructures
![Page 12: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/12.jpg)
DFSADefinition
ADFSAcanbeformallydefinedasA=(Q,S,¶,q0,F):
– Q,afinitesetofstates– S,afinitealphabetofinputsymbols– q0 Î Q,aninitialstartstate– FÍ Q,asetoffinalstates
– ¶ (delta):QxS® Q,atransitionfunction
1215-121IntroductiontoDataStructures
![Page 13: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/13.jpg)
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
![Page 14: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/14.jpg)
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
![Page 15: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/15.jpg)
ImplementingDFSAinJava(afirstattempt)
• ImplementingaDFSAinJavahassomesimilaritiestoimplementingagraphstructure.Asmentionedearlier,thestatesinaDFSAcorrespondtonodesinagraph,andthetransitionscorrespondtoedgesinagraph.
• First,let’sconsiderhowtoimplementthetransitionfunction,¶.Recallthat¶(<state>,<letter>)=<state>.So,foranygivenstateq,weneedtoknowifthereisatransitiontotheotherstates,andifso,whatletterofthealphabetmustbereadforthetransitiontooccur.
1515-121IntroductiontoDataStructures
![Page 16: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/16.jpg)
• 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
![Page 17: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/17.jpg)
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
![Page 18: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/18.jpg)
• Notethatthisworksonlywhenthereisasingletransitionfromonestatetoanother.IfS wereexpandedtocontaintwocharactersratherthanone,andifthereweretwotransitionsleadingfromQ0tosay,Q1,thenwecouldnotusethisimplementation.
• BeforewewriteaJavaclassforthisDFSA,wehaveonemorethingtoconsider.– Howcanwerepresentthesetoffinalstates?
• Astraightforwardsolutionistouseaone-dimensionalbooleanarray,final,oflengthn(assumingwehavenstates).Thenfinal(i)=trueifqi isafinalstate,andfinal(i)=falseotherwise.
1815-121IntroductiontoDataStructures
![Page 19: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/19.jpg)
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
![Page 20: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/20.jpg)
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
![Page 21: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/21.jpg)
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
![Page 22: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/22.jpg)
>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
![Page 23: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/23.jpg)
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
![Page 24: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/24.jpg)
• Considermodifyingtheprogramabovesothatitusesanadjacenysetrepresentationtocompute¶(Qm,<letter>).Thesetcouldbeimplementedasadigitalsearchtree.TheDSTclasswouldallowfortheinsertionandsearchof(<letter>,<next-state>)pairs.The<letter>partofthepairwouldbethesearchkey.Wheninsertingorsearching,thedecisiontomoveleftorrightinthesearchtreewouldbebasedonthebitsofthekey.Theimplementationeffortforthistypeofdatastructureisaboutthesameasbinarysearchtreesbutusuallyhasbetterperformance.
2415-121IntroductiontoDataStructures
![Page 25: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/25.jpg)
AnotherExampleConsiderrunningsuchaprogramthatwouldmodeltheDFSAbelow.Suchaprogrammightreadaseriesofstringsfromthecommandline(justliketheprogramabove)andtelltheuserwhetherthemachinebelowacceptsorrejectseachstring.
Q={q0,q1,q2 }S ={R,0,1,2}q0:thestartstateF={q0 }¶ (delta):QxS® Q
2515-121IntroductiontoDataStructures
![Page 26: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/26.jpg)
Thisautomatonkeepsarunningcountofthesumofthenumericalinputsymbolsitreads,modulo3.EverytimeitreceivestheR(reset)symbolitresetsthecountto0.Itacceptstheifthesumis0,modulo3,orinotherwords,ifthesumisamultipleof3.Thisautomatonisfrom“IntroductiontotheTheoryofComputation”byMichaelSipser
2615-121IntroductiontoDataStructures
![Page 27: Finite State Machines 1 - Carnegie Mellon Universitymm6/15-121/PowerPoint/FSM1.pdf · 2016. 11. 28. · Finite State Machines 1 15-121 Introduction to Data Structures 15-121 Introduction](https://reader036.vdocument.in/reader036/viewer/2022071418/6115de09d5038b563861c797/html5/thumbnails/27.jpg)
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