GraphDistanceMatrix

GraphDistanceMatrix[g]
gives a matrix in which the ^(th) entry is the length of a shortest path in g between vertices i and j.


returns a three-dimensional matrix in which the ^(th) entry is the length of a shortest path from i to j and the ^(th) entry is the predecessor of j in a shortest path from i to j.

Details and OptionsDetails and Options

ExamplesExamplesopen allclose all

Basic Examples (1)Basic Examples (1)

In[1]:=
Click for copyable input

This defines a simple directed graph:

In[2]:=
Click for copyable input
In[3]:=
Click for copyable input
Out[3]=

This calculates the distance between the vertices:

In[4]:=
Click for copyable input
Out[4]//MatrixForm=

This shows also the predecessors in the shortest path:

In[5]:=
Click for copyable input
Out[5]//MatrixForm=

The path from 1 to 3 is :

In[6]:=
Click for copyable input
Out[6]=

This confirms the shortest path:

In[7]:=
Click for copyable input
Out[7]=
New to Mathematica? Find your learning path »
Have a question? Ask support »