I have a networkx
graph. With G.edges()
I can get a list of all the edges. But is there a way to get a list of all other non-existing edges? So if there are 3 nodes: a, b, c and we suppose a and b are connected only, then I would like to get a list of edges that don't exist, so like this: (a,c), (c,b)
. Is there an easy pythonic way to do this?
There is actually a new function in networkx 1.9 called non_edges just for this purpose:
import networkx as nx
G = nx.MultiGraph()
G.add_edges_from([('A', 'B'), ('B', 'C')])
list(nx.non_edges(G))
Out[3]:
[('A', 'C')]
I've put non_edges into a list()
command here to materialize the output, as nx.non_edges
is a generator. Having a generator can be very helpful when handling large graphs.
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