You are on page 1of 9

Wanshu(Wayne)Sun

153002432
ProgrammingMethodologyII

1.DjikstrasAlgorithmwilltakea2Darray,anintegersize,andaweightarrayasparameterarguments.
However,itwillnottakeamatrixthathasrows/columnsofsizegreaterthan2^10(1028).Wheneveryoutry
torunmyimplementationwithasizelargerthan1028,asegmentationfaultoccurs.Thisislikelyfromnot
havingenoughmemorytoallocatesuchanarray.SeeProject2_UML.pdfforUML,orseeattachedhard
copy.

2.IchosetheFloydWarshallAlgorithm.FloydWarshallwilltakea2Darrayandaintegersizeasparameter
arguments.However,itwillnottakeamatrixthathasrows/columnsofsizegreaterthan2^9(512).
Wheneveryoutrytorunmyimplementationwithasizelargerthan512,asegmentationfaultoccurs.Thisis
likelyfromnothavingenoughmemorytoallocatesuchanarray.TheFloydWarshalldiffersfromDjikstras
Algorithmgreatlyinthatitalterstheadjacencymatrix,ratherthanaweightarraylikeinDijkstra's.However,
FloydWarshallisverysimilartoDjikstrasinhowitisimplemented/howitdecideswhichpathtochoose.
Thisisbecauseateachiteration,FloydWarshallchecksifitisquickertogofromvertexitovertexjbyfirst
goingtoavertexk.ThisissimilartohowDjikstracomparesweightstoadjusttheweightarrayateach
iteration.UnlikeDjikstrasAlgorithm,FloydWarshallwilladjusttheadjacencymatrixbasedonwhetheror
notitiseasiertogofromitojtok,orjustitoj.Attheendofthealgorithm,FloydWarshallaltersthe
adjacencymatrixsuchthatelement(i,j)containstheweightoftheshortestpathfromvertexitovertexj.See
Project2_UML.pdfforUML,orseeattachedhardcopy.

3.Ichosethevertex0,andthelastvertexinthegraphtobethesourceandnode,respectively.Beloware
screenshotsoftheAverageNumberofHopsandAverageTimeTakenforeachgraphsize.Irandomized
100graphsofsize2^nforn=[6,10]forDjikstrasAlgorithmandn=[6,9]forFloydWarshallAlgorithm.For
whateverreason,whenItriedtoallocateamatrixofasizeof2^11and2^10forDjikstrasand
FloydWarshallrespectively,Iwouldgetasegmentationfault.Thus,myimplementationisnotabletoreach
2^13(8192),andIdidasmuchastheLinuxcomputersintheDSVLabcouldhandle,andalsotrieditonthe
MacsintheLSM.

Djikstra

FloydWarshall

3i.Belowaretablesthatdisplaytheaveragenumberofhopsversusgraphsize.

DjikstrasAlgorithm
GraphSize(NumberofVertices)

AverageNumberofHops(100trialspergraph
size)

64

1.57

128

1.75

256

1.71

512

1.52

1024

1.67

FloydWarshallAlgorithm
GraphSize(NumberofVertices)

AverageNumberofHops(100trialspergraph
size)

64

1.55

128

1.75

256

1.71

512

1.52

BelowisaplotoftheaveragenumberofhopsforDjikstraandFloydWarshall.Notehowbotharevery
similarinvalue.

BelowisabestfitplotforDjikstrasAlgorithmusingMATLABsBasicFittingTool.

BelowisabestfitplotfortheFloydWarshallAlgorithmusingMATLABsBasicFittingTool.

Notehowbothgraphshavesomewhatofasinusoidalshape.Furthermore,therangeofvaluesisbetween
1.5and1.75.Thus,nomatterhowlargethegraphsizeis,thisleadsmetobelievethattheaveragenumber
ofhopswillusuallybebetween1.5and1.75.Thismakessenseaseachadjacencymatrixisrandomly
generatedwithedgeweightsbetween[1,5],and100sforinfinityandvalueswherei=j.Thus,forany
giveniandjindex,thevaluehasabouta5/6chanceofbeingaweightof[1,5],whichmeansthatthereis
abouta5/6chanceofvertexibeingadjacenttovertexj,evenifitisvertex0tothelastvertex(mysource
anddestinationvertices).Therefore,itmakessensethattheaveragenumberofhopsisverysimilarand
between1and2foreverygraph.FromthisIcanmakeastrongpredictionthatevenforvaluesthatmy
implementationcouldnothandle,theaveragenumberofhopswilllikelybebetween1.5and1.75.

Belowarescreenshotsoftheaveragedistance(Icompletelyforgotthatwehadtogettheaveragedistance
too,soIhadtorecompileitwhenIhadalreadydonethesectionabove.Sotheaveragehops/executiontime
maybealittledifferentfromthescreenshotsabove)

Djikstras

FloydWarshall

Belowaretablesthatdisplaytheaveragedistancetogetfromsourcetodestinationversusgraphsize.

Djikstras
GraphSize(NumberofVertices)

AverageDistancefromSourcetoDestination

64

128

1.81

256

1.88

512

1.87

1024

1.8

FloydWarshall

GraphSize(NumberofVertices)

AverageComputationTime(Seconds)

64

128

1.81

256

1.88

512

1.87

BelowisaplotoftheaveragenumberofhopsforDjikstraandFloydWarshall.Notehowtheaverage
distancesarethesame(whichmakessense).

Belowisaplotofabestfitlineforthetwodatasets.

Asonecansee,theaveragedistancesarethesameforbothimplementations.Thiswillnotalwaysbethe
case,butitistruethattheyshouldbeverysimilar.Thisisbecausethegraphsarerandomizedthesame
way,sotheshortestpathshouldhavethesameaveragedistanceaswell.Asonecansee,theaverage
distancewillalwaysbearound2,whichmakessense,asedgeweightsarefrom1to5,andattimesinfinity.

3ii.Belowaretablesthatdisplaytheaveragecomputationtimeversusgraphsize.ComputationTimeformy
implementationisdefinedastheexecutiontimeofthealgorithm.

DjikstrasAlgorithm
GraphSize(NumberofVertices)

AverageComputationTime(Seconds)

64

0.00772862

128

0.0577015

256

0.444136

512

3.46556

1024

27.3302

FloydWarshallAlgorithm
GraphSize(NumberofVertices)

AverageComputationTime(Seconds)

64

0.00164824

128

0.0124669

256

0.097261

512

0.77268

BelowisaplotoftheaveragenumberofhopsforDjikstraandFloydWarshall.NotehowDjikstrasAlgorithm
hasalargercomputationtimeasthesizeofthegraphincreases.

BelowisabestfitplotforDjikstrasAlgorithmusingMATLABsBasicFittingTool.

BelowisabestfitplotforFloydWarshallAlgorithmusingMATLABsBasicFittingTool.

TheplotsclearlyshowthatFloydWarshallisfasterthanDjikstras.However,thisislikelyonlytheactual
computationtimethatisfaster,ratherthantheactualcomplexityofsteps.Thetimecomplexityintermsof
stepsshouldbearoundthesame,asthecomparisonsareverysimilar.However,theactualloopsinthe
FloydWarshallImplementationislikelyjustfasterthanDjikstrastimewise.ForDjikstra,Icouldnotgopasta
graphsizeof2^10=1024vertices.ForFloydWarshall,Icouldnotgopastagraphsizeof2^9=512
vertices.AnytimeIattempttoruntheprogramwithalargergraphsize,asegmentationfaultoccurs.Thisis
likelybecauseIamusingarraysand2Darrays.Thus,with2Darrays,thecomputermustbeabletoallocate
asizeofn^2,wherenisthenumberofvertices.Inaddition,thisoccursoverandoveragain,takingupalot
ofmemoryoverhead.Thus,forDjikstra,thepeakmemoryisa2Darraywithrows/columnsof1024elements
(1048576elementstotal).ForFloydWarshall,thepeakmemoryisa2Darraywithrows/columnsof512
elements(262144elementstotal).Inanattempttosolvethis,Itriedtopassthearraysandmatricesas
references,butthesegmentationfaultsstilloccured.

3iii.Arealworldproblemwhereonewouldhavetoconsidermemoryrequirementsalongwithperformance
iswhenimplementingthealgorithmforaGPS(ex.GoogleMaps).Firstly,thegraphmustbelargeenoughto
spantheroutefromwhereyouaretoyourdestination,ifnotlargeenoughtospantheentireworld.Second,
performancemustbequick,asyoudonotwanttobestuckonabusyhighwaywhileyourGPSisstill
computingyourroute.