random_tree(n, seed=None, create_using=None)
The current implementation of this function generates a uniformly random Prüfer sequence then converts that to a tree via the ~networkx.from_prufer_sequence
function. Since there is a bijection between Prüfer sequences of length n - 2 and trees on n nodes, the tree is chosen uniformly at random from the set of all trees on n nodes.
A positive integer representing the number of nodes in the tree.
Indicator of random number generation state. See Randomness<randomness>
.
Graph type to create. If graph instance, then cleared before populated.
If n
is zero (because the null graph is not a tree).
A tree, given as an undirected graph, whose nodes are numbers in the set {0, …, n - 1}.
Returns a uniformly random tree on n
nodes.
>>> tree = nx.random_tree(n=10, seed=0)
... print(nx.forest_str(tree, sources=[0])) ╙── 0 ├── 3 └── 4 ├── 6 │ ├── 1 │ ├── 2 │ └── 7 │ └── 8 │ └── 5 └── 9
>>> tree = nx.random_tree(n=10, seed=0, create_using=nx.DiGraph)See :
... print(nx.forest_str(tree)) ╙── 0 ├─╼ 3 └─╼ 4 ├─╼ 6 │ ├─╼ 1 │ ├─╼ 2 │ └─╼ 7 │ └─╼ 8 │ └─╼ 5 └─╼ 9
The following pages refer to to this document either explicitly or contain code examples using this.
networkx.generators.trees.random_tree
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them