trees.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. """Functions for generating trees."""
  2. from collections import defaultdict
  3. import networkx as nx
  4. from networkx.utils import py_random_state
  5. __all__ = ["prefix_tree", "random_tree", "prefix_tree_recursive"]
  6. def prefix_tree(paths):
  7. """Creates a directed prefix tree from a list of paths.
  8. Usually the paths are described as strings or lists of integers.
  9. A "prefix tree" represents the prefix structure of the strings.
  10. Each node represents a prefix of some string. The root represents
  11. the empty prefix with children for the single letter prefixes which
  12. in turn have children for each double letter prefix starting with
  13. the single letter corresponding to the parent node, and so on.
  14. More generally the prefixes do not need to be strings. A prefix refers
  15. to the start of a sequence. The root has children for each one element
  16. prefix and they have children for each two element prefix that starts
  17. with the one element sequence of the parent, and so on.
  18. Note that this implementation uses integer nodes with an attribute.
  19. Each node has an attribute "source" whose value is the original element
  20. of the path to which this node corresponds. For example, suppose `paths`
  21. consists of one path: "can". Then the nodes `[1, 2, 3]` which represent
  22. this path have "source" values "c", "a" and "n".
  23. All the descendants of a node have a common prefix in the sequence/path
  24. associated with that node. From the returned tree, the prefix for each
  25. node can be constructed by traversing the tree up to the root and
  26. accumulating the "source" values along the way.
  27. The root node is always `0` and has "source" attribute `None`.
  28. The root is the only node with in-degree zero.
  29. The nil node is always `-1` and has "source" attribute `"NIL"`.
  30. The nil node is the only node with out-degree zero.
  31. Parameters
  32. ----------
  33. paths: iterable of paths
  34. An iterable of paths which are themselves sequences.
  35. Matching prefixes among these sequences are identified with
  36. nodes of the prefix tree. One leaf of the tree is associated
  37. with each path. (Identical paths are associated with the same
  38. leaf of the tree.)
  39. Returns
  40. -------
  41. tree: DiGraph
  42. A directed graph representing an arborescence consisting of the
  43. prefix tree generated by `paths`. Nodes are directed "downward",
  44. from parent to child. A special "synthetic" root node is added
  45. to be the parent of the first node in each path. A special
  46. "synthetic" leaf node, the "nil" node `-1`, is added to be the child
  47. of all nodes representing the last element in a path. (The
  48. addition of this nil node technically makes this not an
  49. arborescence but a directed acyclic graph; removing the nil node
  50. makes it an arborescence.)
  51. Notes
  52. -----
  53. The prefix tree is also known as a *trie*.
  54. Examples
  55. --------
  56. Create a prefix tree from a list of strings with common prefixes::
  57. >>> paths = ["ab", "abs", "ad"]
  58. >>> T = nx.prefix_tree(paths)
  59. >>> list(T.edges)
  60. [(0, 1), (1, 2), (1, 4), (2, -1), (2, 3), (3, -1), (4, -1)]
  61. The leaf nodes can be obtained as predecessors of the nil node::
  62. >>> root, NIL = 0, -1
  63. >>> list(T.predecessors(NIL))
  64. [2, 3, 4]
  65. To recover the original paths that generated the prefix tree,
  66. traverse up the tree from the node `-1` to the node `0`::
  67. >>> recovered = []
  68. >>> for v in T.predecessors(NIL):
  69. ... prefix = ""
  70. ... while v != root:
  71. ... prefix = str(T.nodes[v]["source"]) + prefix
  72. ... v = next(T.predecessors(v)) # only one predecessor
  73. ... recovered.append(prefix)
  74. >>> sorted(recovered)
  75. ['ab', 'abs', 'ad']
  76. """
  77. def get_children(parent, paths):
  78. children = defaultdict(list)
  79. # Populate dictionary with key(s) as the child/children of the root and
  80. # value(s) as the remaining paths of the corresponding child/children
  81. for path in paths:
  82. # If path is empty, we add an edge to the NIL node.
  83. if not path:
  84. tree.add_edge(parent, NIL)
  85. continue
  86. child, *rest = path
  87. # `child` may exist as the head of more than one path in `paths`.
  88. children[child].append(rest)
  89. return children
  90. # Initialize the prefix tree with a root node and a nil node.
  91. tree = nx.DiGraph()
  92. root = 0
  93. tree.add_node(root, source=None)
  94. NIL = -1
  95. tree.add_node(NIL, source="NIL")
  96. children = get_children(root, paths)
  97. stack = [(root, iter(children.items()))]
  98. while stack:
  99. parent, remaining_children = stack[-1]
  100. try:
  101. child, remaining_paths = next(remaining_children)
  102. # Pop item off stack if there are no remaining children
  103. except StopIteration:
  104. stack.pop()
  105. continue
  106. # We relabel each child with an unused name.
  107. new_name = len(tree) - 1
  108. # The "source" node attribute stores the original node name.
  109. tree.add_node(new_name, source=child)
  110. tree.add_edge(parent, new_name)
  111. children = get_children(new_name, remaining_paths)
  112. stack.append((new_name, iter(children.items())))
  113. return tree
  114. def prefix_tree_recursive(paths):
  115. """Recursively creates a directed prefix tree from a list of paths.
  116. The original recursive version of prefix_tree for comparison. It is
  117. the same algorithm but the recursion is unrolled onto a stack.
  118. Usually the paths are described as strings or lists of integers.
  119. A "prefix tree" represents the prefix structure of the strings.
  120. Each node represents a prefix of some string. The root represents
  121. the empty prefix with children for the single letter prefixes which
  122. in turn have children for each double letter prefix starting with
  123. the single letter corresponding to the parent node, and so on.
  124. More generally the prefixes do not need to be strings. A prefix refers
  125. to the start of a sequence. The root has children for each one element
  126. prefix and they have children for each two element prefix that starts
  127. with the one element sequence of the parent, and so on.
  128. Note that this implementation uses integer nodes with an attribute.
  129. Each node has an attribute "source" whose value is the original element
  130. of the path to which this node corresponds. For example, suppose `paths`
  131. consists of one path: "can". Then the nodes `[1, 2, 3]` which represent
  132. this path have "source" values "c", "a" and "n".
  133. All the descendants of a node have a common prefix in the sequence/path
  134. associated with that node. From the returned tree, ehe prefix for each
  135. node can be constructed by traversing the tree up to the root and
  136. accumulating the "source" values along the way.
  137. The root node is always `0` and has "source" attribute `None`.
  138. The root is the only node with in-degree zero.
  139. The nil node is always `-1` and has "source" attribute `"NIL"`.
  140. The nil node is the only node with out-degree zero.
  141. Parameters
  142. ----------
  143. paths: iterable of paths
  144. An iterable of paths which are themselves sequences.
  145. Matching prefixes among these sequences are identified with
  146. nodes of the prefix tree. One leaf of the tree is associated
  147. with each path. (Identical paths are associated with the same
  148. leaf of the tree.)
  149. Returns
  150. -------
  151. tree: DiGraph
  152. A directed graph representing an arborescence consisting of the
  153. prefix tree generated by `paths`. Nodes are directed "downward",
  154. from parent to child. A special "synthetic" root node is added
  155. to be the parent of the first node in each path. A special
  156. "synthetic" leaf node, the "nil" node `-1`, is added to be the child
  157. of all nodes representing the last element in a path. (The
  158. addition of this nil node technically makes this not an
  159. arborescence but a directed acyclic graph; removing the nil node
  160. makes it an arborescence.)
  161. Notes
  162. -----
  163. The prefix tree is also known as a *trie*.
  164. Examples
  165. --------
  166. Create a prefix tree from a list of strings with common prefixes::
  167. >>> paths = ["ab", "abs", "ad"]
  168. >>> T = nx.prefix_tree(paths)
  169. >>> list(T.edges)
  170. [(0, 1), (1, 2), (1, 4), (2, -1), (2, 3), (3, -1), (4, -1)]
  171. The leaf nodes can be obtained as predecessors of the nil node.
  172. >>> root, NIL = 0, -1
  173. >>> list(T.predecessors(NIL))
  174. [2, 3, 4]
  175. To recover the original paths that generated the prefix tree,
  176. traverse up the tree from the node `-1` to the node `0`::
  177. >>> recovered = []
  178. >>> for v in T.predecessors(NIL):
  179. ... prefix = ""
  180. ... while v != root:
  181. ... prefix = str(T.nodes[v]["source"]) + prefix
  182. ... v = next(T.predecessors(v)) # only one predecessor
  183. ... recovered.append(prefix)
  184. >>> sorted(recovered)
  185. ['ab', 'abs', 'ad']
  186. """
  187. def _helper(paths, root, tree):
  188. """Recursively create a trie from the given list of paths.
  189. `paths` is a list of paths, each of which is itself a list of
  190. nodes, relative to the given `root` (but not including it). This
  191. list of paths will be interpreted as a tree-like structure, in
  192. which two paths that share a prefix represent two branches of
  193. the tree with the same initial segment.
  194. `root` is the parent of the node at index 0 in each path.
  195. `tree` is the "accumulator", the :class:`networkx.DiGraph`
  196. representing the branching to which the new nodes and edges will
  197. be added.
  198. """
  199. # For each path, remove the first node and make it a child of root.
  200. # Any remaining paths then get processed recursively.
  201. children = defaultdict(list)
  202. for path in paths:
  203. # If path is empty, we add an edge to the NIL node.
  204. if not path:
  205. tree.add_edge(root, NIL)
  206. continue
  207. child, *rest = path
  208. # `child` may exist as the head of more than one path in `paths`.
  209. children[child].append(rest)
  210. # Add a node for each child, connect root, recurse to remaining paths
  211. for child, remaining_paths in children.items():
  212. # We relabel each child with an unused name.
  213. new_name = len(tree) - 1
  214. # The "source" node attribute stores the original node name.
  215. tree.add_node(new_name, source=child)
  216. tree.add_edge(root, new_name)
  217. _helper(remaining_paths, new_name, tree)
  218. # Initialize the prefix tree with a root node and a nil node.
  219. tree = nx.DiGraph()
  220. root = 0
  221. tree.add_node(root, source=None)
  222. NIL = -1
  223. tree.add_node(NIL, source="NIL")
  224. # Populate the tree.
  225. _helper(paths, root, tree)
  226. return tree
  227. # From the Wikipedia article on Prüfer sequences:
  228. #
  229. # > Generating uniformly distributed random Prüfer sequences and
  230. # > converting them into the corresponding trees is a straightforward
  231. # > method of generating uniformly distributed random labelled trees.
  232. #
  233. @py_random_state(1)
  234. def random_tree(n, seed=None, create_using=None):
  235. """Returns a uniformly random tree on `n` nodes.
  236. Parameters
  237. ----------
  238. n : int
  239. A positive integer representing the number of nodes in the tree.
  240. seed : integer, random_state, or None (default)
  241. Indicator of random number generation state.
  242. See :ref:`Randomness<randomness>`.
  243. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  244. Graph type to create. If graph instance, then cleared before populated.
  245. Returns
  246. -------
  247. NetworkX graph
  248. A tree, given as an undirected graph, whose nodes are numbers in
  249. the set {0, …, *n* - 1}.
  250. Raises
  251. ------
  252. NetworkXPointlessConcept
  253. If `n` is zero (because the null graph is not a tree).
  254. Notes
  255. -----
  256. The current implementation of this function generates a uniformly
  257. random Prüfer sequence then converts that to a tree via the
  258. :func:`~networkx.from_prufer_sequence` function. Since there is a
  259. bijection between Prüfer sequences of length *n* - 2 and trees on
  260. *n* nodes, the tree is chosen uniformly at random from the set of
  261. all trees on *n* nodes.
  262. Examples
  263. --------
  264. >>> tree = nx.random_tree(n=10, seed=0)
  265. >>> nx.write_network_text(tree, sources=[0])
  266. ╙── 0
  267. ├── 3
  268. └── 4
  269. ├── 6
  270. │ ├── 1
  271. │ ├── 2
  272. │ └── 7
  273. │ └── 8
  274. │ └── 5
  275. └── 9
  276. >>> tree = nx.random_tree(n=10, seed=0, create_using=nx.DiGraph)
  277. >>> nx.write_network_text(tree)
  278. ╙── 0
  279. ├─╼ 3
  280. └─╼ 4
  281. ├─╼ 6
  282. │ ├─╼ 1
  283. │ ├─╼ 2
  284. │ └─╼ 7
  285. │ └─╼ 8
  286. │ └─╼ 5
  287. └─╼ 9
  288. """
  289. if n == 0:
  290. raise nx.NetworkXPointlessConcept("the null graph is not a tree")
  291. # Cannot create a Prüfer sequence unless `n` is at least two.
  292. if n == 1:
  293. utree = nx.empty_graph(1, create_using)
  294. else:
  295. sequence = [seed.choice(range(n)) for i in range(n - 2)]
  296. utree = nx.from_prufer_sequence(sequence)
  297. if create_using is None:
  298. tree = utree
  299. else:
  300. tree = nx.empty_graph(0, create_using)
  301. if tree.is_directed():
  302. # Use a arbitrary root node and dfs to define edge directions
  303. edges = nx.dfs_edges(utree, source=0)
  304. else:
  305. edges = utree.edges
  306. # Populate the specified graph type
  307. tree.add_nodes_from(utree.nodes)
  308. tree.add_edges_from(edges)
  309. return tree