CS1010:TheoryofComputation
LorenzoDeStefaniFall2020
Lecture2:Non-DeterministicFiniteStatesAutomata
Outline• WhatareNon-deterministicFiniteStateAutomata
• NFAconstruction• FormalDefinitionofNFA• EquivalencebetweenNFAsandDFAs• RegularOperations
– Closureunderunion– Closureunderconcatenation– Closureunderstar
1
FromSipser Chapter1.2
9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani
Nondeterminism
• SofarourFAisdeterministicinthatthestateandnextsymboldeterminesthenextstate
• NondeterministicFiniteStatesAutomataintroducetwomaindifferences:– DFAshaveonetransitionarrowperalphabetsymbol,whileNFAshave0or
moreforeachandε the“empty”symbol– DFAsareinaspecificsinglestateatalltimes,whileNFAsmaybein
multiplestates
0,1
0,ε1q1 q3q2
1q4
0,1
9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 2
HowdoesanNFACompute?• Whenthereisachoice,allpathsarefollowed
– Thinkofitascloning aprocessandcontinuing– Ifthereisnoarrow,thepathterminatesandtheclonedies (itdoesnotacceptifatanacceptstatewhenthathappens)
– AnNFAmayhavetheemptystringcauseatransition– TheNFAacceptsifanypathofthecloneprocessesterminatesinanacceptingstate
– Canalsobemodeledasatreeofpossibilities
• Analternativewayofthinkingofthis– Ateachchoiceyoumakeoneguessofwhichwaytogo– Youmagicallyalwaysguesstherightwaytogo
9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 3
Letsgiveitatry
• Tryout010110– Isitaccepted?
• Yes
• Whatisthelanguage?– Stringscontainingasubstringof101or11
0,1
0,ε1q1 q3q2
1q4
0,1
9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 4
NFAconstruction
ConstructanNFAthatacceptsallstringover{0,1}witha1inthethirdpositionfromtheend
Hint:theNFAstaysinthestartstateuntilitguessesthatitisthreeplacesfromtheend
0,1
0, 11q1 q3
q2
0,1q4
9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 5
FormalDefinitionofNFAAnondeterministicfiniteautomatonisa5-tuple(Q,S,δ,q0,F)where:
– Qisafinitesetofstates– S isafinitesetcalledthealphabet– δ:QxSε® P(Q)isthetransitionfunction– q0Î Qisthestartstate– FÍ Qisthesetofacceptstates
• SimilartoDFAexcept– S includesε– Thetransitionfunctionmatchesstatesandsymbolswithasetofpossiblestates
9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 6
ExampleofFormalDefinitionofNFA
NFAN1is(Q,S,δ,q1,F)Q={q1,q2,q3,q4}S ={0,1}q1 isthestartstateF={q4}
0 1 εq1 {q1} {q1,q2} Æq2 {q3} Æ {q3}q3 Æ {q4} Æq4 {q4} {q4} Æ
0,1
0,ε1q1 q3q2
1q4
0,1
9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 7
EquivalenceofNFAandDFA
• NFAandDFArecognizesameclassoflanguages• Whatdoesthismean?Whatistheimplication?
– NFAhavenomorepowerthanDFA• Withrespecttowhatcanbeexpressed• EveryNFAhasanequivalentDFA• ButNFAmaymakeiteasiertodescribesomelanguages
– Terminology:Twomachinesareequivalent iftheyrecognizethesamelanguage
9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 8
CompilerEquivalence
• C,C++,Python,Pascal,Fortran,…• Aretheselanguagesequivalent?
– Somearemoresuitedtosometasks,butwithenougheffortanyoftheselanguagescancomputeanythingtheotherscan
– Ifnecessary,youcanevenwriteacompilerforonelanguageusinganother
9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 9
ProofofEquivalenceofNFA&DFA
Proofidea:– SimulateanNFAwithaDFA– WithNFAs,givenaninputwefollowallpossiblebranchesandkeepafingeronthestateforeach
– IntheequivalentDFAweneedtokeeptrackofallthepossiblestateswewouldbeinforeachoftheNFAexecution
– IftheNFAhaskstatesthenithas2k possiblesubsets• EachsubsetcorrespondstooneofthepossibilitiesthattheDFAneedstoremember
• TheDFAwillhave2k states
Theory of Computation - Fall'20 Lorenzo De Stefani 109/18/20
ProofbyConstruction• LetN=(Q,S,δ,q0,F)betheNFArecognizingA• ConstructequivalentDFAM=(Q’,S,δ’,q0’,F’)
– Q=P(Q)– Carefulinhandlingε!
• LetE(R)denotethecollectionofstatesreachablebymembersofRjustfollowingε arrows
– q0’ ={q0E({q0}) }– F’ ={RÎ Q’|RcontainsanacceptstateofN}– Transitionfunction
• ThestateRinMcorrespondstoasetofstatesinN• WhenMreadssymbola instateR,itshowswherea takeseachstate• δ’(R,a)=( rÎR δ(r,a))E(R)[
<latexit sha1_base64="(null)">(null)</latexit><latexit sha1_base64="(null)">(null)</latexit><latexit sha1_base64="(null)">(null)</latexit><latexit sha1_base64="(null)">(null)</latexit>
[<latexit sha1_base64="(null)">(null)</latexit><latexit sha1_base64="(null)">(null)</latexit><latexit sha1_base64="(null)">(null)</latexit><latexit sha1_base64="(null)">(null)</latexit>
[<latexit sha1_base64="(null)">(null)</latexit><latexit sha1_base64="(null)">(null)</latexit><latexit sha1_base64="(null)">(null)</latexit><latexit sha1_base64="(null)">(null)</latexit>
9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 11
Example:ConvertanNFAtoaDFA
Example1.41(pg.572nd ed.)– TheNFAhas3states:Q={1,2,3}– WhatarethestatesintheDFA?
• {Æ,{1},{2},{3},{1,2},{1,3},{2,3},{1,2,3}}
– WhatarethestartstatesoftheDFA?• StartstatesoftheDFAcorrespondthecollectionofjustthestatingstateoftheNFAandallthestatesreachableviaε
• {1,3}
– Whataretheacceptstates?• AllthestatesoftheDFAwhichincludeanacceptstateoftheNFA• {{1},{1,2},{1,3},{1,2,3}}
2 3
1
a,b
ab
a
ɛ
9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 12
Example:ConvertanNFAtoaDFA
Nowletsworkonsomeofthetransitions– Let’slookatstate2inNFAandcompletethetransitionsforstate2intheDFA
• Wheredowegofromstate2onan“a” and“b”?– On“a” tostate2and3andon“b” tostate3
• Sowhatstatedoes{2}inDFAgotoforaandb?– Onato{2,3}and{3}forb
– Nowletsdostate{3}• On“a” goesto{1,3}andonbgoestoÆ
– Why{1,3}?Becausefirstgoesto1thenε permitsamovebackto3!
2 3
1
a,b
ab
a
ɛ
9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 13
ClosureunderRegularOperations
• WestartedthisbeforeanddiditforUniononly– UnionmuchsimplerusingNFA
• ConcatenationandStarmucheasierusingNFA• SinceDFAsequivalenttoNFAs,wecannowjustuseNFAs
• Fewerstatestokeeptrackofbecausewecanactasifwealways“guess” correctly
9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 14
Whydowecareaboutclosure?
Weneedtolookahead– AregularlanguageiswhataDFA/NFAaccepts– Wearenowintroducingregularoperatorsandthenwillgenerateregularexpressionsfromthem(Ch 1.3)
– WewillwanttoshowthatthelanguageofregularexpressionsisequivalenttothelanguageacceptedbyNFAs/DFAs(i.e.,aregularlanguage)
– Howdoweshowthis?• BasictermsinregularexpressioncangeneratedbyaFA• WecanimplementeachoperatorusingaFAandthecombinationisstillabletoberepresentedusingaFA
9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 15
ClosureUnderUnion
• GiventworegularlanguagesA1 andA2recognizedbytwoNFAsN1 andN2,constructNtorecognizeA1È A2
• HowdoweconstructN?– StartbywritingdownN1 andN2.Nowwhat?– AddanewstartstateandthenhaveittakeεbranchestothestartstatesofN1 andN2
9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 16
ClosureunderConcatenation• GiventworegularlanguagesA1 andA2recognizedbytwoNFAsN1 andN2,constructNtorecognizeA1 × A2
• Howdowedothis?– ThecomplicationisthatwedidnotknowwhentoswitchfromhandlingA1 toA2 sincecanlooponanacceptstate
– SolutionwithNFA:• ConnecteveryacceptstateinN1 toeverystartstateinN2usinganε transition
– DonotremovetransitionsfromacceptstateinN1 backtoN1
9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 17
ClosureunderConcatenation• Given:
– N1 =(Q1,S,δ1,q1,F1)recognizesA1
– N2 =(Q2,S,δ2,q2,F2)recognizesA2
• ConstructN=(Q1È Q2,S,δ,q1,F2)sothatitrecognizesA1× A2
δ1(q,a) qÎ Q1 andqÏ F1δ1(q,a) qÎ F1 anda¹ εδ1(q,a)È{q2} qÎ F1 anda=εδ2(q,a) qÎQ2
δ(q,a)=
9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 18
ClosureunderStar• GivenregularlanguageA1 proveA1*isalsoregular
– Note(ab)*={ε,ab,abab,ababab,...}• Proofbyconstruction
– TakeNFAN1 thatrecognizesA1 andconstructNfromitthatrecognizesA1*
– Howdowedothis?• Addnewε-transitionfromacceptstatestostartstate• Thenmakethestartstatetheacceptstatesothatε isaccepted
– Thisalmostworks,butnotquite.Whatistheproblem?» Mayhavetransitionfromintermediatestatetostartstateandshouldnotacceptonthisloop-back
• Solution:addanew startstatethatisacceptstate,withanε-transitiontotheoriginalstartstateandhaveε-transitionsfromacceptstatestooldstartstate
9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 19
ClosureunderStar
ε
εε
9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 20