Reputation: 17934
I have been using networkx for a bit, and it is really neat how it lets me create Graph objects directly from dictionaries: simply nx.Graph(my_dictionary)
.
Now I am trying to use graph-tool, and I notice I cannot do the same.
While googling I came across this blog post which shows how to create a graph-tool Graph from a networkx graph. The process, however, is quite involved, and I would not want to depend on both a home-brewed function and networkx whenever I use graph-tool.
I guess it would be just as easy to write a function that iterates through my dictionary. But before I set out doing that, I wanted to make sure nobody had already written a dict-to-graph-tool parser. It seems like an obvious thing to do, but I can find absolutely nothing in the graph-tool docs....
Upvotes: 1
Views: 985
Reputation: 5450
This looks quite simple. Assuming that your dictionary represents an adjacency list of the graph (that is what you said in the comment), it can be done as follows:
In [3]: d # This dictionary contains the adjacency list
Out[3]: {0: [1, 2], 1: [3]}
You can create edge list out of it and add it to an empty graph:
In [4]: edges = [(i, j) for i in d for j in d[i]]
In [6]: G = gt.Graph(directed = False)
In [7]: G.add_edge_list(edges)
In [8]: G
Out[8]: <Graph object, undirected, with 4 vertices and 3 edges at 0x7fdbc8257780>
Which is what you wanted.
Upvotes: 2