({{, (RefLink[ShortestPathFunction, paclet:ref/ShortestPathFunction])[type,data] 
represents a function that gives the shortest path from a source vertex s to target vertex t in a graph.}})

OpenersNotesSection(Spacer1)Details

(RefLink[ShortestPathFunction, paclet:ref/ShortestPathFunction])[…] is generated by (RefLink[FindShortestPath, paclet:ref/FindShortestPath]).

(RefLink[ShortestPathFunction, paclet:ref/ShortestPathFunction])[{s,(RefLink[All, paclet:ref/All])},…][t] gives the shortest path from s to t.

(RefLink[ShortestPathFunction, paclet:ref/ShortestPathFunction])[{(RefLink[All, paclet:ref/All]),t},…][s] gives the shortest path from s to t.

(RefLink[ShortestPathFunction, paclet:ref/ShortestPathFunction])[{(RefLink[All, paclet:ref/All]),(RefLink[All, paclet:ref/All])},…][s,t] gives the shortest path from s to t.

OpenersPrimaryExamplesSection(Spacer1)Examples  (8)

OpenersExampleSection0(Spacer1)Basic 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]}]

OpenersExampleSection1(Spacer1)Scope  (7)

(RefLink[ShortestPathFunction, paclet:ref/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]}]

Directed graphs:

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]}]

Weighted graphs:

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 (RefLink[ShortestPathFunction, paclet:ref/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]}]

To a fixed vertex:

g=CycleGraph[8,DirectedEdges->True,EdgeStyle->Arrowheads[Small]];

spf=FindShortestPath[g,All,5]

Table[HighlightGraph[g,PathGraph[spf[t],DirectedEdges->True]],{t,VertexList[g]}]

Between any pair of vertices:

g=GridGraph[{4,7},VertexShapeFunction->"Name"]

spf=FindShortestPath[g,All,All]

spf[8,18]

(RefLink[ShortestPathFunction, paclet:ref/ShortestPathFunction]) returns {} when no path exists:

GridGraph[{2,3},DirectedEdges->True,VertexShapeFunction->"Name"]

FindShortestPath[%,3,All]

%[1]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[FindShortestPath, paclet:ref/FindShortestPath])  ▪  (RefLink[GraphDistance, paclet:ref/GraphDistance])  ▪  (RefLink[GraphDistanceMatrix, paclet:ref/GraphDistanceMatrix])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Paths, Cycles, and Flows, paclet:guide/GraphPathsCyclesAndFlows])}})}})