GraphDistanceMatrix[g]
gives a matrix in which the (i,j)
entry is the length of a shortest path in g between vertices i and j.
GraphDistanceMatrix[g,Parent]
returns a three-dimensional matrix in which the (1,i,j)
entry is the length of a shortest path from i to j and the (2,i,j)
entry is the predecessor of j in a shortest path from i to j.
GraphDistanceMatrix
GraphDistanceMatrix[g]
gives a matrix in which the (i,j)
entry is the length of a shortest path in g between vertices i and j.
GraphDistanceMatrix[g,Parent]
returns a three-dimensional matrix in which the (1,i,j)
entry is the length of a shortest path from i to j and the (2,i,j)
entry is the predecessor of j in a shortest path from i to j.
Details and Options
- GraphDistanceMatrix functionality is now available in the built-in Wolfram Language function GraphDistanceMatrix.
- To use GraphDistanceMatrix, you first need to load the Graph Utilities Package using Needs["GraphUtilities`"].
- The following options can be given:
-
Method Automatic the method used to compute the shortest path Weighted True whether edge weights are to be taken into account
Examples
open all close allBasic Examples (2)
Needs["GraphUtilities`"]This defines a simple directed graph:
g = SparseArray[{{1, 2} -> 1., {2, 3} -> 1., {1, 5} -> 1., {4, 3} -> 1., {5, 4} -> -2.}, {5, 5}];GraphPlot[g, VertexLabeling -> True, EdgeRenderingFunction -> ({Arrow[#, 0.1], Text[g[[First[#2], Last[#2]]], LineScaledCoordinate[#1], Background -> White]}&)]This calculates the distance between the vertices:
GraphDistanceMatrix[g]//MatrixFormThis function has been superseded by GraphDistanceMatrix in the Wolfram System:
g = WeightedAdjacencyGraph[SparseArray[{{1, 2} -> 1., {2, 3} -> 1., {1, 5} -> 1., {4, 3} -> 1., {5, 4} -> -2.}, {5, 5}, Infinity]]GraphDistanceMatrix[g]//MatrixFormScope (1)
Needs["GraphUtilities`"]This defines a simple directed graph:
g = SparseArray[{{1, 2} -> 1., {2, 3} -> 1., {1, 5} -> 1., {4, 3} -> 1., {5, 4} -> -2.}, {5, 5}];GraphPlot[g, VertexLabeling -> True, EdgeRenderingFunction -> ({Arrow[#, 0.1], Text[g[[First[#2], Last[#2]]], LineScaledCoordinate[#1], Background -> White]}&)]This calculates the distance between the vertices:
GraphDistanceMatrix[g]//MatrixFormThis shows also the predecessors in the shortest path:
prd = GraphDistanceMatrix[g, Parent][[2]];
prd//MatrixFormThe path from 1 to 3 is {1,5,4,3}:
Drop[FixedPointList[prd[[1, #]]&, 3], -1]//ReverseThis confirms the shortest path:
GraphPath[g, 1, 3]Options (2)
Method (2)
Needs["GraphUtilities`"]g0 = ToCombinatoricaGraph[{1 -> 2, 2 -> 3, 3 -> 1, 1 -> 3}];
g = SetEdgeWeights[g0, {{1, 2}, {2, 3}, {3, 1}, {1, 3}}, {-1, -2, 3, 1}];
adj = AdjacencyMatrix[g];GraphPlot[g0, MultiedgeStyle -> True, VertexLabeling -> True, EdgeRenderingFunction -> ({Arrow[#, 0.1], Text[adj[[First[#2], Last[#2]]], LineScaledCoordinate[#1], Background -> White]}&)]Because of the negative edge weight, the Dijkstra algorithm cannot be applied:
GraphDistanceMatrix[g, Method -> "Dijkstra"]Both the Floyd–Warshall and Johnson algorithms work:
GraphDistanceMatrix[g, Method -> "FloydWarshall"]GraphDistanceMatrix[g, Method -> "Johnson"]Needs["GraphUtilities`"]This defines a small graph with a negative cycle:
g = {{0, 5, 4, 0}, {0, 0, 3, -3}, {0, 0.0000001, 0, -1}, {-2, 0, 2, 0}};GraphPlot[{{1 -> 2, 5}, {1 -> 3, 4}, {2 -> 3, 3}, {2 -> 4, -3}, {3 -> 2, 0.0000001}, {3 -> 4, -1}, {4 -> 1, -2}, {4 -> 3, 2}}, VertexLabeling -> True, EdgeRenderingFunction -> ({Arrow[#, .05], Text[#3, LineScaledCoordinate[#1, .3], Background -> White]}&)]The Dijkstra algorithm does not work for negative edge weights:
GraphDistanceMatrix[g, Method -> "Dijkstra"]The Floyd–Warshall algorithm does not detect any negative weight cycle, and gives the wrong answer:
GraphDistanceMatrix[g, Method -> "FloydWarshall"]The Johnson algorithm detects a negative weight cycle:
GraphDistanceMatrix[g, Method -> "Johnson"]The default algorithm for graphs with negative edge weights is Johnson:
GraphDistanceMatrix[g]See Also
Tech Notes
Related Guides
-
▪
- Graph Utilities Package ▪
- Graphs & Networks ▪
- Graph Visualization ▪
- Computation on Graphs ▪
- Graph Construction & Representation ▪
- Graphs and Matrices ▪
- Graph Properties & Measurements ▪
- Graph Operations and Modifications ▪
- Statistical Analysis ▪
- Social Network Analysis ▪
- Graph Properties ▪
- Mathematical Data Formats ▪
- Discrete Mathematics
Text
Wolfram Research (2007), GraphDistanceMatrix, Wolfram Language function, https://reference.wolfram.com/language/GraphUtilities/ref/GraphDistanceMatrix.html.
CMS
Wolfram Language. 2007. "GraphDistanceMatrix." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/GraphUtilities/ref/GraphDistanceMatrix.html.
APA
Wolfram Language. (2007). GraphDistanceMatrix. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/GraphUtilities/ref/GraphDistanceMatrix.html
BibTeX
@misc{reference.wolfram_2026_graphdistancematrix, author="Wolfram Research", title="{GraphDistanceMatrix}", year="2007", howpublished="\url{https://reference.wolfram.com/language/GraphUtilities/ref/GraphDistanceMatrix.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_graphdistancematrix, organization={Wolfram Research}, title={GraphDistanceMatrix}, year={2007}, url={https://reference.wolfram.com/language/GraphUtilities/ref/GraphDistanceMatrix.html}, note=[Accessed: 13-June-2026]}