trees.py 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  1. """Functions for generating trees.
  2. The functions sampling trees at random in this module come
  3. in two variants: labeled and unlabeled. The labeled variants
  4. sample from every possible tree with the given number of nodes
  5. uniformly at random. The unlabeled variants sample from every
  6. possible *isomorphism class* of trees with the given number
  7. of nodes uniformly at random.
  8. To understand the difference, consider the following example.
  9. There are two isomorphism classes of trees with four nodes.
  10. One is that of the path graph, the other is that of the
  11. star graph. The unlabeled variant will return a line graph or
  12. a star graph with probability 1/2.
  13. The labeled variant will return the line graph
  14. with probability 3/4 and the star graph with probability 1/4,
  15. because there are more labeled variants of the line graph
  16. than of the star graph. More precisely, the line graph has
  17. an automorphism group of order 2, whereas the star graph has
  18. an automorphism group of order 6, so the line graph has three
  19. times as many labeled variants as the star graph, and thus
  20. three more chances to be drawn.
  21. Additionally, some functions in this module can sample rooted
  22. trees and forests uniformly at random. A rooted tree is a tree
  23. with a designated root node. A rooted forest is a disjoint union
  24. of rooted trees.
  25. """
  26. from collections import Counter, defaultdict
  27. from math import comb, factorial
  28. import networkx as nx
  29. from networkx.utils import py_random_state
  30. __all__ = [
  31. "prefix_tree",
  32. "prefix_tree_recursive",
  33. "random_labeled_tree",
  34. "random_labeled_rooted_tree",
  35. "random_labeled_rooted_forest",
  36. "random_unlabeled_tree",
  37. "random_unlabeled_rooted_tree",
  38. "random_unlabeled_rooted_forest",
  39. ]
  40. @nx._dispatchable(graphs=None, returns_graph=True)
  41. def prefix_tree(paths):
  42. """Creates a directed prefix tree from a list of paths.
  43. Usually the paths are described as strings or lists of integers.
  44. A "prefix tree" represents the prefix structure of the strings.
  45. Each node represents a prefix of some string. The root represents
  46. the empty prefix with children for the single letter prefixes which
  47. in turn have children for each double letter prefix starting with
  48. the single letter corresponding to the parent node, and so on.
  49. More generally the prefixes do not need to be strings. A prefix refers
  50. to the start of a sequence. The root has children for each one element
  51. prefix and they have children for each two element prefix that starts
  52. with the one element sequence of the parent, and so on.
  53. Note that this implementation uses integer nodes with an attribute.
  54. Each node has an attribute "source" whose value is the original element
  55. of the path to which this node corresponds. For example, suppose `paths`
  56. consists of one path: "can". Then the nodes `[1, 2, 3]` which represent
  57. this path have "source" values "c", "a" and "n".
  58. All the descendants of a node have a common prefix in the sequence/path
  59. associated with that node. From the returned tree, the prefix for each
  60. node can be constructed by traversing the tree up to the root and
  61. accumulating the "source" values along the way.
  62. The root node is always `0` and has "source" attribute `None`.
  63. The root is the only node with in-degree zero.
  64. The nil node is always `-1` and has "source" attribute `"NIL"`.
  65. The nil node is the only node with out-degree zero.
  66. Parameters
  67. ----------
  68. paths: iterable of paths
  69. An iterable of paths which are themselves sequences.
  70. Matching prefixes among these sequences are identified with
  71. nodes of the prefix tree. One leaf of the tree is associated
  72. with each path. (Identical paths are associated with the same
  73. leaf of the tree.)
  74. Returns
  75. -------
  76. tree: DiGraph
  77. A directed graph representing an arborescence consisting of the
  78. prefix tree generated by `paths`. Nodes are directed "downward",
  79. from parent to child. A special "synthetic" root node is added
  80. to be the parent of the first node in each path. A special
  81. "synthetic" leaf node, the "nil" node `-1`, is added to be the child
  82. of all nodes representing the last element in a path. (The
  83. addition of this nil node technically makes this not an
  84. arborescence but a directed acyclic graph; removing the nil node
  85. makes it an arborescence.)
  86. Notes
  87. -----
  88. The prefix tree is also known as a *trie*.
  89. Examples
  90. --------
  91. Create a prefix tree from a list of strings with common prefixes::
  92. >>> paths = ["ab", "abs", "ad"]
  93. >>> T = nx.prefix_tree(paths)
  94. >>> list(T.edges)
  95. [(0, 1), (1, 2), (1, 4), (2, -1), (2, 3), (3, -1), (4, -1)]
  96. The leaf nodes can be obtained as predecessors of the nil node::
  97. >>> root, NIL = 0, -1
  98. >>> list(T.predecessors(NIL))
  99. [2, 3, 4]
  100. To recover the original paths that generated the prefix tree,
  101. traverse up the tree from the node `-1` to the node `0`::
  102. >>> recovered = []
  103. >>> for v in T.predecessors(NIL):
  104. ... prefix = ""
  105. ... while v != root:
  106. ... prefix = str(T.nodes[v]["source"]) + prefix
  107. ... v = next(T.predecessors(v)) # only one predecessor
  108. ... recovered.append(prefix)
  109. >>> sorted(recovered)
  110. ['ab', 'abs', 'ad']
  111. """
  112. def get_children(parent, paths):
  113. children = defaultdict(list)
  114. # Populate dictionary with key(s) as the child/children of the root and
  115. # value(s) as the remaining paths of the corresponding child/children
  116. for path in paths:
  117. # If path is empty, we add an edge to the NIL node.
  118. if not path:
  119. tree.add_edge(parent, NIL)
  120. continue
  121. child, *rest = path
  122. # `child` may exist as the head of more than one path in `paths`.
  123. children[child].append(rest)
  124. return children
  125. # Initialize the prefix tree with a root node and a nil node.
  126. tree = nx.DiGraph()
  127. root = 0
  128. tree.add_node(root, source=None)
  129. NIL = -1
  130. tree.add_node(NIL, source="NIL")
  131. children = get_children(root, paths)
  132. stack = [(root, iter(children.items()))]
  133. while stack:
  134. parent, remaining_children = stack[-1]
  135. try:
  136. child, remaining_paths = next(remaining_children)
  137. # Pop item off stack if there are no remaining children
  138. except StopIteration:
  139. stack.pop()
  140. continue
  141. # We relabel each child with an unused name.
  142. new_name = len(tree) - 1
  143. # The "source" node attribute stores the original node name.
  144. tree.add_node(new_name, source=child)
  145. tree.add_edge(parent, new_name)
  146. children = get_children(new_name, remaining_paths)
  147. stack.append((new_name, iter(children.items())))
  148. return tree
  149. @nx._dispatchable(graphs=None, returns_graph=True)
  150. def prefix_tree_recursive(paths):
  151. """Recursively creates a directed prefix tree from a list of paths.
  152. The original recursive version of prefix_tree for comparison. It is
  153. the same algorithm but the recursion is unrolled onto a stack.
  154. Usually the paths are described as strings or lists of integers.
  155. A "prefix tree" represents the prefix structure of the strings.
  156. Each node represents a prefix of some string. The root represents
  157. the empty prefix with children for the single letter prefixes which
  158. in turn have children for each double letter prefix starting with
  159. the single letter corresponding to the parent node, and so on.
  160. More generally the prefixes do not need to be strings. A prefix refers
  161. to the start of a sequence. The root has children for each one element
  162. prefix and they have children for each two element prefix that starts
  163. with the one element sequence of the parent, and so on.
  164. Note that this implementation uses integer nodes with an attribute.
  165. Each node has an attribute "source" whose value is the original element
  166. of the path to which this node corresponds. For example, suppose `paths`
  167. consists of one path: "can". Then the nodes `[1, 2, 3]` which represent
  168. this path have "source" values "c", "a" and "n".
  169. All the descendants of a node have a common prefix in the sequence/path
  170. associated with that node. From the returned tree, ehe prefix for each
  171. node can be constructed by traversing the tree up to the root and
  172. accumulating the "source" values along the way.
  173. The root node is always `0` and has "source" attribute `None`.
  174. The root is the only node with in-degree zero.
  175. The nil node is always `-1` and has "source" attribute `"NIL"`.
  176. The nil node is the only node with out-degree zero.
  177. Parameters
  178. ----------
  179. paths: iterable of paths
  180. An iterable of paths which are themselves sequences.
  181. Matching prefixes among these sequences are identified with
  182. nodes of the prefix tree. One leaf of the tree is associated
  183. with each path. (Identical paths are associated with the same
  184. leaf of the tree.)
  185. Returns
  186. -------
  187. tree: DiGraph
  188. A directed graph representing an arborescence consisting of the
  189. prefix tree generated by `paths`. Nodes are directed "downward",
  190. from parent to child. A special "synthetic" root node is added
  191. to be the parent of the first node in each path. A special
  192. "synthetic" leaf node, the "nil" node `-1`, is added to be the child
  193. of all nodes representing the last element in a path. (The
  194. addition of this nil node technically makes this not an
  195. arborescence but a directed acyclic graph; removing the nil node
  196. makes it an arborescence.)
  197. Notes
  198. -----
  199. The prefix tree is also known as a *trie*.
  200. Examples
  201. --------
  202. Create a prefix tree from a list of strings with common prefixes::
  203. >>> paths = ["ab", "abs", "ad"]
  204. >>> T = nx.prefix_tree(paths)
  205. >>> list(T.edges)
  206. [(0, 1), (1, 2), (1, 4), (2, -1), (2, 3), (3, -1), (4, -1)]
  207. The leaf nodes can be obtained as predecessors of the nil node.
  208. >>> root, NIL = 0, -1
  209. >>> list(T.predecessors(NIL))
  210. [2, 3, 4]
  211. To recover the original paths that generated the prefix tree,
  212. traverse up the tree from the node `-1` to the node `0`::
  213. >>> recovered = []
  214. >>> for v in T.predecessors(NIL):
  215. ... prefix = ""
  216. ... while v != root:
  217. ... prefix = str(T.nodes[v]["source"]) + prefix
  218. ... v = next(T.predecessors(v)) # only one predecessor
  219. ... recovered.append(prefix)
  220. >>> sorted(recovered)
  221. ['ab', 'abs', 'ad']
  222. """
  223. def _helper(paths, root, tree):
  224. """Recursively create a trie from the given list of paths.
  225. `paths` is a list of paths, each of which is itself a list of
  226. nodes, relative to the given `root` (but not including it). This
  227. list of paths will be interpreted as a tree-like structure, in
  228. which two paths that share a prefix represent two branches of
  229. the tree with the same initial segment.
  230. `root` is the parent of the node at index 0 in each path.
  231. `tree` is the "accumulator", the :class:`networkx.DiGraph`
  232. representing the branching to which the new nodes and edges will
  233. be added.
  234. """
  235. # For each path, remove the first node and make it a child of root.
  236. # Any remaining paths then get processed recursively.
  237. children = defaultdict(list)
  238. for path in paths:
  239. # If path is empty, we add an edge to the NIL node.
  240. if not path:
  241. tree.add_edge(root, NIL)
  242. continue
  243. child, *rest = path
  244. # `child` may exist as the head of more than one path in `paths`.
  245. children[child].append(rest)
  246. # Add a node for each child, connect root, recurse to remaining paths
  247. for child, remaining_paths in children.items():
  248. # We relabel each child with an unused name.
  249. new_name = len(tree) - 1
  250. # The "source" node attribute stores the original node name.
  251. tree.add_node(new_name, source=child)
  252. tree.add_edge(root, new_name)
  253. _helper(remaining_paths, new_name, tree)
  254. # Initialize the prefix tree with a root node and a nil node.
  255. tree = nx.DiGraph()
  256. root = 0
  257. tree.add_node(root, source=None)
  258. NIL = -1
  259. tree.add_node(NIL, source="NIL")
  260. # Populate the tree.
  261. _helper(paths, root, tree)
  262. return tree
  263. @py_random_state("seed")
  264. @nx._dispatchable(graphs=None, returns_graph=True)
  265. def random_labeled_tree(n, *, seed=None):
  266. """Returns a labeled tree on `n` nodes chosen uniformly at random.
  267. Generating uniformly distributed random Prüfer sequences and
  268. converting them into the corresponding trees is a straightforward
  269. method of generating uniformly distributed random labeled trees.
  270. This function implements this method.
  271. Parameters
  272. ----------
  273. n : int
  274. The number of nodes, greater than zero.
  275. seed : random_state
  276. Indicator of random number generation state.
  277. See :ref:`Randomness<randomness>`
  278. Returns
  279. -------
  280. :class:`networkx.Graph`
  281. A `networkx.Graph` with nodes in the set {0, …, *n* - 1}.
  282. Raises
  283. ------
  284. NetworkXPointlessConcept
  285. If `n` is zero (because the null graph is not a tree).
  286. Examples
  287. --------
  288. >>> G = nx.random_labeled_tree(5, seed=42)
  289. >>> nx.is_tree(G)
  290. True
  291. >>> G.edges
  292. EdgeView([(0, 1), (0, 3), (0, 2), (2, 4)])
  293. A tree with *arbitrarily directed* edges can be created by assigning
  294. generated edges to a ``DiGraph``:
  295. >>> DG = nx.DiGraph()
  296. >>> DG.add_edges_from(G.edges)
  297. >>> nx.is_tree(DG)
  298. True
  299. >>> DG.edges
  300. OutEdgeView([(0, 1), (0, 3), (0, 2), (2, 4)])
  301. """
  302. # Cannot create a Prüfer sequence unless `n` is at least two.
  303. if n == 0:
  304. raise nx.NetworkXPointlessConcept("the null graph is not a tree")
  305. if n == 1:
  306. return nx.empty_graph(1)
  307. return nx.from_prufer_sequence([seed.choice(range(n)) for i in range(n - 2)])
  308. @py_random_state("seed")
  309. @nx._dispatchable(graphs=None, returns_graph=True)
  310. def random_labeled_rooted_tree(n, *, seed=None):
  311. """Returns a labeled rooted tree with `n` nodes.
  312. The returned tree is chosen uniformly at random from all labeled rooted trees.
  313. Parameters
  314. ----------
  315. n : int
  316. The number of nodes
  317. seed : integer, random_state, or None (default)
  318. Indicator of random number generation state.
  319. See :ref:`Randomness<randomness>`.
  320. Returns
  321. -------
  322. :class:`networkx.Graph`
  323. A `networkx.Graph` with integer nodes 0 <= node <= `n` - 1.
  324. The root of the tree is selected uniformly from the nodes.
  325. The "root" graph attribute identifies the root of the tree.
  326. Notes
  327. -----
  328. This function returns the result of :func:`random_labeled_tree`
  329. with a randomly selected root.
  330. Raises
  331. ------
  332. NetworkXPointlessConcept
  333. If `n` is zero (because the null graph is not a tree).
  334. """
  335. t = random_labeled_tree(n, seed=seed)
  336. t.graph["root"] = seed.randint(0, n - 1)
  337. return t
  338. @py_random_state("seed")
  339. @nx._dispatchable(graphs=None, returns_graph=True)
  340. def random_labeled_rooted_forest(n, *, seed=None):
  341. """Returns a labeled rooted forest with `n` nodes.
  342. The returned forest is chosen uniformly at random using a
  343. generalization of Prüfer sequences [1]_ in the form described in [2]_.
  344. Parameters
  345. ----------
  346. n : int
  347. The number of nodes.
  348. seed : random_state
  349. See :ref:`Randomness<randomness>`.
  350. Returns
  351. -------
  352. :class:`networkx.Graph`
  353. A `networkx.Graph` with integer nodes 0 <= node <= `n` - 1.
  354. The "roots" graph attribute is a set of integers containing the roots.
  355. References
  356. ----------
  357. .. [1] Knuth, Donald E. "Another Enumeration of Trees."
  358. Canadian Journal of Mathematics, 20 (1968): 1077-1086.
  359. https://doi.org/10.4153/CJM-1968-104-8
  360. .. [2] Rubey, Martin. "Counting Spanning Trees". Diplomarbeit
  361. zur Erlangung des akademischen Grades Magister der
  362. Naturwissenschaften an der Formal- und Naturwissenschaftlichen
  363. Fakultät der Universität Wien. Wien, May 2000.
  364. """
  365. # Select the number of roots by iterating over the cumulative count of trees
  366. # with at most k roots
  367. def _select_k(n, seed):
  368. r = seed.randint(0, (n + 1) ** (n - 1) - 1)
  369. cum_sum = 0
  370. for k in range(1, n):
  371. cum_sum += (factorial(n - 1) * n ** (n - k)) // (
  372. factorial(k - 1) * factorial(n - k)
  373. )
  374. if r < cum_sum:
  375. return k
  376. return n
  377. F = nx.empty_graph(n)
  378. if n == 0:
  379. F.graph["roots"] = {}
  380. return F
  381. # Select the number of roots k
  382. k = _select_k(n, seed)
  383. if k == n:
  384. F.graph["roots"] = set(range(n))
  385. return F # Nothing to do
  386. # Select the roots
  387. roots = seed.sample(range(n), k)
  388. # Nonroots
  389. p = set(range(n)).difference(roots)
  390. # Coding sequence
  391. N = [seed.randint(0, n - 1) for i in range(n - k - 1)]
  392. # Multiset of elements in N also in p
  393. degree = Counter([x for x in N if x in p])
  394. # Iterator over the elements of p with degree zero
  395. iterator = iter(x for x in p if degree[x] == 0)
  396. u = last = next(iterator)
  397. # This loop is identical to that for Prüfer sequences,
  398. # except that we can draw nodes only from p
  399. for v in N:
  400. F.add_edge(u, v)
  401. degree[v] -= 1
  402. if v < last and degree[v] == 0:
  403. u = v
  404. else:
  405. last = u = next(iterator)
  406. F.add_edge(u, roots[0])
  407. F.graph["roots"] = set(roots)
  408. return F
  409. # The following functions support generation of unlabeled trees and forests.
  410. def _to_nx(edges, n_nodes, root=None, roots=None):
  411. """
  412. Converts the (edges, n_nodes) input to a :class:`networkx.Graph`.
  413. The (edges, n_nodes) input is a list of even length, where each pair
  414. of consecutive integers represents an edge, and an integer `n_nodes`.
  415. Integers in the list are elements of `range(n_nodes)`.
  416. Parameters
  417. ----------
  418. edges : list of ints
  419. The flattened list of edges of the graph.
  420. n_nodes : int
  421. The number of nodes of the graph.
  422. root: int (default=None)
  423. If not None, the "root" attribute of the graph will be set to this value.
  424. roots: collection of ints (default=None)
  425. If not None, he "roots" attribute of the graph will be set to this value.
  426. Returns
  427. -------
  428. :class:`networkx.Graph`
  429. The graph with `n_nodes` nodes and edges given by `edges`.
  430. """
  431. G = nx.empty_graph(n_nodes)
  432. G.add_edges_from(edges)
  433. if root is not None:
  434. G.graph["root"] = root
  435. if roots is not None:
  436. G.graph["roots"] = roots
  437. return G
  438. def _num_rooted_trees(n, cache_trees):
  439. """Returns the number of unlabeled rooted trees with `n` nodes.
  440. See also https://oeis.org/A000081.
  441. Parameters
  442. ----------
  443. n : int
  444. The number of nodes
  445. cache_trees : list of ints
  446. The $i$-th element is the number of unlabeled rooted trees with $i$ nodes,
  447. which is used as a cache (and is extended to length $n+1$ if needed)
  448. Returns
  449. -------
  450. int
  451. The number of unlabeled rooted trees with `n` nodes.
  452. """
  453. for n_i in range(len(cache_trees), n + 1):
  454. cache_trees.append(
  455. sum(
  456. [
  457. d * cache_trees[n_i - j * d] * cache_trees[d]
  458. for d in range(1, n_i)
  459. for j in range(1, (n_i - 1) // d + 1)
  460. ]
  461. )
  462. // (n_i - 1)
  463. )
  464. return cache_trees[n]
  465. def _select_jd_trees(n, cache_trees, seed):
  466. """Returns a pair $(j,d)$ with a specific probability
  467. Given $n$, returns a pair of positive integers $(j,d)$ with the probability
  468. specified in formula (5) of Chapter 29 of [1]_.
  469. Parameters
  470. ----------
  471. n : int
  472. The number of nodes
  473. cache_trees : list of ints
  474. Cache for :func:`_num_rooted_trees`.
  475. seed : random_state
  476. See :ref:`Randomness<randomness>`.
  477. Returns
  478. -------
  479. (int, int)
  480. A pair of positive integers $(j,d)$ satisfying formula (5) of
  481. Chapter 29 of [1]_.
  482. References
  483. ----------
  484. .. [1] Nijenhuis, Albert, and Wilf, Herbert S.
  485. "Combinatorial algorithms: for computers and calculators."
  486. Academic Press, 1978.
  487. https://doi.org/10.1016/C2013-0-11243-3
  488. """
  489. p = seed.randint(0, _num_rooted_trees(n, cache_trees) * (n - 1) - 1)
  490. cumsum = 0
  491. for d in range(n - 1, 0, -1):
  492. for j in range(1, (n - 1) // d + 1):
  493. cumsum += (
  494. d
  495. * _num_rooted_trees(n - j * d, cache_trees)
  496. * _num_rooted_trees(d, cache_trees)
  497. )
  498. if p < cumsum:
  499. return (j, d)
  500. def _random_unlabeled_rooted_tree(n, cache_trees, seed):
  501. """Returns an unlabeled rooted tree with `n` nodes.
  502. Returns an unlabeled rooted tree with `n` nodes chosen uniformly
  503. at random using the "RANRUT" algorithm from [1]_.
  504. The tree is returned in the form: (list_of_edges, number_of_nodes)
  505. Parameters
  506. ----------
  507. n : int
  508. The number of nodes, greater than zero.
  509. cache_trees : list ints
  510. Cache for :func:`_num_rooted_trees`.
  511. seed : random_state
  512. See :ref:`Randomness<randomness>`.
  513. Returns
  514. -------
  515. (list_of_edges, number_of_nodes) : list, int
  516. A random unlabeled rooted tree with `n` nodes as a 2-tuple
  517. ``(list_of_edges, number_of_nodes)``.
  518. The root is node 0.
  519. References
  520. ----------
  521. .. [1] Nijenhuis, Albert, and Wilf, Herbert S.
  522. "Combinatorial algorithms: for computers and calculators."
  523. Academic Press, 1978.
  524. https://doi.org/10.1016/C2013-0-11243-3
  525. """
  526. if n == 1:
  527. edges, n_nodes = [], 1
  528. return edges, n_nodes
  529. if n == 2:
  530. edges, n_nodes = [(0, 1)], 2
  531. return edges, n_nodes
  532. j, d = _select_jd_trees(n, cache_trees, seed)
  533. t1, t1_nodes = _random_unlabeled_rooted_tree(n - j * d, cache_trees, seed)
  534. t2, t2_nodes = _random_unlabeled_rooted_tree(d, cache_trees, seed)
  535. t12 = [(0, t2_nodes * i + t1_nodes) for i in range(j)]
  536. t1.extend(t12)
  537. for _ in range(j):
  538. t1.extend((n1 + t1_nodes, n2 + t1_nodes) for n1, n2 in t2)
  539. t1_nodes += t2_nodes
  540. return t1, t1_nodes
  541. @py_random_state("seed")
  542. @nx._dispatchable(graphs=None, returns_graph=True)
  543. def random_unlabeled_rooted_tree(n, *, number_of_trees=None, seed=None):
  544. """Returns a number of unlabeled rooted trees uniformly at random
  545. Returns one or more (depending on `number_of_trees`)
  546. unlabeled rooted trees with `n` nodes drawn uniformly
  547. at random.
  548. Parameters
  549. ----------
  550. n : int
  551. The number of nodes
  552. number_of_trees : int or None (default)
  553. If not None, this number of trees is generated and returned.
  554. seed : integer, random_state, or None (default)
  555. Indicator of random number generation state.
  556. See :ref:`Randomness<randomness>`.
  557. Returns
  558. -------
  559. :class:`networkx.Graph` or list of :class:`networkx.Graph`
  560. A single `networkx.Graph` (or a list thereof, if `number_of_trees`
  561. is specified) with nodes in the set {0, …, *n* - 1}.
  562. The "root" graph attribute identifies the root of the tree.
  563. Notes
  564. -----
  565. The trees are generated using the "RANRUT" algorithm from [1]_.
  566. The algorithm needs to compute some counting functions
  567. that are relatively expensive: in case several trees are needed,
  568. it is advisable to use the `number_of_trees` optional argument
  569. to reuse the counting functions.
  570. Raises
  571. ------
  572. NetworkXPointlessConcept
  573. If `n` is zero (because the null graph is not a tree).
  574. References
  575. ----------
  576. .. [1] Nijenhuis, Albert, and Wilf, Herbert S.
  577. "Combinatorial algorithms: for computers and calculators."
  578. Academic Press, 1978.
  579. https://doi.org/10.1016/C2013-0-11243-3
  580. """
  581. if n == 0:
  582. raise nx.NetworkXPointlessConcept("the null graph is not a tree")
  583. cache_trees = [0, 1] # initial cache of number of rooted trees
  584. if number_of_trees is None:
  585. return _to_nx(*_random_unlabeled_rooted_tree(n, cache_trees, seed), root=0)
  586. return [
  587. _to_nx(*_random_unlabeled_rooted_tree(n, cache_trees, seed), root=0)
  588. for i in range(number_of_trees)
  589. ]
  590. def _num_rooted_forests(n, q, cache_forests):
  591. """Returns the number of unlabeled rooted forests with `n` nodes, and with
  592. no more than `q` nodes per tree. A recursive formula for this is (2) in
  593. [1]_. This function is implemented using dynamic programming instead of
  594. recursion.
  595. Parameters
  596. ----------
  597. n : int
  598. The number of nodes.
  599. q : int
  600. The maximum number of nodes for each tree of the forest.
  601. cache_forests : list of ints
  602. The $i$-th element is the number of unlabeled rooted forests with
  603. $i$ nodes, and with no more than `q` nodes per tree; this is used
  604. as a cache (and is extended to length `n` + 1 if needed).
  605. Returns
  606. -------
  607. int
  608. The number of unlabeled rooted forests with `n` nodes with no more than
  609. `q` nodes per tree.
  610. References
  611. ----------
  612. .. [1] Wilf, Herbert S. "The uniform selection of free trees."
  613. Journal of Algorithms 2.2 (1981): 204-207.
  614. https://doi.org/10.1016/0196-6774(81)90021-3
  615. """
  616. for n_i in range(len(cache_forests), n + 1):
  617. q_i = min(n_i, q)
  618. cache_forests.append(
  619. sum(
  620. [
  621. d * cache_forests[n_i - j * d] * cache_forests[d - 1]
  622. for d in range(1, q_i + 1)
  623. for j in range(1, n_i // d + 1)
  624. ]
  625. )
  626. // n_i
  627. )
  628. return cache_forests[n]
  629. def _select_jd_forests(n, q, cache_forests, seed):
  630. """Given `n` and `q`, returns a pair of positive integers $(j,d)$
  631. such that $j\\leq d$, with probability satisfying (F1) of [1]_.
  632. Parameters
  633. ----------
  634. n : int
  635. The number of nodes.
  636. q : int
  637. The maximum number of nodes for each tree of the forest.
  638. cache_forests : list of ints
  639. Cache for :func:`_num_rooted_forests`.
  640. seed : random_state
  641. See :ref:`Randomness<randomness>`.
  642. Returns
  643. -------
  644. (int, int)
  645. A pair of positive integers $(j,d)$
  646. References
  647. ----------
  648. .. [1] Wilf, Herbert S. "The uniform selection of free trees."
  649. Journal of Algorithms 2.2 (1981): 204-207.
  650. https://doi.org/10.1016/0196-6774(81)90021-3
  651. """
  652. p = seed.randint(0, _num_rooted_forests(n, q, cache_forests) * n - 1)
  653. cumsum = 0
  654. for d in range(q, 0, -1):
  655. for j in range(1, n // d + 1):
  656. cumsum += (
  657. d
  658. * _num_rooted_forests(n - j * d, q, cache_forests)
  659. * _num_rooted_forests(d - 1, q, cache_forests)
  660. )
  661. if p < cumsum:
  662. return (j, d)
  663. def _random_unlabeled_rooted_forest(n, q, cache_trees, cache_forests, seed):
  664. """Returns an unlabeled rooted forest with `n` nodes, and with no more
  665. than `q` nodes per tree, drawn uniformly at random. It is an implementation
  666. of the algorithm "Forest" of [1]_.
  667. Parameters
  668. ----------
  669. n : int
  670. The number of nodes.
  671. q : int
  672. The maximum number of nodes per tree.
  673. cache_trees :
  674. Cache for :func:`_num_rooted_trees`.
  675. cache_forests :
  676. Cache for :func:`_num_rooted_forests`.
  677. seed : random_state
  678. See :ref:`Randomness<randomness>`.
  679. Returns
  680. -------
  681. (edges, n, r) : (list, int, list)
  682. The forest (edges, n) and a list r of root nodes.
  683. References
  684. ----------
  685. .. [1] Wilf, Herbert S. "The uniform selection of free trees."
  686. Journal of Algorithms 2.2 (1981): 204-207.
  687. https://doi.org/10.1016/0196-6774(81)90021-3
  688. """
  689. if n == 0:
  690. return ([], 0, [])
  691. j, d = _select_jd_forests(n, q, cache_forests, seed)
  692. t1, t1_nodes, r1 = _random_unlabeled_rooted_forest(
  693. n - j * d, q, cache_trees, cache_forests, seed
  694. )
  695. t2, t2_nodes = _random_unlabeled_rooted_tree(d, cache_trees, seed)
  696. for _ in range(j):
  697. r1.append(t1_nodes)
  698. t1.extend((n1 + t1_nodes, n2 + t1_nodes) for n1, n2 in t2)
  699. t1_nodes += t2_nodes
  700. return t1, t1_nodes, r1
  701. @py_random_state("seed")
  702. @nx._dispatchable(graphs=None, returns_graph=True)
  703. def random_unlabeled_rooted_forest(n, *, q=None, number_of_forests=None, seed=None):
  704. """Returns a forest or list of forests selected at random.
  705. Returns one or more (depending on `number_of_forests`)
  706. unlabeled rooted forests with `n` nodes, and with no more than
  707. `q` nodes per tree, drawn uniformly at random.
  708. The "roots" graph attribute identifies the roots of the forest.
  709. Parameters
  710. ----------
  711. n : int
  712. The number of nodes
  713. q : int or None (default)
  714. The maximum number of nodes per tree.
  715. number_of_forests : int or None (default)
  716. If not None, this number of forests is generated and returned.
  717. seed : integer, random_state, or None (default)
  718. Indicator of random number generation state.
  719. See :ref:`Randomness<randomness>`.
  720. Returns
  721. -------
  722. :class:`networkx.Graph` or list of :class:`networkx.Graph`
  723. A single `networkx.Graph` (or a list thereof, if `number_of_forests`
  724. is specified) with nodes in the set {0, …, *n* - 1}.
  725. The "roots" graph attribute is a set containing the roots
  726. of the trees in the forest.
  727. Notes
  728. -----
  729. This function implements the algorithm "Forest" of [1]_.
  730. The algorithm needs to compute some counting functions
  731. that are relatively expensive: in case several trees are needed,
  732. it is advisable to use the `number_of_forests` optional argument
  733. to reuse the counting functions.
  734. Raises
  735. ------
  736. ValueError
  737. If `n` is non-zero but `q` is zero.
  738. References
  739. ----------
  740. .. [1] Wilf, Herbert S. "The uniform selection of free trees."
  741. Journal of Algorithms 2.2 (1981): 204-207.
  742. https://doi.org/10.1016/0196-6774(81)90021-3
  743. """
  744. if q is None:
  745. q = n
  746. if q == 0 and n != 0:
  747. raise ValueError("q must be a positive integer if n is positive.")
  748. cache_trees = [0, 1] # initial cache of number of rooted trees
  749. cache_forests = [1] # initial cache of number of rooted forests
  750. if number_of_forests is None:
  751. g, nodes, rs = _random_unlabeled_rooted_forest(
  752. n, q, cache_trees, cache_forests, seed
  753. )
  754. return _to_nx(g, nodes, roots=set(rs))
  755. res = []
  756. for i in range(number_of_forests):
  757. g, nodes, rs = _random_unlabeled_rooted_forest(
  758. n, q, cache_trees, cache_forests, seed
  759. )
  760. res.append(_to_nx(g, nodes, roots=set(rs)))
  761. return res
  762. def _num_trees(n, cache_trees):
  763. """Returns the number of unlabeled trees with `n` nodes.
  764. See also https://oeis.org/A000055.
  765. Parameters
  766. ----------
  767. n : int
  768. The number of nodes.
  769. cache_trees : list of ints
  770. Cache for :func:`_num_rooted_trees`.
  771. Returns
  772. -------
  773. int
  774. The number of unlabeled trees with `n` nodes.
  775. """
  776. r = _num_rooted_trees(n, cache_trees) - sum(
  777. [
  778. _num_rooted_trees(j, cache_trees) * _num_rooted_trees(n - j, cache_trees)
  779. for j in range(1, n // 2 + 1)
  780. ]
  781. )
  782. if n % 2 == 0:
  783. r += comb(_num_rooted_trees(n // 2, cache_trees) + 1, 2)
  784. return r
  785. def _bicenter(n, cache, seed):
  786. """Returns a bi-centroidal tree on `n` nodes drawn uniformly at random.
  787. This function implements the algorithm Bicenter of [1]_.
  788. Parameters
  789. ----------
  790. n : int
  791. The number of nodes (must be even).
  792. cache : list of ints.
  793. Cache for :func:`_num_rooted_trees`.
  794. seed : random_state
  795. See :ref:`Randomness<randomness>`
  796. Returns
  797. -------
  798. (edges, n)
  799. The tree as a list of edges and number of nodes.
  800. References
  801. ----------
  802. .. [1] Wilf, Herbert S. "The uniform selection of free trees."
  803. Journal of Algorithms 2.2 (1981): 204-207.
  804. https://doi.org/10.1016/0196-6774(81)90021-3
  805. """
  806. t, t_nodes = _random_unlabeled_rooted_tree(n // 2, cache, seed)
  807. if seed.randint(0, _num_rooted_trees(n // 2, cache)) == 0:
  808. t2, t2_nodes = t, t_nodes
  809. else:
  810. t2, t2_nodes = _random_unlabeled_rooted_tree(n // 2, cache, seed)
  811. t.extend([(n1 + (n // 2), n2 + (n // 2)) for n1, n2 in t2])
  812. t.append((0, n // 2))
  813. return t, t_nodes + t2_nodes
  814. def _random_unlabeled_tree(n, cache_trees, cache_forests, seed):
  815. """Returns a tree on `n` nodes drawn uniformly at random.
  816. It implements the Wilf's algorithm "Free" of [1]_.
  817. Parameters
  818. ----------
  819. n : int
  820. The number of nodes, greater than zero.
  821. cache_trees : list of ints
  822. Cache for :func:`_num_rooted_trees`.
  823. cache_forests : list of ints
  824. Cache for :func:`_num_rooted_forests`.
  825. seed : random_state
  826. Indicator of random number generation state.
  827. See :ref:`Randomness<randomness>`
  828. Returns
  829. -------
  830. (edges, n)
  831. The tree as a list of edges and number of nodes.
  832. References
  833. ----------
  834. .. [1] Wilf, Herbert S. "The uniform selection of free trees."
  835. Journal of Algorithms 2.2 (1981): 204-207.
  836. https://doi.org/10.1016/0196-6774(81)90021-3
  837. """
  838. if n % 2 == 1:
  839. p = 0
  840. else:
  841. p = comb(_num_rooted_trees(n // 2, cache_trees) + 1, 2)
  842. if seed.randint(0, _num_trees(n, cache_trees) - 1) < p:
  843. return _bicenter(n, cache_trees, seed)
  844. else:
  845. f, n_f, r = _random_unlabeled_rooted_forest(
  846. n - 1, (n - 1) // 2, cache_trees, cache_forests, seed
  847. )
  848. for i in r:
  849. f.append((i, n_f))
  850. return f, n_f + 1
  851. @py_random_state("seed")
  852. @nx._dispatchable(graphs=None, returns_graph=True)
  853. def random_unlabeled_tree(n, *, number_of_trees=None, seed=None):
  854. """Returns a tree or list of trees chosen randomly.
  855. Returns one or more (depending on `number_of_trees`)
  856. unlabeled trees with `n` nodes drawn uniformly at random.
  857. Parameters
  858. ----------
  859. n : int
  860. The number of nodes
  861. number_of_trees : int or None (default)
  862. If not None, this number of trees is generated and returned.
  863. seed : integer, random_state, or None (default)
  864. Indicator of random number generation state.
  865. See :ref:`Randomness<randomness>`.
  866. Returns
  867. -------
  868. :class:`networkx.Graph` or list of :class:`networkx.Graph`
  869. A single `networkx.Graph` (or a list thereof, if
  870. `number_of_trees` is specified) with nodes in the set {0, …, *n* - 1}.
  871. Raises
  872. ------
  873. NetworkXPointlessConcept
  874. If `n` is zero (because the null graph is not a tree).
  875. Notes
  876. -----
  877. This function generates an unlabeled tree uniformly at random using
  878. Wilf's algorithm "Free" of [1]_. The algorithm needs to
  879. compute some counting functions that are relatively expensive:
  880. in case several trees are needed, it is advisable to use the
  881. `number_of_trees` optional argument to reuse the counting
  882. functions.
  883. References
  884. ----------
  885. .. [1] Wilf, Herbert S. "The uniform selection of free trees."
  886. Journal of Algorithms 2.2 (1981): 204-207.
  887. https://doi.org/10.1016/0196-6774(81)90021-3
  888. """
  889. if n == 0:
  890. raise nx.NetworkXPointlessConcept("the null graph is not a tree")
  891. cache_trees = [0, 1] # initial cache of number of rooted trees
  892. cache_forests = [1] # initial cache of number of rooted forests
  893. if number_of_trees is None:
  894. return _to_nx(*_random_unlabeled_tree(n, cache_trees, cache_forests, seed))
  895. else:
  896. return [
  897. _to_nx(*_random_unlabeled_tree(n, cache_trees, cache_forests, seed))
  898. for i in range(number_of_trees)
  899. ]