繁体   English   中英

python igraph无法添加边缘

[英]python igraph cannot add edges

我想通过igraph包在Python中构建图形,我可以添加顶点,但是当添加边线时,存在一个我不知道如何解决的问题。 我在上面打印顶点和边缘。

g = ig.Graph()
g.add_vertices(dfset)
g.add_edges(edges)

InternalError:在type_indexededgelist.c处发生错误:c:272:无法添加边,无效的顶点ID

print(dfset)= [1437218112, 1710990122, 1346433521, 1750346227, 1487721718, 1528123965]

print(edges) = [(1346433521, 1437218112),
 (1528123965, 1710990122),
 (1528123965, 1750346227),
 (1710990122, 1750346227),
 (1750346227, 1346433521),
 (1750346227, 1437218112),
 (1750346227, 1487721718)]

InternalError                             Traceback (most recent call last)
<ipython-input-75-82114955e940> in <module>()
----> 1 g.add_edges(edges)

/software/python/2.7.10/b1/lib/python2.7/site-packages/igraph/__init__.pyc in add_edges(self, es)
    253           endpoints. Vertices are enumerated from zero.
    254         """
--> 255         return GraphBase.add_edges(self, es)
    256 
    257     def add_vertex(self, name=None, **kwds):

InternalError: Error at type_indexededgelist.c:272: cannot add edges, Invalid vertex id

堆栈跟踪中的其中一行显示为:

Vertices are enumerated from zero

因此,请确保您使用任意整数添加边的方法是可行的(例如在networkx中,它可以使用任何可散列的内容)。

在我看来,您需要自己进行映射(例如,简单的dict;或通过在文档中提到的库中使用属性支持),以便使用range(0,n_vertices)值调用add_edges()函数仅range(0,n_vertices)

来自文档的小摘录:

igraph在其核心中使用顶点和边缘ID。 这些ID是从零开始的整数,并且在图的生存期内,它们在任何给定的时间实例处始终是连续的。 这意味着每当删除顶点和边时,都会对大量的边和可能的顶点ID进行重新编号,以确保连续性。

sascha的答案涵盖了大部分内容。 一些可能有用的附加信息:

从文档中:

add_vertices(n)
source code 
Adds some vertices to the graph.

Parameters:
n - the number of vertices to be added, or the name of a single vertex to be added, or an iterable of strings, each corresponding to the name of a vertex to be added. Names will be assigned to the name vertex attribute.
Overrides: GraphBase.add_vertices

igraph将顶点和边存储为直线阵列。 顶点和边缘ID是它们在该数组中的偏移量; 这就是完成所有索引编制的方式。

与networkx不同,当您添加ID列表时,它们会转换为字符串,并放入多个新顶点的name属性,这些顶点的属性应为0到5。边必须使用实际的顶点ID,而不是用户分配的属性。

暂无
暂无

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

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