# 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
{"ft_lang_label":"__label__en","ft_lang_prob":0.58568275,"math_prob":0.96245545,"size":1396,"snap":"2023-14-2023-23","text_gpt3_token_len":426,"char_repetition_ratio":0.11278736,"word_repetition_ratio":0.0,"special_character_ratio":0.2722063,"punctuation_ratio":0.156,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9850738,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-03-24T02:38:35Z\",\"WARC-Record-ID\":\"<urn:uuid:d12dda4f-a0d5-4221-8ed8-ed3d92986a59>\",\"Content-Length\":\"79208\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:1ccdf1a5-9829-4fae-bc58-0e7fb7232f1b>\",\"WARC-Concurrent-To\":\"<urn:uuid:8b30bb52-84f2-4789-bbd7-295541c7c1ac>\",\"WARC-IP-Address\":\"185.199.111.153\",\"WARC-Target-URI\":\"https://graphscope.io/docs/latest/reference/networkx/generated/graphscope.nx.generators.trees.random_tree.html\",\"WARC-Payload-Digest\":\"sha1:Y7WZMZT3PC7MCAYYF4VVXQ4NUCOTFPD4\",\"WARC-Block-Digest\":\"sha1:FSTTCPLLOGMBN4XIFXYPDIDKPQHRFG7P\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-14/CC-MAIN-2023-14_segments_1679296945242.64_warc_CC-MAIN-20230324020038-20230324050038-00590.warc.gz\"}"}