On 27.04.2015 22:23, Krister wrote:
Thanks for the quick response Thiago!
In this code all the edges and vertices are created by graph-tool and the result is something much faster... is this the best I can do?
It's somewhat annoying to have to keep track of the vertices that will be created like this:
def graph_tool_create_all_at_once(): """ Create a graph_tool graph given a list of pairs. """ G = Graph(directed=False) objectTOi = {} vertexpairs = [] counter = 0 for o1,o2 in get_pairs_of_ints(): if(o1 in objectTOi): u = objectTOi[o1] else: u = counter counter += 1 objectTOi[o1] = u if(o2 in objectTOi): v = objectTOi[o2] else: v = counter counter += 1 objectTOi[o2] = v
vertexpairs.append((u,v))
G.add_edge_list(vertexpairs)
Yes, it is possible to improve this. If your o1 and o2 objects are always ints (as I gather from get_pairs_of_ints()), then you don't need these dictionaries at all. The G.add_edge_list() will create missing nodes as necessary. So you only need to do: G.add_edge_list(list(get_pairs_of_ints())) This will be much faster. However it requires your ints to be within some reasonable range, since missing ints without edges will also be created. But you are better off guaranteeing this is the case when you generate the ints in the first place. Best, Tiago -- Tiago de Paula Peixoto <tiago@skewed.de>