Anda di halaman 1dari 5

3/2/2017 SolveshortestpathproblemingraphMATLABgraphshortestpathMathWorksIndia

Documentation

graphshortestpath
Solveshortestpathproblemingraph

Syntax
[dist,path,pred]=graphshortestpath(G,S)
[dist,path,pred]=graphshortestpath(G,S,T)

[...]=graphshortestpath(...,'Directed',DirectedValue,...)
[...]=graphshortestpath(...,'Method',MethodValue,...)
[...]=graphshortestpath(...,'Weights',WeightsValue,...)

Arguments
G NbyNsparsematrixthatrepresentsagraph.Nonzeroentriesin
matrixGrepresenttheweightsoftheedges.

S NodeinG.

T NodeinG.

DirectedValue Propertythatindicateswhetherthegraphisdirectedor
undirected.Enterfalseforanundirectedgraph.Thisresultsin
theuppertriangleofthesparsematrixbeingignored.Default
istrue.

MethodValue Charactervectorthatspecifiesthealgorithmusedtofindthe
shortestpath.Choicesare:
'BellmanFord'Assumesweightsoftheedgestobe
nonzeroentriesinsparsematrixG.TimecomplexityisO(N*E),
whereNandEarethenumberofnodesandedges
respectively.
'BFS'Breadthfirstsearch.Assumesallweightstobe
equal,andnonzeroentriesinsparsematrixGtorepresent
edges.TimecomplexityisO(N+E),whereNandEarethe
numberofnodesandedgesrespectively.
'Acyclic'AssumesGtobeadirectedacyclicgraphand
thatweightsoftheedgesarenonzeroentriesinsparse
matrixG.TimecomplexityisO(N+E),whereNandEarethe
numberofnodesandedgesrespectively.
'Dijkstra'Defaultalgorithm.Assumesweightsofthe
edgestobepositivevaluesinsparsematrixG.Time
complexityisO(log(N)*E),whereNandEarethenumberof
nodesandedgesrespectively.

WeightsValue Columnvectorthatspecifiescustomweightsfortheedgesin
matrixG.Itmusthaveoneentryforeverynonzerovalue(edge)in
matrixG.Theorderofthecustomweightsinthevectormust
matchtheorderofthenonzerovaluesinmatrixGwhenitis
traversedcolumnwise.Thispropertyletsyouusezerovalued
weights.Bydefault,graphshortestpathsgetsweight
informationfromthenonzeroentriesinmatrixG.

Description
TipForintroductoryinformationongraphtheoryfunctions,seeGraphTheory
Functions.

[dist,path,pred]=graphshortestpath(G,S)determinesthesinglesource
shortestpathsfromnodeStoallothernodesinthegraphrepresentedbymatrixG.
InputGisanNbyNsparsematrixthatrepresentsagraph.Nonzeroentriesin
matrixGrepresenttheweightsoftheedges.distaretheNdistancesfromthe
sourcetoeverynode(usingInfsfornonreachablenodesand0forthesource
node).pathcontainsthewinningpathstoeverynode.predcontainsthe
predecessornodesofthewinningpaths.

[dist,path,pred]=graphshortestpath(G,S,T)determinesthesingle
sourcesingledestinationshortestpathfromnodeStonodeT.

[...]=graphshortestpath(...,
'PropertyName',PropertyValue,...)callsgraphshortestpathwithoptional
propertiesthatusepropertyname/propertyvaluepairs.Youcanspecifyoneormore
propertiesinanyorder.EachPropertyNamemustbeenclosedinsinglequotesand
iscaseinsensitive.Thesepropertyname/propertyvaluepairsareasfollows:

[...]=
graphshortestpath(...,'Directed',DirectedValue,...)indicateswhether
https://in.mathworks.com/help/bioinfo/ref/graphshortestpath.html 1/5
3/2/2017 SolveshortestpathproblemingraphMATLABgraphshortestpathMathWorksIndia
thegraphisdirectedorundirected.SetDirectedValuetofalseforanundirected
graph.Thisresultsintheuppertriangleofthesparsematrixbeingignored.Default
istrue.

[...]=graphshortestpath(...,'Method',MethodValue,...)letsyou
specifythealgorithmusedtofindtheshortestpath.Choicesare:

'BellmanFord'Assumesweightsoftheedgestobenonzeroentriesin
sparsematrixG.TimecomplexityisO(N*E),whereNandEarethenumberof
nodesandedgesrespectively.
'BFS'Breadthfirstsearch.Assumesallweightstobeequal,andnonzero
entriesinsparsematrixGtorepresentedges.TimecomplexityisO(N+E),
whereNandEarethenumberofnodesandedgesrespectively.
'Acyclic'AssumesGtobeadirectedacyclicgraphandthatweightsofthe
edgesarenonzeroentriesinsparsematrixG.TimecomplexityisO(N+E),
whereNandEarethenumberofnodesandedgesrespectively.
'Dijkstra'Defaultalgorithm.Assumesweightsoftheedgestobepositive
valuesinsparsematrixG.TimecomplexityisO(log(N)*E),whereNandEare
thenumberofnodesandedgesrespectively.

[...]=graphshortestpath(...,'Weights',WeightsValue,...)letsyou
specifycustomweightsfortheedges.WeightsValueisacolumnvectorhavingone
entryforeverynonzerovalue(edge)inmatrixG.Theorderofthecustomweightsin
thevectormustmatchtheorderofthenonzerovaluesinmatrixGwhenitis
traversedcolumnwise.Thispropertyletsyouusezerovaluedweights.By
default,graphshortestpathgetsweightinformationfromthenonzeroentriesin
matrixG.

Examples
FindingtheShortestPathinaDirectedGraph

1.Createandviewadirectedgraphwith6nodesand11edges.
W=[.41.99.51.32.15.45.38.32.36.29.21];
DG=sparse([61223445561],[26354163435],W)

DG=

(4,1)0.4500
(6,2)0.4100
(2,3)0.5100
(5,3)0.3200
(6,3)0.2900
(3,4)0.1500
(5,4)0.3600
(1,5)0.2100
(2,5)0.3200
(1,6)0.9900
(4,6)0.3800

h=view(biograph(DG,[],'ShowWeights','on'))
Biographobjectwith6nodesand11edges.

2.Findtheshortestpathinthegraphfromnode1tonode6.
[dist,path,pred]=graphshortestpath(DG,1,6)

dist=

https://in.mathworks.com/help/bioinfo/ref/graphshortestpath.html 2/5
3/2/2017 SolveshortestpathproblemingraphMATLABgraphshortestpathMathWorksIndia
0.9500

path=

1546

pred=

065514

3.Markthenodesandedgesoftheshortestpathbycoloringthemredand
increasingthelinewidth.
set(h.Nodes(path),'Color',[10.40.4])
edges=getedgesbynodeid(h,get(h.Nodes(path),'ID'));
set(edges,'LineColor',[100])
set(edges,'LineWidth',1.5)

FindingtheShortestPathinanUndirectedGraph

1.Createandviewanundirectedgraphwith6nodesand11edges.
UG=tril(DG+DG')

UG=

(4,1)0.4500
(5,1)0.2100
(6,1)0.9900
(3,2)0.5100
(5,2)0.3200
(6,2)0.4100
(4,3)0.1500
(5,3)0.3200
(6,3)0.2900
(5,4)0.3600
(6,4)0.3800

h=view(biograph(UG,[],'ShowArrows','off','ShowWeights','on'))
Biographobjectwith6nodesand11edges.

https://in.mathworks.com/help/bioinfo/ref/graphshortestpath.html 3/5
3/2/2017 SolveshortestpathproblemingraphMATLABgraphshortestpathMathWorksIndia

2.Findtheshortestpathinthegraphfromnode1tonode6.
[dist,path,pred]=graphshortestpath(UG,1,6,'directed',false)

dist=

0.8200

path=

1536

pred=

055113

3.Markthenodesandedgesoftheshortestpathbycoloringthemredand
increasingthelinewidth.
set(h.Nodes(path),'Color',[10.40.4])
fowEdges=getedgesbynodeid(h,get(h.Nodes(path),'ID'));
revEdges=getedgesbynodeid(h,get(h.Nodes(fliplr(path)),'ID'));
edges=[fowEdges;revEdges];
set(edges,'LineColor',[100])
set(edges,'LineWidth',1.5)

References
[1]Dijkstra,E.W.(1959).Anoteontwoproblemsinconnexionwithgraphs.
NumerischeMathematik1,269271.

[2]Bellman,R.(1958).OnaRoutingProblem.QuarterlyofApplied
Mathematics16(1),8790.

https://in.mathworks.com/help/bioinfo/ref/graphshortestpath.html 4/5
3/2/2017 SolveshortestpathproblemingraphMATLABgraphshortestpathMathWorksIndia
[3]Siek,J.G.,Lee,LQ,andLumsdaine,A.(2002).TheBoostGraphLibraryUser
GuideandReferenceManual,(UpperSaddleRiver,NJ:PearsonEducation)

https://in.mathworks.com/help/bioinfo/ref/graphshortestpath.html 5/5

Anda mungkin juga menyukai