I am using the NetworkX graph library for Python. At some point in my program I would like to "consolidate" my nodeIDs into a sequence of numbers. Here's my naive approach:
start = 1 # could be anything
for i, n in enumerate(g.nodes()):
if i+start == n:
continue
g.add_node(i+start, attr_dict=g.node[n])
g.add_edges_from([(i+start, v, g[n][v]) for v in g.neighbors(n)])
g.remove_node(n)
Is there a faster way than this exhaustive copy of all the neighbors? For example, I tried g[i+start] = g[n]
, but that is forbidden.
Thanks!