GraphDistance[g,s,t]
gives the distance from source vertex s to target vertex t in the graph g.
GraphDistance[g,s]
gives the distance from s to all vertices of the graph g.
GraphDistance[{vw,…},…]
uses rules vw to specify the graph g.
GraphDistance
GraphDistance[g,s,t]
gives the distance from source vertex s to target vertex t in the graph g.
GraphDistance[g,s]
gives the distance from s to all vertices of the graph g.
GraphDistance[{vw,…},…]
uses rules vw to specify the graph g.
Details and Options
- GraphDistance is also known as geodesic distance.
- GraphDistance[g,s,t] will give the length of the shortest path between s and t.
- The distance is Infinity when there is no path between s and t.
- For a weighted graph, the distance is the minimum of the sum of weights along any path between s and t.
- The following options can be given:
-
Method Automatic method to use - Possible Method settings include "Dijkstra", "BellmanFord", and "UnitWeight".
Examples
open all close allBasic Examples (1)
Scope (7)
GraphDistance works with undirected graphs:
GraphDistance[[image], 2, 7]GraphDistance[[image], 1, 4]GraphDistance[[image], 2, 3]GraphDistance[[image], 1, 4]GraphDistance[[image], 1, 4]Use rules to specify the graph:
GraphDistance[{1 -> 2, 2 -> 3, 3 -> 1, 3 -> 4, 4 -> 5, 3 -> 5}, 1, 4]GraphDistance works with large graphs:
g = GridGraph[{10, 10, 10, 10}];Timing[GraphDistance[g, 1, 10]]Options (4)
Method (4)
The method is automatically chosen, depending on input:
{PetersenGraph[4, 1, VertexSize -> {1 -> Medium, 7 -> Medium}, PlotLabel -> "Unweighted"], PetersenGraph[4, 1, EdgeWeight -> {4, 0, 3, 1, 3, 2, 7, 8, 5, 2, 1, 6}, VertexSize -> {1 -> Medium, 7 -> Medium}, PlotLabel -> "Weighted"]}Table[GraphDistance[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}]GraphDistance[%, 1, 5, Method -> "UnitWeight"]"Dijkstra" can be used for graphs with positive edge weights only:
GridGraph[{2, 3}, EdgeWeight -> {2, 5, 2, 1, 5, 2, 2}, VertexSize -> {1 -> Small, 5 -> Small}]GraphDistance[%, 1, 5, Method -> "Dijkstra"]"BellmanFord" can be used for directed graphs, including negative edge weights:
WeightedAdjacencyGraph[(| | | | | | |
| - | - | -- | - | - | - |
| ∞ | 2 | 5 | ∞ | ∞ | ∞ |
| ∞ | ∞ | ∞ | 2 | ∞ | ∞ |
| ∞ | ∞ | ∞ | ∞ | 5 | ∞ |
| ∞ | ∞ | -2 | ∞ | ∞ | 2 |
| ∞ | ∞ | ∞ | ∞ | ∞ | ∞ |
| ∞ | ∞ | ∞ | ∞ | 2 | ∞ |), VertexSize -> {1 -> Small, 5 -> Small}, AbsoluteOptions[GridGraph[{2, 3}], VertexCoordinates]]GraphDistance[%, 1, 5, Method -> "BellmanFord"]Applications (5)
Find the distance between opposite corners of a GridGraph of size {6,6}:
GridGraph[{6, 6}, VertexSize -> {1 -> Medium, 6^2 -> Medium}]GraphDistance[%, 1, 6^2]Find the distance between opposite corners in a
-dimensional GridGraph of size {6,6,…,6}:
TableForm[Table[{d, GraphDistance[GridGraph[ConstantArray[6, {d}]], 1, 6^d]}, {d, 2, 6}], TableHeadings -> {None, {"Dimension", "Distance"}}]Visualize distance from a vertex in a tree:
g = CompleteKaryTree[6, 2, VertexSize -> 0.5]Obtain the maximum distance from the vertex to any other vertex:
t = 12;
max = VertexEccentricity[g, t]Set color proportionally to distance:
HighlightGraph[g, Table[Style[s, GrayLevel[GraphDistance[g, s, t] / max]], {s, VertexList[g]}]]The expected distance between two vertices for Bernoulli graphs with probability
is
:
p = 0.9;Mean[GraphDistance[#, 1, 6]& /@ RandomGraph[BernoulliGraphDistribution[9, p], 2000]]//N2 - pIllustrate the DamerauLevenshteinDistance for short words over a small alphabet:
alphabet = {"a", "b", "c"};
maxLen = 4;words = StringJoin@@#& /@ Join@@Table[Tuples[alphabet, i], {i, 1, maxLen}];insertEdits[word_ /; StringLength[word] ≥ maxLen] := {}insertEdits[word_] := Union@@Table[StringInsert[word, e, i], {e, alphabet}, {i, 1, Length[word] + 1}]substituteEdits[word_] := Join@@Table[StringReplacePart[word, e, {i, i}], {i, 1, StringLength[word]}, {e, Select[alphabet, ¬MatchQ[#, StringTake[word, {i}]]&]}]transposeEdits[word_] := Select[Table[StringJoin@@StringTake[word, {{1, i - 1}, {i + 1}, {i}, {i + 2, StringLength[word]}}], {i, 1, StringLength[word] - 1}], # ≠ word&]edges = Union@@Join[Table[#w, {w, Join@@Through[{insertEdits, substituteEdits, transposeEdits}[#]]}]& /@ words, {SameTest -> (Sort[List@@#1] === Sort[List@@#2]&)}];g = Graph[words, edges]Find the Damerau–Levenshtein distance between two words:
GraphDistance[g, "abc", "cba"]% == DamerauLevenshteinDistance["abc", "cba"]Properties & Relations (3)
The distance between two vertices can be found using FindShortestPath:
g = GridGraph[{3, 4}]FindShortestPath[g, 1, 12]Length[%] - 1GraphDistance[g, 1, 12]GraphDistanceMatrix[g][[1, 12]]In a connected graph, the VertexEccentricity can be computed using GraphDistance:
g = GridGraph[{3, 4}]GraphDistance[g, 1, #]& /@ VertexList[g]Max[%]VertexEccentricity[g, 1]The distance between two vertices belonging to different connected components is Infinity:
g = [image];ConnectedGraphQ[g]ConnectedComponents[g]GraphDistance[g, 3, 5]See Also
GraphDistanceMatrix BreadthFirstScan DepthFirstScan
Function Repository: DistanceLayeredGraph
Text
Wolfram Research (2010), GraphDistance, Wolfram Language function, https://reference.wolfram.com/language/ref/GraphDistance.html (updated 2015).
CMS
Wolfram Language. 2010. "GraphDistance." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015. https://reference.wolfram.com/language/ref/GraphDistance.html.
APA
Wolfram Language. (2010). GraphDistance. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/GraphDistance.html
BibTeX
@misc{reference.wolfram_2026_graphdistance, author="Wolfram Research", title="{GraphDistance}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/GraphDistance.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_graphdistance, organization={Wolfram Research}, title={GraphDistance}, year={2015}, url={https://reference.wolfram.com/language/ref/GraphDistance.html}, note=[Accessed: 12-June-2026]}