I want to visualize all unlabeled trees with \$n\$ or fewer nodes, not just count them.
First idea/attempt: Take a list of all \$n-1\$ node trees, then append a new leaf to every tree in every way to get a new list of \$n\$ node trees. Clearly, this new list will contain a lot of isomorphic duplicates. To fix this, we start adding the \$n\$ trees to a new list, and doing so only if they are not isomorphic to any of the trees in the new list. Since graph isomorphism problem is not known to be solvable in polynomial time, this makes the entire process even more horrible performance wise, because this process will do a lot of such checks.
My question is if this can be done more efficiently, or in a better way?
The python code implementing this idea/attempt using networkX and pyplot:
""" trees of order N or less will be generated """ N = 9 import networkx as nx """ return copy of graph with newNode node appended to toNode node """ def leaf_copy(graph, newNode, toNode): g = nx.Graph.copy(graph) g.add_node(newNode) g.add_edge(newNode,toNode) return g from networkx.algorithms import isomorphism """ get all n+1 node cases out of all n node cases in prevTreeList """ def genNextTreeList(prevTreeList): """ one node case """ if prevTreeList == None or prevTreeList == : g = nx.Graph() g.add_node(1) return [g] """ new loads of n+1 graphs by all possible list appendations """ """ this will include loads of isomprhic duplicates... """ nextTreeList =  for g in prevTreeList: v = len(g.nodes())+1 for node in g.nodes(): nextTreeList.append(leaf_copy(g,v,node)) """ remove isomorphic duplicates """ """ it will check every graph to be added with all added graphs for isomorphism... """ nextTreeListClean =  for g in nextTreeList: isomorphic = False for clean_g in nextTreeListClean: i = isomorphism.GraphMatcher(g,clean_g) if i.is_isomorphic(): isomorphic = True break if not isomorphic: nextTreeListClean.append(g) return nextTreeListClean import matplotlib.pyplot as plt if __name__ == "__main__": print(0, "\t", 1) G =  figure = 0 for n in range(N): G = genNextTreeList(G) """ print the number of examples to check if the code is working properly """ print(n+1, "\t", len(G)) """ draw and save the plots """ for g in G: figure += 1 fig = plt.figure(figure) plt.title(str(n+1)+'.'+str(G.index(g)+1)) nx.draw(g, with_labels=False) plt.figure(figure).savefig('plot'+str(figure)+'.png',bbox_inches='tight',dpi=100) plt.close(fig)