获取有向图的所有边对。networkx

4
什么是获取有向图所有边对的最佳方法?我只需要那些具有相反方向的边。我需要这样做来比较关系的对称性。
我寻求以下结果(虽然我不确定这是否是获取结果的最佳形式) 输入:
[(a,b,{'weight':13}),
 (b,a,{'weight':5}),
 (b,c,{'weight':8}),
 (c,b,{'weight':6}),

 (c,d,{'weight':3}), 
 (c,e,{'weight':5})] #Last two should not appear in output because they do not have inverse edge.

输出:

[set(a,b):[13,5], 
 set(b,c):[8,6]] 

序列在这里很重要,因为它告诉方向。

我应该看什么?


1
您的输入似乎不是有效的 - ab等是否真的是变量?weight也是一个变量吗? - Tom Dalton
1个回答

7

在遍历边的过程中检查是否存在反向边:

In [1]: import networkx as nx

In [2]: G = nx.DiGraph()

In [3]: G.add_edge('a','b')

In [4]: G.add_edge('b','a')

In [5]: G.add_edge('c','d')

In [6]: [(u,v,d) for (u,v,d) in G.edges_iter(data=True) if G.has_edge(v,u)]
Out[6]: [('a', 'b', {}), ('b', 'a', {})]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接