I.M.
I.M.

Reputation: 344

Missing edges when using add_edges_from with networkx- python

I have a dict with the edges in two tuples format and their values I want to add to a networkx graph.

mydict looks like what is printed bellow:

#print(mydict)
{('StopPoint:59:3786294', 'StopPoint:59:3730342'): {'time_tc': 120.0},
 ('StopPoint:59:3730342', 'StopPoint:59:4255013'): {'time_tc': 60.0},
 ('StopPoint:59:4255013', 'StopPoint:59:3786297'): {'time_tc': 120.0},
 ('StopPoint:59:3786297', 'StopPoint:59:3786298'): {'time_tc': 180.0},
 ('StopPoint:59:3786298', 'StopPoint:59:3786299'): {'time_tc': 60.0}, ...}

#len(mydict) = 233116

I noticed that I was missing almost 100 000 edges after using G.add_edges_from(mydict) (len(G.edges) = 123976). It is also rather obvious when I draw G since many nodes are not linked together with edges. Bellow is what G.edges looks like:

#print(G.edges)
{('StopPoint:93:1184', 'StopPoint:93:1260'): {'time_tc': 180.0},
 ('StopPoint:93:1184', 'StopPoint:55:79'): {'time_map': 126},
 ('StopPoint:93:1184', 'StopPoint:55:80'): {'time_map': 126},
 ('StopPoint:93:1184', 'StopPoint:8711610:800:P'): {'time_map': 336},
 ('StopPoint:93:1184', 'StopPoint:93:1183'): {'time_map': 252}}, ...}

#len(G.edges) = 123976

There shouldn't be any duplicates in mydict so I don't understand why the totality of the edges is not added to the graph. Does someone have any idea?

Upvotes: 1

Views: 874

Answers (1)

Riccardo Bucco
Riccardo Bucco

Reputation: 15384

This is due to the fact that your mydict variable contains both (x,y) and (y,x). If you build a networkx.Graph (instead of a networkx.DiGraph) then (x,y) is considered to be the same of (y,x). Example:

import networkx as nx
mydict = {(3,4): 5, (4,3): 6, (1,2): 6}
g = nx.Graph()
di_g = nx.DiGraph()
g.add_edges_from(mydict)
di_g.add_edges_from(mydict)
print(g.edges) # [(3, 4), (1, 2)]
print(di_g.edges) # [(3, 4), (4, 3), (1, 2)]

You can either build a DiGraph or remove doubles from mydict (this really depends on your specific needs). To extract all the doubles from mydict simply do this:

doubles = set(frozenset(k) for k in mydict if (k[1], k[0]) in mydict)

Upvotes: 2

Related Questions