18

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?

user2327814
  • 523
  • 3
  • 8
  • 18

3 Answers3

16

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)
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
7

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.

JAugust
  • 557
  • 1
  • 5
  • 14
4

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())
K Vij
  • 1,783
  • 1
  • 12
  • 19