计算机算法分析与设计课程综合实验.docx

上传人:b****7 文档编号:25691818 上传时间:2023-06-11 格式:DOCX 页数:15 大小:52.14KB
下载 相关 举报
计算机算法分析与设计课程综合实验.docx_第1页
第1页 / 共15页
计算机算法分析与设计课程综合实验.docx_第2页
第2页 / 共15页
计算机算法分析与设计课程综合实验.docx_第3页
第3页 / 共15页
计算机算法分析与设计课程综合实验.docx_第4页
第4页 / 共15页
计算机算法分析与设计课程综合实验.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

计算机算法分析与设计课程综合实验.docx

《计算机算法分析与设计课程综合实验.docx》由会员分享,可在线阅读,更多相关《计算机算法分析与设计课程综合实验.docx(15页珍藏版)》请在冰豆网上搜索。

计算机算法分析与设计课程综合实验.docx

计算机算法分析与设计课程综合实验

算法分析与设计课程综合实验

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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 艺术创意

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1