Getting vertex list from python-igraph

14.9k Views Asked by At

I have been scouring the docs trying to find a function that will return a list vertices from an iGraph graph but can't find one. Does anyone know how to do get a list of vertices from an iGraph graph?

3

There are 3 best solutions below

0
Cory Kramer On BEST ANSWER

The property vs of the igraph.Graph object refers to its VertexSeq object:

g = Graph.Full(3)
vseq = g.vs
print type(vseq)
# <class 'igraph.VertexSeq'>

You can also create one from your graph:

g = Graph.Full(3)
vs = VertexSeq(g)

You can use the property as an iterator:

g = Graph.Full(3)
for v in g.vs:
    # do stuff with v (which is an individual vertex)
1
JAugust On

You can also try a much simpler version of the command in python by

G = igraph.Graph.Read("your_inputgraph.txt", format="edgelist", directed=False) # for reading the graph to igraph

nodes = G.vs.indices

nodes will be the list of all nodes in igraph.

0
K Vij On

If you are looking for names (in case you used names for your vertices), the following code will give you the list of names of all vertices in sequence:

named_vertex_list = g.vs()["name"]

If you are looking for just the indices, then simply creating a range object with vcount will give you the indices

vertex_indices = range(g.vcount())