简体   繁体   English

如何在python中找到由iGraph创建的图形中的顶点数?

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

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. 我的意思是,我可以使用g.summary() (它返回一个包含顶点数的字符串),但是我会解析字符串以获得顶点的数量 - 而这只是令人讨厌的。

To get the number of edges I'm using len(g.get_edgelist()) , which works. 为了得到我正在使用len(g.get_edgelist())的边数,这是有效的。 But there is no g.get_vertexlist() , so I can't use the same method. 但是没有g.get_vertexlist() ,所以我不能使用相同的方法。

Surely there is an easy way to do this that doesn't involve parsing strings. 当然有一种简单的方法可以做到这一点,不涉及解析字符串。

g.vcount() is a dedicated function in igraph that returns the number of vertices. g.vcount()是igraph中的一个专用函数,它返回顶点数。 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. 类似地, g.ecount()返回边数,并且它比len(g.get_edgelist())更快,因为它不必提前构造完整的边列表。

As some functions in igraph have been renamed in meantime, I found the answers here out of date. 由于igraph某些功能已在此期间重命名,因此我发现此处的答案已过时。 What the docs suggest now is calling 文档现在建议的是调用

gorder(g)

which works for me. 这适合我。 Analogy for ecount is ecount比喻是

gsize(g)   # vcount(g) still works, but not g.vcount()

It's useful to note that help pages are cleverly redirected, so 注意帮助页面被巧妙地重定向是很有用的

?vcount

brings you to gorder docs etc. gorder docs等。

g.vs should return the sequence of vertices as an igraph.VertexSeq object: g.vs应该将顶点序列作为igraph.VertexSeq对象返回:

>>> from igraph import Graph
>>> g = Graph.Formula("A-B")
>>> g.vs["name"]
['A', 'B']
>>> len(g.vs)
2
>>> g.vcount()
2

Edit: As @Tamas mentions below, g.vcount() will also return the number of vertices. 编辑:正如@Tamas在下面提到的那样, g.vcount()也会返回顶点数。 Example edited to account for this. 编辑示例以解释此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM