I am trying to find out which edges from a graph are bidirectional. Each row is an edge. For each starting node A, I am searching each corresponding end node B if they have node A as an ending point:
for ending_point_B in nodeA:
nodeA in ending_points_of_B
Disregard for now repeated entries in df[‘S’]. How can I optimize this search? I suspect something along the lines of groupby. This way takes too much time for my real graph.
Thank you
from pandas import *
def missing_node(node):
set1 = set(df[df.E == node].S.values)
set2 = set(df.E[df.S == node].values)
return list(set1.difference(set2))
x = [1,1,2,2,3]
y = [2,3,1,3,1]
df = DataFrame([x,y]).T
df.columns = ['S','E'] #Start & End
df['Missing'] = df.S.apply(missing_node)
df:
S E Missing
0 1 2 []
1 1 3 []
2 2 1 []
3 2 3 []
4 3 1 [2]
Pandas is great, but not sure you need it here. Something like the following should give you the links that aren’t bidirectional:
This returns: