# 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:
try:
my.graph.vertex(end)
except: