A rooted tree is an acyclic graph such that there is exactly one path from one node, called the root, to each other node. A node
v is called the parent of another node
u if and only if the path from the root to
u goes through
v and there is an edge connecting
v. If node
v is the parent of node
u is a child of node
Write a program or function that, given a positive integer number of nodes and a set of non-negative integer numbers of children each parent has, outputs the number of possible rooted trees with that number of nodes (including the root) and each vertex having a number of children in the set, not counting those trees isomorphic to trees already found.
Two trees are isomorphic if one can be transformed into another by renaming the nodes, or in other words look the same when the nodes are unlabelled.
We shall represent trees as a 0-indexed list of children per index where 0 represents the root, for example
[,,] represents that the root
1 as a child, node
1 has node
2 as a child, and node
2 has no children.
n=3 and set =
[0,1,2]. This is equal to binary trees with three nodes.
The two possible trees are:
[[1,2],,]. Because they are identical in structure to the two trees, we count neither
[[2,1],,]. There are two trees, so the output is
2 or equivalent.
Here is a visualization: You can see that the second set of two trees are identical in structure to the first set of two and are thus not counted. Both sets are composed of two trees which have one of the following two structures (the root is the node at the top):
n=5 and set=
[0,2]. The only possible tree is
[[1,2],[3,4],,,]. Note that, for example,
[[1,3],,,[2,4],] are not counted again because they are isomorphic to the sole tree which is counted. The output is
1 or equivalent.
Here is another visualization:
[0,2]. There are no possible trees because each time children are generated from a node, there are either
2 more nodes. Clearly, 4 nodes cannot be produced by adding
0 successively to
1 node, the root. Output:
0 or falsey.
Input and output should be taken in a reasonable format.
Input is a positive integer representing
n and a list of non-negative integers representing the set of valid numbers of children.
The output is a non-negative integer corresponding to how many trees can be formed.
n ; set ; output 3 ; [0,1,2] ; 2 5 ; [0,2] ; 1 4 ; [0,2] ; 0 3 ; [0,1] ; 1 3 ;  ; 0 1 ;  ; 1 6 ; [0,2,3] ; 2 7 ; [0,2,3] ; 3
- The set of numbers of children will always include zero.
- The root node always counts as a node, even if it has no children (see the
1; test case)
- This is code-golf, so shortest code wins.