交集#

intersection(G, H)[source]#

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

参数:
G,H

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

返回:
GH一个与 G 类型相同的新图。
引发:
NetworkXError

如果其中一个是多重图(MultiGraph)而另一个是图(graph)。

说明

图、节点和边的属性不会被复制到新图。如果您想要一个包含 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)

示例

>>> 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)])
----

其他后端实现了此函数

graphblas : 支持 OpenMP 的稀疏线性代数后端。