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?
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', {})]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With