gives the greatest distance between any pair of vertices in the graph g.
GraphDiameter[{vw,…}]
uses rules vw to specify the graph g.
GraphDiameter
gives the greatest distance between any pair of vertices in the graph g.
GraphDiameter[{vw,…}]
uses rules vw to specify the graph g.
Details and Options
- The following options can be given:
-
EdgeWeight Automatic weight for each edge Method Automatic method to use - With the default setting EdgeWeight->Automatic, the edge weight of an edge is taken to be the EdgeWeight of the graph g if available; otherwise, it is 1.
- Possible Method settings include "Dijkstra", "FloydWarshall", "Johnson", and "PseudoDiameter".
- GraphDiameter works with undirected graphs, directed graphs, weighted graphs, multigraphs, and mixed graphs.
Examples
open all close allBasic Examples (1)
Scope (7)
GraphDiameter works with undirected graphs:
GraphDiameter[[image]]GraphDiameter[[image]]GraphDiameter[[image]]GraphDiameter[[image]]GraphDiameter[[image]]Use rules to specify the graph:
GraphDiameter[{1 -> 2, 2 -> 3, 3 -> 1, 3 -> 4, 4 -> 5, 5 -> 3}]GraphDiameter works with large graphs:
g = GridGraph[{10, 10, 10, 10}];GraphDiameter[g]//TimingApplications (2)
Illustrate the diameter in two Petersen graphs:
findDiameterPath[g_ ? UndirectedGraphQ] := Module[{d = GraphDistanceMatrix[g], u, v, pos}, pos = First@Position[d, Max[d]];
{u, v} = Part[VertexList[g], pos];
PathGraph@FindShortestPath[g, u, v]]HighlightGraph[#, findDiameterPath[#]]& /@ {PetersenGraph[5, 2], PetersenGraph[6, 2]}For a CompleteGraph, the diameter is 1:
HighlightGraph[#, findDiameterPath[#]]& /@ Table[CompleteGraph[n, VertexSize -> Small], {n, 3, 6}]For a PathGraph of size
, the diameter is
:
HighlightGraph[#, findDiameterPath[#]]& /@ Table[PathGraph[Range[n], VertexSize -> Small], {n, 5, 8}]For a CycleGraph of size
, the diameter is
:
HighlightGraph[#, findDiameterPath[#]]& /@ Table[CycleGraph[n, VertexSize -> Small], {n, 3, 6}]For a WheelGraph of size 5 or more, the diameter is 2:
HighlightGraph[#, findDiameterPath[#]]& /@ Table[WheelGraph[n, VertexSize -> Small], {n, 5, 8}]A WheelGraph of size 4 is a complete graph, so the diameter is 1:
HighlightGraph[#, findDiameterPath[#]]&[WheelGraph[4, VertexSize -> Small]]For a GridGraph of size {m,n}, the diameter is
:
HighlightGraph[#, findDiameterPath[#]]& /@ Table[GridGraph[dim, VertexSize -> Small, ImageSize -> {Tiny, Tiny}], {dim, {{3, 3}, {3, 4}, {4, 3}, {4, 4}}}]For a CompleteKaryTree tree of depth
, the diameter is
:
HighlightGraph[#, findDiameterPath[#]]& /@ Table[CompleteKaryTree[n, VertexSize -> Small], {n, 3, 5}]Find the largest number of steps separating two people at a family gathering network:
g = ExampleData[{"NetworkGraph", "FamilyGathering"}]GraphDiameter[g]Properties & Relations (3)
For a connected graph, the diameter can be computed by VertexEccentricity:
g = PetersenGraph[5, 2]GraphDiameter[g]Max[VertexEccentricity[g, #]& /@ VertexList[g]]If a simple graph has diameter greater than 3, then its complement has diameter less than 3:
g = GridGraph[{5, 5}]GraphDiameter[g] ≥ 3GraphDiameter[GraphComplement[g]] ≤ 3The graph diameter is unchanged when reversing every edge:
g = [image];h = ReverseGraph[g]{GraphDiameter[g], GraphDiameter[h]}See Also
VertexEccentricity GraphRadius GraphPeriphery GraphCenter GraphDistanceMatrix
Function Repository: GraphAntipodes
Related Guides
Text
Wolfram Research (2010), GraphDiameter, Wolfram Language function, https://reference.wolfram.com/language/ref/GraphDiameter.html (updated 2015).
CMS
Wolfram Language. 2010. "GraphDiameter." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015. https://reference.wolfram.com/language/ref/GraphDiameter.html.
APA
Wolfram Language. (2010). GraphDiameter. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/GraphDiameter.html
BibTeX
@misc{reference.wolfram_2026_graphdiameter, author="Wolfram Research", title="{GraphDiameter}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/GraphDiameter.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_graphdiameter, organization={Wolfram Research}, title={GraphDiameter}, year={2015}, url={https://reference.wolfram.com/language/ref/GraphDiameter.html}, note=[Accessed: 12-June-2026]}