Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding pairs of nodes not having an edge-networkx python

I want to find all the possible pairs of nodes that do not have an edge connecting them and then check if these pairs of nodes have an edge in another graph. Any suggestions?

like image 406
irfanbukhari Avatar asked Jan 24 '26 19:01

irfanbukhari


1 Answers

If you don't care for performance then you can try:

g1Edges = Graph1.edges()
notG1Edges = set()
for a in Graph1.nodes():
    for b in Graph1.nodes():
        if a != b and (a,b) not in g1Edges:
            notG1Edges.add( (a, b) )
# print edges missed in Graph1 and Graph2
print notG1Edges.difference( Graph2.edges_iter() )

NOTE1: this is for directed graphs

NOTE2: if you want to find subset of edges from Graph2 not present in Graph1 then suppose it's better to operate on edges from Graph2

like image 151
ddzialak Avatar answered Jan 27 '26 07:01

ddzialak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!