Hi:
I found that, under some conditions, the process of writing and reading a graph in GML format doesn't preserve the node labels. The following code illustrates the issue:
#########################################################
import networkx as nx
n = 3
G1 = nx.grid_2d_graph(n, n)
# Rename the nodes sequentially as 0, 1, ... n**2-1
sorted_nodes = sorted(G1.nodes(), key=lambda x: x[1] + n*x[0])
inverse_mapping = dict(enumerate(sorted_nodes))
mapping = dict(kv[::-1] for kv in inverse_mapping.iteritems())
nx.relabel_nodes(G1, mapping, copy=False)
nx.write_gml(G1, 'G1.gml')
G2 = nx.read_gml('G1.gml')
nx.write_gpickle(G1, 'G1.pickle')
G3 = nx.read_gpickle('G1.pickle')
print 'Original', G1.edges(1)
print 'GML ', G2.edges(1)
print 'gpickle ', G3.edges(1)
#########################################################
The output is
Original [(1, 0), (1, 2), (1, 4)]
GML [(1, 4), (1, 6)]
gpickle [(1, 0), (1, 2), (1, 4)]
So in this example, after writing/reading the graph, the node labeled 1 in G1 and G2 are not the same. If, in the other hand, I save G1 using gpickle, then the labels are preserved. Note that although the labels are different, G1 and G2 are isomorphic.
Note also that if I rename the nodes of G1 using `nx.convert_node_labels_to_integers(G1, ordering='sorted')` instead of the relabel_nodes approach used in the code above, then the labels are preserved.
Is this the expected behavior of nx.write_gml/nx.read_gml, or is it a bug?
I am using version 1.7.dev_20120519061446.
Alejandro