FindShortestPath[g,s,t]
finds the shortest path from source vertex s to target vertex t in the graph g.
FindShortestPath[g,s,All]
generates a ShortestPathFunction[…] that can be applied repeatedly to different t.
FindShortestPath[g,All,t]
generates a ShortestPathFunction[…] that can be applied repeatedly to different s.
generates a ShortestPathFunction[…] that can be applied to different s and t.
FindShortestPath[{vw,…},…]
uses rules vw to specify the graph g.
FindShortestPath
FindShortestPath[g,s,t]
finds the shortest path from source vertex s to target vertex t in the graph g.
FindShortestPath[g,s,All]
generates a ShortestPathFunction[…] that can be applied repeatedly to different t.
FindShortestPath[g,All,t]
generates a ShortestPathFunction[…] that can be applied repeatedly to different s.
generates a ShortestPathFunction[…] that can be applied to different s and t.
FindShortestPath[{vw,…},…]
uses rules vw to specify the graph g.
Details and Options
- FindShortestPath[g,s,t] gives a path from s to t.
- FindShortestPath[g,s] is equivalent to FindShortestPath[g,s,All].
- FindShortestPath[g] is equivalent to FindShortestPath[g,All,All].
- For an unweighted graph, edge length is assumed to be 1.
- For a weighted graph, edge length is taken to be the weight.
- A Method option can also be given. Possible Method settings include:
-
"BellmanFord" supports positive and negative weights "Dijkstra" supports positive weights - FindShortestPath works with undirected graphs, directed graphs, weighted graphs, multigraphs, and mixed graphs.
Examples
open all close allBasic Examples (1)
Scope (9)
Specification (7)
FindShortestPath works with undirected graphs:
GridGraph[{3, 4}, VertexSize -> {2 -> Medium, 7 -> Medium}]HighlightGraph[%, PathGraph@FindShortestPath[%, 2, 7]]g = [image];HighlightGraph[g, PathGraph[FindShortestPath[g, 1, 4], DirectedEdges -> True]]g = [image];HighlightGraph[g, PathGraph[FindShortestPath[g, 1, 4]]]g = [image];HighlightGraph[g, Subgraph[g, FindShortestPath[g, 1, 4]]]WeightedAdjacencyGraph[(| | | | |
| --- | --- | ---- | ---- |
| ∞ | 1.6 | 2 | 1.4 |
| 1.6 | ∞ | 1.9 | ∞ |
| 2 | 1.9 | ∞ | 0.62 |
| 1.4 | ∞ | 0.62 | ∞ |), VertexSize -> {2 -> Small, 4 -> Small}]The shortest path has the smallest total edge weight:
HighlightGraph[%, PathGraph@FindShortestPath[%, 2, 4]]Use rules to specify the graph:
g = {12, 23, 31, 34, 45, 35};HighlightGraph[g, PathGraph[FindShortestPath[g, 1, 4], DirectedEdges -> True]]g = GridGraph[{10, 10, 10, 10}];Timing[FindShortestPath[g, 1, 100]//Length]VertexCount[g]Collection (2)
Find the shortest paths from one vertex to all vertices:
g = GridGraph[{2, 3}, DirectedEdges -> True, EdgeStyle -> Arrowheads[Small], VertexSize -> {1 -> Small}]spf = FindShortestPath[%, 1, All]Apply the ShortestPathFunction to every vertex of the graph:
Table[spf[v], {v, VertexList[g]}]HighlightGraph[g, PathGraph[#, DirectedEdges -> True]]& /@ %Find the shortest paths to one vertex from all vertices:
g = GridGraph[{2, 3}, DirectedEdges -> True, EdgeStyle -> Arrowheads[Small], VertexSize -> {6 -> Small}]spf = FindShortestPath[%, All, 6]Apply the ShortestPathFunction to every vertex of the graph:
Table[spf[v], {v, VertexList[g]}]HighlightGraph[g, PathGraph[#, DirectedEdges -> True]]& /@ %Options (3)
Method (3)
The method is automatically chosen depending on input:
{PetersenGraph[4, 1, VertexSize -> {1 -> Medium, 7 -> Medium}], PetersenGraph[4, 1, EdgeWeight -> {4, 0, 3, 1, 3, 2, 7, 8, 5, 2, 1, 6}, VertexSize -> {1 -> Medium, 7 -> Medium}]}Table[HighlightGraph[g, PathGraph@FindShortestPath[g, 1, 7]], {g, %}]"UnitWeight" method will use the weight 1 for every edge:
GridGraph[{2, 3}, EdgeWeight -> {5, 2, 2, 5, 1, 2, 2}, VertexSize -> {1 -> Small, 5 -> Small}]HighlightGraph[%, PathGraph@FindShortestPath[%, 1, 5, Method -> "UnitWeight"]]"Dijkstra" can be used for graphs with positive edge weights only:
GridGraph[{2, 3}, EdgeWeight -> {5, 2, 2, 5, 1, 2, 2}, VertexSize -> {1 -> Small, 5 -> Small}]HighlightGraph[%, PathGraph@FindShortestPath[%, 1, 5, Method -> "Dijkstra"]]Applications (3)
Find the shortest path in a tree:
CompleteKaryTree[6, 2]HighlightGraph[%, PathGraph@FindShortestPath[%, 12, 57]]Find the shortest path along the seams of a soccer ball:
coords = PolyhedronData["SoccerBall", "Vertices"];
{Graphics3D[Join[{Glow[Gray], PolyhedronData["SoccerBall", "GraphicsComplex"], Red}, Sphere[#, 0.3]& /@ coords[[{19, 30}]]]], g = PolyhedronData["SoccerBall", "Skeleton"]}path = PathGraph@FindShortestPath[g, 19, 30];
{HighlightGraph[g, path], Graphics3D[Join[{Glow[Gray], PolyhedronData["SoccerBall", "GraphicsComplex"], Red}, Sphere[#, 0.3]& /@ coords[[{19, 30}]], {Thick, Red, Line[coords[[VertexList[path]]]]}]]}Create a maze by random depth-first search of a grid graph:
g = GridGraph[{20, 30}]m = Graph[VertexList[g], Module[{stack = {RandomChoice[VertexList[g]]}},
Do[AnnotationValue[{g, v}, "Visited"] = False, {v, VertexList[g]}];
AnnotationValue[{g, First@stack}, "Visited"] = True;
While[Not[stack == {}],
With[{v = First@stack},
With[{adj = Cases[EdgeList[g, v_] /. {vu_ :> u, u_v :> u}, x_ /; ¬AnnotationValue[{g, x}, "Visited"]]},
If[adj == {},
stack = Rest@stack,
With[{w = RandomChoice[adj]},
PrependTo[stack, w];
AnnotationValue[{g, w}, "Visited"] = True;
Sow[vw]]]]]]]//Reap//Last//First, VertexCoordinates -> GraphEmbedding[g]];HighlightGraph[m, PathGraph@FindShortestPath[m, First@VertexList[m], Last@VertexList[m]]]See Also
FindPath FindHamiltonianPath FindShortestTour ShortestPathFunction GraphDistance GraphDistanceMatrix FindMaximumFlow
Function Repository: RegionFindShortestPath FindLongestPath
Related Guides
Text
Wolfram Research (2010), FindShortestPath, Wolfram Language function, https://reference.wolfram.com/language/ref/FindShortestPath.html (updated 2015).
CMS
Wolfram Language. 2010. "FindShortestPath." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015. https://reference.wolfram.com/language/ref/FindShortestPath.html.
APA
Wolfram Language. (2010). FindShortestPath. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/FindShortestPath.html
BibTeX
@misc{reference.wolfram_2026_findshortestpath, author="Wolfram Research", title="{FindShortestPath}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/FindShortestPath.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_findshortestpath, organization={Wolfram Research}, title={FindShortestPath}, year={2015}, url={https://reference.wolfram.com/language/ref/FindShortestPath.html}, note=[Accessed: 12-June-2026]}