21
CS1010: Theory of Computation Lorenzo De Stefani Fall 2020 Lecture 2: Non-Deterministic Finite States Automata

CS1010: Theory of Computation

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS1010: Theory of Computation

CS1010:TheoryofComputation

LorenzoDeStefaniFall2020

Lecture2:Non-DeterministicFiniteStatesAutomata

Page 2: CS1010: Theory of Computation

Outline• WhatareNon-deterministicFiniteStateAutomata

• NFAconstruction• FormalDefinitionofNFA• EquivalencebetweenNFAsandDFAs• RegularOperations

– Closureunderunion– Closureunderconcatenation– Closureunderstar

1

FromSipser Chapter1.2

9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani

Page 3: CS1010: Theory of Computation

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

Page 4: CS1010: Theory of Computation

HowdoesanNFACompute?• Whenthereisachoice,allpathsarefollowed

– Thinkofitascloning aprocessandcontinuing– Ifthereisnoarrow,thepathterminatesandtheclonedies (itdoesnotacceptifatanacceptstatewhenthathappens)

– AnNFAmayhavetheemptystringcauseatransition– TheNFAacceptsifanypathofthecloneprocessesterminatesinanacceptingstate

– Canalsobemodeledasatreeofpossibilities

• Analternativewayofthinkingofthis– Ateachchoiceyoumakeoneguessofwhichwaytogo– Youmagicallyalwaysguesstherightwaytogo

9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 3

Page 5: CS1010: Theory of Computation

Letsgiveitatry

• Tryout010110– Isitaccepted?

• Yes

• Whatisthelanguage?– Stringscontainingasubstringof101or11

0,1

0,ε1q1 q3q2

1q4

0,1

9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 4

Page 6: CS1010: Theory of Computation

NFAconstruction

ConstructanNFAthatacceptsallstringover{0,1}witha1inthethirdpositionfromtheend

Hint:theNFAstaysinthestartstateuntilitguessesthatitisthreeplacesfromtheend

0,1

0, 11q1 q3

q2

0,1q4

9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 5

Page 7: CS1010: Theory of Computation

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

Page 8: CS1010: Theory of Computation

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

Page 9: CS1010: Theory of Computation

EquivalenceofNFAandDFA

• NFAandDFArecognizesameclassoflanguages• Whatdoesthismean?Whatistheimplication?

– NFAhavenomorepowerthanDFA• Withrespecttowhatcanbeexpressed• EveryNFAhasanequivalentDFA• ButNFAmaymakeiteasiertodescribesomelanguages

– Terminology:Twomachinesareequivalent iftheyrecognizethesamelanguage

9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 8

Page 10: CS1010: Theory of Computation

CompilerEquivalence

• C,C++,Python,Pascal,Fortran,…• Aretheselanguagesequivalent?

– Somearemoresuitedtosometasks,butwithenougheffortanyoftheselanguagescancomputeanythingtheotherscan

– Ifnecessary,youcanevenwriteacompilerforonelanguageusinganother

9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 9

Page 11: CS1010: Theory of Computation

ProofofEquivalenceofNFA&DFA

Proofidea:– SimulateanNFAwithaDFA– WithNFAs,givenaninputwefollowallpossiblebranchesandkeepafingeronthestateforeach

– IntheequivalentDFAweneedtokeeptrackofallthepossiblestateswewouldbeinforeachoftheNFAexecution

– IftheNFAhaskstatesthenithas2k possiblesubsets• EachsubsetcorrespondstooneofthepossibilitiesthattheDFAneedstoremember

• TheDFAwillhave2k states

Theory of Computation - Fall'20 Lorenzo De Stefani 109/18/20

Page 12: CS1010: Theory of Computation

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

Page 13: CS1010: Theory of Computation

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

Page 14: CS1010: Theory of Computation

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

Page 15: CS1010: Theory of Computation

ClosureunderRegularOperations

• WestartedthisbeforeanddiditforUniononly– UnionmuchsimplerusingNFA

• ConcatenationandStarmucheasierusingNFA• SinceDFAsequivalenttoNFAs,wecannowjustuseNFAs

• Fewerstatestokeeptrackofbecausewecanactasifwealways“guess” correctly

9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 14

Page 16: CS1010: Theory of Computation

Whydowecareaboutclosure?

Weneedtolookahead– AregularlanguageiswhataDFA/NFAaccepts– Wearenowintroducingregularoperatorsandthenwillgenerateregularexpressionsfromthem(Ch 1.3)

– WewillwanttoshowthatthelanguageofregularexpressionsisequivalenttothelanguageacceptedbyNFAs/DFAs(i.e.,aregularlanguage)

– Howdoweshowthis?• BasictermsinregularexpressioncangeneratedbyaFA• WecanimplementeachoperatorusingaFAandthecombinationisstillabletoberepresentedusingaFA

9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 15

Page 17: CS1010: Theory of Computation

ClosureUnderUnion

• GiventworegularlanguagesA1 andA2recognizedbytwoNFAsN1 andN2,constructNtorecognizeA1È A2

• HowdoweconstructN?– StartbywritingdownN1 andN2.Nowwhat?– AddanewstartstateandthenhaveittakeεbranchestothestartstatesofN1 andN2

9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 16

Page 18: CS1010: Theory of Computation

ClosureunderConcatenation• GiventworegularlanguagesA1 andA2recognizedbytwoNFAsN1 andN2,constructNtorecognizeA1 × A2

• Howdowedothis?– ThecomplicationisthatwedidnotknowwhentoswitchfromhandlingA1 toA2 sincecanlooponanacceptstate

– SolutionwithNFA:• ConnecteveryacceptstateinN1 toeverystartstateinN2usinganε transition

– DonotremovetransitionsfromacceptstateinN1 backtoN1

9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 17

Page 19: CS1010: Theory of Computation

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

Page 20: CS1010: Theory of Computation

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

Page 21: CS1010: Theory of Computation

ClosureunderStar

ε

εε

9/18/20 TheoryofComputation- Fall'20LorenzoDeStefani 20