GraphDistanceMatrix
✖
GraphDistanceMatrix
gives the matrix of distances between vertices of maximal distance d in 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 allclose allBasic Examples (1)Summary of the most common use cases
Scope (8)Survey of the scope of standard use cases
GraphDistanceMatrix works with undirected graphs:

https://wolfram.com/xid/0bniox635m7aa-6x5vth


https://wolfram.com/xid/0bniox635m7aa-h8fgmr


https://wolfram.com/xid/0bniox635m7aa-zk71wo


https://wolfram.com/xid/0bniox635m7aa-mqgkub


https://wolfram.com/xid/0bniox635m7aa-zc89h

Use rules to specify the graph:

https://wolfram.com/xid/0bniox635m7aa-bndh30

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

https://wolfram.com/xid/0bniox635m7aa-i1la8a

Using GraphDistance to compute the same result takes more time:

https://wolfram.com/xid/0bniox635m7aa-3crmlo

GraphDistanceMatrix works with large graphs:

https://wolfram.com/xid/0bniox635m7aa-r64pi

https://wolfram.com/xid/0bniox635m7aa-x6vlgs

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

https://wolfram.com/xid/0bniox635m7aa-qteknj

Options (6)Common values & functionality for each option
Method (6)
The method is automatically chosen depending on input:

https://wolfram.com/xid/0bniox635m7aa-bucy2s


https://wolfram.com/xid/0bniox635m7aa-wvbg31

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

https://wolfram.com/xid/0bniox635m7aa-y0asw2


https://wolfram.com/xid/0bniox635m7aa-4i73mg

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

https://wolfram.com/xid/0bniox635m7aa-d7080i


https://wolfram.com/xid/0bniox635m7aa-ifee4a

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

https://wolfram.com/xid/0bniox635m7aa-1m9g73


https://wolfram.com/xid/0bniox635m7aa-fgt3fm

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

https://wolfram.com/xid/0bniox635m7aa-zlgrax


https://wolfram.com/xid/0bniox635m7aa-fmhs37

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

https://wolfram.com/xid/0bniox635m7aa-nzpa6


https://wolfram.com/xid/0bniox635m7aa-i6i3d0

Applications (2)Sample problems that can be solved with this function
Find the vertex eccentricity, taking the entire graph into account:

https://wolfram.com/xid/0bniox635m7aa-ldb7w8


https://wolfram.com/xid/0bniox635m7aa-0j3xjv

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

https://wolfram.com/xid/0bniox635m7aa-v44cwp

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

https://wolfram.com/xid/0bniox635m7aa-r2py27


https://wolfram.com/xid/0bniox635m7aa-ujhopa


https://wolfram.com/xid/0bniox635m7aa-1ldjkv

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:

https://wolfram.com/xid/0bniox635m7aa-b9x6e

https://wolfram.com/xid/0bniox635m7aa-cwu88m


https://wolfram.com/xid/0bniox635m7aa-18iohr

The diagonal entries of the distance matrix are always zero:

https://wolfram.com/xid/0bniox635m7aa-b0i77r


https://wolfram.com/xid/0bniox635m7aa-ev1mrp

The distance matrix can be found using GraphDistance:

https://wolfram.com/xid/0bniox635m7aa-c04req


https://wolfram.com/xid/0bniox635m7aa-0wuo2a

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

https://wolfram.com/xid/0bniox635m7aa-c9jm2d


https://wolfram.com/xid/0bniox635m7aa-ys095y


https://wolfram.com/xid/0bniox635m7aa-d9k98q

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

https://wolfram.com/xid/0bniox635m7aa-fs0db

https://wolfram.com/xid/0bniox635m7aa-col2m4


https://wolfram.com/xid/0bniox635m7aa-b8grwg

Possible Issues (2)Common pitfalls and unexpected behavior
The matrix indices may not have the expected correspondence to vertices:

https://wolfram.com/xid/0bniox635m7aa-b88rhv


https://wolfram.com/xid/0bniox635m7aa-z5zk77

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

https://wolfram.com/xid/0bniox635m7aa-x3cesy

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

https://wolfram.com/xid/0bniox635m7aa-u4zy75

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

https://wolfram.com/xid/0bniox635m7aa-6i0gkf


https://wolfram.com/xid/0bniox635m7aa-sl4xl2


https://wolfram.com/xid/0bniox635m7aa-50fztd

Now the distance is found at the expected position:

https://wolfram.com/xid/0bniox635m7aa-v09am0

"BellmanFord" is not a valid Method option:

https://wolfram.com/xid/0bniox635m7aa-ht2rtk


https://wolfram.com/xid/0bniox635m7aa-eacdeu


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

https://wolfram.com/xid/0bniox635m7aa-q56zfg


https://wolfram.com/xid/0bniox635m7aa-r4f2bs


https://wolfram.com/xid/0bniox635m7aa-7jftl1

Neat Examples (1)Surprising or curious use cases

https://wolfram.com/xid/0bniox635m7aa-rky9b


https://wolfram.com/xid/0bniox635m7aa-bk1h7z

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
]}
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
]}