Documente Academic
Documente Profesional
Documente Cultură
Syntax
P = shortestpath(G,s,t)
P = shortestpath(G,s,t,'Method',algorithm)
[P,d] = shortestpath(___)
[P,d,edgepath] = shortestpath(___)
Description
example
P = shortestpath(G,s,t) computes the shortest path starting at source node s and ending at
target node t. If the graph is weighted (that is, G.Edges contains a variable Weight), then those
weights are used as the distances along the edges in the graph. Otherwise, all edge distances are
taken to be 1.
example
P = shortestpath(G,s,t,'Method',algorithm) optionally specifies the algorithm to use in
computing the shortest path. For example, if G is a weighted graph,
thenshortestpath(G,s,t,'Method','unweighted') ignores the edge weights in G and instead
treats all edge weights as 1.
example
[P,d] = shortestpath(___) additionally returns the length of the shortest path, d, using any of the
input arguments in previous syntaxes.
example
[P,d,edgepath] = shortestpath(___) additionally returns the edge indices edgepath of all edges
on the shortest path from s to t.
Examples
collapse all
Shortest Path Between Specified Nodes
Create and plot a directed graph.
s = [1 1 2 3 3 4 4 6 6 7 8 7 5];
t = [2 3 4 4 5 5 6 1 8 1 3 2 8];
G = digraph(s,t);
plot(G)
Calculate the shortest path between nodes 7 and 8.
P = shortestpath(G,7,8)
P = 1×5
7 1 3 5 8
3 9 5 7 8
d = 4
Since the edges in the center of the graph have large weights, the shortest path between nodes 3 and
8 goes around the boundary of the graph where the edge weights are smallest. This path has a total
length of 4.
Shortest Path Ignoring Edge Weights
Create and plot a graph with weighted edges, using custom node coordinates.
s = [1 1 1 1 1 2 2 7 7 9 3 3 1 4 10 8 4 5 6 8];
t = [2 3 4 5 7 6 7 5 9 6 6 10 10 10 11 11 8 8 11 9];
weights = [1 1 1 1 3 3 2 4 1 6 2 8 8 9 3 2 10 12 15 16];
G = graph(s,t,weights);
6 3 1 4 8
d = 14
highlight(p,path1,'EdgeColor','g')
Specify Method as unweighted to ignore the edge weights, instead treating all edges as if they had a
weight of 1. This method produces a different path between the nodes, one that previously had too
large of a path length to be the shortest path. Highlight this path in red.
[path2,d] = shortestpath(G,6,8,'Method','unweighted')
path2 = 1×3
6 9 8
d = 2
highlight(p,path2,'EdgeColor','r')
1 2 4 3 5
d = 11
edgepath = 1×4
1 7 9 10
The results indicate that the shortest path has a total length of 11 and follows the edges given
by G.Edges(edgepath,:).
G.Edges(edgepath,:)
ans=4×2 table
EndNodes Weight
________ ______
1 2 2
2 4 3
3 4 1
3 5 5
Highlight this edge path by using the highlight function with the 'Edges' name-value pair to specify
the indices of the edges traversed.
highlight(p,'Edges',edgepath)
Input Arguments
collapse all
G — Input graph
graph object | digraph object
Input graph, specified as either a graph or digraph object. Use graph to create an undirected graph
or digraph to create a directed graph.
Example: G = graph(1,2)
Example: G = digraph([1 2],[2 3])
s — Source node ID
numeric node index | node name
Source node ID, specified as either a numeric node index or a node name.
Example: shortestpath(G,2,5) computes the shortest path between node 2 and node 5.
Example: shortestpath(G,'node1','node2') computes the shortest path between the named
nodes node1 and node2.
t — Target node ID
numeric node index | node name
Target node ID, specified as either a numeric node index or a node name.
Example: shortestpath(G,2,5) computes the shortest path between node 2 and node 5.
Example: shortestpath(G,'node1','node2') computes the shortest path between the named
nodes node1 and node2.
Name-Value Pair Arguments
Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name
and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify
several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.
Example: P = shortestpath(G,s,t,'Method','acyclic')
collapse all
'Method' — Shortest path algorithm
'auto' (default) | 'unweighted' | 'positive' | 'mixed' | 'acyclic'
Shortest path algorithm, specified as the comma-separated pair consisting of 'Method' and one of the
options in the table.
Option Description
While 'mixed' is slower than 'positive' for the same problem, 'mixed' is more versatile as it allows
'acyclic' (only Algorithm designed to improve performance for directed, acyclic graphs (DAGs) with weighted edges.
for digraph)
Use isdag to confirm if a directed graph is acyclic.
Note
For most graphs, 'unweighted' is the fastest algorithm, followed by 'acyclic', 'positive',
and 'mixed'.
Example: shortestpath(G,s,t,'Method','acyclic')
Output Arguments
collapse all
P — Shortest path between nodes
vector of node indices | cell array
Shortest path between nodes, returned as a vector of node indices if s and t are numeric, or as a cell
array of node names if s and t are character vectors. P is empty, {}, if there is no path between the
nodes.
If there are multiple shortest paths between s and t, then P contains only one of the paths. The path
that is returned can change depending on which algorithm Method specifies.
d — Shortest path distance
scalar
Shortest path distance, returned as a numeric scalar. d is the summation of the edge weights between
consecutive nodes in P. If there is no path between the nodes, then d is Inf.
edgepath — Edges on shortest path
vector of edge indices
Edges on shortest path, returned as a vector of edge indices. For multigraphs, this output indicates
which edge between two nodes is on the path. This output is compatible with the 'Edges' name-value
pair of highlight, for example: highlight(p,'Edges',edgepath).
Tips
The shortestpath, shortestpathtree, and distances functions do not support undirected graphs
with negative edge weights, or more generally any graph containing a negative cycle, for these
reasons:
o A negative cycle is a path that leads from a node back to itself, with the sum of the edge weights on
the path being negative. If a negative cycle is on a path between two nodes, then no shortest path
exists between the nodes, since a shorter path can always be found by traversing the negative cycle.
o A single negative edge weight in an undirected graph creates a negative cycle.
See Also
digraph | distances | graph | nearest | shortestpathtree
Introduced in R2015b