swiss_knight
swiss_knight

Reputation: 7781

Networkx inverts some edges' order in nx.Graph

I have an issue with networkx (Python 2.7): some points coordinates are inverted after having been passed in a graph.

Here is an example:

inputdata= [('766', '7628'),
 ('7233', '7369'),
 ('7233', '7575'),
 ('7233', '7576'),
 ('7233', '7577'),
 ('7235', '7236'),
 ('7369', '7575'),
 ('7369', '7576'),
 ('7369', '7577'),
 ('7370', '7377'),
 ('7575', '7576'),
 ('7575', '7577'),
 ('7576', '7577'),
 ('7578', '7579'),
 ('7580', '7587'),
 ('7607', '7608'),
 ('7673', '7674'),
 ('7676', '7627'),
 ('7677', '7678'),
 ('7623', '7624'),
 ('7628', '7629'),
 ('7637', '7633')]

Gr = nx.Graph()
Gr.add_edges_from(inputdata)

Gr.edges()
Out[108]: 
[('7370', '7377'),
 ('7580', '7587'),
 ('766', '7628'),
 ('7678', '7677'),
 ('7676', '7627'),
 ('7674', '7673'),
 ('7369', '7575'),
 ('7369', '7233'),
 ('7369', '7577'),
 ('7369', '7576'),
 ('7575', '7233'),
 ('7575', '7577'),
 ('7575', '7576'),
 ('7577', '7233'),
 ('7577', '7576'),
 ('7576', '7233'),
 ('7579', '7578'),
 ('7637', '7633'),
 ('7236', '7235'),
 ('7624', '7623'),
 ('7629', '7628'),
 ('7608', '7607')]

See for example pair ('7607', '7608') which is ('7608', '7607') when calling for Gr.edges().

I need to retrieve the same order as the input data.

Upvotes: 1

Views: 352

Answers (1)

Niklas Braun
Niklas Braun

Reputation: 413

If order matters, you may need to consider using a directed graph

Gr = nx.DiGraph()

Otherwise, I'm not sure what the purpose of keeping the node order is.

Upvotes: 3

Related Questions