【问题标题】:python igraph cannot add edgespython igraph无法添加边
【发布时间】:2017-07-25 00:51:02
【问题描述】:

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

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

InternalError:type_indexededgelist.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

【问题讨论】:

  • 有什么问题?我看到了回溯堆栈,所以你显然有一个错误。你想做什么?您尝试采取什么措施来修复这些错误?

标签: python pandas igraph


【解决方案1】:

堆栈跟踪中的一行如下:

Vertices are enumerated from zero

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

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

摘自docs

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

【讨论】:

    【解决方案2】:

    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,而不是用户-分配的属性。

    【讨论】:

    • 这样更准确! (字符串 -> 属性映射)。
    猜你喜欢
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    相关资源
    最近更新 更多