cs1010: theory of computation - brown university · 2020. 9. 15. · 2 accepts a 2 –construct m 3...

31
CS1010: Theory of Computation Lorenzo De Stefani Fall 2020 Lecture 1: Deterministic Finite State Automata (DFA)

Upload: others

Post on 19-Feb-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

  • CS1010:TheoryofComputation

    LorenzoDeStefaniFall2020

    Lecture1:DeterministicFiniteStateAutomata(DFA)

  • Outline• WhatisaFiniteStateAutomaton• DFAdefinition• ExampleDFAconstruction• ThelanguageofaDFA• RegularOperations• Closureunderunion• Closureunderconcatenation

    1

    FromSipser Chapter1.1

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani

  • WhatisaComputer?

    • Westartwithasimplecomputationalmodel– Differentmodelswillhavedifferentfeaturesandmaymatcharealcomputerbetterinsomewaysandworseinothers

    – Tradeoffbetweengeneralityandprecision• Ourfirstmodelisthefinitestatemachineorfiniteautomaton– Modelformachineswithfinitememory

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 2

  • FiniteAutomataModelsofcomputerswithextremelylimitedmemory

    – Manysimplecomputershaveextremelylimitedmemories andare,infact,finitestatemachines

    – Canyounameany?• Vendingmachine• Elevator• Thermostat• Automaticdooratsupermarket

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 3

  • AutomaticDoor

    • Whatisthedesiredbehavior?Describetheactionsandthenlistthestates.– Personapproaches,doorshould open– Doorshould stayopenwhilepersongoingthrough– Doorshould shutifnoonenearthedoorway– Statesareopen andclosed

    • Moredetailsaboutautomaticdoor– FrontpadDoorRearPad– Describebehaviornow:

    • Hint:actiondependsnotjustonwhathappens,butwhatstate youarecurrentlyin

    • Ifyouwalkthrough,thedoorshouldstayopenwhileyouareonrearpad• Butifdoorisclosedandsomeonestepsonrearpad,doordoesnotopen

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 4

    Input“signals/actions”

  • AutomaticDoor

    NEITHER FRONT REAR BOTH

    CLOSED CLOSED OPEN CLOSED CLOSED

    OPEN CLOSED OPEN OPEN OPEN

    5Theory of Computation - Fall'20 Lorenzo De Stefani

    CLOSED OPEN

    FRONT

    NEITHER

    REAR,BOTH,NEITHER FRONT,REAR,BOTH

    9/15/20

  • MoreonFiniteAutomata

    • HowmanybitsofdatadoesthisFiniteStatesMachine(FSM)store?– 1bitforstates:openorclosed

    • Whataboutstateinformationforelevators,thermostats,vendingmachines,etc?

    • FSMusedinspeechprocessing,opticalcharacterrecognition,etc.

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 6

  • Afiniteautomaton

    AfiniteautomatonM1 with3states– Statediagram

    • Startstateq0 (enteringarrowfromnostate),• Acceptstateq1 (doublecircle),• Transitions(arrows)

    – M1 willaccept(orrecognize)astringlike“1101”ifitendsinacceptstate.Otherwisethestringisrejected!

    – Canyoudescribeallstrings(i.e.,apropertysharedbyallandonlythestrings)thatthisautomatonwillaccept?

    • Itwillacceptallstringsendingina1andanystringwithanevennumberof0’sfollowingthelast1

    00

    1

    1

    0,1

    q0 q2q1

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 7

  • FormalDefinitionofFiniteAutomata

    Afiniteautomatonisa5-tuple(Q,S,δ,q0,F)– Qisafinitesetcalledstates– S isafinitesetcalledthealphabet– δ:QxS® Qisthetransitionfunction– q0Î Qisthestartstate– FÍ Qisthesetofacceptstates

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 8

  • FormalDefinition

    • M1 =(Q,S,δ,q0,F)– Q=– S =– q0 isthestartstate– F=

    0 1q0 q0 q1q1 q2 q1q2 q1 q1

    0 0

    1

    1

    0,1

    q0 q2q1

    Transitionfunctionδ

    {q0,q1,q2}{0,1}

    {q1}

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 9

  • TheLanguageofanautomaton• ThelanguageofDFAMisthesetAofallstringsacceptedbytheDFAM– L(M)=A– WealsosaythatMrecognizesAorMacceptsA

    • Convention:Macceptsstringsandrecognizesalanguage

    • Attentiontoquantifiers:a machinemayacceptmanystrings,butonlyonelanguage

    • Multiple automatacanrecognizethesame language,butanautomatononly recognizesasingle language

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 10

  • WhatistheLanguageofM1?

    • L(M1)=AorM1 recognizesA• WhatisA?

    – A={w|…….}– A={w|wcontainsatleastone1andanevennumberof0’sfollowsthelast1}

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 11

  • WhatistheLanguageofM2?

    • M2 ={{q0,q1},{0,1},δ,q0,{q1}}– Ileaveδ asanexercise– WhatisthelanguageofM2?

    • L(M2)={w|?}• L(M2)={w|wendsina1}

    0

    1

    q0

    1

    0

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 12

    q1

  • WhatistheLanguageofM3?

    • M3 isM2 withadifferentacceptstate• WhatisthelanguageofM3?

    – L(M3)={w|?}– L(M3)={w|wendsin0}[Notquiteright!Why?]– L(M3)={w|wistheemptystringε orendsin0}

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 13

    1

    0

    q1q0

    0 1

  • WhatistheLanguageofM4?WhatlanguagedoesM4 recognize?

    s

    r1

    r2

    q1

    q2

    Figure1.12onpage38

    a b b

    bb

    b

    a

    a a

    a

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 14

  • WhatistheLanguageofM4?

    WhatlanguagedoesM4 recognize?– M4 acceptsallstringsthatstartandendwith“a”orstartandendwith“b”

    – Moresimply,languageL(M4)isthesetofallstringstartingandendingwiththesamesymbol

    • Notethatlength1isallowed!

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 15

  • ConstructM5 todoModulo-3Arithmetic

    • Alphabetå ={RESET,0,1,2}• ConstructM5 toacceptastringonlyifthesumofeachinputsymbolmodulo3is0andRESETsetsthesumbackto0(1.13,page39)– ({q0,q1,q2},{0,1,2,RESET},δi,q0,{q0})

    • δi(qj,0)=qj• δi(qj,1)=qk wherek=j+1modulo3• δi(qj,2)=qk wherek=j+2modulo3• δi(qj,RESET)=qo

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 16

  • NowGeneralizeM5• GeneralizeM5 toacceptifsumofsymbolsisamultipleofi insteadof3– ({q0,q1,q2,q3,…,qi-1},{0,1,2,RESET},δi,q0,{q0})

    • δi(qj,0)=qj• δi(qj,1)=qk wherek=j+1moduloi• δi(qj,2)=qk wherek=j+2moduloi• δi(qj,RESET)=qo

    • Note:aslongasi isfinite,weonlyneedfinitememory (log#ofstates)

    • Couldyougeneralizeonå ={1,2,3,…k}?– Setofstateswouldnotchange– Moretransitions!

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 17

  • FormalDefinitionofAccept

    • LetM=(Q,S,δ,q0,F)beafiniteautomatonandletw=w1w2 …wn beastringwherewi Î å

    • ThenMacceptswifthereexistsasequenceofstatesr0,r1,…,rn inQsuchthat:– r0=q0– δ(ri,wi+1)=ri+1,fori =0,1,…,n-1– rn Î F

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

  • RegularLanguages

    Definition: Alanguageiscalledaregularlanguage ifthereexistsafiniteautomatonwhichrecognizesit• Thatis,ifthereexistsafiniteautomatonthatacceptsallandonly thestringsinthelanguage

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 19

  • DesigningFiniteAutomata• YouwillneedtodesignFAwhichacceptagivenlanguage

    • Strategy:– Determinewhatyouneedtoremember(thestates)

    • E.g.,Howmanystatestodetermineeven/oddnumberof1’sinaninput?

    • Whatdoeseachstaterepresent– Setthestartandacceptstatesbasedonwhateachstaterepresents

    – Assignthetransitions– Checkyoursolution:itshouldacceptwÎ LandnotacceptwnotinL

    – Becarefulabouttheemptystring

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 20

  • DesigningFAs

    • DesignaFAtoacceptthelanguageofbinarystringswherethenumberof1’sisodd,zerocountsaseven(page43)

    • DesignaFAtoacceptallstringwith001asasubstring(page44)

    • DesignaFAtoacceptastringwithsubstringabab

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 21

    ALWAYSstartbyasking:“WhatdoIneedtoremember?”

  • RegularOperations

    LetAandBbelanguages.Wedefine3regularoperations:• Union:AÈ B={x|xÎAorxÎB}• Concatenation:A× Bwhere{xy|xÎAandyÎB}• Star:A* ={x1x2 ….xk|k≥0andeachxi Î A}

    – Starisaunaryoperatoronasinglelanguage– Starrepeatsastring0ormoretimes

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 22

  • ExamplesofRegularOperations

    • LetA={0,1}andB={c,d}• Then:

    – AU B=– A× B=– A*=

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 23

    {0,1,c,d}{0c,0d,1c,1d}

    {ε,0,1,00,01,10,11,000,…}

  • Closure

    • Acollectionofobjectsisclosedunderanoperation ifapplyingthatoperationtomembersofthecollectionreturnsanobjectinthecollection

    • E.g.,Thesetofnaturalnumbersisclosedunderadditionandmultiplication(butnotdivisionandsubtraction)

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 24

  • ClosureforRegularLanguages

    • Regularlanguagesareclosedundertheregularoperatorsunion,concatenationandstar

    • Whywecare?– Iftheseoperatorsareclosed,then,ifwecanimplementeachoperatorusingaFA,wecanalsobuildaFAtorecognizeanyoftheircombinations

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 25

  • ClosureofUnionTheorem1.25: Theclassofregularlanguagesisclosedundertheunionoperation• IfA1 andA2 areregularlanguagesthensoisA1È A2• Howcanweprovethis?Useproofbyconstruction:

    – AssumeM1 acceptsA1 andM2 acceptsA2– ConstructM3 usingM1 andM2 toacceptA1È A2– Weneedtosimulate M1 andM2 runninginparallel andstopifeitherreachesanacceptstate

    • Thislastpartisfeasiblesincewecanhavemultipleacceptstates• Youneedtoremember whereyouwouldbeinbothmachines

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 26

  • ClosureofUnion• YouneedtogenerateastatetorepresentthestateyouwouldbeinforbothM1 andM2

    • LetM1=(Q1,S,δ1,q1,F1)andM2=(Q2,S,δ2,q2,F2)• BuildM3=(Q,S, δ,q0,F)asfollows:

    – Q={(r1,r2)|r1 Î Q1 andr2 Î Q2 }(CartesianproductQ=Q1xQ2)• Careful:orderof(r1,r2)doesnotmatter!• Morecorrect:QisthesetofunorderedpairssuchthatonestateisinQ1 andtheotherisinQ2

    – S =S1È S2– q0 isthepair(q1,q2)– F={(r1,r2)|r1 Î F1 orr2 Î F2}– δ((r1,r2),a)=(δ(r1,a),δ2(r2,a))

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 27

  • ClosureofConcatenationTheorem1.26:Theclassofregularlanguagesisclosedundertheconcatenationoperator

    – IfA1 andA2 areregularlanguagesthensoisA1 × A2– CanweconstructaDFAwhichrecognizesA1 × A2?– Canyouseehowtodothissimply?

    • SinceA1 andA2 areregular,thereexistsaDFAM1 whichrecognizesA1andM2 whichrecognizesA2

    • CombiningM1 andM2 isnottrivial:– WecannotjustconcatenateM1 andM2,wherestartstateofM2becomethefinishstatesofM1

    – Becausewedonotacceptastringassoonasitentersthefinishstate(waituntilstringisdone)itcanleaveandcomeback

    – ThuswedonotknowwhentostartusingM2;ifwemakethewrongchoicewillnotacceptastringthatcanbeaccepted

    – ThisproofiseasyifwehaveNondeterministicFA(NFA)

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 28

  • Concatenation:SimpleExample

    Concatenationofthefollowing:– L(M1)=A,whereS ={0,1}andA=binarystringwithexactly2“1”’s

    – L(M2)=B,whereS ={0,1}andB=binarystringwithexactly3“1”’s

    • M1 willenteracceptstateassoonassees2“1”’s.• Itcanthenloopbackonany“0”’sormove toM2withoutissue.

    • ItcanmoveimmediatelytoM2 ona1,andnothaveanissuesinceitcannotloopback

    • SinceAincludesonlystringswithexactly 2“1”’s.OnceinM2 everythingwillworkokay.

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 29

  • Concatenation:HardExample• L(M1)=A,whereS ={0,1}andA=binarystringwithatleast2“1”’s• L(M2)=B,whereS ={0,1}andB=binarystringwithexactly 2“1”’s• Previousconstructiondoesnotwork(buteasywithNFAormore

    complicatedDFA)– IfwhileinM1 andsee2“1”’s,enteracceptstate.Whenseeanother1,

    havechoicetoloopbackintoacceptstateinM1,orstartmovingintoM2,tothestatethatrepresentssawfirst1forstringinB.

    • Iftheconcatenatedstringhasexactly4“1”’stotal,thenwillonlyacceptifmoveintoM2 asearlyaspossible(afterseeingthefirst21’s)

    • Iftheconcatenatedstringhasmorethan4“1”’s,thenwillonlyacceptifloopinM1 acceptstateuntilonly2“1”’sleft.

    9/15/20 Theory of Computation - Fall'20 Lorenzo De Stefani 30