networkx 2.8.2 Pypi GitHub Homepage
Other Docs
NotesParametersRaisesReturnsBackRef
random_tree(n, seed=None, create_using=None)

Notes

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.

Parameters

n : int

A positive integer representing the number of nodes in the tree.

seed : integer, random_state, or None (default)

Indicator of random number generation state. See Randomness<randomness> .

create_using : NetworkX graph constructor, optional (default=nx.Graph)

Graph type to create. If graph instance, then cleared before populated.

Raises

NetworkXPointlessConcept

If n is zero (because the null graph is not a tree).

Returns

NetworkX graph

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.

Examples

>>> 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)
... print(nx.forest_str(tree)) ╙── 0 ├─╼ 3 └─╼ 4 ├─╼ 6 │   ├─╼ 1 │   ├─╼ 2 │   └─╼ 7 │   └─╼ 8 │   └─╼ 5 └─╼ 9
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

networkx.generators.trees.random_tree

Local connectivity graph

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


GitHub : /networkx/generators/trees.py#289
type: <class 'function'>
Commit: