Trees · Cayley's Formula

Lesson 6

Nikolai Chukhin · Alexander S. Kulikov

It is interesting to note that the function \(\texttt{random\_{tree}}\) from the library \(\texttt{networkx}\), used in the code above, employs the Prüfer code to generate a random tree: a random code (from \(\{0,1,\dotsc,n-1\}^{n-2}\)) is generated, after which the tree is reconstructed from it. Similarly, one can iterate over all labeled trees with \(n\) vertices: iterate over all sequences from \(\{0,1,\dotsc,n-1\}^{n-2}\) and reconstruct a tree for each one.