outlinecs.brown.edu/courses/csci1010/files/doc/notes/lecture-1...outline • what is a finite state...

30
Outline What is a Finite State Automata DFA definition Example DFA construction The language of a DFA Regular Operations Closure under union Closure under concatenation 1 From Sipser Chapter 1.1 9/10/19 Theory of Computation - Fall'19 Lorenzo De Stefani

Upload: others

Post on 16-Feb-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

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

1

FromSipserChapter1.1

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani

Page 2: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

WhatisaComputer?

•  Westartwithacomputationalmodel– Differentmodelswillhavedifferentfeaturesandmaymatcharealcomputerbetterinsomewaysandworseinothers

– Tradeoffbetweengeneralityandprecision•  Ourfirstmodelisthefinitestatemachineorfiniteautomata– Modelformachineswithfinitememory

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 2

Page 3: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

FiniteAutomataModelsofcomputerswithextremelylimitedmemory

– Manysimplecomputershaveextremelylimitedmemoriesandareinfactfinitestatemachines

– Canyounameany?Sareinthisbuildingbuthavenothingspecificallytodowithourdepartment

•  Vendingmachine•  Elevator•  Thermostat•  Automaticdooratsupermarket

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 3

Page 4: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

AutomaticDoor

•  Whatisthedesiredbehavior?Describetheactionsandthenlistthestates.–  Personapproaches,doorshouldopen–  Doorshouldstayopenwhilepersongoingthrough–  Doorshouldshutifnoonenearthedoorway–  Statesareopenandclosed

•  Moredetailsaboutautomaticdoor–  FrontpadDoorRearPad–  Describebehaviornow

•  Hint:actiondependsnotjustonwhathappens,butwhatstateyouarecurrentlyin

•  Ifyouwalkthrough,thedoorshouldstayopenwhileyouareonrearpad•  Butifdoorisclosedandsomeonestepsonrearpad,doordoesnotopen

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 4

Page 5: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

AutomaticDoor

NEITHER FRONT REAR BOTH

CLOSED CLOSED OPEN CLOSED CLOSED

OPEN CLOSED OPEN OPEN OPEN

5 Theory of Computation - Fall'19 Lorenzo De Stefani

Closed Open

FRONT

NEITHER

REAR, BOTH, NEITHER

FRONT, REAR, BOTH

9/10/19

Page 6: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

MoreonFiniteAutomata

•  HowmanybitsofdatadoesthisFSMstore?– 1bit:openorclosed

•  Whataboutstateinformationforelevators,thermostats,vendingmachines,etc?

•  FSMusedinspeechprocessing,opticalcharacterrecognition,etc.

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 6

Page 7: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

Afiniteautomata

AfiniteautomataM1with3states–  Statediagram

•  Startstateq0(enteringarrowfromnostate),•  acceptstateq1(doublecircle),•  andseveraltransitions(arrows)

–  M1willaccept(orrecognize)astringlike“1101”ifitendsinacceptstate.Otherwisethestringisrejected!Whatwillitdo?

–  Canyoudescribeallstringthatthismodelwillaccept?•  Itwillacceptallstringsendingina1andanystringwithanevennumberof0’sfollowingthelast1

0 0

1 1

0,1

q0 q2 q1

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 7

Page 8: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

FormalDefinitionofFiniteAutomata

•  Afiniteautomataisa5-tuple(Q,Σ,δ,q0,F)– Qisafinitesetcalledstates– Σisafinitesetcalledthealphabet– δ:QxΣ→Qisthetransitionfunction– q0∈Qisthestartstate– F⊆Qisthesetofacceptstates

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 8

Page 9: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

FormalDefinition

•  M1=(Q,Σ,δ,q0,F)–  Q=–  Σ=–  q0isthestartstate–  F=

0 1q0 q0 q1q1 q2 q1q2 q1 q1

0 0

1 1

0,1

q0 q2 q1

Transitionfunctionδ

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

{q1}

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 9

Page 10: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

TheLanguageofM1

•  ThelanguageofDFAMisthesetAofallstringsacceptedbytheDFAM– L(M)=A– WealsosaythatMrecognizesAorMacceptsA

•  Amachinemayacceptmanystrings,butonlyonelanguage

•  Convention:Macceptsstringandrecognizesalanguage

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 10

Page 11: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

WhatistheLanguageofM1?

•  L(M1)=AorM1recognizesA•  WhatisA?

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

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 11

Page 12: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

WhatistheLanguageofM2?

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

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

0

1

q0 q1

1

0

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 12

Page 13: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

WhatistheLanguageofM3?

•  M3isM2withdifferentacceptstate•  WhatisthelanguageofM3?

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

0

1

q1

1

0

q0

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 13

Page 14: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

WhatistheLanguageofM4?WhatlanguagedoesM4accept?

s

r1

r2

q1

q2

Figure1.12onpage38

a b

b

b b

b

a

a a

a

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 14

Page 15: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

WhatistheLanguageofM4?

•  WhatdoesM4accept?– Allstringsthatstartandendwithaorstartandendwithb

– Moresimply,languageisallstringstartingandendingwiththesamesymbol

•  Notethatlengthof1isokay

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 15

Page 16: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

ConstructM5todoModuloArithmetic

•  Let∑={RESET,0,1,2}•  ConstructM5toacceptastringonlyifthesumofeachinputsymbolmodulo3is0andRESETsetsthesumbackto0(1.13,page39)

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 16

Page 17: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

NowGeneralizeM5

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

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

•  Note:aslongasiisfinite,weareokayandonlyneedfinitememory(log#ofstates)

•  Couldyougeneralizeon∑={1,2,3,…k}?

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 17

Page 18: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

FormalDefinitionofAccept

•  LetM=(Q,Σ,δ,q0,F)beafiniteautomataandletw=w1w2…wnbeastringwherewi∈∑.

•  ThenMacceptswifasequenceofstatesr0,r1,…,rninQexistswith3conditions–  r0=q0– δ(ri,wi+1)=ri+1,fori=0,1,…,n-1–  rn∈F

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 18

Page 19: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

RegularLanguages

Definition:Alanguageiscalledaregularlanguageifsomefiniteautomatarecognizesit•  Thatis,ifthereexistsafiniteautomatathatrecognizesallandonlythestringsinthelanguage

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 19

Page 20: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

DesigningFiniteAutomata•  YouwillneedtodesignFAstoacceptalanguage•  Strategies

–  Determinewhatyouneedtoremember(thestates)•  Howmanystatestodetermineeven/oddnumberof1’sinaninput?

•  Whatdoeseachstaterepresent–  Setthestartandacceptstatesbasedonwhateachstaterepresents

–  Assignthetransitions–  Checkyoursolution:itshouldacceptw∈LandnotacceptwnotinL

–  Becarefulabouttheemptystring

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 20

Page 21: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

DesigningFAs

•  DesignaFAtoacceptthelanguageofbinarystringswherethenumberof1’sisodd,zerocountsaseven(page43)

•  DesignaFAtoacceptallstringwith001asasubstring(page44)

•  DesignaFAtoacceptastringwithsubstringabab

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 21

ALWAYSstartbyasking:“WhatdoIneedtoremember?”

Page 22: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

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/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 22

Page 23: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

ExamplesofRegularOperations

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

– AUB={0,1,c,d}– A⋅B={0c,0d,1c,1d}– A*={ε,0,1,00,01,10,11,000,…}

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 23

Page 24: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

Closure

•  Acollectionofobjectsisclosedunderanoperationifapplyingthatoperationtomembersofthecollectionreturnsanobjectinthecollection

•  Thesetofnaturalnumbersisclosedunderadditionandmultiplication(butnotdivisionandsubtraction)

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 24

Page 25: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

ClosureforRegularLanguages

•  Regularlanguagesareclosedunderthe3regularoperatorswejustintroduced

•  Whywecare?–  Iftheseoperatorsareclosed,thenifwecanimplementeachoperatorusingaFA,thenwecanbuildaFAtorecognizearegularexpression

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 25

Page 26: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

ClosureofUnionTheorem1.25:Theclassofregularlanguagesisclosedundertheunionoperation•  IfA1andA2areregularlanguagesthensoisA1∪A2

•  Howcanweprovethis?Useproofbyconstruction.– AssumeM1acceptsA1andM2acceptsA2

–  ConstructM3usingM1andM2toacceptA1∪A2

– WeneedtosimulateM1andM2runninginparallelandstopifeitherreachesanacceptstate

•  Thislastpartisfeasiblesincewecanhavemultipleacceptstates

•  Youneedtorememberwhereyouwouldbeinbothmachines

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 26

Page 27: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

ClosureofUnion•  YouneedtogenerateastatetorepresentthestateyouwouldbeinforbothM1andM2

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

–  Q={(r1,r2)|r1∈Q1andr2∈Q2(Cartesianproduct)•  Careful:orderof(r1,r2)doesnotmatter!•  Morecorrect:QisthesetofunorderedpairssuchthatonestateisinQ1andtheotherisinQ2

–  Σ=Σ1∪Σ2

–  q0isthepair(q1,q2)–  F={(r1,r2)|r1∈F1orr2∈F2}–  δ((r1,r2),a)=(δ(r1,a),δ2(r2,a))

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 27

Page 28: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

ClosureofConcatenation

Theorem1.26:Theclassofregularlanguagesisclosedundertheconcatenationoperator

–  IfA1andA2areregularlanguagesthensoisA1⋅A2– Canyouseehowtodothissimply?

•  NottrivialsincecannotjustconcatenateM1andM2,wherestartstatesofM2becomethefinishstatesofM1

–  Becausewedonotacceptastringassoonasitentersthefinishstate(waituntilstringisdone)itcanleaveandcomeback

–  ThuswedonotknowwhentostartusingM2;ifwemakethewrongchoicewillnotacceptastringthatcanbeaccepted

–  ThisproofiseasyifwehavenondeterministicFA

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 28

Page 29: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

Concatenation:SimpleExample

•  Concatenationofthefollowing:–  L(M1)=A,whereΣ={0,1}andA=binarystringwithexactly21’s

–  L(M2)=B,whereΣ={0,1}andB=binarystringwithexactly31’s

•  M1willenteracceptstateassoonassees21’s.Itcanthenloopbackonany0’sormovetoM2withoutissue.ItcanmoveimmediatelytoM2ona1,andnothaveanissuesinceitcannotloopback,sinceAacceptsonlyexactly21’s.OnceinM2everythingwillworkokay.

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 29

Page 30: Outlinecs.brown.edu/courses/csci1010/files/doc/notes/Lecture-1...Outline • What is a Finite State Automata • DFA definition • Example DFA construction • The language of a DFA

Concatenation:HardExample•  L(M1)=A,whereΣ={0,1}andA=binarystringwithatleast21’s•  L(M2)=B,whereΣ={0,1}andB=binarystringwithexactly21’s•  Thisdoesnotwork(buteasywithNFAormorecomplicatedDFA)

–  IfinM1andsee21’s,enteracceptstate.Whenseeanother1,havechoicetoloopbackintoacceptstateinM1,orstartmovingintoM2,tothestatethatrepresentssawfirst1forstringinB.

•  Iftheconcatenatedstringhasexactly41’stotal,thenwillonlyacceptifmoveintoM2asearlyaspossible(afterseeingthefirst21’s)

•  Iftheconcatenatedstringhasmorethan41’s,thenwillonlyacceptifloopinM1acceptstateuntilonly21’sleft.

•  NotethatthegeneralprocedureforputtingM1andM2togetherinvolvessuperimposingthestartstateforM2ontoacceptstateofM1andremovingtheoriginalarcsinM1forthatstate.

9/10/19 Theory of Computation - Fall'19

Lorenzo De Stefani 30