Hi I am trying to create a graph from a list of nodes. my list of nodes was like this [(John, Harold),(Root, Shaw),(Control,decima)]. i tried add_edge_list(edges) but it only takes a list of int tuples. is there any way around it?
On 05/22/2014 06:47 AM, Shubham Bhushan wrote:
Hi I am trying to create a graph from a list of nodes. my list of nodes was like this [(John, Harold),(Root, Shaw),(Control,decima)]. i tried add_edge_list(edges) but it only takes a list of int tuples. is there any way around it?
In graph-tool, vertices are always indexed by integers. So in your case, you need first to create a mapping of the strings to integers, and then use that.
names = ["John", "Harold", "Root", "Shaw", "Control", "decima"] idxs = dict(zip(names, range(len(names))))
edges = [("John", "Harold"),("Root", "Shaw"),("Control","decima")] iedges = [(idxs[e[0]], idxs[e[1]]) for e in edges]
g = Graph() g.add_edge_list(iedges)
# you may want to add the names to a property map for later lookup
vname = g.new_vertex_property("string") for v in g.vertices(): vname[v] = names[int(v)]
Best, Tiago