Hello,
I recently had to switch to python-igraph as a graph processing library.
For quite a few standard tasks I was unable to work out a way to use the
API efficiently and I'm ending up writing code that feels clumsy.
I would really appreciate some pointers as of how to do the following
tasks more elegantly and with less lines of code.
Throughout the following code, g is an igraph.Graph() object.
*Task 1: add a vertex*
I'd like to add a vertex to an existing graph and retrieve the Vertex
descriptor object in a variable.
My best solution is to generate a unique name and then retrieve the
vertex afterwards.
while True: name = 'VERT_%i' % random.randint(1, 100 + 100 * len(g.vs))
try:
g.vs.find(name)
except ValueError:
break
g.add_vertex(name=name)
vertex = g.vs.find(name)
I would expect the API to be a one-liner like
vertex = g.add_vertex()
but testing this shows that v is None.
*Task 2: add an edge*
The problem of retrieving the Edge handle after adding an edge is
similar to the issue when adding a vertex. Here I don't even know how to
write a version that can handle parallel edges. My code is
try:
g.es.find(_source=source_vertex.index, _target=target_vertex.index)
except ValueError:
pass
else:
raise ValueError("Can't handle parallel edges.")
g.add_edge(source_vertex, target_vertex)
edge = g.es.find(_source=source_vertex.index,
_target=target_vertex.index)
Again, I would expect the API to be as simple as
edge = g.add_edge(source_vertex, target_vertex)
Nb: It also feels inconsistent that I have to use the index explicitly
in order to search for an edge.
*Task 3: find outbound and inbound edges*
A standard task for any graph algorithm is getting the outbound and/or
inbound edges of a vertex. Thus, I would expect there to be explicit
methods for this as part of the Vertex object. However, the best I could
do is
out_edges = g.es.select(_source=vertex.index)
I would have expected something as simple as
out_edges = vertex.out_edges
It is quite tedious to write the "select" statement all the time. And to
make it worse, one has to remember to use the "index" of the vertex
instead of the Vertex object itself. If the index is forgotten, the
returned sequence is empty, but no error is shown. This is very prone to
buggy code.
I'd really appreciate it if you could point me towards more elegant ways
of achieving these very basic tasks. If there really isn't any better
way, please consider this email as a feature request to make the API
more usable.
Thanks,
Konstantin
_______________________________________________
igraph-help mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/igraph-help