cs1010: theory of computation - brown university

26
CS1010: Theory of Computation Lorenzo De Stefani Fall 2020 Lecture 8: Turning Machines

Upload: others

Post on 28-Oct-2021

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS1010: Theory of Computation - Brown University

CS1010:TheoryofComputation

LorenzoDeStefaniFall2020

Lecture8:TurningMachines

Page 2: CS1010: Theory of Computation - Brown University

Outline

• WhatareTuringMachines• TuringMachineScheme• FormalDefinition• LanguagesofaTM• Decidability

10/8/20 TheoryofComputation- Fall'20LorenzoDeStefani 1

FromSipser Chapter3.1

Page 3: CS1010: Theory of Computation - Brown University

FA,PDAandTuringMachines• FiniteAutomata:– Modelsfordeviceswithfinite memory

• PushdownAutomata:–Modelsfordeviceswithunlimitedmemory(stack)thatisaccessibleonlyinLast-In-First-Outorder

• TuringMachines (Turing1936)– Usesunlimitedmemoryasaninfinitetapewhichcanberead/writtenandmovedtoleftorright

– Onlymodelthusfarthatcanmodelgeneralpurposecomputers– Church-Turingthesis

– Still,TMcannot solveallproblems

10/8/20 TheoryofComputation- Fall'20LorenzoDeStefani 2

Page 4: CS1010: Theory of Computation - Brown University

TuringMachineScheme

Control

a b a b – – – …

Turingmachinesincludeaninfinitetape– TapeusesitsownalphabetΓ,with– Initiallycontainstheinputstringandblankseverywhereelse

–Machinecanreadandwritefromtapeandmoveleftandrightaftereachaction

–MuchmorepowerfulthanFIFOstackofPDAs

⌃ ⇢ �<latexit sha1_base64="(null)">(null)</latexit><latexit sha1_base64="(null)">(null)</latexit><latexit sha1_base64="(null)">(null)</latexit><latexit sha1_base64="(null)">(null)</latexit>

Tape

Head

10/8/20 TheoryofComputation- Fall'20LorenzoDeStefani 3

Page 5: CS1010: Theory of Computation - Brown University

TuringMachineSchemeControl

a b a b – – – …

Thecontroloperatesasastatemachine– Startsinaninitialstate– Proceedsinaseriesoftransitions triggeredbythesymbolsonthetape(oneatatime)

– Themachinecontinuesuntilitentersanaccept orreject stateatwhichpointitimmediatelyhaltsandoutputs“accept”or“reject”

– NotethisisverydifferentfromFAsandPDAs

10/8/20 TheoryofComputation- Fall'20LorenzoDeStefani 4

Page 6: CS1010: Theory of Computation - Brown University

TuringMachineSchemeControl

a b a b – – – …

• Themachinecanloopforever!– InthiscasewesaythattheTMdoesnothalt foragiveninput

• CanaFAoraPDAloopforever?– NO!itwillterminatewheninputstringisfullyprocessedandwillonlytakeone“action”foreachinputsymbol

10/8/20 TheoryofComputation- Fall'20LorenzoDeStefani 5

Page 7: CS1010: Theory of Computation - Brown University

DesigningTuringMachines

DesignaTMtorecognizethelanguage:B={w#w|wÎ {0,1}*}

– Thinkofaninformaldescription– NOTPALINDROMES– ImaginethatyouarestandingonaninfinitetapewithsymbolsonitandwanttochecktoseeifthestringbelongstoB?• Whatprocedurewouldyouusegiventhatyoucanread/write andmoveonthetapeinbothdirections?

• Youhaveafinitecontrolsocannotremembermuchandthusmustrelyontheinformationonthetape

10/8/20 TheoryofComputation- Fall'20LorenzoDeStefani 6

Page 8: CS1010: Theory of Computation - Brown University

ATuringMachineforB ={w#w|w Î {0,1}*}

• M1loopsandineachiterationitmatchessymbolsoneachsideofthe#– Itreadstheleftmostsymbolremainingandreplacesitwith“x”– Scanstotherightuntilthe“#” andproceedstothefirstnon-xsymbol– Isitthesame?

• Yes!Wehaveamatch!Wegobacktotheleftmostremainingsymbolandrepeat

• No!Wehaveamismatch=(theTMtransitiontoa“reject state”andhalts

– Ifboththesymbolstotherightandtotheleftofthe“#”arexthenwehaveacompletematch!Alsochecksamelength!!!

– TheTMhalts andthestringisaccepted• Isloopingpossible?

– NO! Guaranteedtoterminate/haltsincemakesprogresseachiteration.

10/8/20 TheoryofComputation- Fall'20LorenzoDeStefani 7

Page 9: CS1010: Theory of Computation - Brown University

10/8/20 TheoryofComputation- Fall'20LorenzoDeStefani 8

qstart

q0

q1

q#1

q#0

qrev qrejectqrev2

qalmost

qbegin

0àx,R

#à#,R

0à0,R1à1,R xàx,L

0àx,L

1à1,R

xàx,L

#à#,L

0à0,L

1à1,L

1àx,R

#à#,R

1àx,L 0à0,R

_à_,R

_à_,R

xàx,L

qaccept

0à0,L1à1,L

xàx,R

0àx,R

1àx,R

#à#,R

_à_,R

xàx,R

0à0,R1à1,R

ATuringMachineforB ={w#w|w Î {0,1}*}

Page 10: CS1010: Theory of Computation - Brown University

Conventionsofrepresentation

• Read“aàb,R”as:ifsymbol“a”isreadonthetapethenreplaceitwith“b”andmovetotherightcellonthetape

• Formovingtotheleft wouldbe“aàb,L”• Weassumemissingtransitionsleadtorejectstate,andtheTMhalts

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

Page 11: CS1010: Theory of Computation - Brown University

ExecutionExample

Inputstring011000#011000Thetapeheadisatrightofthecurrentstate

qstart0 11000#011000- -xq1 1 1000#011000- -

…X11000#qrevX 11000- -qrev2X 11000#X11000- -xxq1 1 000#X11000- -

…XXXXXX#XXXXXXqaccept - -

10/8/20 TheoryofComputation- Fall'20LorenzoDeStefani 10

Page 12: CS1010: Theory of Computation - Brown University

FormalDefinitionofaTuringMachine

ATuringMachineisa7-tuple{𝑄, Σ, Γ, 𝛿, 𝑞0, 𝑞𝑎𝑐𝑐𝑒𝑝𝑡, 𝑞𝑟𝑒𝑗𝑒𝑐𝑡}where:• 𝑄 setofstates• Σ istheinputalphabetnotcontainingtheblank• Γ isthetapealphabet,whereblank_ÎΓ andΣÍΓ• 𝛿: 𝑄×Γ → 𝑄×Γ×{𝐿, 𝑅}isthetransitionfunction• 𝑞0, 𝑞𝑎𝑐𝑐𝑒𝑝𝑡, 𝑎𝑛𝑑𝑞𝑟𝑒𝑗𝑒𝑐𝑡arethestart,accept,andrejectstates– Doweneedmorethanonerejectoracceptstate?– No:sinceonceentereithersuchastatetheTMhalts

10/8/20 TheoryofComputation- Fall'20LorenzoDeStefani 11

Page 13: CS1010: Theory of Computation - Brown University

TransitionsintheTM

Thetransitionfunctionδ iskey:– 𝑄×Γ → 𝑄×Γ×{𝐿, 𝑅}• Amachineisinastate𝑞 ∈ 𝑄 andtheheadisoverthetapeatsymbol𝑎 ∈ Γ,thenafterthemoveweareinastate𝑟 ∈ 𝑄 with𝑏 ∈ Γ replacingtheaonthetapeandtheheadhasmovedeitherleft(𝐿) orright(𝑅)

10/8/20 TheoryofComputation- Fall'20LorenzoDeStefani 12

Page 14: CS1010: Theory of Computation - Brown University

Configurationsandtransitions• AtanystepaTMisinsomeconfiguration• Aconfigurationisspecifiedby:– thestate– theposition(i.e.,currentlocationreaderhead)– thesymbol atthecurrentheadlocation

• WesaythataconfigurationC1yields C2ifthereifthetransitionfunction𝛿 allowstogofromC1toC2

10/8/20 TheoryofComputation- Fall'20LorenzoDeStefani 13

Page 15: CS1010: Theory of Computation - Brown University

Typesofconfigurations• Startingconfiguration:instartingstate,headpositionatthebeginningoftheinput– Leftmostpositionofthetapeoccupiedbytheinput

• Acceptingconfiguration:inacceptingstate• Rejectingconfiguration:inrejectingstate• Haltingconfiguration:eitheracceptingorrejectingconfigurations

• Therecanbemultipleaccepting/rejectingconfigs. butasingle accept/reject state

10/8/20 TheoryofComputation- Fall'20LorenzoDeStefani 14

Page 16: CS1010: Theory of Computation - Brown University

TuringRecognizable&DecidableLanguagesThesetofstringsthataTuringMachineMacceptsisthelanguageofM,denotedas𝐿(𝑀), orthelanguagerecognizedbyM– Alanguage𝐿 isTuring-recognizable ifsome Turingmachinerecognizes it• I.e.,ThereexistsaTM𝑀 suchthat𝑀 haltsintheacceptstateforallandonlythestrings𝑠 ∈ 𝐿

• Haltingisnotrequiredfor𝑠 ∉ 𝐿,justnon-acceptance• Turing-recognizablelanguages aresometimesreferredas“recursivelyenumerablelanguages”

• ATMwhichrecognizesalanguage𝐿 iscalledarecognizer for𝐿– ATuringmachinethathaltsonall inputsisadecider.– Adecider thatrecognizes alanguagedecidesit.– AlanguageisTuring-decidable,orsimplydecidable, ifsomeTuringmachinedecidesit.• Sometimesreferredas“recursivelanguage”

10/8/20 TheoryofComputation- Fall'20LorenzoDeStefani 15

Page 17: CS1010: Theory of Computation - Brown University

TuringRecognizable&DecidableLanguages

Remarks:– Alanguageisdecidable ifitisTuring-recognizable andthereexistsaTMdecider forit(i.e.,aTMthatalways halts)

– EverydecidablelanguageisTuring-recognizable• ATMMwhichdecidesalanguagealsorecognizesit• Decidability isstrictlystrongerpropertythanrecognizability

– ItispossibleforaTMtohaltonlyonthosestringsitaccepts!• Itisonlyarecognizernotadecider

10/8/20 TheoryofComputation- Fall'20LorenzoDeStefani 16

Page 18: CS1010: Theory of Computation - Brown University

LimitsofTuringMachines

• Church-Turingthesis:AnythingthatcanbeprogrammedcanbeprogrammedonaTM

• NotalllanguagesareTuringDecidable!– ATM ={<M,w>,MisadescriptionofaTuringMachineTM,wisadescriptionofaninputandTMacceptsw}• WeshallseethisinChapter4• ATM isnotevenTuring-recognizable!

10/8/20 TheoryofComputation- Fall'20LorenzoDeStefani 17

Page 19: CS1010: Theory of Computation - Brown University

TuringMachineExampleDesignaTMM2thatdecidesA={02n|n≥0},thelanguageofallstringsof0swithlength2n.• Withoutdesigningit,doyouthinkthiscanbedone?Why?

– Yes:wecouldwriteaprogramtodoitandthereforeweknowaTMcoulddoitsincewesaidaTMcandoanythingacomputercando

• Howwouldyoudesignit?• Solution:

Idea:divideby2eachtimeandseeifresultisaone1. Sweeplefttorightacrossthetape,crossingoffeveryother0.2. Ifinstep1:

– thetapecontainsexactlyone0,thenaccept– thetapecontainsanoddnumberof0’s,rejectimmediately– Onlyalternativeiseven0’s.Inthiscasereturnheadtostartandloop

backtostep1.

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

Page 20: CS1010: Theory of Computation - Brown University

SampleExecutionofTMM20 000- - Numberis4,whichis22

x 000- -x0x 0- - Nowwehave2,or21

x 0x0- -x0 x0- -xx x0- -xxx0 - - Nowwehave1,or20

x xx0- - Seekbacktostartxxx0 - - Scanright;one0,soaccept

10/8/20 TheoryofComputation- Fall'20LorenzoDeStefani 19

Page 21: CS1010: Theory of Computation - Brown University

TuringMachineExampleII

DesignTMM3todecide thelanguage:C={aibjck|i x j=kandi,j,k≥1}–WhatisthistestingaboutthecapabilityofaTM?• Thatitcando(oratleastcheck)multiplication• Aswehaveseenbefore,weoftenuseunary

– Howwouldyouapproachthis?• Imaginethatweweretrying2x3=6

10/8/20 TheoryofComputation- Fall'20LorenzoDeStefani 20

Page 22: CS1010: Theory of Computation - Brown University

TuringMachineExampleII

Solution:1. Firstscanthestringfromlefttorighttoverifythatitisof

forma+b+c+;ifitis,scantostartoftapeandifnot,reject.2. Crossoffthefirstaandscanuntilthefirstboccurs.Shuttle

betweenb’sandc’scrossingoffoneofeachuntilallb’saregone.Ifallc’shavebeencrossedoffandsomeb’sremain,reject.

3. Restore thecrossedoffb’sandrepeatstep2iftherearea’sremaining.Ifalla’sgone,checkifallc’sarecrossedoff;ifso,accept;elsereject.

*Usedifferentsymbolsforcrossingouttheb’ssothatiseasiertorestorethem=)

10/8/20 TheoryofComputation- Fall'20LorenzoDeStefani 21

Page 23: CS1010: Theory of Computation - Brown University

Transducers

• Sofarwehavealwaystalkedaboutrecognizing alanguage,notgenerating alanguage.Thisiscommoninlanguagetheory.

• Whentalkingaboutcomputationthisseemsstrangeandlimiting.– Computerstypicallytransform inputintooutput– Wearemorelikelytohaveacomputerperformmultiplicationthancheckthattheequationiscorrect.

– TuringMachinescanalsogenerate/transduce– Howwouldyoucomputeck givenaibj andixj =k

• Inasimilarmanner.Foreverya,youscanthroughtheb’sandforeachyougototheendofthestringandaddac.Thusbyzig-zagging atimes,youcangeneratetheappropriatenumberofc’s.

10/8/20 TheoryofComputation- Fall'20LorenzoDeStefani 22

Page 24: CS1010: Theory of Computation - Brown University

TuringMachineExampleIII

Theelementdistinctnessproblem:• Givenalistofstringsoveralphabet{0,1}eachseparatedbya#,acceptifallstringsaredifferent.

• E={#x1#x2#…#xn|each xi Î {0,1}*andxi ≠xjforeachi ≠j}

10/8/20 TheoryofComputation- Fall'20LorenzoDeStefani 23

Page 25: CS1010: Theory of Computation - Brown University

TuringMachineExampleIV• Solution:

1. Placeamarksymbolontopoftheleft-mostsymbol.Ifitwasablank,accept.Ifitwasa#continue;elsereject

2. Scanrighttonext#andplaceamarksymbol onit.Ifno#isencountered,weonlyhadx1soaccept.

3. Byzig-zagging,comparethetwostringtotherightofthetwomarked#s.Iftheyareequal,reject.

4. Movetherightmostofthetwomarkstothenext#symboltotheright.Ifno#symbolisencounteredbeforeablank,movetheleftmostmarktothenext#toitsrightandtherightmostmarktothe#afterthat.Thistime,ifno#isavailablefortherightmostmark,allthestringshavebeencompared,soaccept.

5. Gobacktostep3

10/8/20 TheoryofComputation- Fall'20LorenzoDeStefani 24

Page 26: CS1010: Theory of Computation - Brown University

Decidability• Alloftheseexampleshavebeendecidable,andhenceTuring-recognizable.

• Howdoweknowthattheseexamplesaredecidable?– Ateachiterationprogressismadetowardthegoal,sothegoalitselfisreachable

– Nothardtoproveformally.Forexample,ifthetheinputiscomposedbyn symbolsandasymboliserasedateachiteration,thealgorithmwillfinishaftern iterations

• ShowingthatalanguageisTuringrecognizablebutnotdecidableischallenging

10/8/20 TheoryofComputation- Fall'20LorenzoDeStefani 25