This is documentation for Mathematica 8, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)

GraphDistance

GraphDistance
gives the distance from source vertex s to target vertex t in the graph g.
GraphDistance
gives the distance from s to all vertices of the graph g.
  • GraphDistance 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.
Give the distance for a grid graph:
Give the distance for a grid graph:
In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=
GraphDistance works with undirected graphs:
Directed graphs:
Weighted graphs:
The distance between two vertices is the smallest total edge weight along any path:
Works with large graphs:
The method is automatically chosen depending on input:
method will use the weight 1 for every edge:
can be used for graphs with positive edge weights only:
Find the distance between opposite corners of a GridGraph of size :
Find the distance between opposite corners in a -dimensional GridGraph of size :
Visualize distance from a vertex in a tree:
Obtain the maximum distance from the vertex to any other vertex:
Set color proportionally to distance:
The expected distance between two vertices for Bernoulli graphs with probability is :
Illustrate the DamerauLevenshteinDistance for short words over a small alphabet:
Find the Damerau-Levenshtein distance between two words:
Check the result:
The distance between two vertices can be found using FindShortestPath:
Distance matrix:
In a connected graph, the VertexEccentricity can be computed using GraphDistance:
The distance between two vertices belonging to different connected components is Infinity:
New in 8