WOLFRAM

gives the matrix of distances between vertices for the graph g.

gives the matrix of distances between vertices of maximal distance d in the graph g.

GraphDistanceMatrix[{vw,},]

uses rules vw to specify the graph g.

Details and Options

  • GraphDistanceMatrix is also known as the shortest path matrix.
  • GraphDistanceMatrix returns a SparseArray object or an ordinary matrix.
  • The entries of the distance matrix dij give the shortest distance from vertex vi to vertex vj.
  • The diagonal entries dii of the distance matrix are always zero.
  • The entry dij is Infinity () if there is no path from vertex vi to vertex vj.
  • In GraphDistanceMatrix[g,d], an entry dij will be Infinity if there is no path from vertex vi to vertex vj in d steps or less.
  • The vertices vi are assumed to be in the order given by VertexList[g].
  • For a weighted graph, the distance is the minimum of the sum of weights along any path from vertex vi to vertex vj.
  • The following options can be given:
  • EdgeWeightAutomaticweight for each edge
    Method Automaticmethod to use
  • Possible Method settings include "Dijkstra", "FloydWarshall", and "Johnson".

Examples

open allclose all

Basic Examples  (1)Summary of the most common use cases

Give the distance matrix for a Petersen graph:

Scope  (8)Survey of the scope of standard use cases

GraphDistanceMatrix works with undirected graphs:

Directed graphs:

Weighted graphs:

Multigraphs:

Mixed graphs:

Use rules to specify the graph:

Extract a single matrix column for a graph of modest size:

Out[1]=1

Using GraphDistance to compute the same result takes more time:

Out[2]=2

GraphDistanceMatrix works with large graphs:

Out[2]=2

When just a single column is needed and the graph is large, using GraphDistance is faster:

Out[3]=3

Options  (6)Common values & functionality for each option

Method  (6)

The method is automatically chosen depending on input:

Out[1]=1
Out[2]=2

"UnitWeight" method will use the weight 1 for every edge:

Out[1]=1

"Dijkstra" can be used for graphs with positive edge weights only:

Out[1]=1

"FloydWarshall" can be used for directed graphs including negative edge weights:

Out[1]=1

"Johnson" can be used for directed graphs including negative edge weights:

Out[1]=1

"Johnson" can be faster than the "FloydWarshall" method on sparse graphs:

Out[1]=1

Applications  (2)Sample problems that can be solved with this function

Find the vertex eccentricity, taking the entire graph into account:

Out[6]=6
Out[7]=7

For the strongly connected graph, the result is in agreement with VertexEccentricity:

Out[8]=8

Find the vertex eccentricity of every vertex in a connected graph:

Out[1]=1
Out[2]=2

Check the result:

Out[3]=3

Properties & Relations  (5)Properties of the function, and connections to other functions

Rows and columns of the distance matrix follow the order given by VertexList:

Out[2]=2

The diagonal entries of the distance matrix are always zero:

Out[1]=1
Out[2]=2

The distance matrix can be found using GraphDistance:

Out[1]=1
Out[2]=2

In a connected graph, the VertexEccentricity can be obtained from the distance matrix:

Out[1]=1
Out[2]=2
Out[3]=3

The distance between two vertices belonging to different connected components is Infinity:

Out[2]=2

Possible Issues  (2)Common pitfalls and unexpected behavior

The matrix indices may not have the expected correspondence to vertices:

Out[1]=1

The distance from 2 to 3 is not at the expected matrix position:

Out[3]=3

The reason is that vertices are not in the expected order:

Out[4]=4

Solve the problem by listing vertices explicitly when calling functions such as Graph:

Out[6]=6
Out[7]=7

Now the distance is found at the expected position:

Out[9]=9

"BellmanFord" is not a valid Method option:

Out[1]=1

Use "FloydWarshall", "Johnson", or the default choice of Method instead:

Neat Examples  (1)Surprising or curious use cases

Out[1]=1
Out[2]=2
Wolfram Research (2010), GraphDistanceMatrix, Wolfram Language function, https://reference.wolfram.com/language/ref/GraphDistanceMatrix.html (updated 2015).
Wolfram Research (2010), GraphDistanceMatrix, Wolfram Language function, https://reference.wolfram.com/language/ref/GraphDistanceMatrix.html (updated 2015).

Text

Wolfram Research (2010), GraphDistanceMatrix, Wolfram Language function, https://reference.wolfram.com/language/ref/GraphDistanceMatrix.html (updated 2015).

Wolfram Research (2010), GraphDistanceMatrix, Wolfram Language function, https://reference.wolfram.com/language/ref/GraphDistanceMatrix.html (updated 2015).

CMS

Wolfram Language. 2010. "GraphDistanceMatrix." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015. https://reference.wolfram.com/language/ref/GraphDistanceMatrix.html.

Wolfram Language. 2010. "GraphDistanceMatrix." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015. https://reference.wolfram.com/language/ref/GraphDistanceMatrix.html.

APA

Wolfram Language. (2010). GraphDistanceMatrix. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/GraphDistanceMatrix.html

Wolfram Language. (2010). GraphDistanceMatrix. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/GraphDistanceMatrix.html

BibTeX

@misc{reference.wolfram_2025_graphdistancematrix, author="Wolfram Research", title="{GraphDistanceMatrix}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/GraphDistanceMatrix.html}", note=[Accessed: 31-May-2025 ]}

@misc{reference.wolfram_2025_graphdistancematrix, author="Wolfram Research", title="{GraphDistanceMatrix}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/GraphDistanceMatrix.html}", note=[Accessed: 31-May-2025 ]}

BibLaTeX

@online{reference.wolfram_2025_graphdistancematrix, organization={Wolfram Research}, title={GraphDistanceMatrix}, year={2015}, url={https://reference.wolfram.com/language/ref/GraphDistanceMatrix.html}, note=[Accessed: 31-May-2025 ]}

@online{reference.wolfram_2025_graphdistancematrix, organization={Wolfram Research}, title={GraphDistanceMatrix}, year={2015}, url={https://reference.wolfram.com/language/ref/GraphDistanceMatrix.html}, note=[Accessed: 31-May-2025 ]}