GraphTheory
ShortestPath
find a shortest path between two vertices
Calling Sequence
Parameters
Options
Description
Examples
Compatibility
ShortestPath(G, u, v)
G
-
graph
u, v
vertices of the graph
opts
(optional) one or more options as specified below
avoidvertices=list or set
Specifies vertices to avoid in building a path. The default is the empty set.
If vertices are specified here, the resulting path may not be shortest in G.
distance=truefalse
Specifies whether to return the distance along with the shortest path.
If true, the output of ShortestPath will be a two-element list, the first element of which is the path and the second of which is the weighted path distance.
ignoreweights=truefalse
Specifies whether to ignore edge weights if present. The default is false.
ShortestPath(G, u, v) returns a shortest path from u to v in G using a breadth-first search. The output is a list of vertices in the order they appear on the path. If no such a path exists, an error message is displayed.
If G is undirected, the output is a shortest path between u and v. If G is directed, the output is a shortest directed path from u to v.
If G has weighted edges then the output is a shortest weighted path between u and v computed using either DijkstrasAlgorithm or BellmanFordAlgorithm depending on whether there are negative edge weights. To ignore the edge weights and compute the path with the fewest edges, use the option ignoreweights.
If v is not reachable from u, the empty path will be returned.
withGraphTheory:
C6≔CycleGraph6
C6≔Graph 1: an undirected graph with 6 vertices and 6 edges
IsReachableC6,1,5
true
ShortestPathC6,1,5
1,6,5
W6≔Graph1,2,10,1,6,6,2,3,−10,3,4,10,4,5,−10,5,6,5
W6≔Graph 2: a directed weighted graph with 6 vertices and 6 arcs
DrawGraphW6,layout=circle
ShortestPathW6,1,6
1,2,3,4,5,6
ShortestPathW6,6,1
ShortestPathW6,1,6,ignoreweights
1,6
The ignoreweights option was introduced in Maple 2024.
For more information on Maple 2024 changes, see Updates in Maple 2024.
The GraphTheory[ShortestPath] command was updated in Maple 2025.
The avoidvertices option was introduced in Maple 2025.
For more information on Maple 2025 changes, see Updates in Maple 2025.
See Also
BellmanFordAlgorithm
DijkstrasAlgorithm
Distance
IsReachable
Reachable
Download Help Document