ShortestPathFunction[type,data]
represents a function that gives the shortest path from a source vertex s to target vertex t in a graph.
ShortestPathFunction
ShortestPathFunction[type,data]
represents a function that gives the shortest path from a source vertex s to target vertex t in a graph.
Details
- ShortestPathFunction[…] is generated by FindShortestPath.
- ShortestPathFunction[{s,All},…][t] gives the shortest path from s to t.
- ShortestPathFunction[{All,t},…][s] gives the shortest path from s to t.
- ShortestPathFunction[{All,All},…][s,t] gives the shortest path from s to t.
Examples
open all close allBasic Examples (1)
Obtain a function that gives shortest paths from vertex 4:
g = PetersenGraph[4, 1, VertexSize -> {4 -> Medium}]spf = FindShortestPath[g, 4, All]Use it to show the shortest paths to all vertices:
Table[HighlightGraph[g, PathGraph@spf[v]], {v, VertexList[g]}]Scope (7)
ShortestPathFunction works with undirected graphs:
s = 3;
g = GridGraph[{2, 3}, VertexSize -> {s -> Small}]spf = FindShortestPath[g, s, All]Table[HighlightGraph[g, PathGraph@spf[t]], {t, VertexList[g]}]s = 3;
g = GridGraph[{2, 3}, DirectedEdges -> True, VertexSize -> Small, VertexLabels -> {s -> "s"}, ImagePadding -> 10, EdgeStyle -> Arrowheads[Small]]spf = FindShortestPath[g, s, All]Table[HighlightGraph[g, {PathGraph[spf[t], DirectedEdges -> True], Labeled[t, "t", {Above, Before}]}], {t, VertexList[g]}]s = 1;
g = PetersenGraph[4, 1, EdgeWeight -> {4, 0, 3, 1, 3, 2, 7, 8, 5, 2, 1, 6}, VertexSize -> {s -> Medium}]spf = FindShortestPath[g, s, All]Table[HighlightGraph[g, PathGraph@spf[t]], {t, VertexList[g]}]A ShortestPathFunction can give shortest paths from a fixed vertex:
g = CycleGraph[8, DirectedEdges -> True, EdgeStyle -> Arrowheads[Small]];spf = FindShortestPath[g, 5, All]Table[HighlightGraph[g, PathGraph[spf[t], DirectedEdges -> True]], {t, VertexList[g]}]g = CycleGraph[8, DirectedEdges -> True, EdgeStyle -> Arrowheads[Small]];spf = FindShortestPath[g, All, 5]Table[HighlightGraph[g, PathGraph[spf[t], DirectedEdges -> True]], {t, VertexList[g]}]g = GridGraph[{4, 7}, VertexShapeFunction -> "Name"]spf = FindShortestPath[g, All, All]spf[8, 18]ShortestPathFunction returns {} when no path exists:
GridGraph[{2, 3}, DirectedEdges -> True, VertexShapeFunction -> "Name"]FindShortestPath[%, 3, All]%[1]Related Guides
History
Text
Wolfram Research (2010), ShortestPathFunction, Wolfram Language function, https://reference.wolfram.com/language/ref/ShortestPathFunction.html.
CMS
Wolfram Language. 2010. "ShortestPathFunction." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ShortestPathFunction.html.
APA
Wolfram Language. (2010). ShortestPathFunction. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ShortestPathFunction.html
BibTeX
@misc{reference.wolfram_2026_shortestpathfunction, author="Wolfram Research", title="{ShortestPathFunction}", year="2010", howpublished="\url{https://reference.wolfram.com/language/ref/ShortestPathFunction.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_shortestpathfunction, organization={Wolfram Research}, title={ShortestPathFunction}, year={2010}, url={https://reference.wolfram.com/language/ref/ShortestPathFunction.html}, note=[Accessed: 13-June-2026]}