I solved this once by making a NoDupesGraph where you could add edges with just the names of vertices .
class NoDupesGraph(Graph):
'''Add nodes without worrying if it is a duplicate.
Add edges without worrying if nodes exist '''
def __init__(self,*args,**kwargs):
Graph.__init__(self,*args,**kwargs)
self._nodes = {}
def add_nodupe_vertex(self,label,*args,**kwargs):
'''Return a node with label. Create node if label is new'''
try:
n = self._nodes[label]
except KeyError:
n = self.add_vertex()
self._nodes[label]=n
return n
def add_nodupe_edge(self, n1_label, n2_label,directed=False):
"""
Get or create edges using get_or_create_node
"""
#there may be two if graph is directed but edge isn't
edges = []
n1 = self.add_nodupe_vertex(n1_label)
n2 = self.add_nodupe_vertex(n2_label)
edges.append(self.add_edge(n1,n2))
if self.is_directed() and not directed:
edges.append(self.add_edge(n2,n1))
return edges
def flush_empty_nodes(self):
'''not implemented'''
pass
def condense_edges(self):
'''if a node connects to only two edges, combine those
edges and delete the node.
not implemented
'''
pass
This could be easily modified to suit your need.