0

I am working with some graphs that were previously exported from networkx library in graphml format.

When I import those files to graph-tool, the following remap the nodes to 0-base index. How can I prevent that? Because I really need to preserve the order.

Example:

# Create a graph using networkx with only one edge (10 <-> 20)
import networkx as nx
g = nx.Graph()
g.add_edge(10, 20)
nx.write_graphml(g, 'teste.graphml')

# load the previous graph using graph-tool
import graph_tool.all as gt
g = gt.load_graph('teste.graphml')
print(list(g.edges()) # output is an edge from 0 to 1
Guilherme
  • 443
  • 5
  • 22

1 Answers1

2

In graph-tool vertices are always indexed from zero, but if the graphml file uses noncanonical indexing, this can be obtained via the "_graphml_vertex_id" property map:

g = gt.load_graph('teste.graphml')
v = g.vertex(0)
print(g.vp._graphml_vertex_id[v])
Tiago Peixoto
  • 5,149
  • 2
  • 28
  • 28