I was wondering is there a way to obtain all the different trees using Networkx? It's a known fact that the number of trees with n nodes is n^(n-2) (using Cayley's Formula) so if there is 3 nodes, then it has 3 tree graphs, if it has 4 nodes then it has 16 trees and so on. What I want is to code all the trees using the prufer sequence, I know that Networkx has a function to create random trees, but there is a chance I can get duplicates, all I can think of is use Numpy so I can find all the unique elements in a list, here is my code:
import numpy as np
import networkx as nx
n = 3 #Number of nodes
aux = []
prufer = []
for i in range(10):
aux.append(nx.random_tree(n))
for j in aux:
prufer.append(nx.to_prufer_sequence(j))
arr = np.array(prufer)
newarr = np.unique(arr, axis = 0)
The problem here it's that I generated 10 random trees, but in the end I only want 3 but when I want to find all the trees using 4 nodes I don't want to generate 50 if I'm only going to use 16. Is there a way I can do this more efficiently? Thank you!
