binary.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. """
  2. Operations on graphs including union, intersection, difference.
  3. """
  4. import networkx as nx
  5. __all__ = [
  6. "union",
  7. "compose",
  8. "disjoint_union",
  9. "intersection",
  10. "difference",
  11. "symmetric_difference",
  12. "full_join",
  13. ]
  14. def union(G, H, rename=()):
  15. """Combine graphs G and H. The names of nodes must be unique.
  16. A name collision between the graphs will raise an exception.
  17. A renaming facility is provided to avoid name collisions.
  18. Parameters
  19. ----------
  20. G, H : graph
  21. A NetworkX graph
  22. rename : iterable , optional
  23. Node names of G and H can be changed by specifying the tuple
  24. rename=('G-','H-') (for example). Node "u" in G is then renamed
  25. "G-u" and "v" in H is renamed "H-v".
  26. Returns
  27. -------
  28. U : A union graph with the same type as G.
  29. See Also
  30. --------
  31. compose
  32. :func:`~networkx.Graph.update`
  33. disjoint_union
  34. Notes
  35. -----
  36. To combine graphs that have common nodes, consider compose(G, H)
  37. or the method, Graph.update().
  38. disjoint_union() is similar to union() except that it avoids name clashes
  39. by relabeling the nodes with sequential integers.
  40. Edge and node attributes are propagated from G and H to the union graph.
  41. Graph attributes are also propagated, but if they are present in both G and H,
  42. then the value from H is used.
  43. Examples
  44. --------
  45. >>> G = nx.Graph([(0, 1), (0, 2), (1, 2)])
  46. >>> H = nx.Graph([(0, 1), (0, 3), (1, 3), (1, 2)])
  47. >>> U = nx.union(G, H, rename=("G", "H"))
  48. >>> U.nodes
  49. NodeView(('G0', 'G1', 'G2', 'H0', 'H1', 'H3', 'H2'))
  50. >>> U.edges
  51. EdgeView([('G0', 'G1'), ('G0', 'G2'), ('G1', 'G2'), ('H0', 'H1'), ('H0', 'H3'), ('H1', 'H3'), ('H1', 'H2')])
  52. """
  53. return nx.union_all([G, H], rename)
  54. def disjoint_union(G, H):
  55. """Combine graphs G and H. The nodes are assumed to be unique (disjoint).
  56. This algorithm automatically relabels nodes to avoid name collisions.
  57. Parameters
  58. ----------
  59. G,H : graph
  60. A NetworkX graph
  61. Returns
  62. -------
  63. U : A union graph with the same type as G.
  64. See Also
  65. --------
  66. union
  67. compose
  68. :func:`~networkx.Graph.update`
  69. Notes
  70. -----
  71. A new graph is created, of the same class as G. It is recommended
  72. that G and H be either both directed or both undirected.
  73. The nodes of G are relabeled 0 to len(G)-1, and the nodes of H are
  74. relabeled len(G) to len(G)+len(H)-1.
  75. Renumbering forces G and H to be disjoint, so no exception is ever raised for a name collision.
  76. To preserve the check for common nodes, use union().
  77. Edge and node attributes are propagated from G and H to the union graph.
  78. Graph attributes are also propagated, but if they are present in both G and H,
  79. then the value from H is used.
  80. To combine graphs that have common nodes, consider compose(G, H)
  81. or the method, Graph.update().
  82. Examples
  83. --------
  84. >>> G = nx.Graph([(0, 1), (0, 2), (1, 2)])
  85. >>> H = nx.Graph([(0, 3), (1, 2), (2, 3)])
  86. >>> G.nodes[0]["key1"] = 5
  87. >>> H.nodes[0]["key2"] = 10
  88. >>> U = nx.disjoint_union(G, H)
  89. >>> U.nodes(data=True)
  90. NodeDataView({0: {'key1': 5}, 1: {}, 2: {}, 3: {'key2': 10}, 4: {}, 5: {}, 6: {}})
  91. >>> U.edges
  92. EdgeView([(0, 1), (0, 2), (1, 2), (3, 4), (4, 6), (5, 6)])
  93. """
  94. return nx.disjoint_union_all([G, H])
  95. def intersection(G, H):
  96. """Returns a new graph that contains only the nodes and the edges that exist in
  97. both G and H.
  98. Parameters
  99. ----------
  100. G,H : graph
  101. A NetworkX graph. G and H can have different node sets but must be both graphs or both multigraphs.
  102. Raises
  103. ------
  104. NetworkXError
  105. If one is a MultiGraph and the other one is a graph.
  106. Returns
  107. -------
  108. GH : A new graph with the same type as G.
  109. Notes
  110. -----
  111. Attributes from the graph, nodes, and edges are not copied to the new
  112. graph. If you want a new graph of the intersection of G and H
  113. with the attributes (including edge data) from G use remove_nodes_from()
  114. as follows
  115. >>> G = nx.path_graph(3)
  116. >>> H = nx.path_graph(5)
  117. >>> R = G.copy()
  118. >>> R.remove_nodes_from(n for n in G if n not in H)
  119. >>> R.remove_edges_from(e for e in G.edges if e not in H.edges)
  120. Examples
  121. --------
  122. >>> G = nx.Graph([(0, 1), (0, 2), (1, 2)])
  123. >>> H = nx.Graph([(0, 3), (1, 2), (2, 3)])
  124. >>> R = nx.intersection(G, H)
  125. >>> R.nodes
  126. NodeView((0, 1, 2))
  127. >>> R.edges
  128. EdgeView([(1, 2)])
  129. """
  130. return nx.intersection_all([G, H])
  131. def difference(G, H):
  132. """Returns a new graph that contains the edges that exist in G but not in H.
  133. The node sets of H and G must be the same.
  134. Parameters
  135. ----------
  136. G,H : graph
  137. A NetworkX graph. G and H must have the same node sets.
  138. Returns
  139. -------
  140. D : A new graph with the same type as G.
  141. Notes
  142. -----
  143. Attributes from the graph, nodes, and edges are not copied to the new
  144. graph. If you want a new graph of the difference of G and H with
  145. the attributes (including edge data) from G use remove_nodes_from()
  146. as follows:
  147. >>> G = nx.path_graph(3)
  148. >>> H = nx.path_graph(5)
  149. >>> R = G.copy()
  150. >>> R.remove_nodes_from(n for n in G if n in H)
  151. Examples
  152. --------
  153. >>> G = nx.Graph([(0, 1), (0, 2), (1, 2), (1, 3)])
  154. >>> H = nx.Graph([(0, 1), (1, 2), (0, 3)])
  155. >>> R = nx.difference(G, H)
  156. >>> R.nodes
  157. NodeView((0, 1, 2, 3))
  158. >>> R.edges
  159. EdgeView([(0, 2), (1, 3)])
  160. """
  161. # create new graph
  162. if not G.is_multigraph() == H.is_multigraph():
  163. raise nx.NetworkXError("G and H must both be graphs or multigraphs.")
  164. R = nx.create_empty_copy(G)
  165. if set(G) != set(H):
  166. raise nx.NetworkXError("Node sets of graphs not equal")
  167. if G.is_multigraph():
  168. edges = G.edges(keys=True)
  169. else:
  170. edges = G.edges()
  171. for e in edges:
  172. if not H.has_edge(*e):
  173. R.add_edge(*e)
  174. return R
  175. def symmetric_difference(G, H):
  176. """Returns new graph with edges that exist in either G or H but not both.
  177. The node sets of H and G must be the same.
  178. Parameters
  179. ----------
  180. G,H : graph
  181. A NetworkX graph. G and H must have the same node sets.
  182. Returns
  183. -------
  184. D : A new graph with the same type as G.
  185. Notes
  186. -----
  187. Attributes from the graph, nodes, and edges are not copied to the new
  188. graph.
  189. Examples
  190. --------
  191. >>> G = nx.Graph([(0, 1), (0, 2), (1, 2), (1, 3)])
  192. >>> H = nx.Graph([(0, 1), (1, 2), (0, 3)])
  193. >>> R = nx.symmetric_difference(G, H)
  194. >>> R.nodes
  195. NodeView((0, 1, 2, 3))
  196. >>> R.edges
  197. EdgeView([(0, 2), (0, 3), (1, 3)])
  198. """
  199. # create new graph
  200. if not G.is_multigraph() == H.is_multigraph():
  201. raise nx.NetworkXError("G and H must both be graphs or multigraphs.")
  202. R = nx.create_empty_copy(G)
  203. if set(G) != set(H):
  204. raise nx.NetworkXError("Node sets of graphs not equal")
  205. gnodes = set(G) # set of nodes in G
  206. hnodes = set(H) # set of nodes in H
  207. nodes = gnodes.symmetric_difference(hnodes)
  208. R.add_nodes_from(nodes)
  209. if G.is_multigraph():
  210. edges = G.edges(keys=True)
  211. else:
  212. edges = G.edges()
  213. # we could copy the data here but then this function doesn't
  214. # match intersection and difference
  215. for e in edges:
  216. if not H.has_edge(*e):
  217. R.add_edge(*e)
  218. if H.is_multigraph():
  219. edges = H.edges(keys=True)
  220. else:
  221. edges = H.edges()
  222. for e in edges:
  223. if not G.has_edge(*e):
  224. R.add_edge(*e)
  225. return R
  226. def compose(G, H):
  227. """Compose graph G with H by combining nodes and edges into a single graph.
  228. The node sets and edges sets do not need to be disjoint.
  229. Composing preserves the attributes of nodes and edges.
  230. Attribute values from H take precedent over attribute values from G.
  231. Parameters
  232. ----------
  233. G, H : graph
  234. A NetworkX graph
  235. Returns
  236. -------
  237. C: A new graph with the same type as G
  238. See Also
  239. --------
  240. :func:`~networkx.Graph.update`
  241. union
  242. disjoint_union
  243. Notes
  244. -----
  245. It is recommended that G and H be either both directed or both undirected.
  246. For MultiGraphs, the edges are identified by incident nodes AND edge-key.
  247. This can cause surprises (i.e., edge `(1, 2)` may or may not be the same
  248. in two graphs) if you use MultiGraph without keeping track of edge keys.
  249. If combining the attributes of common nodes is not desired, consider union(),
  250. which raises an exception for name collisions.
  251. Examples
  252. --------
  253. >>> G = nx.Graph([(0, 1), (0, 2)])
  254. >>> H = nx.Graph([(0, 1), (1, 2)])
  255. >>> R = nx.compose(G, H)
  256. >>> R.nodes
  257. NodeView((0, 1, 2))
  258. >>> R.edges
  259. EdgeView([(0, 1), (0, 2), (1, 2)])
  260. By default, the attributes from `H` take precedent over attributes from `G`.
  261. If you prefer another way of combining attributes, you can update them after the compose operation:
  262. >>> G = nx.Graph([(0, 1, {'weight': 2.0}), (3, 0, {'weight': 100.0})])
  263. >>> H = nx.Graph([(0, 1, {'weight': 10.0}), (1, 2, {'weight': -1.0})])
  264. >>> nx.set_node_attributes(G, {0: 'dark', 1: 'light', 3: 'black'}, name='color')
  265. >>> nx.set_node_attributes(H, {0: 'green', 1: 'orange', 2: 'yellow'}, name='color')
  266. >>> GcomposeH = nx.compose(G, H)
  267. Normally, color attribute values of nodes of GcomposeH come from H. We can workaround this as follows:
  268. >>> node_data = {n: G.nodes[n]['color'] + " " + H.nodes[n]['color'] for n in G.nodes & H.nodes}
  269. >>> nx.set_node_attributes(GcomposeH, node_data, 'color')
  270. >>> print(GcomposeH.nodes[0]['color'])
  271. dark green
  272. >>> print(GcomposeH.nodes[3]['color'])
  273. black
  274. Similarly, we can update edge attributes after the compose operation in a way we prefer:
  275. >>> edge_data = {e: G.edges[e]['weight'] * H.edges[e]['weight'] for e in G.edges & H.edges}
  276. >>> nx.set_edge_attributes(GcomposeH, edge_data, 'weight')
  277. >>> print(GcomposeH.edges[(0, 1)]['weight'])
  278. 20.0
  279. >>> print(GcomposeH.edges[(3, 0)]['weight'])
  280. 100.0
  281. """
  282. return nx.compose_all([G, H])
  283. def full_join(G, H, rename=(None, None)):
  284. """Returns the full join of graphs G and H.
  285. Full join is the union of G and H in which all edges between
  286. G and H are added.
  287. The node sets of G and H must be disjoint,
  288. otherwise an exception is raised.
  289. Parameters
  290. ----------
  291. G, H : graph
  292. A NetworkX graph
  293. rename : tuple , default=(None, None)
  294. Node names of G and H can be changed by specifying the tuple
  295. rename=('G-','H-') (for example). Node "u" in G is then renamed
  296. "G-u" and "v" in H is renamed "H-v".
  297. Returns
  298. -------
  299. U : The full join graph with the same type as G.
  300. Notes
  301. -----
  302. It is recommended that G and H be either both directed or both undirected.
  303. If G is directed, then edges from G to H are added as well as from H to G.
  304. Note that full_join() does not produce parallel edges for MultiGraphs.
  305. The full join operation of graphs G and H is the same as getting
  306. their complement, performing a disjoint union, and finally getting
  307. the complement of the resulting graph.
  308. Graph, edge, and node attributes are propagated from G and H
  309. to the union graph. If a graph attribute is present in both
  310. G and H the value from H is used.
  311. Examples
  312. --------
  313. >>> G = nx.Graph([(0, 1), (0, 2)])
  314. >>> H = nx.Graph([(3, 4)])
  315. >>> R = nx.full_join(G, H, rename=("G", "H"))
  316. >>> R.nodes
  317. NodeView(('G0', 'G1', 'G2', 'H3', 'H4'))
  318. >>> R.edges
  319. EdgeView([('G0', 'G1'), ('G0', 'G2'), ('G0', 'H3'), ('G0', 'H4'), ('G1', 'H3'), ('G1', 'H4'), ('G2', 'H3'), ('G2', 'H4'), ('H3', 'H4')])
  320. See Also
  321. --------
  322. union
  323. disjoint_union
  324. """
  325. R = union(G, H, rename)
  326. def add_prefix(graph, prefix):
  327. if prefix is None:
  328. return graph
  329. def label(x):
  330. return f"{prefix}{x}"
  331. return nx.relabel_nodes(graph, label)
  332. G = add_prefix(G, rename[0])
  333. H = add_prefix(H, rename[1])
  334. for i in G:
  335. for j in H:
  336. R.add_edge(i, j)
  337. if R.is_directed():
  338. for i in H:
  339. for j in G:
  340. R.add_edge(i, j)
  341. return R