GraphTheory
LongestPath
find a longest path in a directed acyclic graph
Calling Sequence
Parameters
Options
Description
Examples
Compatibility
LongestPath(G, opts)
LongestPath(G, u, v, opts)
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.
If vertices are specified here, the resulting path may not be longest in G.
distance=truefalse
Specifies whether to return the distance along with the shortest path.
If true, each element of the list output of LongestPath will be a two-element list, the first element of which is the path and the second of which is the weighted path distance to the common ancestor.
endvertex=a valid vertex in G
Specifies a final vertex. If provided, the algorithm only considers paths terminating at endvertex.
ignoreweights=truefalse
Specifies whether to ignore edge weights if present. The default is false.
startvertex=a valid vertex in G
Specifies the starting vertex. If provided, the algorithm only considers paths beginning at startvertex.
LongestPath(G) returns a path of maximum length in a directed acyclic graph G. The output is a list of vertices in the order they appear on the path.
If G is not a directed graph or has a directed cycle, an error is returned.
with⁡GraphTheory:
G≔Graph⁡6,1,2,2,3,2,6,3,4,3,5,4,5,5,6
G≔Graph 1: a directed graph with 6 vertices and 7 arcs
DrawGraph⁡G,style=spring
LongestPath⁡G
1,2,3,4,5,6
The GraphTheory[LongestPath] command was introduced in Maple 2025.
For more information on Maple 2025 changes, see Updates in Maple 2025.
See Also
ShortestPath
TopologicSort
Download Help Document