gives the matrix of distances between vertices for the graph g.
GraphDistanceMatrix[g,d]
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.
GraphDistanceMatrix
gives the matrix of distances between vertices for the graph g.
GraphDistanceMatrix[g,d]
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:
-
EdgeWeight Automatic weight for each edge Method Automatic method to use - Possible Method settings include "Dijkstra", "FloydWarshall", and "Johnson".
Examples
open all close allBasic Examples (1)
Scope (8)
GraphDistanceMatrix works with undirected graphs:
GraphDistanceMatrix[[image]]//MatrixFormGraphDistanceMatrix[[image]]//MatrixFormGraphDistanceMatrix[[image]]//MatrixFormGraphDistanceMatrix[[image]]//MatrixFormGraphDistanceMatrix[[image]]//MatrixFormUse rules to specify the graph:
GraphDistanceMatrix[{1 -> 2, 2 -> 3, 3 -> 1, 3 -> 4, 4 -> 5, 3 -> 5}]//MatrixFormExtract a single matrix column for a graph of modest size:
g = GridGraph[{10, 10}];Timing[GraphDistanceMatrix[g][[All, 1]]//Dimensions]Using GraphDistance to compute the same result takes more time:
Timing[Table[GraphDistance[g, 1, v], {v, VertexList[g]}]//Dimensions]GraphDistanceMatrix works with large graphs:
g = GridGraph[{10, 10, 10, 10}];Timing[GraphDistanceMatrix[g][[All, 1]]//Dimensions]When just a single column is needed and the graph is large, using GraphDistance is faster:
Timing[Table[GraphDistance[g, 1, v], {v, VertexList[g]}]//Dimensions]Options (6)
Method (6)
The method is automatically chosen depending on input:
{PetersenGraph[4, 1, VertexShapeFunction -> "Name"], PetersenGraph[4, 1, EdgeWeight -> {4, 0, 3, 1, 3, 2, 7, 8, 5, 2, 1, 6}, VertexShapeFunction -> "Name"]}Table[GraphDistanceMatrix[g]//MatrixForm, {g, %}]"UnitWeight" method will use the weight 1 for every edge:
GridGraph[{2, 3}, EdgeWeight -> {5, 2, 2, 5, 1, 2, 2}, VertexShapeFunction -> "Name"]GraphDistanceMatrix[%, Method -> "UnitWeight"]//MatrixForm"Dijkstra" can be used for graphs with positive edge weights only:
GridGraph[{2, 3}, EdgeWeight -> {5, 2, 2, 5, 1, 2, 2}, VertexShapeFunction -> "Name"]GraphDistanceMatrix[%, Method -> "Dijkstra"]//MatrixForm"FloydWarshall" can be used for directed graphs including negative edge weights:
WeightedAdjacencyGraph[(| | | | | | |
| - | - | -- | - | - | - |
| ∞ | 2 | 5 | ∞ | ∞ | ∞ |
| ∞ | ∞ | ∞ | 2 | ∞ | ∞ |
| ∞ | ∞ | ∞ | ∞ | 5 | ∞ |
| ∞ | ∞ | -2 | ∞ | ∞ | 2 |
| ∞ | ∞ | ∞ | ∞ | ∞ | ∞ |
| ∞ | ∞ | ∞ | ∞ | 2 | ∞ |), VertexShapeFunction -> "Name", AbsoluteOptions[GridGraph[{2, 3}], VertexCoordinates]]GraphDistanceMatrix[%, Method -> "FloydWarshall"]//MatrixForm"Johnson" can be used for directed graphs including negative edge weights:
WeightedAdjacencyGraph[(| | | | | | |
| - | - | -- | - | - | - |
| ∞ | 2 | 5 | ∞ | ∞ | ∞ |
| ∞ | ∞ | ∞ | 2 | ∞ | ∞ |
| ∞ | ∞ | ∞ | ∞ | 5 | ∞ |
| ∞ | ∞ | -2 | ∞ | ∞ | 2 |
| ∞ | ∞ | ∞ | ∞ | ∞ | ∞ |
| ∞ | ∞ | ∞ | ∞ | 2 | ∞ |), VertexShapeFunction -> "Name", AbsoluteOptions[GridGraph[{2, 3}], VertexCoordinates]]GraphDistanceMatrix[%, Method -> "Johnson"]//MatrixForm"Johnson" can be faster than the "FloydWarshall" method on sparse graphs:
StarGraph[6, EdgeWeight -> RandomReal[{1, 5}, 5]]GraphDistanceMatrix[%, Method -> "Johnson"]//MatrixFormApplications (2)
Find the vertex eccentricity, taking the entire graph into account:
{a = PetersenGraph[7, 2, VertexSize -> {1 -> Medium}], b = Graph[Range[4], {12, 34}, VertexSize -> {1 -> Medium}], c = Graph[Range[3], {12, 32, 31}, VertexSize -> {1 -> Small}, EdgeStyle -> Arrowheads[Small]]}Table[Max[GraphDistanceMatrix[g][[1]]], {g, {a, b, c}}]For the strongly connected graph, the result is in agreement with VertexEccentricity:
Table[VertexEccentricity[g, 1], {g, {a, b, c}}]Find the vertex eccentricity of every vertex in a connected graph:
g = GridGraph[{2, 3, 4}]Max /@ GraphDistanceMatrix[g]% === Table[VertexEccentricity[g, v], {v, VertexList[g]}]Properties & Relations (5)
Rows and columns of the distance matrix follow the order given by VertexList:
g = [image];VertexList[g]TableForm[GraphDistanceMatrix[g], TableHeadings -> {c = Style[#, Red]& /@ %, c}]The diagonal entries of the distance matrix are always zero:
PetersenGraph[5, 2]Diagonal[GraphDistanceMatrix[%]]The distance matrix can be found using GraphDistance:
g = GridGraph[{2, 3}]Table[GraphDistance[g, i, j], {i, VertexList[g]}, {j, VertexList[g]}] == GraphDistanceMatrix[g]In a connected graph, the VertexEccentricity can be obtained from the distance matrix:
g = GridGraph[{3, 4}]GraphDistanceMatrix[g][[VertexIndex[g, 1], All]]//MaxVertexEccentricity[g, 1]The distance between two vertices belonging to different connected components is Infinity:
g = [image];ConnectedGraphQ[g]GraphDistanceMatrix[g]//MatrixFormPossible Issues (2)
The matrix indices may not have the expected correspondence to vertices:
g = Graph[{23, 12}, EdgeWeight -> {1, 10}, VertexShapeFunction -> "Name"](d = GraphDistanceMatrix[g])//MatrixFormThe distance from 2 to 3 is not at the expected matrix position:
GraphDistance[g, 2, 3] == d[[2, 3]]The reason is that vertices are not in the expected order:
VertexList[g]Solve the problem by listing vertices explicitly when calling functions such as Graph:
h = Graph[Range[3], {23, 12}, EdgeWeight -> {1, 10}, VertexShapeFunction -> "Name"]VertexList[h]GraphDistanceMatrix[h]//MatrixFormNow the distance is found at the expected position:
GraphDistance[h, 2, 3] == %[[2, 3]]"BellmanFord" is not a valid Method option:
g = WeightedAdjacencyGraph[(| | | | | | |
| - | - | -- | - | - | - |
| ∞ | 2 | 5 | ∞ | ∞ | ∞ |
| ∞ | ∞ | ∞ | 2 | ∞ | ∞ |
| ∞ | ∞ | ∞ | ∞ | 5 | ∞ |
| ∞ | ∞ | -2 | ∞ | ∞ | 2 |
| ∞ | ∞ | ∞ | ∞ | ∞ | ∞ |
| ∞ | ∞ | ∞ | ∞ | 2 | ∞ |), VertexShapeFunction -> "Name", AbsoluteOptions[GridGraph[{2, 3}], VertexCoordinates]]GraphDistanceMatrix[g, Method -> "BellmanFord"]//MatrixFormUse "FloydWarshall", "Johnson", or the default choice of Method instead:
GraphDistanceMatrix[g, Method -> "FloydWarshall"]//MatrixFormGraphDistanceMatrix[g, Method -> "Johnson"]//MatrixFormGraphDistanceMatrix[g]//MatrixFormRelated Guides
Text
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.
APA
Wolfram Language. (2010). GraphDistanceMatrix. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/GraphDistanceMatrix.html
BibTeX
@misc{reference.wolfram_2026_graphdistancematrix, author="Wolfram Research", title="{GraphDistanceMatrix}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/GraphDistanceMatrix.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_graphdistancematrix, organization={Wolfram Research}, title={GraphDistanceMatrix}, year={2015}, url={https://reference.wolfram.com/language/ref/GraphDistanceMatrix.html}, note=[Accessed: 13-June-2026]}