交集#

intersection(G, H)[源代码]#

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

参数
G,H图表

网络X图。G和H可以有不同的节点集,但必须是两个图或两个多图。

返回
GH给出了与G.
加薪
NetworkXError

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

笔记

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