nonisomorphic_trees

nonisomorphic_trees#

graph_tool.generation.nonisomorphic_trees(N)[git][source]#

Iterate over all nonisomorphic (unlabeled) trees with \(N\) vertices.

Parameters:
Nint

Number of vertices.

Returns:
treesiterator over Graph

Iterator over the undirected trees, each a representative of its isomorphism class.

See also

count_nonisomorphic_trees

Number of nonisomorphic trees

Notes

The trees are enumerated with the algorithm of Wright, Richmond, Odlyzko and McKay [wright-constant-1986], which visits the canonical level sequences of the free trees in reverse lexicographic order, in constant amortized time per tree.

References

[wright-constant-1986]

Robert Alan Wright, Bruce Richmond, Andrew Odlyzko and Brendan D. McKay, “Constant time generation of free trees”, SIAM Journal on Computing 15, 540-548 (1986), DOI: 10.1137/0215039 [sci-hub, @tor]

Examples

>>> for g in gt.nonisomorphic_trees(5):
...     print(sorted(g.get_out_degrees(g.get_vertices()).tolist()))
[1, 1, 2, 2, 2]
[1, 1, 1, 2, 3]
[1, 1, 1, 1, 4]

The six trees with six vertices are drawn as follows, as the components of a single graph:

>>> g = gt.Graph(directed=False)
>>> for t in gt.nonisomorphic_trees(6):
...     g = gt.graph_union(g, t, vmap=None)
>>> gt.graph_draw(g, pos=gt.sfdp_layout(g),
...               output="nonisomorphic_trees-six.pdf")
<...>
../_images/nonisomorphic_trees-six.png

The six nonisomorphic trees with six vertices.#