计算机算法分析与设计课程综合实验.docx
《计算机算法分析与设计课程综合实验.docx》由会员分享,可在线阅读,更多相关《计算机算法分析与设计课程综合实验.docx(15页珍藏版)》请在冰豆网上搜索。
计算机算法分析与设计课程综合实验
算法分析与设计课程综合实验
DesignandAnalysisofAlgorithms
1MapRouting
要求:
Mandatory.
实验目的:
ImplementtheclassicDijkstra'sshortestpathalgorithmandoptimizeitformaps.Suchalgorithmsarewidelyusedingeographicinformationsystems(GIS)includingMapQuestandGPS-basedcarnavigationsystems.
实验内容及要求:
Maps.Forthisassignmentwewillbeworkingwithmaps,orgraphswhoseverticesarepointsintheplaneandareconnectedbyedgeswhoseweightsareEuclideandistances.Thinkoftheverticesascitiesandtheedgesasroadsconnectedtothem.Torepresentamapinafile,welistthenumberofverticesandedges,thenlistthevertices(indexfollowedbyitsxandycoordinates),thenlisttheedges(pairsofvertices),andfinallythesourceandsinkvertices.Forexample,Input6representsthemapbelow:
Dijkstra'salgorithm. Dijkstra'salgorithmisaclassicsolutiontotheshortestpathproblem.Itisdescribedinsection24.3inCLRS.Thebasicideaisnotdifficulttounderstand.Wemaintain,foreveryvertexinthegraph,thelengthoftheshortestknownpathfromthesourcetothatvertex,andwemaintaintheselengthsinapriorityqueue.Initially,weputalltheverticesonthequeuewithanartificiallyhighpriorityandthenassignpriority0.0tothesource.Thealgorithmproceedsbytakingthelowest-priorityvertexoffthePQ,thencheckingalltheverticesthatcanbereachedfromthatvertexbyoneedgetoseewhetherthatedgegivesashorterpathtothevertexfromthesourcethantheshortestpreviously-knownpath.Ifso,itlowerstheprioritytoreflectthisnewinformation.
Hereisastep-by-stepdescriptionthatshowshowDijkstra'salgorithmfindstheshortestpath0-1-2-5from0to5intheexampleabove.
process0(0.0)
lower3to3841.9
lower1to1897.4
process1(1897.4)
lower4to3776.2
lower2to2537.7
process2(2537.7)
lower5to6274.0
process4(3776.2)
process3(3841.9)
process5(6274.0)
Thismethodcomputesthelengthoftheshortestpath.Tokeeptrackofthepath,wealsomaintainforeachvertex,itspredecessorontheshortestpathfromthesourcetothatvertex.ThefilesEuclideanGraph.java,Point.java,IndexPQ.java,IntIterator.java,andDijkstra.javaprovideabarebonesimplementationofDijkstra'salgorithmformaps,andyoushouldusethisasastartingpoint.TheclientprogramShortestPath.javasolvesasingleshortestpathproblemandplotstheresultsusingturtlegraphics.TheclientprogramPaths.javasolvesmanyshortestpathproblemsandprintstheshortestpathstostandardoutput.TheclientprogramDistances.javasolvesmanyshortestpathproblemsandprintsonlythedistancestostandardoutput.
Yourgoal. OptimizeDijkstra'salgorithmsothatitcanprocessthousandsofshortestpathqueriesforagivenmap.Onceyoureadin(andoptionallypreprocess)themap,yourprogramshouldsolveshortestpathproblemsinsublineartime.Onemethodwouldbetoprecomputetheshortestpathforallpairsofvertices;howeveryoucannotaffordthequadraticspacerequiredtostoreallofthisinformation.Yourgoalistoreducetheamountofworkinvolvedpershortestpathcomputation,withoutusingexcessivespace.Wesuggestanumberofpotentialideasbelowwhichyoumaychoosetoimplement.Oryoucandevelopandimplementyourownideas.
Idea1. ThenaiveimplementationofDijkstra'salgorithmexaminesallVverticesinthegraph.Anobviousstrategytoreducethenumberofverticesexaminedistostopthesearchassoonasyoudiscovertheshortestpathtothedestination.Withthisapproach,youcanmaketherunningtimepershortestpathqueryproportionaltoE'logV'whereE'andV'arethenumberofedgesandverticesexaminedbyDijkstra'salgorithm.However,thisrequiressomecarebecausejustre-initializingallofthedistancesto∞wouldtaketimeproportionaltoV.Sinceyouaredoingrepeatedqueries,youcanspeedthingsupdramaticallybyonlyre-initializingthosevaluesthatchangedinthepreviousquery.
Idea2. YoucancutdownonthesearchtimefurtherbyexploitingtheEuclideangeometryoftheproblem,asdescribedinsection21.5inthebookofAlgorithminCPartV.Forgeneralgraphs,Dijkstra'srelaxesedgev-wbyupdatingd[w]tothesumofd[v]plusthedistancefromvtow.Formaps,weinsteadupdated[w]tobethesumofd[v]plusthedistancefromvtowplustheEuclideandistancefromwtodminustheEuclideandistancefromvtod.ThisisknownastheA*algorithm.Thisheuristicsaffectsperformance,butnotcorrectness.
Idea3. Useafasterpriorityqueue.Thereissomeroomforoptimizationinthesuppliedpriorityqueue.YoucouldalsoconsiderusingamultiwayheapasinSedgewickProgram20.10.
Testing. Thefileusa.txtcontains87,575intersectionsand121,961roadsinthecontinentalUnitedStates.Thegraphisverysparse-theaveragedegreeis2.8.Yourmaingoalshouldbetoanswershortestpathqueriesquicklyforpairsofverticesonthisnetwork.Youralgorithmwilllikelyperformdifferentlydependingonwhetherthetwoverticesarenearbyorfarapart.Weprovideinputfilesthattestbothcases.Youmayassumethatallofthexandycoordinatesareintegersbetween0and10,000.
实验类型:
Verification.
适用对象:
UndergraduateforComputerSchool
2DocumentDistanceProblem
要求:
Mandatory.
实验目的:
Designandimplementthedocumentdistanceproblemandoptimizeitfordata.
实验内容及要求:
LetDbeatextdocument(e.g.thecompleteworksofWilliamShakespeare).
Awordisaconsecutivesequenceofalphanumericcharacters,suchas"Hamlet"or"2007".We'lltreatallupper-caselettersasiftheyarelower-case,sothat"Hamlet"and"hamlet"arethesameword.Wordsendatanon-alphanumericcharacter,so"can't"containstwowords:
"can"and"t".
ThewordfrequencydistributionofadocumentDisamappingfromwordswtotheirfrequencycount,whichwe'lldenoteasD(w).
WecanviewthefrequencydistributionDasvector,withonecomponentperpossibleword.Eachcomponentwillbeanon-negativeinteger(possiblyzero).
Thenormofthisvectorisdefinedintheusualway:
.
Theinner-productbetweentwovectorsDandD'isdefinedasusual.
.
Finally,theanglebetweentwovectorsDandD'isdefined:
Thisangle(inradians)willbeanumberbetween0and
sincethevectorsarenon-negativeineachcomponent.Clearly,
angle(D,D)=0.0
forallvectorsD,and
angle(D,D')=π/2
ifDandD'havenowordsincommon.
Example:
Theanglebetweenthedocuments"Tobeornottobe"and"Doubttruthtobealiar"is
Wedefinethedistancebetweentwodocumentstobetheanglebetweentheirwordfrequencyvectors.
Thedocumentdistanceproblemisthustheproblemofcomputingthedistancebetweentwogiventextdocuments.
Aninstanceofthedocumentdistanceproblemisthepairofinputtextdocuments.
3EditDistance
要求:
Mandatory.
实验目的:
Manywordprocessorsandkeywordsearchengineshaveaspellingcorrectionfeature.Ifyoutypeinamisspelledwordx,thewordprocessororsearchenginecansuggestacorrectiony.Thecorrectionyshouldbeawordthatisclosetox.Onewaytomeasurethesimilarityinspellingbetweentwotextstringsisby“editdistance”.Thenotionofeditdistanceisusefulinotherfieldsaswell.Forexample,biologistsuseeditdistancetocharacterizethesimilarityofDNAorproteinsequences.
实验内容及要求:
Theeditdistanced(x,y)oftwostringsoftext,x[1..m]andy[1..n],isdefinedtobetheminimumpossiblecostofasequenceof“transformationoperations”(definedbelow)thattransformsstringx[1..m]intostringy[1..n].Todefinetheeffectofthetransformationoperations,weuseanauxiliarystringz[1..s]thatholdstheintermediateresults.Atthebeginningofthetransformationsequences=mandz[1..s]=x[1..m](i.e.,westartwithstringx[1..m]).Attheendofthetransformationsequence,weshouldhaves=nandz[1..s]=y[1..n](i.e.,ourgoalistotransformintostringy[1..n]).Throughoutthetransformation,wemaintainthecurrentlengthsofstringz,aswellasacursorpositioni,i.e.,anindexintostringz.Theinvariant1is+1holdsatalltimesduringthetransformation.(Noticethatthecursorcanmoveonespacebeyondtheendofthestringzinordertoallowinsertionattheendofthestring.)
Eachtransformationoperationmayalterthestringz,thesizes,andthecursorpositioni.Eachtransformationoperationalsohasanassociatedcost.Thecostofasequenceoftransformationoperationsisthesumofthecostsoftheindividualoperationsonthesequence.Thegoaloftheedit-distanceproblemistofindasequenceoftransformationoperationofminimumcostthattransformsx[1..m]intoy[1..n].
Therearefivetransformationoperations:
OperationCostEffect
left0Ifi=1thendonothing.Otherwise,setii-1
right0Ifi=s+1thendonothing.Otherwise,setii-1.
replace4Ifi=s+1thendonothing.Otherwise,replacethecharacterunderthecursorbyanothercharactercbysettingz[i]c,andthenincrementingi.
delete2Ifi=s+1thendonothing.Otherwise,deletethecharactercunderthecursorbysettingz[i..s]z[i+1..s+1]anddecrementings.Thecursorpositionidoesnotchange.
insert3Insertthecharactercintostringzbyincrementings,settingz[i+1..s]z[i..s-1],settingz[i]c,andthenincrementingindexi.
Asanexample,onewaytotransformthesourcestringalgorithmtothetargetstringanalysisistousethesequenceofoperationsshowninTable1,wherethepositionoftheunderlinedcharacterrepresentsthecursorpositioni.ManyothersequencesoftransformationoperationsalsotransformalgorithmtoanalysisthesolutioninTable1isnotuniqueandsomeothersolutions