Reputation: 2915
I encounter some problems when using networkx in python, hope someone could help me. The code is as follows:
G = nx.Graph()
G.add_edge(1,4)
G.add_edge(1,2)
print G.edges()
G.add_node(3)
G.remove_edge(1,2)
G.add_edge(1,3)
G.add_edge(4,3)
print G.nodes()
print G.edges()
the output in python is:
[(1, 2), (1, 4)]
[1, 2, 3, 4]
[(1, 3), (1, 4), (3, 4)]
however, my desired output is:
[(1, 4),(1, 2)]
[1, 2, 3, 4]
[(1, 4), (1, 3), (3, 4)]
the order in the list of edges is extremely important in my implementation, I checked my code for an hour and finally find this bug here, it seems that networkx order the edges automatically with the smallest number goes to first, is there any method to let the order of edges be the order as I add them? Hope someone can help me, I will wait on line, thanks
Upvotes: 1
Views: 317
Reputation: 25319
The order of the nodes and edges produced by G.nodes() and G.edges() is arbitrary and you cannot control it using NetworkX. You can certainly keep track of any ordering yourself with a separate data structure (such as a list).
Upvotes: 1