Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of edges that don't exist in a networkx graph?

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?

like image 974
Jack Twain Avatar asked Aug 06 '14 11:08

Jack Twain


1 Answers

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.

like image 89
Ezekiel Kruglick Avatar answered Oct 12 '22 12:10

Ezekiel Kruglick