networkx图中不存在的边列表?

时间:2023-02-03 19:08:08

I have a networkx graph. With G.edges() I can get a list of all the edges. But is there a way to get a list of all other non-existing edges? So if there are 3 nodes: a, b, c and we suppose a and b are connected only, then I would like to get a list of edges that don't exist, so like this: (a,c), (c,b). Is there an easy pythonic way to do this?

我有一个networkx图。使用G.edges(),我可以得到所有边的列表。但有没有办法获得所有其他不存在的边的列表?所以,如果有3个节点:a,b,c,我们假设a和b只是连接,那么我想获得一个不存在的边列表,所以像这样:(a,c),(c ,b)。有一种简单的pythonic方式来做到这一点?

3 个解决方案

#1


2  

Note that Ezekiel Kruglick shows a better way to do this, now that networkx has anon_edges (and also a non-neighbors) function.

请注意,Ezekiel Kruglick显示了一种更好的方法,现在networkx具有anon_edges(以及非邻居)功能。


You could iterate through all possible edges using itertools.combinations, and check if it is not an edge in G using G.has_edge:

您可以使用itertools.combinations迭代所有可能的边缘,并使用G.has_edge检查它是否不是G中的边缘:

import networkx as nx
import itertools as IT
G = nx.MultiGraph()
G.add_edges_from([('A', 'B'), ('B', 'C')])

missing = [pair for pair in IT.combinations(G.nodes(), 2)
           if not G.has_edge(*pair)]
print(missing)

yields

产量

[('A', 'C')]

#2


5  

There is actually a new function in networkx 1.9 called non_edges just for this purpose:

networkx 1.9中实际上有一个名为non_edges的新功能,仅用于此目的:

import networkx as nx
G = nx.MultiGraph()
G.add_edges_from([('A', 'B'), ('B', 'C')])
list(nx.non_edges(G))

Out[3]:
[('A', 'C')]

I've put non_edges into a list() command here to materialize the output, as nx.non_edges is a generator. Having a generator can be very helpful when handling large graphs.

我在这里将non_edges放入list()命令以实现输出,因为nx.non_edges是一个生成器。在处理大型图形时,使用生成器非常有用。

#3


1  

@unutbu's answer is likely the most efficient way. You could also generate the complement graph and emit the edges.

@ unutbu的答案可能是最有效的方式。您还可以生成补图并发出边缘。

In [1]: import networkx as nx                                                           

In [2]: G = nx.Graph([('A', 'B'), ('B', 'C')])                                          

In [3]: print(nx.complement(G).edges()) 
[('A', 'C')]

#1


2  

Note that Ezekiel Kruglick shows a better way to do this, now that networkx has anon_edges (and also a non-neighbors) function.

请注意,Ezekiel Kruglick显示了一种更好的方法,现在networkx具有anon_edges(以及非邻居)功能。


You could iterate through all possible edges using itertools.combinations, and check if it is not an edge in G using G.has_edge:

您可以使用itertools.combinations迭代所有可能的边缘,并使用G.has_edge检查它是否不是G中的边缘:

import networkx as nx
import itertools as IT
G = nx.MultiGraph()
G.add_edges_from([('A', 'B'), ('B', 'C')])

missing = [pair for pair in IT.combinations(G.nodes(), 2)
           if not G.has_edge(*pair)]
print(missing)

yields

产量

[('A', 'C')]

#2


5  

There is actually a new function in networkx 1.9 called non_edges just for this purpose:

networkx 1.9中实际上有一个名为non_edges的新功能,仅用于此目的:

import networkx as nx
G = nx.MultiGraph()
G.add_edges_from([('A', 'B'), ('B', 'C')])
list(nx.non_edges(G))

Out[3]:
[('A', 'C')]

I've put non_edges into a list() command here to materialize the output, as nx.non_edges is a generator. Having a generator can be very helpful when handling large graphs.

我在这里将non_edges放入list()命令以实现输出,因为nx.non_edges是一个生成器。在处理大型图形时,使用生成器非常有用。

#3


1  

@unutbu's answer is likely the most efficient way. You could also generate the complement graph and emit the edges.

@ unutbu的答案可能是最有效的方式。您还可以生成补图并发出边缘。

In [1]: import networkx as nx                                                           

In [2]: G = nx.Graph([('A', 'B'), ('B', 'C')])                                          

In [3]: print(nx.complement(G).edges()) 
[('A', 'C')]