Getting all pairs of pairs of a directed graph. NetworkX

What is the best way to get all pairs of edges of a directed graph. I only want edges that are in the opposite direction. I need to compare the symmetry of the relationship.

I'm looking for the following result (although I'm not sure if this is the best form to get the result) Input data:

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

      

Output:

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

      

The sequence here is import because it indicates the direction.

What should I be looking at?

+3


source to share


1 answer


Check if a backward border exists when repeating around the edges:



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

      

+5


source







All Articles