recognition.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. """
  2. Recognition Tests
  3. =================
  4. A *forest* is an acyclic, undirected graph, and a *tree* is a connected forest.
  5. Depending on the subfield, there are various conventions for generalizing these
  6. definitions to directed graphs.
  7. In one convention, directed variants of forest and tree are defined in an
  8. identical manner, except that the direction of the edges is ignored. In effect,
  9. each directed edge is treated as a single undirected edge. Then, additional
  10. restrictions are imposed to define *branchings* and *arborescences*.
  11. In another convention, directed variants of forest and tree correspond to
  12. the previous convention's branchings and arborescences, respectively. Then two
  13. new terms, *polyforest* and *polytree*, are defined to correspond to the other
  14. convention's forest and tree.
  15. Summarizing::
  16. +-----------------------------+
  17. | Convention A | Convention B |
  18. +=============================+
  19. | forest | polyforest |
  20. | tree | polytree |
  21. | branching | forest |
  22. | arborescence | tree |
  23. +-----------------------------+
  24. Each convention has its reasons. The first convention emphasizes definitional
  25. similarity in that directed forests and trees are only concerned with
  26. acyclicity and do not have an in-degree constraint, just as their undirected
  27. counterparts do not. The second convention emphasizes functional similarity
  28. in the sense that the directed analog of a spanning tree is a spanning
  29. arborescence. That is, take any spanning tree and choose one node as the root.
  30. Then every edge is assigned a direction such there is a directed path from the
  31. root to every other node. The result is a spanning arborescence.
  32. NetworkX follows convention "A". Explicitly, these are:
  33. undirected forest
  34. An undirected graph with no undirected cycles.
  35. undirected tree
  36. A connected, undirected forest.
  37. directed forest
  38. A directed graph with no undirected cycles. Equivalently, the underlying
  39. graph structure (which ignores edge orientations) is an undirected forest.
  40. In convention B, this is known as a polyforest.
  41. directed tree
  42. A weakly connected, directed forest. Equivalently, the underlying graph
  43. structure (which ignores edge orientations) is an undirected tree. In
  44. convention B, this is known as a polytree.
  45. branching
  46. A directed forest with each node having, at most, one parent. So the maximum
  47. in-degree is equal to 1. In convention B, this is known as a forest.
  48. arborescence
  49. A directed tree with each node having, at most, one parent. So the maximum
  50. in-degree is equal to 1. In convention B, this is known as a tree.
  51. For trees and arborescences, the adjective "spanning" may be added to designate
  52. that the graph, when considered as a forest/branching, consists of a single
  53. tree/arborescence that includes all nodes in the graph. It is true, by
  54. definition, that every tree/arborescence is spanning with respect to the nodes
  55. that define the tree/arborescence and so, it might seem redundant to introduce
  56. the notion of "spanning". However, the nodes may represent a subset of
  57. nodes from a larger graph, and it is in this context that the term "spanning"
  58. becomes a useful notion.
  59. """
  60. import networkx as nx
  61. __all__ = ["is_arborescence", "is_branching", "is_forest", "is_tree"]
  62. @nx.utils.not_implemented_for("undirected")
  63. def is_arborescence(G):
  64. """
  65. Returns True if `G` is an arborescence.
  66. An arborescence is a directed tree with maximum in-degree equal to 1.
  67. Parameters
  68. ----------
  69. G : graph
  70. The graph to test.
  71. Returns
  72. -------
  73. b : bool
  74. A boolean that is True if `G` is an arborescence.
  75. Examples
  76. --------
  77. >>> G = nx.DiGraph([(0, 1), (0, 2), (2, 3), (3, 4)])
  78. >>> nx.is_arborescence(G)
  79. True
  80. >>> G.remove_edge(0, 1)
  81. >>> G.add_edge(1, 2) # maximum in-degree is 2
  82. >>> nx.is_arborescence(G)
  83. False
  84. Notes
  85. -----
  86. In another convention, an arborescence is known as a *tree*.
  87. See Also
  88. --------
  89. is_tree
  90. """
  91. return is_tree(G) and max(d for n, d in G.in_degree()) <= 1
  92. @nx.utils.not_implemented_for("undirected")
  93. def is_branching(G):
  94. """
  95. Returns True if `G` is a branching.
  96. A branching is a directed forest with maximum in-degree equal to 1.
  97. Parameters
  98. ----------
  99. G : directed graph
  100. The directed graph to test.
  101. Returns
  102. -------
  103. b : bool
  104. A boolean that is True if `G` is a branching.
  105. Examples
  106. --------
  107. >>> G = nx.DiGraph([(0, 1), (1, 2), (2, 3), (3, 4)])
  108. >>> nx.is_branching(G)
  109. True
  110. >>> G.remove_edge(2, 3)
  111. >>> G.add_edge(3, 1) # maximum in-degree is 2
  112. >>> nx.is_branching(G)
  113. False
  114. Notes
  115. -----
  116. In another convention, a branching is also known as a *forest*.
  117. See Also
  118. --------
  119. is_forest
  120. """
  121. return is_forest(G) and max(d for n, d in G.in_degree()) <= 1
  122. def is_forest(G):
  123. """
  124. Returns True if `G` is a forest.
  125. A forest is a graph with no undirected cycles.
  126. For directed graphs, `G` is a forest if the underlying graph is a forest.
  127. The underlying graph is obtained by treating each directed edge as a single
  128. undirected edge in a multigraph.
  129. Parameters
  130. ----------
  131. G : graph
  132. The graph to test.
  133. Returns
  134. -------
  135. b : bool
  136. A boolean that is True if `G` is a forest.
  137. Raises
  138. ------
  139. NetworkXPointlessConcept
  140. If `G` is empty.
  141. Examples
  142. --------
  143. >>> G = nx.Graph()
  144. >>> G.add_edges_from([(1, 2), (1, 3), (2, 4), (2, 5)])
  145. >>> nx.is_forest(G)
  146. True
  147. >>> G.add_edge(4, 1)
  148. >>> nx.is_forest(G)
  149. False
  150. Notes
  151. -----
  152. In another convention, a directed forest is known as a *polyforest* and
  153. then *forest* corresponds to a *branching*.
  154. See Also
  155. --------
  156. is_branching
  157. """
  158. if len(G) == 0:
  159. raise nx.exception.NetworkXPointlessConcept("G has no nodes.")
  160. if G.is_directed():
  161. components = (G.subgraph(c) for c in nx.weakly_connected_components(G))
  162. else:
  163. components = (G.subgraph(c) for c in nx.connected_components(G))
  164. return all(len(c) - 1 == c.number_of_edges() for c in components)
  165. def is_tree(G):
  166. """
  167. Returns True if `G` is a tree.
  168. A tree is a connected graph with no undirected cycles.
  169. For directed graphs, `G` is a tree if the underlying graph is a tree. The
  170. underlying graph is obtained by treating each directed edge as a single
  171. undirected edge in a multigraph.
  172. Parameters
  173. ----------
  174. G : graph
  175. The graph to test.
  176. Returns
  177. -------
  178. b : bool
  179. A boolean that is True if `G` is a tree.
  180. Raises
  181. ------
  182. NetworkXPointlessConcept
  183. If `G` is empty.
  184. Examples
  185. --------
  186. >>> G = nx.Graph()
  187. >>> G.add_edges_from([(1, 2), (1, 3), (2, 4), (2, 5)])
  188. >>> nx.is_tree(G) # n-1 edges
  189. True
  190. >>> G.add_edge(3, 4)
  191. >>> nx.is_tree(G) # n edges
  192. False
  193. Notes
  194. -----
  195. In another convention, a directed tree is known as a *polytree* and then
  196. *tree* corresponds to an *arborescence*.
  197. See Also
  198. --------
  199. is_arborescence
  200. """
  201. if len(G) == 0:
  202. raise nx.exception.NetworkXPointlessConcept("G has no nodes.")
  203. if G.is_directed():
  204. is_connected = nx.is_weakly_connected
  205. else:
  206. is_connected = nx.is_connected
  207. # A connected graph with no cycles has n-1 edges.
  208. return len(G) - 1 == G.number_of_edges() and is_connected(G)