intersection#

intersection(G, H)[source]#

返回一个仅包含在G和H中同时存在的节点和边的新图。

Parameters:
G,H

一个NetworkX图。G和H可以有不同的节点集,但必须都是图或都是多重图。

Returns:
GH一个与G类型相同的新图。
Raises:
NetworkXError

如果一个是多重图而另一个是图。

Notes

图、节点和边的属性不会复制到新图中。如果你想得到一个包含G和H交集的新图,并且带有来自G的属性(包括边数据),可以使用remove_nodes_from()方法,如下所示:

>>> G = nx.path_graph(3)
>>> H = nx.path_graph(5)
>>> R = G.copy()
>>> R.remove_nodes_from(n for n in G if n not in H)
>>> R.remove_edges_from(e for e in G.edges if e not in H.edges)

Examples

>>> G = nx.Graph([(0, 1), (0, 2), (1, 2)])
>>> H = nx.Graph([(0, 3), (1, 2), (2, 3)])
>>> R = nx.intersection(G, H)
>>> R.nodes
NodeView((0, 1, 2))
>>> R.edges
EdgeView([(1, 2)])

Additional backends implement this function

graphblas : OpenMP-enabled sparse linear algebra backend.