FINITE STATE AUTOMATA - Politecnico di Milanohome.deib.polimi.it/bersani/API/01FSA.pdf ·...
Transcript of FINITE STATE AUTOMATA - Politecnico di Milanohome.deib.polimi.it/bersani/API/01FSA.pdf ·...
States• AnFSAhasafinitesetofstates
– Asystemhasalimitednumberofconfigurations• Examples
– {On,Off},– {1,2,3,4,…,k}– {TVchannels}– …
• Statescanbegraphicallyrepresentedasfollows:
On Off
Input
• AnFSAisdefinedoveranalphabet• Thesymbolsofthealphabetrepresenttheinputofthesystem
• Examples– {switch_on,switch_off}– {incoming==0,0<incoming<=10,incoming>10}
Transitionsamongstates
• Whenaninputisreceived,thesystemchangesitsstate
• Thepassagebetweenstatesisperformedthroughtransitions
• Atransitionisgraphicallyrepresentedbyarrows:
On Off
FSA
• FSAsarethesimplestmodelofcomputation• ManyusefuldevicescanbemodeledusingFSAs
…buttheyhavesomelimitations
Formally
• AnFSAisatriple<Q,A,δ>,where– Qisafinitesetofstates– Aistheinputalphabet– δ isatransitionfunction(thatcanbepartial),givenby δ:Q× A→ Q
• Remarkifthefunctionispartial,thennotallthetransitionsfromallthepossiblestatesforallthepossibleelementsofthealphabetaredefined
PartialvsTotalTransitionFunction
switch_offswitch_on
On Off
switch_off
switch_on
An FSA with a total transition function is called complete
Recognizinglanguages
• InordertobeabletouseFSAsforrecognizinglanguages,itisimportanttoidentify:– theinitialconditionsofthesystem– thefinaladmissiblestates
• Example:– Thelightshouldbeoffatthebeginningandattheend
Elements
• Theelementsofthemodelare– States– Transitions– Inputandalso– Initialstate(s)– Finalstate(s)
Formally
• AnFSAisatuple<Q,A,δ ,q0,F>,where– Qisafinitesetofstates– Aistheinputalphabet– δ isa(partial)transitionfunction,givenby
δ:Q× A→ Q– q0∈Qiscalledinitialstate– F⊆Qisthesetoffinalstates
Movesequence
• Amovesequence startsfromaninitialstateandisaccepting ifitreachesoneofthefinalstates
On Off
switch_offswitch_on
switch_on
switch_off
Formally
• Movesequence:– δ∗:Q× A∗ → Q
• δ∗ isinductivelydefinedfrom δ– δ∗(q,ε)=q– δ∗(q,yi)=δ(δ∗(q,y),i)
• Initialstate:q0∈ Q• Final(oraccepting)states:F⊆ Q• ∀x(x∈ L↔ δ∗ (q0,x)∈ F)
Apracticalexample
• RecognizingPascalidentifiers
q0 q1
<letter><letter>
<digit>qE
<digit>
<digit> <letter>
Automataaslanguagetranslators
AfinitestatetransducerisanFSAthatworksontwotapes.→itisakindof``translatingmachine''.
Input tape
Output tape
Control device (finite state)
Theidea• y=τ(x)
– x:inputstring– y:output– τ:functionfromL1 toL2
• Examples:– τ1 theoccurrencesof“1”aredoubled(1-->11)– τ2 ‘a’isswappedwith‘b’(a<--->b):
• butalso– Compressionoffiles– Compilingfromhighlevellanguagesintoobjectlanguages– TranslationfromEnglishtoItalian
Informally
• Transitionswithoutput
• Example:τ halvesthenumberof“0”sanddoublesthenumberof“1”s
q q’i/w
q0 q1
0/ε1/11 1/11
0/0
Formally
• Afinitestatetransducer(FST)isatupleT=<Q,I,δ,q0,F,O,η>– <Q,I,δ,q0,F>:justlikeacceptors– O:outputalphabet– η :Q× I→ O*
• Remark:theconditionforacceptanceremainsthesameasinacceptors– Thetranslationisperformedonlyonacceptedstrings
Translatingastring
• Aswedidforδ,wedefineη*inductively– η*(q,ε)=ε– η∗(q,y.i)=η∗(q,y).η(δ∗ (q,y),i)
• Remarkη*:Q× I* → O*
∀x(τ(x)=η∗(q0,x)iff δ∗ (q0,x)∈ F)
CyclesThereisacycle:q1----aabab--->q1
a
a
b
a
a
b
b
b
b
b
a
q1 q2
q3
q0
q4q5
q6 q7
q9
q8
If one goes through the cycle once, then one can also go through it 2,3, …, n times
Moreformally
• Ifx∈ L and|x|≥ |Q|,thenthereexistsaq∈Qandaw∈ I+ suchthat:– x=ywz– δ∗ (q,w)=q
• Thereforethefollowingalsoholds:–∀n≥0 ywnz∈ L
ThisisthePumpingLemma (onecan“pump”w)
Consequencesofpumpinglemma
• L=∅? ∃x∈ L↔∃y∈ L|y|<|Q|:Just“removeallcycles”fromtheFSAacceptingx
• |L|=∞? Checkbyasimilarargumentwhether
∃x∈ L|Q|<=|x|<2|Q|• Notethatingeneral knowinghowtoanswerthequestion“x∈ L?”foragenericx,doesnot entailknowinghowtoanswertheotherquestions– ItworksforFSAs,but…
Impactinpractice
• Areweinterestedinaprogramminglanguageconsistingof…0correctprograms?
• Areweinterestedinaprogramminglanguageinwhichonecanonlywriteafinitenumberofprograms?
• ...
Anegativeconsequenceofpumpinglemma
• IsthelanguageL={anbn |n>0}recognizedbysomeFSA?• Letussupposeitis.Then:• Considerx=ambm,m>|Q|andletusapplyP.L.• Possiblecases:
– x=ywz,w=ak ,k>0====>am+r⋅kbm∈ L,∀r:NO– x=ywz,w=bk ,k>0====>same– x=ywz,w=ak bs ,k,s>0====>am-kakbsakbsbm-s
∈ L:NO
Intuitively• Inorderto“count”anarbitraryn weneedaninfinite
memory!• Rigorouslyspeaking,everycomputerisanFSA,but…itisthe
wrongabstraction:intractablenumberofstates!(samethingasstudyingeverysinglemoleculeintheflightofanairplane)
• Importanceofanabstractnotionofinfinity• Fromthetoyexample{anbn}tomoreconcretecases:
– Checkingwell-balancingofbrackets(typicallyusedinprogramminglanguages)cannotbedonewithfinitememory
• Wethereforeneedmorepowerfulmodels
Closureinmath
• AsetSisclosed w.r.t.anoperationOPif,whenoperationOPisappliedtoelementsofS,theresultisstillanelementofS
• Examples:– Naturalnumbersareclosedw.r.t.sum(butnotsubtraction)
– Integersareclosedw.r.t.sum,subtraction,multiplication(butnotdivision)
– Rationals…– Reals…– …
Closureforlanguages
• L ={Li}:family oflanguages
• L isclosedw.r.t.operationOPifandonlyif,foreveryL1,L2∈ L ,L1OPL2∈L .
• R:regularlanguages(recognizedbyFSAs)
• R isclosedw.r.t.set-theoreticoperations,concatenation, “*”,…
Intersection
The“parallelrun”ofAandBcanbesimulatedby“couplingthem”
a abq1 q2q0
q9
a abp1 p2
p9
A
B p0
p0a abq0<A,B> p1q1 p2q2 p9q9
Formally
• Given– A1 =<Q1,I,δ1,q01,F1>– A2 =<Q2,I,δ2,q02,F2><A1,A2 >=<Q1×Q2,I,δ,<q01,q02>,F1×F2>– δ(<q1,q2>,i)=<δ1(q1,i),δ2(q2,i)>
• Onecanshow(bysimpleinduction)thatL(<A1,A2>)=L(A1)∩L(A2 )
• Canwedothesameforunion?
Union
• Theunionisbuiltanalogously• Given
– A1 =<Q1,I,δ1,q01,F1>– A2 =<Q2,I,δ2,q02,F2><A1,A2>=<Q1xQ2,I,δ,<q01,q02>,F1xQ2UQ1xF2>– δ(<q1,q2>,i)=<δ1(q1,i),δ2(q2,i)>
Complement(2)
• BeforeswappingfinalandnonfinalstatesitisnecessarytocompletetheFSA
q0 q1
10 0
q0 q1
10 0
qE
0,1q0 q1
10 0
qE0,1
1
1
Philosophyofcomplement
• IfIscantheentireinputstring,thenitsufficesto“swapyesandno”(FwithQ-F)
• IfIcannotreachtheendofthestring,thenswappingFwithQ-Fdoesnotwork
• InthecaseofFSAsthereisaneasyworkaround(completingtheFSA)
• Ingeneralwecannotconsiderthenegativeanswertoaquestionasequivalenttothepositiveanswertotheoppositequestion!