# graphscope.nx.generators.trees.random_tree#

graphscope.nx.generators.trees.random_tree(n, seed=None, create_using=None)[source]#

Returns a uniformly random tree 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.

• create_using (NetworkX graph constructor, optional (default=nx.Graph)) – Graph type to create. If graph instance, then cleared before populated.

Returns

A tree, given as an undirected graph, whose nodes are numbers in the set {0, …, n - 1}.

Return type

NetworkX graph

Raises

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

Notes

The current implementation of this function generates a uniformly random Prüfer sequence then converts that to a tree via the 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.

Examples

>>> tree = nx.random_tree(n=10, seed=0)
>>> print(nx.forest_str(tree, sources=))
╙── 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