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.
uses rules vw to specify the graph g.
Details and Options
- 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:
EdgeWeight Automatic weight for each edge Method Automatic method to use
- Possible Method settings include "Dijkstra", "FloydWarshall", and "Johnson".
Examplesopen allclose all
GraphDistanceMatrix works with undirected graphs:
Using GraphDistance to compute the same result takes more time:
GraphDistanceMatrix works with large graphs:
When just a single column is needed and the graph is large, using GraphDistance is faster:
For the strongly connected graph, the result is in agreement with VertexEccentricity:
Properties & Relations (5)
Rows and columns of the distance matrix follow the order given by VertexList:
The distance matrix can be found using GraphDistance:
In a connected graph, the VertexEccentricity can be obtained from the distance matrix:
The distance between two vertices belonging to different connected components is Infinity:
Possible Issues (2)
Solve the problem by listing vertices explicitly when calling functions such as Graph:
"BellmanFord" is not a valid Method option:
Use "FloydWarshall", "Johnson", or the default choice of Method instead:
Neat Examples (1)
Wolfram Research (2010), GraphDistanceMatrix, Wolfram Language function, https://reference.wolfram.com/language/ref/GraphDistanceMatrix.html (updated 2015).
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. Retrieved from https://reference.wolfram.com/language/ref/GraphDistanceMatrix.html