Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting all edge pairs of directed graph. networkx

What is the best way of getting all edge pairs of directed graph. I need only those edges that have their opposite direction. I need that to compare the symmetry of the relationship.

I seek for the following result(although i'm not sure is the best form for getting the result) Input:

[(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 tells the direction.

What should I look into?

like image 670
Aidis Avatar asked Mar 19 '23 19:03

Aidis


1 Answers

Check if the reverse edge exists while iterating over 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', {})]
like image 163
Aric Avatar answered Mar 24 '23 09:03

Aric