【问题标题】:Getting all edge pairs of directed graph. networkx获取有向图的所有边对。网络x
【发布时间】:2014-08-08 14:17:15
【问题描述】:

获取有向图的所有边对的最佳方法是什么。我只需要那些具有相反方向的边缘。我需要它来比较关系的对称性。

我寻求以下结果(虽然我不确定获得结果的最佳形式) 输入:

[(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]] 

这里的序列是重要的,因为它告诉了方向。

我应该调查什么?

【问题讨论】:

  • 您的输入看起来无效 - ab 等真的是变量吗? weight 也是变量吗?

标签: python combinations permutation networkx


【解决方案1】:

在迭代边缘时检查反向边缘是否存在:

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', {})]

【讨论】:

    猜你喜欢
    • 2015-12-14
    • 2015-07-31
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    • 2015-07-15
    • 2015-07-31
    相关资源
    最近更新 更多