Transcript15.docx
- 文档编号:8820836
- 上传时间:2023-02-01
- 格式:DOCX
- 页数:9
- 大小:25.48KB
Transcript15.docx
《Transcript15.docx》由会员分享,可在线阅读,更多相关《Transcript15.docx(9页珍藏版)》请在冰豆网上搜索。
Transcript15
VideoLectures-Lecture15
Topicscovered:
DynamicProgramming,LongestCommonSubsequence
Instructors:
Prof.ErikDemaine
Prof.CharlesLeiserson
Transcript-Lecture15
So,thetopictodayisdynamicprogramming.Thetermprogramminginthenameofthistermdoesn'trefertocomputerprogramming.OK,programmingisanoldwordthatmeansanytabularmethodforaccomplishingsomething.So,you'llhearaboutlinearprogramminganddynamicprogramming.Eitherofthose,eventhoughwenowincorporatethosealgorithmsincomputerprograms,originallycomputerprogramming,youweregivenadatasheetandyouputonelineperlineofcodeasatabularmethodforgivingthemachineinstructionsastowhattodo.
OK,sothetermprogrammingisolder.Ofcourse,andnowconventionallywhenyouseeprogramming,youmeansoftware,computerprogramming.Butthatwasn'talwaysthecase.Andthesetermscontinueintheliterature.So,dynamicprogrammingisadesigntechniquelikeotherdesigntechniqueswe'veseensuchasdividedandconquer.OK,soit'sawayofsolvingaclassofproblemsratherthanaparticularalgorithmorsomething.So,we'regoingtoworkthroughthisfortheexampleofso-calledlongestcommonsubsequenceproblem,sometimescalledLCS,OK,whichisaproblemthatcomesupinavarietyofcontexts.
Andit'sparticularlyimportantincomputationalbiology,whereyouhavelongDNAstrains,andyou'retryingtofindcommonalitiesbetweentwostrings,OK,onewhichmaybeagenome,andonemaybevarious,whenpeopledo,whatisthatthingcalledwhentheydotheevolutionarycomparisons?
Theevolutionarytrees,yeah,right,yeah,exactly,phylogenetictrees,thereyougo,OK,phylogenetictrees.
Good,sohere'stheproblem.So,you'regiventwosequences,xgoingfromonetom,andyrunningfromoneton.Youwanttofindalongestsequencecommontoboth.OK,andhereIsaya,notthe,althoughit'scommontotalkaboutthelongestcommonsubsequence.Usuallythelongestcommentsubsequenceisn'tunique.Therecouldbeseveraldifferentsubsequencesthattieforthat.However,peopletendto,it'soneofthesloppinessesthatpeoplewillsay.Iwilltrytosaya,unlessit'sunique.ButImayslipaswellbecauseit'sjustsuchacommonthingtojusttalkaboutthe,eventhoughtheremightbemultiple.So,here'sanexample.Supposexisthissequence,andyisthissequence.
So,whatisalongestcommonsubsequenceofthosetwosequences?
Seeifyoucanjusteyeballit.AB:
lengthtwo?
Anybodyhaveonelonger?
Excuseme?
BDB,BDB.BDAB,BDAB,BDAB,anythinglonger?
So,BDAB:
that'sthelongestone.Isthereanotheronethat'sthesamelength?
Isthereanotheronethatties?
BCAB,BCAB,anotherone?
BCBA,yeah,thereareabunchofthemalloflengthfour.Thereisn'toneoflengthfive.OK,weareactuallygoingtocomeupwithanalgorithmthat,ifit'scorrect,we'regoingtoshowit'scorrect,guaranteesthatthereisn'toneoflengthfive.Soallthoseare,wecansay,anyoneoftheseisthelongestcommentsubsequenceofxandy.Wetendtouseitthiswayusingfunctionalnotation,butit'snotafunctionthat'sreallyarelation.
So,we'llsaysomethingisanLCSwhenreallyweonlymeanit'sanelement,ifyouwill,ofthesetoflongestcommonsubsequences.Onceagain,it'sclassicabusivenotation.Aslongasweknowwhatwemean,it'sOKtoabusenotation.Whatwecan'tdoismisuseit.Butabuse,yeah!
Makeitsoit'seasytodealwith.Butyouhavetoknowwhat'sgoingonunderneath.OK,solet'ssee,sothere'safairlysimplebruteforcealgorithmforsolvingthisproblem.
Andthatis,let'sjustcheckevery,maybesomeofyoudidthisinyourheads,subsequenceofxfromonetomtoseeifit'salsoasubsequenceofyofoneton.So,justtakeeverysubsequencethatyoucangethere,checkittoseeifit'sinthere.Solet'sanalyzethat.So,tocheck,soifIgiveyouasubsequenceofx,howlongdoesittakeyoutocheckwhetheritis,infact,asubsequenceofy?
So,IgiveyousomethinglikeBCAB.Howlongdoesittakemetochecktoseeifit'sasubsequenceofy?
Lengthofy,whichisordern.Andhowdoyoudoit?
Yeah,youjustscan.Soasyouhitthefirstcharacterthatmatches,great.Now,ifyouwill,recursivelyseewhetherthesuffixofyourstringmatchesthesuffixofx.OK,andso,youarejustsimplywalkingdownthetreetoseeifitmatches.You'rewalkingdownthestringtoseeifitmatches.OK,thenthesecondthingis,thenhowmanysubsequencesofxarethere?
Twotothen?
xjustgoesfromonetom,twotothemsubsequencesofx,OK,twotothem.Twotothemsubsequencesofx,OK,onewaytoseethat,yousay,well,howmanysubsequencesarethereofsomethingthere?
IfIconsiderabitvectoroflengthm,OK,that'soneorzero,justeverypositionwherethere'saone,Itakeout,thatidentifiesanelementthatI'mgoingtotakeout.OK,thenthatgivesmeamappingfromeachsubsequenceofx,fromeachbitvectortoadifferentsubsequenceofx.Now,ofcourse,youcouldhavematchingcharactersthere,thatintheworstcase,allofthecharactersaredifferent.
OK,andsoeveryoneofthosewillbeauniquesubsequence.So,eachbitvectoroflengthmcorrespondstoasubsequence.That'sagenerallygoodtricktoknow.So,theworst-caserunningtimeofthismethodisorderntimestwotothem,whichis,sincemisintheexponent,isexponentialtime.Andthere'satechnicaltermthatweusewhensomethingisexponentialtime.Slow:
good.OK,verygood.OK,slow,OK,sothisisreallybad.Thisistakingalongtimetocrankouthowlongthelongestcommonsubsequenceisbecausethere'ssomanysubsequences.OK,sowe'regoingtonowgothroughaprocessofdevelopingafarmoreefficientalgorithmforthisproblem.OK,andwe'reactuallygoingtogothroughseveralstages.
Thefirstoneistogothroughsimplificationstage.OK,andwhatwe'regoingtodoislookatsimplythelengthofthelongestcommonsequenceofxandy.Andthenwhatwe'lldoisextendthealgorithmtofindthelongestcommonsubsequenceitself.OK,sowe'regoingtolookatthelength.So,simplifytheproblem,ifyouwill,tojusttrytocomputethelength.What'sniceisthelengthisunique.OK,there'sonlygoingtobeonelengththat'sgoingtobethelongest.OK,andwhatwe'lldoisjustfocusontheproblemofcomputingthelength.Andthenwe'lldoiswecanbackupfromthatandfigureoutwhatactuallyisthesubsequencethatrealizesthatlength.
OK,andthatwillbeabigsimplificationbecausewedon'thavetokeeptrackofalotofdifferentpossibilitiesateverystage.Wejusthavetokeeptrackoftheonenumber,whichisthelength.So,it'ssortofreducesittoanumericalproblem.We'lladoptthefollowingnotation.It'sprettystandardnotation,butIjustwant,ifIputabsolutevaluesaroundthestringorasequence,itdenotesthelengthofthesequence,S.OK,sothat'sthefirstthing.Thesecondthingwe'regoingtodois,actually,we'regoingto,whichtakesalotmoreinsightwhenyoucomeupwithaproblemlikethis,
andinsomesense,endsupbeingthehardestpartofdesigningagooddynamicprogrammingalgorithmfromanyproblem,whichiswe'regoingtoactuallylooknotatallsubsequencesofxandy,butjustprefixes.OK,we'rejustgoingtolookatprefixesandwe'regoingtoshowhowwecanexpressthelengthofthelongestcommonsubsequenceofprefixesintermsofeachother.Inparticular,we'regoingtodefinecofijtobethelength,thelongestcommonsubsequenceoftheprefixofxgoingfromonetoi,andyofgoingtoonetoj.
Andwhatwearegoingtodoiswe'regoingtocalculatec[i,j]forallij.Andifwedothat,howthendowesolvetheproblemofthelongestcommonofsequenceofxandy?
Howdowesolvethelongestcommonsubsequence?
Supposewe'vesolvedthisforallIandj.Howthendowecomputethelengthofthelongestcommonsubsequenceofxandy?
Yeah,c[m,n],that'sall,OK?
Sothen,cofm,nisjustequaltothelongestcommonsubsequenceofxandy,becauseifIgofromoneton,I'mdone,OK?
Andso,it'sgoingtoturnoutthatwhatwewanttodoisfigureouthowtoexpresstoc[m,n],ingeneral,c[i,j],intermsofotherc[i,j].
So,let'sseehowwedothat.OK,soourtheoremisgoingtosaythatc[i,j]isjust--OK,itsaysthatifthei'thcharactermatchesthej'thcharacter,theni'thcharacterofxmatchesthej'thcharacterofy,thencofijisjustcofIminusone,jminusoneplusone.Andiftheydon'tmatch,thenit'seithergoingtobethelongerofc[i,j-1],andc[i-1,j],OK?
Sothat'swhatwe'regoingtoprove.Andthat'sgoingtogiveusawayofrelatingthecalculationofagivenc[i,j]tovaluesthatarestrictlysmaller,OK,thatisatleastoneoftheargumentsissmallerofthetwoarguments.OK,andthat'sgoingtogiveusawayofbeingable,then,tounderstandhowtocalculatec[i,j].
So,let'sprovethistheorem.So,we'llstartwithacasex[i]equalsyofj.Andso,let'sdrawapicturehere.So,wehavexhere.Andhereisy.OK,sohere'smysequence,x,whichI'msortofdrawingasthiselongatedbox,sequencey,andI'msayingthatx[i]andy[j],thoseareequal.OK,solet'sseewhatthatmeans.OK,solet'sletzofonetokbe,infact,thelongestcommonsubsequenceofxofonetoi,yofonetoj,wherecofijisequaltok.
OK,sothelongestcommonsubsequenceofxandyofonetoIandyofonetojhassomevalue.Let'scallitk.Andso,let'ssaythatwehavesomesequencewhichrealizesthat.OK,we'llcallitz.OK,sothen,cansomebodytellmewhatzofkis?
Whatiszofkhere?
Yeah,it'sactuallyequaltoxofI,whichisalsoequaltoyofj?
Whyisthat?
Whycouldn'titbesomeothervalue?
Yeah,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Transcript15