TreeGraphQ[g]
TreeGraphQ
TreeGraphQ[g]
Examples
open all close allBasic Examples (2)
Test whether a graph is a tree:
Graph[{12, 13, 24, 25}]TreeGraphQ[%]CompleteKaryTree[2, 2]TreeGraphQ[%]AdjacencyGraph[{{0, 1, 1}, {0, 0, 0}, {0, 0, 0}}]TreeGraphQ[%]A graph with cycles is not a tree:
CycleGraph[5]TreeGraphQ[%]Scope (6)
TreeGraphQ works with undirected graphs:
TreeGraphQ[[image]]TreeGraphQ[[image]]TreeGraphQ[[image]]TreeGraphQ[[image]]TreeGraphQ gives False for anything that is not a tree graph:
TreeGraphQ[x]TreeGraphQ[Graph[garbage]]TreeGraphQ works with large graphs:
g = GridGraph[{1, 1000000}];TreeGraphQ[g]//Timing{VertexCount[g], EdgeCount[g]}Properties & Relations (10)
A tree graph can be a simple graph:
CompleteKaryTree[2, 3]{TreeGraphQ[%], SimpleGraphQ[%]}A tree graph can be a weighted graph:
CompleteKaryTree[2, 3, VertexWeight -> RandomInteger[5, 4]]{TreeGraphQ[%], WeightedGraphQ[%]}A star is a special tree with as many leaves as possible:
StarGraph[10]TreeGraphQ[%]A path graph with no repeated vertices is a tree with two leaves:
GridGraph[{1, 5}]{PathGraphQ[%], TreeGraphQ[%]}A graph with self-loops is not a tree graph:
Graph[{11, 12}]TreeGraphQ[%]A graph with cycles is not a tree graph:
CycleGraph[3]TreeGraphQ[%]A disconnected graph is not a tree graph:
Graph[{12, 23, 31, 45}]{ConnectedGraphQ[%], TreeGraphQ[%]} A tree graph with
vertices has
edges:
CompleteKaryTree[3, 3]VertexCount[%] - EdgeCount[%]A tree graph is a bipartite graph:
BipartiteGraphQ[CompleteKaryTree[3, 3]]A tree graph is not Hamiltonian:
HamiltonianGraphQ[CompleteKaryTree[3, 3]]Possible Issues (1)
TreeGraphQ gives False for non-explicit graphs:
TreeGraphQ[CompleteKaryTree[n, m]]Related Guides
History
Text
Wolfram Research (2010), TreeGraphQ, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeGraphQ.html.
CMS
Wolfram Language. 2010. "TreeGraphQ." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/TreeGraphQ.html.
APA
Wolfram Language. (2010). TreeGraphQ. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TreeGraphQ.html
BibTeX
@misc{reference.wolfram_2026_treegraphq, author="Wolfram Research", title="{TreeGraphQ}", year="2010", howpublished="\url{https://reference.wolfram.com/language/ref/TreeGraphQ.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_treegraphq, organization={Wolfram Research}, title={TreeGraphQ}, year={2010}, url={https://reference.wolfram.com/language/ref/TreeGraphQ.html}, note=[Accessed: 13-June-2026]}