# Deciding if vertex is in subgraph

Hi,

given a graph G and a subgraph S of G (computed using GraphView). What
is the best way to check if a vertex v in V(G) is also in V(S).

Is there are better way than iterating over all vertices in S?

Best regards
Chris

Hi,

given a graph G and a subgraph S of G (computed using GraphView). What is the best way to check if a vertex v in V(G) is also in V(S).

Is there are better way than iterating over all vertices in S?

Yes, if you try to obtain a vertex in filtered graph via its index, a
ValueError exception will be raised if this vertex is being filtered
out. For example:

>>> g = Graph()
>>> u = GraphView(g, vfilt=lambda v: int(v) < 1)
>>> u.vertex(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/count0/.local/lib/python3.4/site-packages/graph_tool/__init__.py", line 1683, in vertex
raise ValueError("Invalid vertex index: %d" % int(i))
ValueError: Invalid vertex index: 1

Hence you could do:

def vertex_belongs(v, g):
try:
g.vertex(int(v))
return True
except ValueError:
return False

Best,
Tiago

Ni! Woudn't simply:

u.get_vertex_filter()[0][v]

work?

.~´

attachment.html (2.29 KB)

Yes, of course. And it would also be much faster!

Thanks!

Tiago