I use graph-tool (I love it for its sheer speed) with python as a basis for developing and running own graph algorithms. Sometimes I want to reorder the vertices of the graph, i.e. the indices of the vertices, like swapping vertex indices, without changing the topology of the graph. This doesn’t seem to be a standard operation in graph-tool and I’m wondering if I’m using it the wrong way or simply overlooked a fast way to do reorderings. So far, I found two ways to reorder graphs in graph-tool:
-
Recreate graph
For substantial changes, e.g. for randomly shuffling the vertex order, it’s good to recreate a new graph based on the old one and pass a desired vertex order, like so:import numpy as np import graph_tool as gt g = gt.Graph(...) rng = np.random.default_rng(123) new_vertex_order = rng.choice(g.num_vertices()) np.random.default_rng(seed=None) new_vertex_order_prop_map = self.gtg.new_vertex_property("int", vals=new_vertex_order) g_reordered = gt.Graph(g, vorder=new_vertex_order_prop_map)
-
Swap Vertex Data
For small changes, e.g. for swapping two vertices, it’s good to keep the vertices with their indices, but to swap all associated data about them, which are edges and vertex properties for my case.
Both strategies work, but each of them seem unnecessarily complicated. Recreating the graph needs two graphs in the RAM at one point, and swapping all vertex data is way more performance hungry than it would need to be. Are there better ways to either reorder the indices or else to decorate a graph and use it as if the indices were reordered? Swapping indices in a fast O(1) would be ideal for me, but I couldn’t find a way to do this.