1
$\begingroup$

A graph $G$ is said to be a tree if it is a connected (i.e. every two distinct vertices of $G$ can be connected by a path of vertices of $G$), acyclic (i.e., $G$ has no cycles) and simple (i.e., $G$ has no loops). A rooted tree is a directed tree with a fixed vertex, called the root, which is directed implicitly away from the root.

Question. Is there any existing code or known package that generates all rooted trees with a given number of vertices (up to isomorphism)?

I have searched a bit, but could not find any ready-to-use implementation in software like Macaulay2, SageMath, or similar systems.

$\endgroup$
4
  • 1
    $\begingroup$ Well, one easy thing to do to get all rooted trees, is generate all undirected trees and then choose all possible roots ($n$ choices for each tree). Of course, you'll have to check for isomorphism of these after that. There are more efficient things you could do too (using ideas from Polya theory) but this should work ok. Anyways, it looks like Sage does have rooted trees built in already: doc.sagemath.org/html/en/reference/combinat/sage/combinat/… $\endgroup$ Commented May 12 at 22:23
  • 5
    $\begingroup$ Sorry, I was not trying to be rude, just trying to extract the precise mathematical question here. I hope you figure out how to solve the problem you are working on! Sage also has a question and answer forum where you can possibly get help with more technical aspects of your inquiry: ask.sagemath.org/questions $\endgroup$ Commented May 12 at 22:46
  • 1
    $\begingroup$ gentreeg from the nauty software package will generate unrooted trees, which is a starting point for Sam's proposal. If you want to roll your own which just creates rooted trees directly, it's doable in Python in about 50 to 100 lines of code (I don't have it to hand, but I've done it before) using a generator of integer partitions and the itertools package for combinations_with_replacement. $\endgroup$ Commented May 13 at 7:33
  • 3
    $\begingroup$ I've answered at ask.sagemath.org but just in case here is a link to my conversion routine at Sagecell with an example showing all trees on 5 vertices. $\endgroup$ Commented May 13 at 22:05

1 Answer 1

2
$\begingroup$

The question was asked at ask.sagemath.org, where two approaches were proposed (mine included).

SageMath offers RootedTrees functionality, which however generates combinatorial objects rather than actual tree graphs, and conversion between the two classes is currently missing (requested in SageMath issue #29270). Still, it is possible to convert a given RootedTree into a tree graph recursively using its sortkey, which I did in this code generating all rooted trees on 5 vertices as an example.

Alternative approach proposed at ask.sagemath.org performs conversion of RootedTree to OrderedTree to poset to Hasse diagram to get a tree graph.

$\endgroup$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.