Building a Graph with Repeated Non-Unique Nodes
I'm trying to read data into a graph using Graph-Tool. My code is further below. I'm having the issue of invalid vertex descriptors. Each row in my.data has a start, end, and transition weight. My goal is to iterate through each row, adding the edges (start,end) that have that edge weight of trans. Suppose on the first iteration, my.data has (1,1,2) = (start, trans, end) which is to describe an edge between vertices labeled 1 and 2, with the weight of '1'. Then on the second iteration, suppose my.data has (1,5,3) = (start, trans, end), which is to describe an edge between vertices labeled 1 and 3, with the weight of '5'. Then I'd want a graph with three vertices, 1, 2, 3 and has two edges (1,2) and (1,3). My strategy is to check if a vertex of each label (start,end) exists, and then to add those vertices if they do not exist. The try-catch statements seem to work properly. But when we try to access the added vertices, I get the invalid vertex descriptor (value error) exception. def gtBuildGraph(my): """build a graph using 'Graph-Tool' """ my.graph = gt.Graph() my.weights = my.graph.new_edge_property("int") for row in my.data: start = int(row['start_state']) end = int(row['end_state']) trans = int(row['transition']) #sometimes, start and end nodes are non-unique try: my.graph.vertex(start) except: my.graph.add_vertex(start) try: my.graph.vertex(end) except: my.graph.add_vertex(end) start = my.graph.vertex(start) end = my.graph.vertex(end) newEdge = my.graph.add_edge(start, end) my.weights[newEdge] = trans -- View this message in context: http://main-discussion-list-for-the-graph-tool-project.982480.n3.nabble.com/... Sent from the Main discussion list for the graph-tool project mailing list archive at Nabble.com.
On 13.10.2014 21:44, tanzairatier wrote:
I'm trying to read data into a graph using Graph-Tool. My code is further below. I'm having the issue of invalid vertex descriptors.
Each row in my.data has a start, end, and transition weight. My goal is to iterate through each row, adding the edges (start,end) that have that edge weight of trans.
Suppose on the first iteration, my.data has (1,1,2) = (start, trans, end) which is to describe an edge between vertices labeled 1 and 2, with the weight of '1'.
Then on the second iteration, suppose my.data has (1,5,3) = (start, trans, end), which is to describe an edge between vertices labeled 1 and 3, with the weight of '5'.
Then I'd want a graph with three vertices, 1, 2, 3 and has two edges (1,2) and (1,3).
My strategy is to check if a vertex of each label (start,end) exists, and then to add those vertices if they do not exist. The try-catch statements seem to work properly. But when we try to access the added vertices, I get the invalid vertex descriptor (value error) exception.
def gtBuildGraph(my): """build a graph using 'Graph-Tool' """
my.graph = gt.Graph() my.weights = my.graph.new_edge_property("int")
for row in my.data: start = int(row['start_state']) end = int(row['end_state']) trans = int(row['transition'])
#sometimes, start and end nodes are non-unique try: my.graph.vertex(start) except: my.graph.add_vertex(start) try: my.graph.vertex(end) except: my.graph.add_vertex(end)
start = my.graph.vertex(start) end = my.graph.vertex(end) newEdge = my.graph.add_edge(start, end) my.weights[newEdge] = trans
Vertices are indexed from 0 to N - 1. When you call g.add_vertex(n) it adds n vertices to the graph. If you had zero vertices before, the vertex with index n does not exist yet in the graph (the last one has index n-1), and hence g.vertex(n) is invalid. Best, Tiago -- Tiago de Paula Peixoto <tiago@skewed.de>
participants (2)
-
tanzairatier -
Tiago de Paula Peixoto