difference#

difference(G, H)[source]#

返回一个新图,该图包含在G中存在但不在H中的边。

H和G的节点集必须相同。

Parameters:
G,H

一个NetworkX图。G和H必须具有相同的节点集。

Returns:
D一个与G类型相同的新图。

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 in H)

Examples

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

Additional backends implement this function

graphblas : OpenMP-enabled sparse linear algebra backend.