How do I find the number of vertices in a graph created by iGraph in python?

Austin Williams picture Austin Williams · Nov 14, 2015 · Viewed 13.3k times · Source

I'm writing a function that receives a graph as input. The very first thing I need to do is determine the order of the graph (that is, the number of vertices in the graph).

I mean, I could use g.summary() (which returns a string that includes the number of vertices), but then I'd have parse the string to get at the number of vertices -- and that's just nasty.

To get the number of edges I'm using len(g.get_edgelist()), which works. But there is no g.get_vertexlist(), so I can't use the same method.

Surely there is an easy way to do this that doesn't involve parsing strings.

Answer

Tamás picture Tamás · Nov 14, 2015

g.vcount() is a dedicated function in igraph that returns the number of vertices. Similarly, g.ecount() returns the number of edges, and it is way faster than len(g.get_edgelist()) as it does not have to construct the full edge list in advance.