I'm looking for code that produces all possible trees with no self edges (or their adjacent matrices) with n nodes, anyone have any idea if this is written anywhere?
Remember to vote up questions/answers you find interesting or helpful (requires 15 reputation points)
|
1
|
||||||||||||||||||||||||||
|
closed as off topic by Harry Gindi, Igor Pak, Felipe Voloch, Mark Sapir, Andy Putman Jan 18 2011 at 17:14 |
|
14
|
In sage the command list(graphs.trees(9)) produces a list of all trees on 9 vertices. As sage is open source, the code is available for inspection. The command [tt.am() for tt in graphs.trees(9)] will provide the adjacency matrices. |
||
|
|
You can accept an answer to one of your own questions by clicking the check mark next to it. This awards 15 reputation points to the person who answered and 2 reputation points to you.
|
5
|
It is well known that there is a bijection between the set of trees on $n$ nodes and sequences of length $n-2$ with values in $[n]$. These sequences are called Prüfer sequences. Indeed, the wikipedia page has code which will convert any Prüfer sequence into a tree. So a naïve algorithm would be to run the wikipedia algorithm over all Prüfer sequences. |
||
|
|

