Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting vertex list from python-igraph

Tags:

python

igraph

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?

like image 218
user2327814 Avatar asked Jun 22 '15 17:06

user2327814


3 Answers

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)
like image 105
Cory Kramer Avatar answered Nov 08 '22 13:11

Cory Kramer


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.

like image 7
JAugust Avatar answered Nov 08 '22 14:11

JAugust


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())
like image 4
K Vij Avatar answered Nov 08 '22 13:11

K Vij