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

GraphDistance


gives the distance from vertex i to vertex j in the graph g.
  • returns the graph distance from one vertex to another. Infinity is returned if no path exists from i to j. By default every edge is assumed to have an edge weight of 1.
  • The following option can be used:
WeightedFalsespecifies whether edge weight is to be used in calculating distance
This defines a simple directed graph:
This calculates the distance from vertex 1 to vertex 5:
There is no path from vertex 4 to vertex 1:
This defines a simple directed graph with edge weights:
This shows that the distance from vertex 1 to 3 is 2 if unit edge weights are assumed:
But the distance becomes 0 if edge weights are taken into account:
Needs["GraphUtilities`"]
This defines a simple directed graph:
In[2]:=
Click for copyable input
In[3]:=
Click for copyable input
Out[3]=
This calculates the distance from vertex 1 to vertex 5:
In[4]:=
Click for copyable input
Out[4]=
There is no path from vertex 4 to vertex 1:
In[5]:=
Click for copyable input
Out[5]=
This defines a simple directed graph with edge weights:
In[6]:=
Click for copyable input
In[7]:=
Click for copyable input
Out[7]=
This shows that the distance from vertex 1 to 3 is 2 if unit edge weights are assumed:
In[8]:=
Click for copyable input
Out[8]=
But the distance becomes 0 if edge weights are taken into account:
In[9]:=
Click for copyable input
Out[9]=
This generates a random directed graph with n nodes and degree d:
Average distance between vertices 1 and 100, of 200 random directed graphs of 100 vertices and degree 2:
The average distance decreases with an increase in the degree:
This defines a small directed graph:
This shows that the distance between vertices 1 and 3 is 2, assuming a unit edge weight:
This shows that when edge weights are taken into account, the distance between vertices 1 and 3 is 0:
This shows the shortest path when edge weights are taken into account:
This shows the shortest path when edge weights are ignored: